FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
ReadMeOssPlugin.php
Go to the documentation of this file.
1 <?php
2 /*
3  Copyright (C) 2014-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 
30 
36 {
37  const NAME = 'ui_readmeoss';
38 
39  function __construct()
40  {
41  parent::__construct(self::NAME, array(
42  self::TITLE => _("ReadME_OSS generation"),
43  self::PERMISSION => Auth::PERM_WRITE,
44  self::REQUIRES_LOGIN => TRUE
45  ));
46  }
47 
52  function preInstall()
53  {
54  $text = _("Generate ReadMe_OSS");
55  menu_insert("Browse-Pfile::Export&nbsp;ReadMe_OSS", 0, self::NAME, $text);
56 
57  menu_insert("UploadMulti::Generate&nbsp;ReadMe_OSS", 0, self::NAME, $text);
58  }
59 
64  protected function handle(Request $request)
65  {
66  $groupId = Auth::getGroupId();
67  $uploadIds = $request->get('uploads') ?: array();
68  $uploadIds[] = intval($request->get('upload'));
69  $addUploads = array();
70  foreach ($uploadIds as $uploadId) {
71  if (empty($uploadId)) {
72  continue;
73  }
74  try {
75  $addUploads[$uploadId] = $this->getUpload($uploadId, $groupId);
76  } catch(Exception $e) {
77  return $this->flushContent($e->getMessage());
78  }
79  }
80  $folderId = $request->get('folder');
81  if (!empty($folderId)) {
82  /* @var $folderDao FolderDao */
83  $folderDao = $this->getObject('dao.folder');
84  $folderUploads = $folderDao->getFolderUploads($folderId, $groupId);
85  foreach ($folderUploads as $uploadProgress) {
86  $addUploads[$uploadProgress->getId()] = $uploadProgress;
87  }
88  }
89  if (empty($addUploads)) {
90  return $this->flushContent(_('No upload selected'));
91  }
92  $upload = array_pop($addUploads);
93  try {
94  list($jobId,$jobQueueId) = $this->getJobAndJobqueue($groupId, $upload, $addUploads);
95  } catch (Exception $ex) {
96  return $this->flushContent($ex->getMessage());
97  }
98 
99  $vars = array('jqPk' => $jobQueueId,
100  'downloadLink' => Traceback_uri(). "?mod=download&report=".$jobId,
101  'reportType' => "ReadMe_OSS");
102  $text = sprintf(_("Generating ReadMe_OSS for '%s'"), $upload->getFilename());
103  $vars['content'] = "<h2>".$text."</h2>";
104  $content = $this->renderer->loadTemplate("report.html.twig")->render($vars);
105  $message = '<h3 id="jobResult"></h3>';
106  $request->duplicate(array('injectedMessage'=>$message,'injectedFoot'=>$content,'mod'=>'showjobs'))->overrideGlobals();
107  $showJobsPlugin = \plugin_find('showjobs');
108  $showJobsPlugin->OutputOpen();
109  return $showJobsPlugin->getResponse();
110  }
111 
120  protected function getJobAndJobqueue($groupId, $upload, $addUploads)
121  {
122  $uploadId = $upload->getId();
123  $readMeOssAgent = plugin_find('agent_readmeoss');
124  $userId = Auth::getUserId();
125  $jqCmdArgs = $readMeOssAgent->uploadsAdd($addUploads);
126  $dbManager = $this->getObject('db.manager');
127  $sql = 'SELECT jq_pk,job_pk FROM jobqueue, job '
128  . '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';
129  $params = array($readMeOssAgent->AgentName,$uploadId,$userId,$groupId);
130  $log = __METHOD__;
131  if ($jqCmdArgs) {
132  $sql .= ' AND jq_cmd_args=$5';
133  $params[] = $jqCmdArgs;
134  $log .= '.args';
135  } else {
136  $sql .= ' AND jq_cmd_args IS NULL';
137  }
138  $scheduled = $dbManager->getSingleRow($sql,$params,$log);
139  if (!empty($scheduled)) {
140  return array($scheduled['job_pk'],$scheduled['jq_pk']);
141  }
142  if (empty($jqCmdArgs)) {
143  $jobName = $upload->getFilename();
144  } else {
145  $jobName = "Multi File ReadmeOSS";
146  }
147  $jobId = JobAddJob($userId, $groupId, $jobName, $uploadId);
148  $error = "";
149  $jobQueueId = $readMeOssAgent->AgentAdd($jobId, $uploadId, $error, array(), $jqCmdArgs);
150  if ($jobQueueId < 0) {
151  throw new Exception(_("Cannot schedule").": ".$error);
152  }
153  return array($jobId, $jobQueueId, $error);
154  }
155 
164  protected function getUpload($uploadId, $groupId)
165  {
166  if ($uploadId <= 0) {
167  throw new Exception(_("parameter error: $uploadId"));
168  }
169  /* @var $uploadDao UploadDao */
170  $uploadDao = $this->getObject('dao.upload');
171  if (!$uploadDao->isAccessible($uploadId, $groupId)) {
172  throw new Exception(_("permission denied"));
173  }
175  $upload = $uploadDao->getUpload($uploadId);
176  if ($upload === null) {
177  throw new Exception(_('cannot find uploadId'));
178  }
179  return $upload;
180  }
181 
191  public function scheduleAgent($groupId, $upload, $addUploads = array())
192  {
193  return $this->getJobAndJobqueue($groupId, $upload, $addUploads);
194  }
195 }
196 
197 register_plugin(new ReadMeOssPlugin());
Traceback_uri()
Get the URI without query to this location.
scheduleAgent($groupId, $upload, $addUploads=array())
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
handle(Request $request)
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...
Agent plugin for Readme_OSS agent.
const NAME
Mod name for the plugin.
#define PERM_WRITE
Read-Write permission.
Definition: libfossology.h:45
getJobAndJobqueue($groupId, $upload, $addUploads)
Get parameters from job queue and schedule them.