FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
HistogramBase.php
1 <?php
2 /***********************************************************
3  * Copyright (C) 2014-2017 Siemens AG
4  * Author: J.Najjar
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  ***********************************************************/
19 
22 
28 abstract class HistogramBase extends FO_Plugin {
32  protected $agentName;
36  protected $viewName;
40  private $uploadtree_tablename;
44  private $uploadDao;
45 
46  function __construct()
47  {
48  $this->DBaccess = PLUGIN_DB_READ;
49  $this->LoginFlag = 0;
50 
51  parent::__construct();
52 
53  global $container;
54  $this->uploadDao = $container->get('dao.upload');
55  $this->renderer = $container->get('twig.environment');
56 
57  $this->vars['name']=$this->Name;
58  }
59 
70  public function getTableForSingleType($type, $description, $uploadId, $uploadTreeId, $filter, $agentId)
71  {
72  $sorting = json_encode($this->returnSortOrder());
73 
74  $out = array("type" => $type, "sorting" => $sorting, "uploadId" => $uploadId,
75  "uploadTreeId" => $uploadTreeId, "agentId" => $agentId, "filter" => $filter, "description" => $description);
76  $typeDescriptor = "";
77  if($type !== "statement")
78  {
79  $typeDescriptor = $description;
80  }
81  $output = "<h4>Activated $typeDescriptor statements:</h4>
82  <div><table border=1 width='100%' id='copyright".$type."'></table></div>
83  <br/><br/>
84  <div>
85  <span>
86  Replace: <input type='text' id='replaceText".$type."' style='width:80%'>
87  </span>
88  <br/><br/>
89  <a style='cursor: pointer; margin-left:10px;' id='replaceSelected".$type."' class='buttonLink'>Mark selected rows for replace</a>
90  <a style='cursor: pointer; margin-left:10px;' id='deleteSelected".$type."' class='buttonLink'>Mark selected rows for deletion</a>
91  <br/><br/>
92  <h4>Deactivated $typeDescriptor statements:</h4>
93  </div>
94  <div><table border=1 width='100%' id='copyright".$type."deactivated'></table></div>";
95 
96  return array($output, $out);
97  }
98 
99 
109  abstract protected function fillTables($upload_pk, $Uploadtree_pk, $filter, $agentId, $VF);
110 
123  protected function ShowUploadHist($upload_pk, $Uploadtree_pk, $Uri, $filter, $uploadtree_tablename, $Agent_pk)
124  {
125  list($ChildCount, $VF) = $this->getFileListing($Uploadtree_pk, $Uri, $uploadtree_tablename, $Agent_pk, $upload_pk);
126  $this->vars['childcount'] = $ChildCount;
127  $this->vars['fileListing'] = $VF;
128 
129  /***************************************
130  @todo
131  Problem: $ChildCount can be zero!
132  This happens if you have a container that does not
133  unpack to a directory. For example:
134  file.gz extracts to archive.txt that contains a license.
135  Same problem seen with .pdf and .Z files.
136  Solution: if $ChildCount == 0, then just view the license!
137  $ChildCount can also be zero if the directory is empty.
138  ***************************************/
139  if ($ChildCount == 0)
140  {
141  $isADirectory = $this->isADirectory($Uploadtree_pk);
142  if ($isADirectory) {
143  return;
144  }
145  $ModLicView = plugin_find($this->viewName);
146  return $ModLicView->execute();
147  }
148  return $this->fillTables($upload_pk, $Uploadtree_pk, $filter, $Agent_pk, $VF);
149  }
150 
155  function OutputOpen()
156  {
157  if ($this->State != PLUGIN_STATE_READY) {
158  return 0;
159  }
160  return parent::OutputOpen();
161  }
162 
167  public function Output()
168  {
169  $OutBuf="";
170  $uploadId = GetParm("upload",PARM_INTEGER);
171  $item = GetParm("item",PARM_INTEGER);
172  $filter = GetParm("filter",PARM_STRING);
173 
174  /* check upload permissions */
175  if (!$this->uploadDao->isAccessible($uploadId, Auth::getGroupId()))
176  {
177  $text = _("Permission Denied");
178  return "<h2>$text</h2>";
179  }
180 
181  /* Get uploadtree_tablename */
182  $uploadtree_tablename = GetUploadtreeTableName($uploadId);
183  $this->uploadtree_tablename = $uploadtree_tablename;
184 
185  /************************/
186  /* Show the folder path */
187  /************************/
188 
189  $this->vars['dir2browse'] = Dir2Browse($this->Name,$item,NULL,1,"Browse",-1,'','',$uploadtree_tablename);
190  if (empty($uploadId))
191  {
192  return 'no item selected';
193  }
194 
195  /* advanced interface allowing user to select dataset (agent version) */
196  $dataset = $this->agentName."_dataset";
197  $arstable = $this->agentName."_ars";
198  /* get proper agent_id */
199  $agentId = GetParm("agent", PARM_INTEGER);
200  if (empty($agentId))
201  {
202  $agentId = LatestAgentpk($uploadId, $arstable);
203  }
204 
205  if ($agentId == 0)
206  {
207  /* schedule copyright */
208  $OutBuf .= ActiveHTTPscript("Schedule");
209  $OutBuf .= "<script language='javascript'>\n";
210  $OutBuf .= "function Schedule_Reply()\n";
211  $OutBuf .= " {\n";
212  $OutBuf .= " if ((Schedule.readyState==4) && (Schedule.status==200 || Schedule.status==400))\n";
213  $OutBuf .= " document.getElementById('msgdiv').innerHTML = Schedule.responseText;\n";
214  $OutBuf .= " }\n";
215  $OutBuf .= "</script>\n";
216 
217  $OutBuf .= "<form name='formy' method='post'>\n";
218  $OutBuf .= "<div id='msgdiv'>\n";
219  $OutBuf .= _("No data available.");
220  $OutBuf .= "<input type='button' name='scheduleAgent' value='Schedule Agent'";
221  $OutBuf .= "onClick=\"Schedule_Get('" . Traceback_uri() . "?mod=schedule_agent&upload=$uploadId&agent=agent_{$this->agentName}')\">\n";
222  $OutBuf .= "</input>";
223  $OutBuf .= "</div> \n";
224  $OutBuf .= "</form>\n";
225 
226  $this->vars['pageContent'] = $OutBuf;
227  return;
228  }
229 
230  $AgentSelect = AgentSelect($this->agentName, $uploadId, $dataset, $agentId, "onchange=\"addArsGo('newds', 'copyright_dataset');\"");
231 
232  /* change the copyright result when selecting one version of copyright */
233  if (!empty($AgentSelect))
234  {
235  $action = Traceback_uri() . '?mod=' . GetParm('mod',PARM_RAW) . Traceback_parm_keep(array('upload','item'));
236 
237  $OutBuf .= "<script type='text/javascript'>
238  function addArsGo(formid, selectid)
239  {
240  var selectobj = document.getElementById(selectid);
241  var Agent_pk = selectobj.options[selectobj.selectedIndex].value;
242  document.getElementById(formid).action='$action'+'&agent='+Agent_pk;
243  document.getElementById(formid).submit();
244  return;
245  }
246  </script>";
247 
248  $OutBuf .= "<form action=\"$action\" id=\"newds\" method=\"POST\">$AgentSelect</form>";
249  }
250 
251  $selectKey = $filter == 'nolic' ? 'nolic' : 'all';
252  $OutBuf .= "<select name='view_filter' id='view_filter' onchange='ChangeFilter(this,$uploadId, $item);'>";
253  foreach(array('all'=>_("Show all"), 'nolic'=> _("Show files without licenses")) as $key=>$text)
254  {
255  $selected = ($selectKey == $key) ? "selected" : "";
256  $OutBuf .= "<option $selected value=\"$key\">$text</option>";
257  }
258  $OutBuf .= "</select>";
259 
260  $uri = preg_replace("/&item=([0-9]*)/", "", Traceback());
261  list($tables, $tableVars) = $this->ShowUploadHist($uploadId, $item, $uri, $selectKey, $uploadtree_tablename, $agentId);
262  $this->vars['tables'] = $tableVars;
263  $this->vars['pageContent'] = $OutBuf . $tables;
264  $this->vars['scriptBlock'] = $this->createScriptBlock();
265 
266  return;
267  }
268 
277  protected function getFileListing($Uploadtree_pk, $Uri, $uploadtree_tablename, $Agent_pk, $upload_pk)
278  {
279  $VF=""; // return values for file listing
280  /******* File Listing ************/
281  /* Get ALL the items under this Uploadtree_pk */
282  $Children = GetNonArtifactChildren($Uploadtree_pk, $uploadtree_tablename);
283  $ChildCount = 0;
284  $ChildLicCount = 0;
285  $ChildDirCount = 0; /* total number of directory or containers */
286  foreach ($Children as $c)
287  {
288  if (Iscontainer($c['ufile_mode']))
289  {
290  $ChildDirCount++;
291  }
292  }
293 
294  $VF .= "<table border=0>";
295  foreach ($Children as $child)
296  {
297  if (empty($child))
298  {
299  continue;
300  }
301  $ChildCount++;
302 
303  global $Plugins;
304  $ModLicView = &$Plugins[plugin_find_id($this->viewName)];
305  /* Determine the hyperlink for non-containers to view-license */
306  if (!empty($child['pfile_fk']) && !empty($ModLicView))
307  {
308  $LinkUri = Traceback_uri();
309  $LinkUri .= "?mod=".$this->viewName."&agent=$Agent_pk&upload=$upload_pk&item=$child[uploadtree_pk]";
310  } else
311  {
312  $LinkUri = NULL;
313  }
314 
315  /* Determine link for containers */
316  if (Iscontainer($child['ufile_mode']))
317  {
318  $uploadtree_pk = DirGetNonArtifact($child['uploadtree_pk'], $uploadtree_tablename);
319  $LicUri = "$Uri&item=" . $uploadtree_pk;
320  } else
321  {
322  $LicUri = NULL;
323  }
324 
325  /* Populate the output ($VF) - file list */
326  /* id of each element is its uploadtree_pk */
327  $LicCount = 0;
328 
329  $cellContent = Isdir($child['ufile_mode']) ? $child['ufile_name'].'/' : $child['ufile_name'];
330  if (Iscontainer($child['ufile_mode']))
331  {
332  $cellContent = "<a href='$LicUri'><b>$cellContent</b></a>";
333  }
334  else if (!empty($LinkUri)) // && ($LicCount > 0))
335  {
336  $cellContent = "<a href='$LinkUri'>$cellContent</a>";
337  }
338  $VF .= "<tr><td id='$child[uploadtree_pk]' align='left'>$cellContent</td><td>";
339 
340  if ($LicCount)
341  {
342  $VF .= "[" . number_format($LicCount, 0, "", ",") . "&nbsp;";
343  $VF .= "license" . ($LicCount == 1 ? "" : "s");
344  $VF .= "</a>";
345  $VF .= "]";
346  $ChildLicCount += $LicCount;
347  }
348  $VF .= "</td></tr>\n";
349  }
350  $VF .= "</table>\n";
351  return array($ChildCount, $VF);
352  }
353 
359  protected function isADirectory($Uploadtree_pk)
360  {
361  $row = $this->uploadDao->getUploadEntry($Uploadtree_pk, $this->uploadtree_tablename);
362  $isADirectory = IsDir($row['ufile_mode']);
363  return $isADirectory;
364  }
365 
370  public function returnSortOrder () {
371  $defaultOrder = array (
372  array(0, "desc"),
373  array(1, "desc"),
374  );
375  return $defaultOrder;
376  }
377 
382  public function getTemplateName()
383  {
384  return "copyrighthist.html.twig";
385  }
386 
391  abstract protected function createScriptBlock();
392 
393 }
fillTables($upload_pk, $Uploadtree_pk, $filter, $agentId, $VF)
Get copyright statements and fill the main content table.
FUNCTION char * GetUploadtreeTableName(PGconn *pgConn, int upload_pk)
Get the uploadtree table name for this upload_pk If upload_pk does not exist, return "uploadtree"...
Definition: libfossagent.c:421
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
Traceback_uri()
Get the URI without query to this location.
isADirectory($Uploadtree_pk)
Check if passed element is a directory.
createScriptBlock()
Create JavaScript block for histogram.
ShowUploadHist($upload_pk, $Uploadtree_pk, $Uri, $filter, $uploadtree_tablename, $Agent_pk)
Given an $Uploadtree_pk, display: (1) The histogram for the directory BY LICENSE. (2) The file listing for the directory.
int IsDir(char *Fname)
Given a filename, is it a directory?
Definition: utils.c:330
ActiveHTTPscript($RequestName, $IncludeScriptTags=1)
Given a function name, create the JavaScript needed for doing the request.
Iscontainer($mode)
Definition: common-dir.php:49
getTableForSingleType($type, $description, $uploadId, $uploadTreeId, $filter, $agentId)
OutputOpen()
This function is called when user output is requested. This function is responsible for assigning hea...
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
Definition: libfossology.h:49
DirGetNonArtifact($UploadtreePk, $uploadtree_tablename='uploadtree')
Given an artifact directory (uploadtree_pk), return the first non-artifact directory (uploadtree_pk)...
Definition: common-dir.php:169
Definition: state.hpp:26
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
LatestAgentpk($upload_pk, $arsTableName, $arsSuccess=false)
Given an upload_pk, find the latest enabled agent_pk with results.
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
const PARM_STRING
Definition: common-parm.php:29
AgentSelect($TableName, $upload_pk, $SLName, &$agent_pk, $extra="")
const PARM_INTEGER
Definition: common-parm.php:25
char * uploadtree_tablename
upload.uploadtree_tablename
Definition: adj2nest.c:112
Isdir($mode)
Definition: common-dir.php:31
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:67
getFileListing($Uploadtree_pk, $Uri, $uploadtree_tablename, $Agent_pk, $upload_pk)
Traceback_parm_keep($List)
Create a new URI, keeping only these items.
Output()
This function is called when user output is requested. This function is responsible for content...
Traceback()
Get the URI + query to this location.
returnSortOrder()
Get sorting orders.
Base class for histogram plugins.