FOSSology  4.4.0
Open Source License Compliance by Open Source Software
FoDecisionExporter.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2022 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
15 namespace Fossology\DecisionExporter\UI;
16 
17 use Exception;
21 use Symfony\Component\HttpFoundation\Request;
23 
29 {
30  const NAME = 'agent_fodecisionexporter';
31 
32  function __construct()
33  {
34  parent::__construct(self::NAME, array(
35  self::TITLE => _("FOSSology Dump Exporter"),
36  self::PERMISSION => Auth::PERM_WRITE,
37  self::REQUIRES_LOGIN => TRUE
38  ));
39  }
40 
45  protected function handle(Request $request)
46  {
47  $groupId = Auth::getGroupId();
48  $uploadId = intval($request->get('upload'));
49  try {
50  $upload = $this->getUpload($uploadId, $groupId);
51  } catch(Exception $e) {
52  return $this->flushContent($e->getMessage());
53  }
54 
55  list($jobId, $jobQueueId, $error) = $this->scheduleAgent($groupId, $upload);
56 
57  if ($jobQueueId < 0) {
58  return $this->flushContent(_('Cannot schedule').": $error");
59  }
60 
61  $vars = array('jqPk' => $jobQueueId,
62  'downloadLink' => Traceback_uri(). "?mod=download&report=".$jobId,
63  'reportType' => "dumpexporter");
64  $text = sprintf(_("Generating FOSSology Decisions for '%s'"), $upload->getFilename());
65  $vars['content'] = "<h2>".$text."</h2>";
66  $content = $this->renderer->load("report.html.twig")->render($vars);
67  $message = '<h3 id="jobResult"></h3>';
68  $request->duplicate(array('injectedMessage'=>$message,'injectedFoot'=>$content,'mod'=>'showjobs'))->overrideGlobals();
69  $showJobsPlugin = \plugin_find('showjobs');
70  $showJobsPlugin->OutputOpen();
71  return $showJobsPlugin->getResponse();
72  }
73 
81  protected function getUpload($uploadId, $groupId)
82  {
83  if ($uploadId <= 0) {
84  throw new Exception(_("parameter error"));
85  }
87  $uploadDao = $this->getObject('dao.upload');
88  if (!$uploadDao->isAccessible($uploadId, $groupId)) {
89  throw new Exception(_("permission denied"));
90  }
92  $upload = $uploadDao->getUpload($uploadId);
93  if ($upload === null) {
94  throw new Exception(_('cannot find uploadId'));
95  }
96  return $upload;
97  }
98 
103  function preInstall()
104  {
105  $text = _("Decision Dump Exporter");
106  menu_insert("Browse-Pfile::Export&nbsp;FOSSology&nbsp;Dump", 0, self::NAME, $text);
107  }
108 
116  public function scheduleAgent($groupId, $upload)
117  {
118  $decisionExpoAgent = plugin_find('agent_decisionexporter');
119  $userId = Auth::getUserId();
120  $uploadId = $upload->getId();
121  $jobId = JobAddJob($userId, $groupId, $upload->getFilename(), $uploadId);
122  $error = "";
123  $url = tracebackTotalUri();
124  $url = preg_replace("/api\/.*/i", "", $url); // Remove api/v1/report
125  $jobQueueId = $decisionExpoAgent->AgentAdd($jobId, $uploadId, $error, array(), $url);
126  return array($jobId, $jobQueueId, $error);
127  }
128 }
129 
130 register_plugin(new FoDecisionExporter());
FOSSology Decision Exporter UI plugin.
FOSSology Decision Exporter UI plugin.
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
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
tracebackTotalUri()
Get the total url without query.
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