FOSSology  4.4.0
Open Source License Compliance by Open Source Software
library.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2010-2013 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
21 function hist_rowcmp_count_asc($a, $b)
22 {
23  return $a['copyright_count'] - $b['copyright_count'];
24 }
25 
32 function hist_rowcmp_count_desc($a, $b)
33 {
34  $res = $a['copyright_count'] - $b['copyright_count'];
35  return -$res;
36 }
37 
44 function hist_rowcmp($rowa, $rowb)
45 {
46  return (strnatcasecmp($rowa['content'], $rowb['content']));
47 }
48 
55 function hist_rowcmp_desc($rowa, $rowb)
56 {
57  return -(strnatcasecmp($rowa['content'], $rowb['content']));
58 }
59 
66 function copyright_namecmp($rowa, $rowb)
67 {
68  return (strnatcasecmp($rowa['ufile_name'], $rowb['ufile_name']));
69 }
70 
71 
87 function GetFilesWithCopyright($agent_pk, $hash, $type, $uploadtree_pk,
88 $PkgsOnly=false, $offset=0, $limit="ALL",
89 $order="")
90 {
91  global $PG_CONN;
92 
93  /* Find lft and rgt bounds for this $uploadtree_pk */
94  $sql = "SELECT lft, rgt, upload_fk FROM uploadtree
95  WHERE uploadtree_pk = $uploadtree_pk";
96  $result = pg_query($PG_CONN, $sql);
97  DBCheckResult($result, $sql, __FILE__, __LINE__);
98  $row = pg_fetch_assoc($result);
99  $lft = $row["lft"];
100  $rgt = $row["rgt"];
101  $upload_pk = $row["upload_fk"];
102  pg_free_result($result);
103 
104  $sql = "select distinct uploadtree_pk, pfile_fk, ufile_name
105  from copyright,
106  (SELECT pfile_fk as PF, uploadtree_pk, ufile_name from uploadtree
107  where upload_fk=$upload_pk
108  and uploadtree.lft BETWEEN $lft and $rgt) as SS
109  where PF=pfile_fk and agent_fk=$agent_pk
110  and hash='$hash' and type='$type'
111  group by uploadtree_pk, pfile_fk, ufile_name
112  $order limit $limit offset $offset";
113  $result = pg_query($PG_CONN, $sql); // Top uploadtree_pk's
114  DBCheckResult($result, $sql, __FILE__, __LINE__);
115 
116  //echo "<br>$sql<br>";
117  return $result;
118 }
119 
133 function CountFilesWithCopyright($agent_pk, $hash, $type, $uploadtree_pk,
134 $PkgsOnly=false, $CheckOnly=false)
135 {
136  global $PG_CONN;
137 
138  /* Find lft and rgt bounds for this $uploadtree_pk */
139  $sql = "SELECT lft, rgt, upload_fk FROM uploadtree
140  WHERE uploadtree_pk = $uploadtree_pk";
141  $result = pg_query($PG_CONN, $sql);
142  DBCheckResult($result, $sql, __FILE__, __LINE__);
143  $row = pg_fetch_assoc($result);
144  $lft = $row["lft"];
145  $rgt = $row["rgt"];
146  $upload_pk = $row["upload_fk"];
147  pg_free_result($result);
148 
149  $chkonly = ($CheckOnly) ? " LIMIT 1" : "";
150 
151  $sql = "SELECT count(DISTINCT pfile_fk) as unique from copyright,
152  (SELECT pfile_fk as PF from uploadtree
153  where upload_fk=$upload_pk and uploadtree.lft BETWEEN $lft and $rgt) as SS
154  where PF=pfile_fk and agent_fk=$agent_pk and hash='$hash' and type='$type'
155  $chkonly";
156 
157  $result = pg_query($PG_CONN, $sql); // Top uploadtree_pk's
158  DBCheckResult($result, $sql, __FILE__, __LINE__);
159 
160  $row = pg_fetch_assoc($result);
161  $FileCount = $row['unique'];
162  pg_free_result($result);
163  return $FileCount;
164 }
165 
166 
179 function StmtReorder($content)
180 {
181  return $content;
182 }
183 
184 
198 function MassageContent(&$row, $hash)
199 {
200  /* Step 1: Clean up content
201  */
202  $OriginalContent = $row['content'];
203 
204  /* remove control characters */
205  $content = preg_replace('/[\x0-\x1f]/', ' ', $OriginalContent);
206 
207  if ($row['type'] == 'statement') {
208  /* !"#$%&' */
209  $content = preg_replace('/([\x21-\x27])|([*@])/', ' ', $content);
210 
211  /* numbers-numbers, two or more digits, ', ' */
212  $content = preg_replace('/(([0-9]+)-([0-9]+))|([0-9]{2,})|(,)/', ' ', $content);
213  $content = preg_replace('/ : /', ' ', $content); // free :, probably followed a date
214  } elseif ($row['type'] == 'email') {
215  $content = str_replace(":;<=>()", " ", $content);
216  }
217 
218  /* remove double spaces */
219  $content = preg_replace('/\s\s+/', ' ', $content);
220 
221  /* remove leading/trailing whitespace and some punctuation */
222  $content = trim($content, "\t \n\r<>./\"\'");
223 
224  /* remove leading "dnl " */
225  if ((strlen($content) > 4) &&
226  (substr_compare($content, "dnl ", 0, 4, true) == 0)) {
227  $content = substr($content, 4);
228  }
229 
230  /* skip empty content */
231  if (empty($content)) {
232  return true;
233  }
234 
235  /* Step 1B: rearrange copyright statments to try and put the holder first,
236  * followed by the rest of the statement, less copyright years.
237  */
238  /* Not yet implemented
239  if ($row['type'] == 'statement') $content = $this->StmtReorder($content);
240  */
241 
242  // $row['original'] = $OriginalContent; // to compare original to new content
243  $row['content'] = $content;
244  $row['copyright_count'] = 1;
245  $row['hash'] = md5($row['content']);
246  if ($hash && ($row['hash'] != $hash)) {
247  return true;
248  }
249 
250  return false;
251 } /* End of MassageContent() */
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:187
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690
copyright_namecmp($rowa, $rowb)
Sort rows by filename.
Definition: library.php:66
hist_rowcmp_count_asc($a, $b)
Sort query histogram results (by content), ascend.
Definition: library.php:21
MassageContent(&$row, $hash)
Input row array contains: pfile, content and type.
Definition: library.php:198
hist_rowcmp_desc($rowa, $rowb)
Sort query histogram results (by content), descend.
Definition: library.php:55
StmtReorder($content)
rearrange copyright statment to try and put the holder first, followed by the rest of the statement.
Definition: library.php:179
GetFilesWithCopyright($agent_pk, $hash, $type, $uploadtree_pk, $PkgsOnly=false, $offset=0, $limit="ALL", $order="")
get files with a given copyright.
Definition: library.php:87
hist_rowcmp($rowa, $rowb)
Sort query histogram results (by content), ascend.
Definition: library.php:44
hist_rowcmp_count_desc($a, $b)
Sort query histogram results (by content), descend.
Definition: library.php:32
CountFilesWithCopyright($agent_pk, $hash, $type, $uploadtree_pk, $PkgsOnly=false, $CheckOnly=false)
Definition: library.php:133
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN