FOSSology  4.7.1
Open Source License Compliance by Open Source Software
ReadMeOssPlugin.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2018 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
13 namespace Fossology\ReadmeOSS\UI;
14 
20 use Symfony\Component\HttpFoundation\Request;
21 
27 {
28  const NAME = 'ui_readmeoss';
29 
30  function __construct()
31  {
32  parent::__construct(self::NAME, array(
33  self::TITLE => _("ReadME_OSS generation"),
34  self::PERMISSION => Auth::PERM_WRITE,
35  self::REQUIRES_LOGIN => TRUE
36  ));
37  }
38 
43  function preInstall()
44  {
45  $text = _("Generate ReadMe_OSS");
46  menu_insert("Browse-Pfile::Export&nbsp;ReadMe_OSS", 0, self::NAME, $text);
47 
48  menu_insert("UploadMulti::Generate&nbsp;ReadMe_OSS", 0, self::NAME, $text);
49  }
50 
55  protected function handle(Request $request)
56  {
57  $groupId = Auth::getGroupId();
58  $uploadIds = $request->get('uploads') ?: array();
59  $uploadIds[] = intval($request->get('upload'));
60  $addUploads = array();
61  foreach ($uploadIds as $uploadId) {
62  if (empty($uploadId)) {
63  continue;
64  }
65  try {
66  $addUploads[$uploadId] = $this->getUpload($uploadId, $groupId);
67  } catch(Exception $e) {
68  return $this->flushContent($e->getMessage());
69  }
70  }
71  $folderId = $request->get('folder');
72  if (!empty($folderId)) {
73  /* @var $folderDao FolderDao */
74  $folderDao = $this->getObject('dao.folder');
75  $folderUploads = $folderDao->getFolderUploads($folderId, $groupId);
76  foreach ($folderUploads as $uploadProgress) {
77  $addUploads[$uploadProgress->getId()] = $uploadProgress;
78  }
79  }
80  if (empty($addUploads)) {
81  return $this->flushContent(_('No upload selected'));
82  }
83  $upload = array_pop($addUploads);
84  try {
85  list($jobId,$jobQueueId) = $this->getJobAndJobqueue($groupId, $upload, $addUploads);
86  } catch (Exception $ex) {
87  return $this->flushContent($ex->getMessage());
88  }
89 
90  $vars = array('jqPk' => $jobQueueId,
91  'downloadLink' => Traceback_uri(). "?mod=download&report=".$jobId,
92  'showJobsLink' => Traceback_uri(). "?mod=showjobs&upload=".$upload->getId(),
93  'reportType' => "ReadMe_OSS");
94  $text = sprintf(_("Generating ReadMe_OSS for '%s'"), $upload->getFilename());
95  $vars['content'] = "<h2>".$text."</h2>";
96  $content = $this->renderer->load("report.html.twig")->render($vars);
97  $message = '<h3 id="jobResult"></h3>';
98  $request->duplicate(array('injectedMessage'=>$message,'injectedFoot'=>$content,'mod'=>'showjobs'))->overrideGlobals();
99  $showJobsPlugin = \plugin_find('showjobs');
100  $showJobsPlugin->OutputOpen();
101  return $showJobsPlugin->getResponse();
102  }
103 
112  protected function getJobAndJobqueue($groupId, $upload, $addUploads)
113  {
114  $uploadId = $upload->getId();
115  $readMeOssAgent = plugin_find('agent_readmeoss');
116  $userId = Auth::getUserId();
117  $jqCmdArgs = $readMeOssAgent->uploadsAdd($addUploads);
118  $dbManager = $this->getObject('db.manager');
119  $sql = 'SELECT jq_pk,job_pk FROM jobqueue, job '
120  . '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';
121  $params = array($readMeOssAgent->AgentName,$uploadId,$userId,$groupId);
122  $log = __METHOD__;
123  if ($jqCmdArgs) {
124  $sql .= ' AND jq_cmd_args=$5';
125  $params[] = $jqCmdArgs;
126  $log .= '.args';
127  } else {
128  $sql .= ' AND jq_cmd_args IS NULL';
129  }
130  $scheduled = $dbManager->getSingleRow($sql,$params,$log);
131  if (!empty($scheduled)) {
132  return array($scheduled['job_pk'],$scheduled['jq_pk']);
133  }
134  if (empty($jqCmdArgs)) {
135  $jobName = $upload->getFilename();
136  } else {
137  $jobName = "Multi File ReadmeOSS";
138  }
139  $jobId = JobAddJob($userId, $groupId, $jobName, $uploadId);
140  $error = "";
141  $jobQueueId = $readMeOssAgent->AgentAdd($jobId, $uploadId, $error, array(), $jqCmdArgs);
142  if ($jobQueueId < 0) {
143  throw new Exception(_("Cannot schedule").": ".$error);
144  }
145  return array($jobId, $jobQueueId, $error);
146  }
147 
156  protected function getUpload($uploadId, $groupId)
157  {
158  if ($uploadId <= 0) {
159  throw new Exception(_("parameter error: $uploadId"));
160  }
161  /* @var $uploadDao UploadDao */
162  $uploadDao = $this->getObject('dao.upload');
163  if (!$uploadDao->isAccessible($uploadId, $groupId)) {
164  throw new Exception(_("permission denied"));
165  }
167  $upload = $uploadDao->getUpload($uploadId);
168  if ($upload === null) {
169  throw new Exception(_('cannot find uploadId'));
170  }
171  return $upload;
172  }
173 
183  public function scheduleAgent($groupId, $upload, $addUploads = array())
184  {
185  return $this->getJobAndJobqueue($groupId, $upload, $addUploads);
186  }
187 }
188 
189 register_plugin(new ReadMeOssPlugin());
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
static getUserId()
Get the current user's id.
Definition: Auth.php:68
static getGroupId()
Get the current user's group id.
Definition: Auth.php:80
Agent plugin for Readme_OSS agent.
const NAME
Mod name for the plugin.
getJobAndJobqueue($groupId, $upload, $addUploads)
Get parameters from job queue and schedule them.
scheduleAgent($groupId, $upload, $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.
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:308