FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
common-compare.php
Go to the documentation of this file.
1 <?php
2 /***********************************************************
3  Copyright (C) 2011-2012 Hewlett-Packard Development Company, L.P.
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License version 2.1 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public License
15  along with this library; if not, write to the Free Software Foundation, Inc.0
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 ***********************************************************/
18 
34 function FuzzyCmp($Master1, $Master2)
35 {
36  $key1 = empty($Master1[1]) ? 2 : 1;
37  $str1 = $Master1[$key1]['fuzzyname'];
38  $key2 = empty($Master2[1]) ? 2 : 1;
39  $str2 = $Master2[$key2]['fuzzyname'];
40  return strcasecmp($str1, $str2);
41 }
42 
43 
58 function MakeMaster($Children1, $Children2)
59 {
60  $Master = array();
61  $row = -1; // Master row number
62 
63  if (! empty($Children1) && (! empty($Children2))) {
64  foreach ($Children1 as $Child1) {
65  $done = false;
66  $row++;
67 
68  /* find complete name match */
69  foreach ($Children2 as $key => $Child2) {
70  if ($Child1['ufile_name'] == $Child2['ufile_name']) {
71  $Master[$row][1] = $Child1;
72  $Master[$row][2] = $Child2;
73  unset($Children2[$key]);
74  $done = true;
75  break;
76  }
77  }
78 
79  /* find fuzzy+extension match */
80  if (! $done) {
81  foreach ($Children2 as $key => $Child2) {
82  if ($Child1['fuzzynameext'] == $Child2['fuzzynameext']) {
83  $Master[$row][1] = $Child1;
84  $Master[$row][2] = $Child2;
85  unset($Children2[$key]);
86  $done = true;
87  break;
88  }
89  }
90  }
91 
92  /* find files that only differ by 1 character in fuzzyext */
93  if (! $done) {
94  foreach ($Children2 as $key => $Child2) {
95  if (levenshtein($Child1['fuzzynameext'], $Child2['fuzzynameext']) == 1) {
96  $Master[$row][1] = $Child1;
97  $Master[$row][2] = $Child2;
98  unset($Children2[$key]);
99  $done = true;
100  break;
101  }
102  }
103  }
104 
105  /* Look for fuzzy match */
106  if (! $done) {
107  foreach ($Children2 as $key => $Child2) {
108  if ($Child1['fuzzyname'] == $Child2['fuzzyname']) {
109  $Master[$row][1] = $Child1;
110  $Master[$row][2] = $Child2;
111  unset($Children2[$key]);
112  $done = true;
113  break;
114  }
115  }
116  }
117 
118  /* no match so add it in by itself */
119  if (! $done) {
120  $Master[$row][1] = $Child1;
121  $Master[$row][2] = array();
122  }
123  }
124  }
125 
126  /* Remaining Child2 recs */
127  foreach ($Children2 as $Child) {
128  $row ++;
129  $Master[$row][1] = '';
130  $Master[$row][2] = $Child;
131  }
132 
133  /* Sort master by child1 */
134  usort($Master, "FuzzyCmp");
135 
136  return($Master);
137 } // MakeMaster()
138 
139 
153 function FileList(&$Master, $agent_pk1, $agent_pk2, $filter, $plugin, $uploadtree_pk1, $uploadtree_pk2)
154 {
155  global $Plugins;
156 
157  $ModLicView = &$Plugins[plugin_find_id("view-license")];
158 
159  if (! empty($Master)) {
160  foreach ($Master as &$MasterRow) {
161  if (! empty($MasterRow[1])) {
162  $MasterRow[1]["linkurl"] = GetDiffLink($MasterRow, 1, $agent_pk1,
163  $filter, $plugin, $ModLicView, $uploadtree_pk1, $uploadtree_pk2);
164  }
165 
166  if (! empty($MasterRow[2])) {
167  $MasterRow[2]["linkurl"] = GetDiffLink($MasterRow, 2, $agent_pk2,
168  $filter, $plugin, $ModLicView, $uploadtree_pk1, $uploadtree_pk2);
169  }
170  }
171  }
172 } // FileList()
173 
174 
189 function GetDiffLink($MasterRow, $side, $agent_pk, $filter, $plugin, $ModLicView, $uploadtree_pk1, $uploadtree_pk2)
190 {
191  /* calculate opposite side number */
192  if ($side == 1) {
193  $OppositeSide = 2;
194  $OppositeItem = $uploadtree_pk2;
195  } else {
196  $OppositeSide = 1;
197  $OppositeItem = $uploadtree_pk1;
198  }
199 
200  $OppositeChild = $MasterRow[$OppositeSide];
201  $Child = $MasterRow[$side];
202 
203  /* if the opposite column element is empty, then use the original uploadtree_pk */
204  if (empty($OppositeChild)) {
205  $OppositeParm = "&item{$OppositeSide}=$OppositeItem";
206  } else {
207  $OppositeParm = "&item{$OppositeSide}=$OppositeChild[uploadtree_pk]";
208  }
209 
210  $IsDir = Isdir($Child['ufile_mode']);
211  $IsContainer = Iscontainer($Child['ufile_mode']);
212 
213  /* Determine the hyperlink for non-containers to view-license */
214  if (! empty($Child['pfile_fk']) && ! empty($ModLicView)) {
215  $LinkUri = Traceback_uri();
216  $LinkUri .= "?mod=view-license&napk=$agent_pk&upload=$Child[upload_fk]&item=$Child[uploadtree_pk]";
217  } else {
218  $LinkUri = null;
219  }
220 
221  /* Determine link for containers */
222  if (Iscontainer($Child['ufile_mode'])) {
223  $Container_uploadtree_pk = $Child['uploadtree_pk'];
224  $LicUri = "?mod=$plugin->Name&item{$side}=$Child[uploadtree_pk]{$OppositeParm}&col=$side";
225  if (! empty($filter)) {
226  $LicUri .= "&filter=$filter";
227  }
228  } else {
229  $LicUri = null;
230  }
231 
232  $HasHref = 0;
233  $HasBold = 0;
234  $Flink = "";
235  if ($IsContainer) {
236  $Flink = "<a href='$LicUri'>";
237  $HasHref = 1;
238  $Flink .= "<b>";
239  $HasBold = 1;
240  } else if (! empty($LinkUri)) {
241  $Flink .= "<a href='$LinkUri'>";
242  $HasHref = 1;
243  }
244  $Flink .= $Child['ufile_name'];
245  if ($IsDir) {
246  $Flink .= "/";
247  }
248  if ($HasBold) {
249  $Flink .= "</b>";
250  }
251  if ($HasHref) {
252  $Flink .= "</a>";
253  }
254  return $Flink;
255 }
256 
257 
267 function NextUploadtree_pk($A_pk, $B_pk)
268 {
269  global $PG_CONN;
270 
271  /* look up the name of the $A_pk file */
272  $sql = "SELECT ufile_name FROM uploadtree WHERE uploadtree_pk = $A_pk";
273  $result = pg_query($PG_CONN, $sql);
274  DBCheckResult($result, $sql, __FILE__, __LINE__);
275  $row = pg_fetch_assoc($result);
276  $AName = $row["ufile_name"];
277  pg_free_result($result);
278 
279  $APhon = metaphone($AName);
280 
281  /* Loop throught all the files under $B_pk and look
282  * for the closest match.
283  */
284  $B_pk = DirGetNonArtifact($B_pk);
285  $sql = "SELECT uploadtree_pk, ufile_name FROM uploadtree WHERE parent = $B_pk";
286  $result = pg_query($PG_CONN, $sql);
287  DBCheckResult($result, $sql, __FILE__, __LINE__);
288  $BestDist = 99999;
289  $BestPk = 0;
290  while ($row = pg_fetch_assoc($result)) {
291  $ChildName = $row["ufile_name"];
292  $ChildPhon = metaphone($ChildName);
293  $PhonDist = levenshtein($APhon, $ChildPhon);
294  if ($PhonDist < $BestDist) {
295  $BestDist = $PhonDist;
296  $BestPk = $row['uploadtree_pk'];
297  }
298  }
299  pg_free_result($result);
300 
301  return $BestPk;
302 }
303 
304 
315 function FuzzyName(&$Children)
316 {
317  foreach ($Children as $key1 => &$Child) {
318  /* remove file extension */
319  if (strstr($Child['ufile_name'], ".") !== false) {
320  $Ext = GetFileExt($Child['ufile_name']);
321  $ExtLen = strlen($Ext);
322  $NoExtName = substr($Child['ufile_name'], 0, - 1 * $ExtLen);
323  } else {
324  $NoExtName = $Child['ufile_name'];
325  }
326 
327  $NoNumbName = preg_replace('/([0-9]|\.|-|_)/', "", $NoExtName);
328  $NoNumbNameext = preg_replace('/([0-9]|\.|-|_)/', "", $Child['ufile_name']);
329  $Child['fuzzyname'] = $NoNumbName;
330  $Child['fuzzynameext'] = $NoNumbName;
331  }
332 
333  return;
334 } /* End of FuzzyName */
335 
336 
347 function Dir2BrowseDiff ($Path1, $Path2, $filter, $Column, $plugin)
348 {
349  if ((count($Path1) < 1) || (count($Path2) < 1)) {
350  return "No path specified";
351  }
352  $filter_clause = (empty($filter)) ? "" : "&filter=$filter";
353  $Path = ($Column == 1) ? $Path1 : $Path2;
354  $Last = $Path[count($Path)-1];
355 
356  /* Banner Box decorations */
357  $V = "<div style='border: double gray; background-color:lightyellow'>\n";
358 
359  /* Get/write the FOLDER list (in banner) */
360  $text = _("Folder");
361  $V .= "<b>$text</b>: ";
362  $List = FolderGetFromUpload($Path[0]['upload_fk']);
363  $Uri2 = Traceback_uri() . "?mod=$plugin->Name";
364 
365  /* Define Freeze button */
366  $text = _("Freeze path");
367  $id = "Freeze{$Column}";
368  $alt = _("Freeze this path so that selecting a new directory in the other path will not change this one.");
369  $Options = "id='$id' onclick='Freeze(\"$Column\")' title='$alt'";
370  $FreezeBtn = "<button type='button' $Options> $text </button>\n";
371 
372  for ($i = 0; $i < count($List); $i ++) {
373  $Folder = $List[$i]['folder_pk'];
374  $FolderName = htmlentities($List[$i]['folder_name']);
375  $V .= "<b>$FolderName/</b> ";
376  }
377 
378  $FirstPath=true; /* If firstpath is true, print FreezeBtn and starts a new line */
379  $V .= "&nbsp;&nbsp;&nbsp;$FreezeBtn";
380  $V .= "<br>";
381 
382  /* Show the path within the upload */
383  for ($PathLev = 0; $PathLev < count($Path); $PathLev ++) {
384  $PathElt1 = @$Path1[$PathLev];
385  $PathElt2 = @$Path2[$PathLev]; // temporarily ignore notice of missing
386  // Path2[PathLev]
387  $PathElt = ($Column == 1) ? $PathElt1 : $PathElt2;
388  /* Prevent a malformed href if any path information is missing */
389  $UseHref = (! empty($PathElt1) && (! empty($PathElt2)));
390  if ($UseHref && ($PathElt != $Last)) {
391  $href = "$Uri2&item1=$PathElt1[uploadtree_pk]&item2=$PathElt2[uploadtree_pk]{$filter_clause}&col=$Column";
392  $V .= "<a href='$href'>";
393  }
394  if (! $FirstPath) {
395  $V .= "<br>";
396  }
397  $V .= "&nbsp;&nbsp;<b>" . $PathElt['ufile_name'] . "/</b>";
398  if ($UseHref && ($PathElt != $Last)) {
399  $V .= "</a>";
400  }
401  $FirstPath = false;
402  }
403 
404  $V .= "</div>\n"; // for box
405  return($V);
406 }
FuzzyName(&$Children)
Add fuzzyname and fuzzynameext to $Children.
Traceback_uri()
Get the URI without query to this location.
FolderGetFromUpload($Uploadpk, $Folder=-1, $Stop=-1)
DEPRECATED! Given an upload number, return the folder path in an array containing folder_pk and name...
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 ...
Iscontainer($mode)
Definition: common-dir.php:49
FuzzyCmp($Master1, $Master2)
FuzzyName comparison function for diff tools.
MakeMaster($Children1, $Children2)
Generate the master array with aligned children.
DirGetNonArtifact($UploadtreePk, $uploadtree_tablename='uploadtree')
Given an artifact directory (uploadtree_pk), return the first non-artifact directory (uploadtree_pk)...
Definition: common-dir.php:169
FileList(&$Master, $agent_pk1, $agent_pk2, $filter, $plugin, $uploadtree_pk1, $uploadtree_pk2)
Adds the element linkurl to the $Master elements.
GetDiffLink($MasterRow, $side, $agent_pk, $filter, $plugin, $ModLicView, $uploadtree_pk1, $uploadtree_pk2)
Generate the link for one side of a diff element.
GetFileExt($fname)
Get File Extension (text after last period)
Definition: common-ui.php:127
Isdir($mode)
Definition: common-dir.php:31
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:198
Dir2BrowseDiff($Path1, $Path2, $filter, $Column, $plugin)
Return a string which is a linked path to the file.