FOSSology  4.4.0
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; // Master row number
51 
52  if (! empty($Children1) && (! empty($Children2))) {
53  foreach ($Children1 as $Child1) {
54  $done = false;
55  $row++;
56 
57  /* find complete name match */
58  foreach ($Children2 as $key => $Child2) {
59  if ($Child1['ufile_name'] == $Child2['ufile_name']) {
60  $Master[$row][1] = $Child1;
61  $Master[$row][2] = $Child2;
62  unset($Children2[$key]);
63  $done = true;
64  break;
65  }
66  }
67 
68  /* find fuzzy+extension match */
69  if (! $done) {
70  foreach ($Children2 as $key => $Child2) {
71  if ($Child1['fuzzynameext'] == $Child2['fuzzynameext']) {
72  $Master[$row][1] = $Child1;
73  $Master[$row][2] = $Child2;
74  unset($Children2[$key]);
75  $done = true;
76  break;
77  }
78  }
79  }
80 
81  /* find files that only differ by 1 character in fuzzyext */
82  if (! $done) {
83  foreach ($Children2 as $key => $Child2) {
84  if (levenshtein($Child1['fuzzynameext'], $Child2['fuzzynameext']) == 1) {
85  $Master[$row][1] = $Child1;
86  $Master[$row][2] = $Child2;
87  unset($Children2[$key]);
88  $done = true;
89  break;
90  }
91  }
92  }
93 
94  /* Look for fuzzy match */
95  if (! $done) {
96  foreach ($Children2 as $key => $Child2) {
97  if ($Child1['fuzzyname'] == $Child2['fuzzyname']) {
98  $Master[$row][1] = $Child1;
99  $Master[$row][2] = $Child2;
100  unset($Children2[$key]);
101  $done = true;
102  break;
103  }
104  }
105  }
106 
107  /* no match so add it in by itself */
108  if (! $done) {
109  $Master[$row][1] = $Child1;
110  $Master[$row][2] = array();
111  }
112  }
113  }
114 
115  /* Remaining Child2 recs */
116  foreach ($Children2 as $Child) {
117  $row ++;
118  $Master[$row][1] = '';
119  $Master[$row][2] = $Child;
120  }
121 
122  /* Sort master by child1 */
123  usort($Master, "FuzzyCmp");
124 
125  return($Master);
126 } // MakeMaster()
127 
128 
142 function FileList(&$Master, $agent_pk1, $agent_pk2, $filter, $plugin, $uploadtree_pk1, $uploadtree_pk2)
143 {
144  global $Plugins;
145 
146  $ModLicView = &$Plugins[plugin_find_id("view-license")];
147 
148  if (! empty($Master)) {
149  foreach ($Master as &$MasterRow) {
150  if (! empty($MasterRow[1])) {
151  $MasterRow[1]["linkurl"] = GetDiffLink($MasterRow, 1, $agent_pk1,
152  $filter, $plugin, $ModLicView, $uploadtree_pk1, $uploadtree_pk2);
153  }
154 
155  if (! empty($MasterRow[2])) {
156  $MasterRow[2]["linkurl"] = GetDiffLink($MasterRow, 2, $agent_pk2,
157  $filter, $plugin, $ModLicView, $uploadtree_pk1, $uploadtree_pk2);
158  }
159  }
160  }
161 } // FileList()
162 
163 
178 function GetDiffLink($MasterRow, $side, $agent_pk, $filter, $plugin, $ModLicView, $uploadtree_pk1, $uploadtree_pk2)
179 {
180  /* calculate opposite side number */
181  if ($side == 1) {
182  $OppositeSide = 2;
183  $OppositeItem = $uploadtree_pk2;
184  } else {
185  $OppositeSide = 1;
186  $OppositeItem = $uploadtree_pk1;
187  }
188 
189  $OppositeChild = $MasterRow[$OppositeSide];
190  $Child = $MasterRow[$side];
191 
192  /* if the opposite column element is empty, then use the original uploadtree_pk */
193  if (empty($OppositeChild)) {
194  $OppositeParm = "&item{$OppositeSide}=$OppositeItem";
195  } else {
196  $OppositeParm = "&item{$OppositeSide}=$OppositeChild[uploadtree_pk]";
197  }
198 
199  $IsDir = Isdir($Child['ufile_mode']);
200  $IsContainer = Iscontainer($Child['ufile_mode']);
201 
202  /* Determine the hyperlink for non-containers to view-license */
203  if (! empty($Child['pfile_fk']) && ! empty($ModLicView)) {
204  $LinkUri = Traceback_uri();
205  $LinkUri .= "?mod=view-license&napk=$agent_pk&upload=$Child[upload_fk]&item=$Child[uploadtree_pk]";
206  } else {
207  $LinkUri = null;
208  }
209 
210  /* Determine link for containers */
211  if (Iscontainer($Child['ufile_mode'])) {
212  $Container_uploadtree_pk = $Child['uploadtree_pk'];
213  $LicUri = "?mod=$plugin->Name&item{$side}=$Child[uploadtree_pk]{$OppositeParm}&col=$side";
214  if (! empty($filter)) {
215  $LicUri .= "&filter=$filter";
216  }
217  } else {
218  $LicUri = null;
219  }
220 
221  $HasHref = 0;
222  $HasBold = 0;
223  $Flink = "";
224  if ($IsContainer) {
225  $Flink = "<a href='$LicUri'>";
226  $HasHref = 1;
227  $Flink .= "<b>";
228  $HasBold = 1;
229  } else if (! empty($LinkUri)) {
230  $Flink .= "<a href='$LinkUri'>";
231  $HasHref = 1;
232  }
233  $Flink .= $Child['ufile_name'];
234  if ($IsDir) {
235  $Flink .= "/";
236  }
237  if ($HasBold) {
238  $Flink .= "</b>";
239  }
240  if ($HasHref) {
241  $Flink .= "</a>";
242  }
243  return $Flink;
244 }
245 
246 
256 function NextUploadtree_pk($A_pk, $B_pk)
257 {
258  global $PG_CONN;
259 
260  /* look up the name of the $A_pk file */
261  $sql = "SELECT ufile_name FROM uploadtree WHERE uploadtree_pk = $A_pk";
262  $result = pg_query($PG_CONN, $sql);
263  DBCheckResult($result, $sql, __FILE__, __LINE__);
264  $row = pg_fetch_assoc($result);
265  $AName = $row["ufile_name"];
266  pg_free_result($result);
267 
268  $APhon = metaphone($AName);
269 
270  /* Loop throught all the files under $B_pk and look
271  * for the closest match.
272  */
273  $B_pk = DirGetNonArtifact($B_pk);
274  $sql = "SELECT uploadtree_pk, ufile_name FROM uploadtree WHERE parent = $B_pk";
275  $result = pg_query($PG_CONN, $sql);
276  DBCheckResult($result, $sql, __FILE__, __LINE__);
277  $BestDist = 99999;
278  $BestPk = 0;
279  while ($row = pg_fetch_assoc($result)) {
280  $ChildName = $row["ufile_name"];
281  $ChildPhon = metaphone($ChildName);
282  $PhonDist = levenshtein($APhon, $ChildPhon);
283  if ($PhonDist < $BestDist) {
284  $BestDist = $PhonDist;
285  $BestPk = $row['uploadtree_pk'];
286  }
287  }
288  pg_free_result($result);
289 
290  return $BestPk;
291 }
292 
293 
304 function FuzzyName(&$Children)
305 {
306  foreach ($Children as $key1 => &$Child) {
307  /* remove file extension */
308  if (strstr($Child['ufile_name'], ".") !== false) {
309  $Ext = GetFileExt($Child['ufile_name']);
310  $ExtLen = strlen($Ext);
311  $NoExtName = substr($Child['ufile_name'], 0, - 1 * $ExtLen);
312  } else {
313  $NoExtName = $Child['ufile_name'];
314  }
315 
316  $NoNumbName = preg_replace('/([0-9]|\.|-|_)/', "", $NoExtName);
317  $NoNumbNameext = preg_replace('/([0-9]|\.|-|_)/', "", $Child['ufile_name']);
318  $Child['fuzzyname'] = $NoNumbName;
319  $Child['fuzzynameext'] = $NoNumbName;
320  }
321 
322  return;
323 } /* End of FuzzyName */
324 
325 
336 function Dir2BrowseDiff ($Path1, $Path2, $filter, $Column, $plugin)
337 {
338  if ((count($Path1) < 1) || (count($Path2) < 1)) {
339  return "No path specified";
340  }
341  $filter_clause = (empty($filter)) ? "" : "&filter=$filter";
342  $Path = ($Column == 1) ? $Path1 : $Path2;
343  $Last = $Path[count($Path)-1];
344 
345  /* Banner Box decorations */
346  $V = "<div style='border: double gray; background-color:lightyellow'>\n";
347 
348  /* Get/write the FOLDER list (in banner) */
349  $text = _("Folder");
350  $V .= "<b>$text</b>: ";
351  $List = FolderGetFromUpload($Path[0]['upload_fk']);
352  $Uri2 = Traceback_uri() . "?mod=$plugin->Name";
353 
354  /* Define Freeze button */
355  $text = _("Freeze path");
356  $id = "Freeze{$Column}";
357  $alt = _("Freeze this path so that selecting a new directory in the other path will not change this one.");
358  $Options = "id='$id' onclick='Freeze(\"$Column\")' title='$alt'";
359  $FreezeBtn = "<button type='button' $Options> $text </button>\n";
360 
361  for ($i = 0; $i < count($List); $i ++) {
362  $Folder = $List[$i]['folder_pk'];
363  $FolderName = htmlentities($List[$i]['folder_name']);
364  $V .= "<b>$FolderName/</b> ";
365  }
366 
367  $FirstPath=true; /* If firstpath is true, print FreezeBtn and starts a new line */
368  $V .= "&nbsp;&nbsp;&nbsp;$FreezeBtn";
369  $V .= "<br>";
370 
371  /* Show the path within the upload */
372  for ($PathLev = 0; $PathLev < count($Path); $PathLev ++) {
373  $PathElt1 = @$Path1[$PathLev];
374  $PathElt2 = @$Path2[$PathLev]; // temporarily ignore notice of missing
375  // Path2[PathLev]
376  $PathElt = ($Column == 1) ? $PathElt1 : $PathElt2;
377  /* Prevent a malformed href if any path information is missing */
378  $UseHref = (! empty($PathElt1) && (! empty($PathElt2)));
379  if ($UseHref && ($PathElt != $Last)) {
380  $href = "$Uri2&item1=$PathElt1[uploadtree_pk]&item2=$PathElt2[uploadtree_pk]{$filter_clause}&col=$Column";
381  $V .= "<a href='$href'>";
382  }
383  if (! $FirstPath) {
384  $V .= "<br>";
385  }
386  $V .= "&nbsp;&nbsp;<b>" . $PathElt['ufile_name'] . "/</b>";
387  if ($UseHref && ($PathElt != $Last)) {
388  $V .= "</a>";
389  }
390  $FirstPath = false;
391  }
392 
393  $V .= "</div>\n"; // for box
394  return($V);
395 }
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:187
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
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN