FOSSology  4.7.1
Open Source License Compliance by Open Source Software
common-compare.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2011-2012 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: LGPL-2.1-only
6 */
7 
23 function FuzzyCmp($Master1, $Master2)
24 {
25  $key1 = empty($Master1[1]) ? 2 : 1;
26  $str1 = $Master1[$key1]['fuzzyname'];
27  $key2 = empty($Master2[1]) ? 2 : 1;
28  $str2 = $Master2[$key2]['fuzzyname'];
29  return strcasecmp($str1, $str2);
30 }
31 
32 
47 function MakeMaster($Children1, $Children2)
48 {
49  $Master = array();
50  $row = -1;
51 
52  if (!empty($Children1) && !empty($Children2)) {
53  /* Build hash-map indices for fast matching */
54  $allChildren2 = [];
55  $byName = [];
56  $byFuzzyExt = [];
57  $byFuzzy = [];
58  foreach ($Children2 as $idx => $child) {
59  $allChildren2[$idx] = $child;
60  $byName[$child['ufile_name']][$idx] = $idx;
61  $byFuzzyExt[$child['fuzzynameext']][$idx] = $idx;
62  $byFuzzy[$child['fuzzyname']][$idx] = $idx;
63  }
64  $consumed = [];
65 
66  foreach ($Children1 as $Child1) {
67  $done = false;
68  $row++;
69 
70  /* find complete name match */
71  if (!$done && !empty($byName[$Child1['ufile_name']])) {
72  foreach ($byName[$Child1['ufile_name']] as $idx) {
73  if (!isset($consumed[$idx])) {
74  $Master[$row][1] = $Child1;
75  $Master[$row][2] = $allChildren2[$idx];
76  $consumed[$idx] = true;
77  $done = true;
78  break;
79  }
80  }
81  }
82 
83  /* find fuzzy+extension match */
84  if (!$done && !empty($byFuzzyExt[$Child1['fuzzynameext']])) {
85  foreach ($byFuzzyExt[$Child1['fuzzynameext']] as $idx) {
86  if (!isset($consumed[$idx])) {
87  $Master[$row][1] = $Child1;
88  $Master[$row][2] = $allChildren2[$idx];
89  $consumed[$idx] = true;
90  $done = true;
91  break;
92  }
93  }
94  }
95 
96  /* find files that only differ by 1 character in fuzzyext */
97  if (!$done) {
98  $best = null;
99  foreach ($byFuzzyExt as $key => $indices) {
100  if (levenshtein($Child1['fuzzynameext'], $key) == 1) {
101  foreach ($indices as $idx) {
102  if (!isset($consumed[$idx])) {
103  $best = $idx;
104  break 2;
105  }
106  }
107  }
108  }
109  if ($best !== null) {
110  $Master[$row][1] = $Child1;
111  $Master[$row][2] = $allChildren2[$best];
112  $consumed[$best] = true;
113  $done = true;
114  }
115  }
116 
117  /* Look for fuzzy match */
118  if (!$done && !empty($byFuzzy[$Child1['fuzzyname']])) {
119  foreach ($byFuzzy[$Child1['fuzzyname']] as $idx) {
120  if (!isset($consumed[$idx])) {
121  $Master[$row][1] = $Child1;
122  $Master[$row][2] = $allChildren2[$idx];
123  $consumed[$idx] = true;
124  $done = true;
125  break;
126  }
127  }
128  }
129 
130  /* no match so add it in by itself */
131  if (!$done) {
132  $Master[$row][1] = $Child1;
133  $Master[$row][2] = array();
134  }
135  }
136  } elseif (!empty($Children1)) {
137  /* Only Children1 exists */
138  foreach ($Children1 as $Child1) {
139  $row++;
140  $Master[$row][1] = $Child1;
141  $Master[$row][2] = array();
142  }
143  }
144 
145  /* Remaining Child2 recs */
146  if (!empty($Children2)) {
147  if (!isset($consumed)) {
148  foreach ($Children2 as $Child) {
149  $row++;
150  $Master[$row][1] = '';
151  $Master[$row][2] = $Child;
152  }
153  } else {
154  foreach ($allChildren2 as $idx => $child) {
155  if (!isset($consumed[$idx])) {
156  $row++;
157  $Master[$row][1] = '';
158  $Master[$row][2] = $child;
159  }
160  }
161  }
162  }
163 
164  /* Sort master by child1 */
165  usort($Master, "FuzzyCmp");
166 
167  return $Master;
168 } // MakeMaster()
169 
170 
184 function FileList(&$Master, $agent_pk1, $agent_pk2, $filter, $plugin, $uploadtree_pk1, $uploadtree_pk2)
185 {
186  global $Plugins;
187 
188  $ModLicView = &$Plugins[plugin_find_id("view-license")];
189 
190  if (! empty($Master)) {
191  foreach ($Master as &$MasterRow) {
192  if (! empty($MasterRow[1])) {
193  $MasterRow[1]["linkurl"] = GetDiffLink($MasterRow, 1, $agent_pk1,
194  $filter, $plugin, $ModLicView, $uploadtree_pk1, $uploadtree_pk2);
195  }
196 
197  if (! empty($MasterRow[2])) {
198  $MasterRow[2]["linkurl"] = GetDiffLink($MasterRow, 2, $agent_pk2,
199  $filter, $plugin, $ModLicView, $uploadtree_pk1, $uploadtree_pk2);
200  }
201  }
202  }
203 } // FileList()
204 
205 
220 function GetDiffLink($MasterRow, $side, $agent_pk, $filter, $plugin, $ModLicView, $uploadtree_pk1, $uploadtree_pk2)
221 {
222  /* calculate opposite side number */
223  if ($side == 1) {
224  $OppositeSide = 2;
225  $OppositeItem = $uploadtree_pk2;
226  } else {
227  $OppositeSide = 1;
228  $OppositeItem = $uploadtree_pk1;
229  }
230 
231  $OppositeChild = $MasterRow[$OppositeSide];
232  $Child = $MasterRow[$side];
233 
234  /* if the opposite column element is empty, then use the original uploadtree_pk */
235  if (empty($OppositeChild)) {
236  $OppositeParm = "&item{$OppositeSide}=$OppositeItem";
237  } else {
238  $OppositeParm = "&item{$OppositeSide}=$OppositeChild[uploadtree_pk]";
239  }
240 
241  $IsDir = Isdir($Child['ufile_mode']);
242  $IsContainer = Iscontainer($Child['ufile_mode']);
243 
244  /* Determine the hyperlink for non-containers to view-license */
245  if (! empty($Child['pfile_fk']) && ! empty($ModLicView)) {
246  $LinkUri = Traceback_uri();
247  $LinkUri .= "?mod=view-license&napk=$agent_pk&upload=$Child[upload_fk]&item=$Child[uploadtree_pk]";
248  } else {
249  $LinkUri = null;
250  }
251 
252  /* Determine link for containers */
253  if (Iscontainer($Child['ufile_mode'])) {
254  $Container_uploadtree_pk = $Child['uploadtree_pk'];
255  $LicUri = "?mod=$plugin->Name&item{$side}=$Child[uploadtree_pk]{$OppositeParm}&col=$side";
256  if (! empty($filter)) {
257  $LicUri .= "&filter=$filter";
258  }
259  } else {
260  $LicUri = null;
261  }
262 
263  $HasHref = 0;
264  $HasBold = 0;
265  $Flink = "";
266  if ($IsContainer) {
267  $Flink = "<a href='$LicUri'>";
268  $HasHref = 1;
269  $Flink .= "<b>";
270  $HasBold = 1;
271  } else if (! empty($LinkUri)) {
272  $Flink .= "<a href='$LinkUri'>";
273  $HasHref = 1;
274  }
275  $Flink .= $Child['ufile_name'];
276  if ($IsDir) {
277  $Flink .= "/";
278  }
279  if ($HasBold) {
280  $Flink .= "</b>";
281  }
282  if ($HasHref) {
283  $Flink .= "</a>";
284  }
285  return $Flink;
286 }
287 
288 
298 function NextUploadtree_pk($A_pk, $B_pk)
299 {
300  global $PG_CONN;
301 
302  /* look up the name of the $A_pk file */
303  $sql = "SELECT ufile_name FROM uploadtree WHERE uploadtree_pk = $A_pk";
304  $result = pg_query($PG_CONN, $sql);
305  DBCheckResult($result, $sql, __FILE__, __LINE__);
306  $row = pg_fetch_assoc($result);
307  $AName = $row["ufile_name"];
308  pg_free_result($result);
309 
310  $APhon = metaphone($AName);
311 
312  /* Loop throught all the files under $B_pk and look
313  * for the closest match.
314  */
315  $B_pk = DirGetNonArtifact($B_pk);
316  $sql = "SELECT uploadtree_pk, ufile_name FROM uploadtree WHERE parent = $B_pk";
317  $result = pg_query($PG_CONN, $sql);
318  DBCheckResult($result, $sql, __FILE__, __LINE__);
319  $BestDist = 99999;
320  $BestPk = 0;
321  while ($row = pg_fetch_assoc($result)) {
322  $ChildName = $row["ufile_name"];
323  $ChildPhon = metaphone($ChildName);
324  $PhonDist = levenshtein($APhon, $ChildPhon);
325  if ($PhonDist < $BestDist) {
326  $BestDist = $PhonDist;
327  $BestPk = $row['uploadtree_pk'];
328  }
329  }
330  pg_free_result($result);
331 
332  return $BestPk;
333 }
334 
335 
346 function FuzzyName(&$Children)
347 {
348  foreach ($Children as $key1 => &$Child) {
349  /* remove file extension */
350  if (strstr($Child['ufile_name'], ".") !== false) {
351  $Ext = GetFileExt($Child['ufile_name']);
352  $ExtLen = strlen($Ext);
353  $NoExtName = substr($Child['ufile_name'], 0, - 1 * $ExtLen);
354  } else {
355  $NoExtName = $Child['ufile_name'];
356  }
357 
358  $NoNumbName = preg_replace('/([0-9]|\.|-|_)/', "", $NoExtName);
359  $NoNumbNameext = preg_replace('/([0-9]|\.|-|_)/', "", $Child['ufile_name']);
360  $Child['fuzzyname'] = $NoNumbName;
361  $Child['fuzzynameext'] = $NoNumbNameext;
362  }
363 
364  return;
365 } /* End of FuzzyName */
366 
367 
378 function Dir2BrowseDiff ($Path1, $Path2, $filter, $Column, $plugin)
379 {
380  if ((count($Path1) < 1) || (count($Path2) < 1)) {
381  return "No path specified";
382  }
383  $filter_clause = (empty($filter)) ? "" : "&filter=$filter";
384  $Path = ($Column == 1) ? $Path1 : $Path2;
385  $Last = $Path[count($Path)-1];
386 
387  /* Banner Box decorations */
388  $V = "<div style='border: double gray; background-color:lightyellow'>\n";
389 
390  /* Get/write the FOLDER list (in banner) */
391  $text = _("Folder");
392  $V .= "<b>$text</b>: ";
393  $List = FolderGetFromUpload($Path[0]['upload_fk']);
394  $Uri2 = Traceback_uri() . "?mod=$plugin->Name";
395 
396  /* Define Freeze button */
397  $text = _("Freeze path");
398  $id = "Freeze{$Column}";
399  $alt = _("Freeze this path so that selecting a new directory in the other path will not change this one.");
400  $Options = "id='$id' onclick='Freeze(\"$Column\")' title='$alt'";
401  $FreezeBtn = "<button type='button' $Options> $text </button>\n";
402 
403  for ($i = 0; $i < count($List); $i ++) {
404  $Folder = $List[$i]['folder_pk'];
405  $FolderName = htmlentities($List[$i]['folder_name']);
406  $V .= "<b>$FolderName/</b> ";
407  }
408 
409  $FirstPath=true; /* If firstpath is true, print FreezeBtn and starts a new line */
410  $V .= "&nbsp;&nbsp;&nbsp;$FreezeBtn";
411  $V .= "<br>";
412 
413  /* Show the path within the upload */
414  for ($PathLev = 0; $PathLev < count($Path); $PathLev ++) {
415  $PathElt1 = @$Path1[$PathLev];
416  $PathElt2 = @$Path2[$PathLev]; // temporarily ignore notice of missing
417  // Path2[PathLev]
418  $PathElt = ($Column == 1) ? $PathElt1 : $PathElt2;
419  /* Prevent a malformed href if any path information is missing */
420  $UseHref = (! empty($PathElt1) && (! empty($PathElt2)));
421  if ($UseHref && ($PathElt != $Last)) {
422  $href = "$Uri2&item1=$PathElt1[uploadtree_pk]&item2=$PathElt2[uploadtree_pk]{$filter_clause}&col=$Column";
423  $V .= "<a href='$href'>";
424  }
425  if (! $FirstPath) {
426  $V .= "<br>";
427  }
428  $V .= "&nbsp;&nbsp;<b>" . $PathElt['ufile_name'] . "/</b>";
429  if ($UseHref && ($PathElt != $Last)) {
430  $V .= "</a>";
431  }
432  $FirstPath = false;
433  }
434 
435  $V .= "</div>\n"; // for box
436  return($V);
437 }
FileList(&$Master, $agent_pk1, $agent_pk2, $filter, $plugin, $uploadtree_pk1, $uploadtree_pk2)
Adds the element linkurl to the $Master elements.
Dir2BrowseDiff($Path1, $Path2, $filter, $Column, $plugin)
Return a string which is a linked path to the file.
GetDiffLink($MasterRow, $side, $agent_pk, $filter, $plugin, $ModLicView, $uploadtree_pk1, $uploadtree_pk2)
Generate the link for one side of a diff element.
MakeMaster($Children1, $Children2)
Generate the master array with aligned children.
NextUploadtree_pk($A_pk, $B_pk)
Given an uploadtree_pk in tree A ($A_pk), find the similarly named one that is immediately under the ...
FuzzyName(&$Children)
Add fuzzyname and fuzzynameext to $Children.
FuzzyCmp($Master1, $Master2)
FuzzyName comparison function for diff tools.
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:189
Isdir($mode)
Definition: common-dir.php:20
DirGetNonArtifact($UploadtreePk, $uploadtree_tablename='uploadtree')
Given an artifact directory (uploadtree_pk), return the first non-artifact directory (uploadtree_pk).
Definition: common-dir.php:158
Iscontainer($mode)
Definition: common-dir.php:38
FolderGetFromUpload($Uploadpk, $Folder=-1, $Stop=-1)
DEPRECATED! Given an upload number, return the folder path in an array containing folder_pk and name.
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
GetFileExt($fname)
Get File Extension (text after last period)
Definition: common-ui.php:142
if(!function_exists('resolve_config_value')) $Options
Definition: fo_dbcheck.php:51
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN