FOSSology  4.4.0
Open Source License Compliance by Open Source Software
ui-license-list-files.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2009-2014 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
13 
21 define("TITLE_LICENSE_LIST_FILES", _("List Files for License"));
22 
24 {
26  private $dbManager;
27 
29  private $uploadDao;
30 
32  private $licenseDao;
33 
35  private $agentDao;
36 
38  protected $agentNames = AgentRef::AGENT_LIST;
39 
40  function __construct()
41  {
42  $this->Name = "license_list_files";
43  $this->Title = TITLE_LICENSE_LIST_FILES;
44  $this->Dependency = array("browse", "view");
45  $this->DBaccess = PLUGIN_DB_READ;
46  $this->LoginFlag = 0;
47  parent::__construct();
48  $this->dbManager = $GLOBALS['container']->get('db.manager');
49  $this->uploadDao = $GLOBALS['container']->get('dao.upload');
50  $this->licenseDao = $GLOBALS['container']->get('dao.license');
51  $this->agentDao = $GLOBALS['container']->get('dao.agent');
52  }
53 
57  function RegisterMenus()
58  {
59  if ($this->State != PLUGIN_STATE_READY) {
60  return (0);
61  }
62 
63  // micro-menu
64  $uploadtree_pk = GetParm("item", PARM_INTEGER);
65  $rf_shortname = GetParm("lic", PARM_RAW);
66  $Excl = GetParm("excl", PARM_RAW);
67  $URL = $this->Name . "&item=$uploadtree_pk&lic=".urlencode($rf_shortname)."&page=-1";
68  if (!empty($Excl)) {
69  $URL .= "&excl=$Excl";
70  }
71  $text = _("Show All Files");
72  menu_insert($this->Name . "::Show All", 0, $URL, $text);
73  } // RegisterMenus()
74 
78  function Output()
79  {
80  $uploadtree_pk = GetParm("item", PARM_INTEGER);
81  $rf_shortname = GetParm("lic", PARM_RAW);
82  $tag_pk = GetParm("tag", PARM_INTEGER);
83  $Excl = GetParm("excl", PARM_RAW);
84  $Exclic = GetParm("exclic", PARM_RAW);
85  if (empty($uploadtree_pk) || empty($rf_shortname)) {
86  $text = _("is missing required parameters.");
87  return $this->Name . " $text";
88  }
89 
90  $Max = 50;
91  $Page = GetParm("page", PARM_INTEGER);
92  if (empty($Page)) {
93  $Page = 0;
94  }
95 
96  // Get upload_pk and $uploadtree_tablename
97  $UploadtreeRec = GetSingleRec("uploadtree", "where uploadtree_pk=$uploadtree_pk");
98  $uploadtree_tablename = $this->uploadDao->getUploadtreeTableName($UploadtreeRec['upload_fk']);
99 
100  // micro menus
101  $V = menu_to_1html(menu_find($this->Name, $MenuDepth), 0);
102 
103  /* Load licenses */
104  $Offset = ($Page < 0) ? 0 : $Page * $Max;
105  $order = "";
106  $PkgsOnly = false;
107 
108  // Count is uploadtree recs, not pfiles
109  $agentId = GetParm('agentId', PARM_INTEGER);
110  if (empty($agentId)) {
111  $scannerAgents = array_keys($this->agentNames);
112  $scanJobProxy = new ScanJobProxy($this->agentDao, $UploadtreeRec['upload_fk']);
113  $scannerVars = $scanJobProxy->createAgentStatus($scannerAgents);
114  $agentId = $scanJobProxy->getLatestSuccessfulAgentIds();
115  }
116  $CountArray = $this->countFilesWithLicense($agentId, $rf_shortname, $uploadtree_pk, $tag_pk, $uploadtree_tablename);
117 
118  if (empty($CountArray)) {
119  $V .= _("<b> No files found for license $rf_shortname !</b>\n");
120  } else {
121  $Count = $CountArray['count'];
122  $Unique = $CountArray['unique'];
123 
124  $text = _("files found");
125  $text2 = _("with license");
126  $text3 = _("files are unique with same file hash.");
127  $V .= "Total $Count $text $text2 <b>$rf_shortname</b>, $Unique $text3";
128  if ($Count < $Max) {
129  $Max = $Count;
130  }
131  $limit = ($Page < 0) ? "ALL" : $Max;
132  $order = " order by ufile_name asc";
134  $filesresult = GetFilesWithLicense($agentId, $rf_shortname, $uploadtree_pk,
135  $PkgsOnly, $Offset, $limit, $order, $tag_pk, $uploadtree_tablename);
136  $NumFiles = pg_num_rows($filesresult);
137 
138  $file_result_temp = pg_fetch_all($filesresult);
139  $sorted_file_result = array(); // the final file list will display
140  $max_num = $NumFiles;
142  for ($i = 0; $i < $max_num; $i++) {
143  $row = $file_result_temp[$i];
144  if (empty($row)) {
145  continue;
146  }
147  $sorted_file_result[] = $row;
148  for ($j = $i + 1; $j < $max_num; $j ++) {
149  $row_next = $file_result_temp[$j];
150  if (! empty($row_next) && ($row['pfile_fk'] == $row_next['pfile_fk'])) {
151  $sorted_file_result[] = $row_next;
152  $file_result_temp[$j] = null;
153  }
154  }
155  }
156 
157  $text = _("Display");
158  $text1 = _("excludes");
159  $text2 = _("files with these extensions");
160  if (! empty($Excl)) {
161  $V .= "<br>$text <b>$text1</b> $text2: $Excl";
162  }
163 
164  $text2 = _("files with these licenses");
165  if (!empty($Exclic)) {
166  $V .= "<br>$text <b>$text1</b> $text2: $Exclic";
167  }
168 
169  /* Get the page menu */
170  if (($Max > 0) && ($Count >= $Max) && ($Page >= 0)) {
171  $VM = "<P />\n" . MenuEndlessPage($Page, intval((($Count + $Offset) / $Max))) . "<P />\n";
172  $V .= $VM;
173  } else {
174  $VM = "";
175  }
176 
177  /* Offset is +1 to start numbering from 1 instead of zero */
178  $RowNum = $Offset;
179  $LinkLast = "view-license";
180  $ShowBox = 1;
181  $ShowMicro = null;
182 
183  // base url
184  $ushortname = rawurlencode($rf_shortname);
185  $baseURL = "?mod=" . $this->Name . "&item=$uploadtree_pk&lic=$ushortname&page=-1";
186 
187  $V .= "<table>";
188  $LastPfilePk = -1;
189  $ExclArray = explode(":", $Excl);
190  $ExclicArray = explode(":", $Exclic);
191  foreach ($sorted_file_result as $row) {
192  $pfile_pk = $row['pfile_fk'];
193  $licstring = GetFileLicenses_string($row['agent_pk'], $pfile_pk, $row['uploadtree_pk'], $uploadtree_tablename);
194  $URLlicstring = urlencode($licstring);
195 
196  // Allow user to exclude files with this extension
197  $FileExt = GetFileExt($row['ufile_name']);
198  $URL = $baseURL;
199  if (!empty($Excl)) {
200  $URL .= "&excl=$Excl:$FileExt";
201  } else {
202  $URL .= "&excl=$FileExt";
203  }
204  if (!empty($Exclic)) {
205  $URL .= "&exclic=" . urlencode($Exclic);
206  }
207  $text = _("Exclude this file type.");
208  $Header = "<a href=$URL>$text</a>";
209 
210  /* Allow user to exclude files with this exact license list */
211  $URL = $baseURL;
212  if (!empty($Exclic)) {
213  $URL .= "&exclic=" . urlencode($Exclic) . ":" . $URLlicstring;
214  } else {
215  $URL .= "&exclic=$URLlicstring";
216  }
217  if (!empty($Excl)) {
218  $URL .= "&excl=$Excl";
219  }
220 
221  $text = _("Exclude files with license");
222  $Header .= "<br><a href=$URL>$text: $licstring.</a>";
223 
224  $excludeByType = $Excl && in_array($FileExt, $ExclArray);
225  $excludeByLicense = $Exclic && in_array($licstring, $ExclicArray);
226 
227  if (!empty($licstring) && !$excludeByType && !$excludeByLicense) {
228  $V .= "<tr><td>";
229  /* Tack on pfile to url - information only */
230  $LinkLastpfile = $LinkLast . "&pfile=$pfile_pk";
231  if ($LastPfilePk == $pfile_pk) {
232  $indent = "<div style='margin-left:2em;'>";
233  $outdent = "</div>";
234  } else {
235  $indent = "";
236  $outdent = "";
237  }
238  $V .= $indent;
239  $V .= Dir2Browse("browse", $row['uploadtree_pk'], $LinkLastpfile,
240  $ShowBox, $ShowMicro, ++$RowNum, $Header, '', $uploadtree_tablename);
241  $V .= $outdent;
242  $V .= "</td>";
243  $V .= "<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>";
244  $V .= "<td>$row[agent_name]: $licstring</td></tr>";
245  $V .= "<tr><td colspan=3><hr></td></tr>";
246  }
247  $LastPfilePk = $pfile_pk;
248  }
249  pg_free_result($filesresult);
250  $V .= "</table>";
251 
252  if (!empty($VM)) {
253  $V .= $VM . "\n";
254  }
255  }
256 
257  return $V;
258  }
259 
270  protected function countFilesWithLicense($agent_pk, $rf_shortname, $uploadtree_pk, $tag_pk, $uploadtree_tablename)
271  {
272  $license = $this->licenseDao->getLicenseByShortname($rf_shortname);
273  if (null == $license) {
274  return array();
275  }
276  $itemBounds = $this->uploadDao->getItemTreeBounds($uploadtree_pk, $uploadtree_tablename);
277 
278  $viewRelavantFiles = "SELECT pfile_fk as PF, uploadtree_pk, ufile_name FROM $uploadtree_tablename";
279  $params = array();
280  $stmt = __METHOD__;
281  if (!empty($tag_pk)) {
282  $params[] = $tag_pk;
283  $viewRelavantFiles .= " INNER JOIN tag_file ON PF=tag_file.pfile_fk and tag_fk=$".count($params);
284  $stmt .= '.tag';
285  }
286  $params[] = $itemBounds->getLeft();
287  $params[] = $itemBounds->getRight();
288  $viewRelavantFiles .= ' WHERE lft BETWEEN $'.(count($params)-1).' AND $'.count($params);
289  if ($uploadtree_tablename == "uploadtree_a" || $uploadtree_tablename == "uploadtree") {
290  $params[] = $itemBounds->getUploadId();
291  $viewRelavantFiles .= " AND upload_fk=$".count($params);
292  $stmt .= '.upload';
293  }
294 
295  $params[] = $license->getId();
296  $sql = "SELECT count(license_file.pfile_fk) as count, count(distinct license_file.pfile_fk) as unique
297  FROM license_file, ($viewRelavantFiles) as SS
298  WHERE PF=license_file.pfile_fk AND rf_fk = $".count($params);
299 
300  if (is_array($agent_pk)) {
301  $params[] = '{' . implode(',', $agent_pk) . '}';
302  $sql .= ' AND agent_fk=ANY($'.count($params).')';
303  $stmt .= '.agents';
304  } elseif (!empty($agent_pk)) {
305  $params[] = $agent_pk;
306  $sql .= " AND agent_fk=$".count($params);
307  $stmt .= '.agent';
308  }
309 
310  $RetArray = $this->dbManager->getSingleRow($sql,$params,$stmt);
311  return $RetArray;
312  }
313 }
314 $NewPlugin = new LicenseListFiles();
315 $NewPlugin->Initialize();
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
Output()
Display the loaded menu and plugins.
__construct()
base constructor. Most plugins will just use this
RegisterMenus()
Customize submenus.
countFilesWithLicense($agent_pk, $rf_shortname, $uploadtree_pk, $tag_pk, $uploadtree_tablename)
Cloned from commen-license-file.php to refactor it.
Definition: state.hpp:16
GetSingleRec($Table, $Where="")
Retrieve a single database record.
Definition: common-db.php:91
Dir2Browse($Mod, $UploadtreePk, $LinkLast=NULL, $ShowBox=1, $ShowMicro=NULL, $Enumerate=-1, $PreText='', $PostText='', $uploadtree_tablename="uploadtree")
Get an html linked string of a file browse path.
Definition: common-dir.php:263
GetFilesWithLicense($agent_pk, $rf_shortname, $uploadtree_pk, $PkgsOnly=false, $offset=0, $limit="ALL", $order="", $tag_pk=null, $uploadtree_tablename="uploadtree")
Get files with a given license (shortname).
GetFileLicenses_string($agent_pk, $pfile_pk, $uploadtree_pk, $uploadtree_tablename='uploadtree')
Same as GetFileLicenses() but returns license list as a single string.
MenuEndlessPage($Page, $Next=1, $Uri='')
Create a "First Prev 1 2 ... Next" page links for paged output.
menu_insert($Path, $LastOrder=0, $URI=NULL, $Title=NULL, $Target=NULL, $HTML=NULL)
Given a Path, order level for the last item, and optional plugin name, insert the menu item.
menu_find($Name, &$MaxDepth, $Menu=NULL)
Given a top-level menu name, find the list of sub-menus below it and max depth of menu.
menu_to_1html($Menu, $ShowRefresh=1, $ShowTraceback=0, $ShowAll=1)
Take a menu and render it as one HTML line.
const PARM_INTEGER
Definition: common-parm.php:14
const PARM_RAW
Definition: common-parm.php:22
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:46
GetFileExt($fname)
Get File Extension (text after last period)
Definition: common-ui.php:142
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
Definition: libfossology.h:37
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16