FOSSology  4.4.0
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  'reportType' => "ReadMe_OSS");
93  $text = sprintf(_("Generating ReadMe_OSS for '%s'"), $upload->getFilename());
94  $vars['content'] = "<h2>".$text."</h2>";
95  $content = $this->renderer->load("report.html.twig")->render($vars);
96  $message = '<h3 id="jobResult"></h3>';
97  $request->duplicate(array('injectedMessage'=>$message,'injectedFoot'=>$content,'mod'=>'showjobs'))->overrideGlobals();
98  $showJobsPlugin = \plugin_find('showjobs');
99  $showJobsPlugin->OutputOpen();
100  return $showJobsPlugin->getResponse();
101  }
102 
111  protected function getJobAndJobqueue($groupId, $upload, $addUploads)
112  {
113  $uploadId = $upload->getId();
114  $readMeOssAgent = plugin_find('agent_readmeoss');
115  $userId = Auth::getUserId();
116  $jqCmdArgs = $readMeOssAgent->uploadsAdd($addUploads);
117  $dbManager = $this->getObject('db.manager');
118  $sql = 'SELECT jq_pk,job_pk FROM jobqueue, job '
119  . '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';
120  $params = array($readMeOssAgent->AgentName,$uploadId,$userId,$groupId);
121  $log = __METHOD__;
122  if ($jqCmdArgs) {
123  $sql .= ' AND jq_cmd_args=$5';
124  $params[] = $jqCmdArgs;
125  $log .= '.args';
126  } else {
127  $sql .= ' AND jq_cmd_args IS NULL';
128  }
129  $scheduled = $dbManager->getSingleRow($sql,$params,$log);
130  if (!empty($scheduled)) {
131  return array($scheduled['job_pk'],$scheduled['jq_pk']);
132  }
133  if (empty($jqCmdArgs)) {
134  $jobName = $upload->getFilename();
135  } else {
136  $jobName = "Multi File ReadmeOSS";
137  }
138  $jobId = JobAddJob($userId, $groupId, $jobName, $uploadId);
139  $error = "";
140  $jobQueueId = $readMeOssAgent->AgentAdd($jobId, $uploadId, $error, array(), $jqCmdArgs);
141  if ($jobQueueId < 0) {
142  throw new Exception(_("Cannot schedule").": ".$error);
143  }
144  return array($jobId, $jobQueueId, $error);
145  }
146 
155  protected function getUpload($uploadId, $groupId)
156  {
157  if ($uploadId <= 0) {
158  throw new Exception(_("parameter error: $uploadId"));
159  }
160  /* @var $uploadDao UploadDao */
161  $uploadDao = $this->getObject('dao.upload');
162  if (!$uploadDao->isAccessible($uploadId, $groupId)) {
163  throw new Exception(_("permission denied"));
164  }
166  $upload = $uploadDao->getUpload($uploadId);
167  if ($upload === null) {
168  throw new Exception(_('cannot find uploadId'));
169  }
170  return $upload;
171  }
172 
182  public function scheduleAgent($groupId, $upload, $addUploads = array())
183  {
184  return $this->getJobAndJobqueue($groupId, $upload, $addUploads);
185  }
186 }
187 
188 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