FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
SpdxTwoGeneratorUi.php
1 <?php
2 /*
3  Copyright (C) 2015-2018 Siemens AG
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 
19 namespace Fossology\SpdxTwo;
20 
26 
32 {
33  const NAME = 'ui_spdx2';
34  const DEFAULT_OUTPUT_FORMAT = "spdx2";
35 
38  protected $outputFormat = self::DEFAULT_OUTPUT_FORMAT;
39 
40  function __construct()
41  {
42  $possibleOutputFormat = trim(GetParm("outputFormat",PARM_STRING));
43  if (strcmp($possibleOutputFormat,"") !== 0 &&
44  strcmp($possibleOutputFormat,self::DEFAULT_OUTPUT_FORMAT) !== 0 &&
45  ctype_alnum($possibleOutputFormat)) {
46  $this->outputFormat = $possibleOutputFormat;
47  }
48  parent::__construct(self::NAME, array(
49  self::TITLE => _(strtoupper($this->outputFormat) . " generation"),
50  self::PERMISSION => Auth::PERM_WRITE,
51  self::REQUIRES_LOGIN => true
52  ));
53  }
54 
59  function preInstall()
60  {
61  $text = _("Generate SPDX report");
62  menu_insert("Browse-Pfile::Export&nbsp;SPDX&nbsp;RDF", 0, self::NAME, $text);
63  menu_insert("UploadMulti::Generate&nbsp;SPDX", 0, self::NAME, $text);
64 
65  $text = _("Generate SPDX report in tag:value format");
66  menu_insert("Browse-Pfile::Export&nbsp;SPDX&nbsp;tag:value", 0, self::NAME . '&outputFormat=spdx2tv', $text);
67 
68  $text = _("Generate Debian Copyright file");
69  menu_insert("Browse-Pfile::Export&nbsp;DEP5", 0, self::NAME . '&outputFormat=dep5', $text);
70  }
71 
76  protected function handle(Request $request)
77  {
78 
79  $groupId = Auth::getGroupId();
80  $uploadIds = $request->get('uploads') ?: array();
81  $uploadIds[] = intval($request->get('upload'));
82  $addUploads = array();
83  foreach ($uploadIds as $uploadId) {
84  if (empty($uploadId)) {
85  continue;
86  }
87  try {
88  $addUploads[$uploadId] = $this->getUpload($uploadId, $groupId);
89  } catch(Exception $e) {
90  return $this->flushContent($e->getMessage());
91  }
92  }
93  $folderId = $request->get('folder');
94  if (!empty($folderId)) {
95  /* @var $folderDao FolderDao */
96  $folderDao = $this->getObject('dao.folder');
97  $folderUploads = $folderDao->getFolderUploads($folderId, $groupId);
98  foreach ($folderUploads as $uploadProgress) {
99  $addUploads[$uploadProgress->getId()] = $uploadProgress;
100  }
101  }
102  if (empty($addUploads)) {
103  return $this->flushContent(_('No upload selected'));
104  }
105  $upload = array_pop($addUploads);
106  try {
107  list($jobId,$jobQueueId) = $this->getJobAndJobqueue($groupId, $upload, $addUploads);
108  } catch (\Exception $ex) {
109  return $this->flushContent($ex->getMessage());
110  }
111 
112  $vars = array('jqPk' => $jobQueueId,
113  'downloadLink' => Traceback_uri(). "?mod=download&report=".$jobId,
114  'reportType' => $this->outputFormat);
115  $text = sprintf(_("Generating ". $this->outputFormat . " report for '%s'"), $upload->getFilename());
116  $vars['content'] = "<h2>".$text."</h2>";
117  $content = $this->renderer->loadTemplate("report.html.twig")->render($vars);
118  $message = '<h3 id="jobResult"></h3>';
119  $request->duplicate(array('injectedMessage'=>$message,'injectedFoot'=>$content,'mod'=>'showjobs'))->overrideGlobals();
120  $showJobsPlugin = \plugin_find('showjobs');
121  $showJobsPlugin->OutputOpen();
122  return $showJobsPlugin->getResponse();
123  }
124 
130  protected function uploadsAdd($uploads)
131  {
132  if (count($uploads) == 0) {
133  return '';
134  }
135  return '--uploadsAdd='. implode(',', array_keys($uploads));
136  }
137 
146  protected function getJobAndJobqueue($groupId, $upload, $addUploads)
147  {
148  $uploadId = $upload->getId();
149  $spdxTwoAgent = plugin_find('agent_'.$this->outputFormat);
150  $userId = Auth::getUserId();
151  $jqCmdArgs = $this->uploadsAdd($addUploads);
152 
153  $dbManager = $this->getObject('db.manager');
154  $sql = 'SELECT jq_pk,job_pk FROM jobqueue, job '
155  . 'WHERE jq_job_fk=job_pk AND jq_type=$1 AND job_group_fk=$4 AND job_user_fk=$3 AND jq_args=$2 AND jq_endtime IS NULL';
156  $params = array($spdxTwoAgent->AgentName,$uploadId,$userId,$groupId);
157  $log = __METHOD__;
158  if ($jqCmdArgs) {
159  $sql .= ' AND jq_cmd_args=$5';
160  $params[] = $jqCmdArgs;
161  $log .= '.args';
162  } else {
163  $sql .= ' AND jq_cmd_args IS NULL';
164  }
165  $scheduled = $dbManager->getSingleRow($sql,$params,$log);
166  if (!empty($scheduled)) {
167  return array($scheduled['job_pk'],$scheduled['jq_pk']);
168  }
169  if (empty($jqCmdArgs)) {
170  $jobName = $upload->getFilename();
171  } else {
172  $jobName = "Multi File SPDX2";
173  }
174  $jobId = JobAddJob($userId, $groupId, $jobName, $uploadId);
175  $error = "";
176  $jobQueueId = $spdxTwoAgent->AgentAdd($jobId, $uploadId, $error, array(), $jqCmdArgs);
177  if ($jobQueueId < 0) {
178  throw new \Exception(_("Cannot schedule").": ".$error);
179  }
180  return array($jobId,$jobQueueId, $error);
181  }
182 
190  protected function getUpload($uploadId, $groupId)
191  {
192  if ($uploadId <= 0) {
193  throw new \Exception(_("parameter error: $uploadId"));
194  }
195  /* @var $uploadDao UploadDao */
196  $uploadDao = $this->getObject('dao.upload');
197  if (!$uploadDao->isAccessible($uploadId, $groupId)) {
198  throw new \Exception(_("permission denied"));
199  }
201  $upload = $uploadDao->getUpload($uploadId);
202  if ($upload === null) {
203  throw new \Exception(_('cannot find uploadId'));
204  }
205  return $upload;
206  }
207 
218  public function scheduleAgent($groupId, $upload,
219  $outputFormat = self::DEFAULT_OUTPUT_FORMAT, $addUploads = array())
220  {
221  $this->outputFormat = $outputFormat;
222  return $this->getJobAndJobqueue($groupId, $upload, $addUploads);
223  }
224 }
225 
226 register_plugin(new SpdxTwoGeneratorUi());
const DEFAULT_OUTPUT_FORMAT
Default report format.
Traceback_uri()
Get the URI without query to this location.
static getUserId()
Get the current user&#39;s id.
Definition: Auth.php:69
getJobAndJobqueue($groupId, $upload, $addUploads)
Get the Job ID and Job queue ID.
const NAME
Mod name of the plugin.
Namespace used by SPDX2 agent.
uploadsAdd($uploads)
Add multiple uploads to the report.
Call SPDX2 agent to generate report from UI.
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
const PARM_STRING
Definition: common-parm.php:29
scheduleAgent($groupId, $upload, $outputFormat=self::DEFAULT_OUTPUT_FORMAT, $addUploads=array())
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...
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:321
static getGroupId()
Get the current user&#39;s group id.
Definition: Auth.php:78
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:695