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