FOSSology  4.7.1
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  'showJobsLink' => Traceback_uri(). "?mod=showjobs&upload=".$upload->getId(),
64  'reportType' => "dumpexporter");
65  $text = sprintf(_("Generating FOSSology Decisions for '%s'"), $upload->getFilename());
66  $vars['content'] = "<h2>".$text."</h2>";
67  $content = $this->renderer->load("report.html.twig")->render($vars);
68  $message = '<h3 id="jobResult"></h3>';
69  $request->duplicate(array('injectedMessage'=>$message,'injectedFoot'=>$content,'mod'=>'showjobs'))->overrideGlobals();
70  $showJobsPlugin = \plugin_find('showjobs');
71  $showJobsPlugin->OutputOpen();
72  return $showJobsPlugin->getResponse();
73  }
74 
82  protected function getUpload($uploadId, $groupId)
83  {
84  if ($uploadId <= 0) {
85  throw new Exception(_("parameter error"));
86  }
88  $uploadDao = $this->getObject('dao.upload');
89  if (!$uploadDao->isAccessible($uploadId, $groupId)) {
90  throw new Exception(_("permission denied"));
91  }
93  $upload = $uploadDao->getUpload($uploadId);
94  if ($upload === null) {
95  throw new Exception(_('cannot find uploadId'));
96  }
97  return $upload;
98  }
99 
104  function preInstall()
105  {
106  $text = _("Decision Dump Exporter");
107  menu_insert("Browse-Pfile::Export&nbsp;FOSSology&nbsp;Dump", 0, self::NAME, $text);
108  }
109 
117  public function scheduleAgent($groupId, $upload)
118  {
119  $decisionExpoAgent = plugin_find('agent_decisionexporter');
120  $userId = Auth::getUserId();
121  $uploadId = $upload->getId();
122  $jobId = JobAddJob($userId, $groupId, $upload->getFilename(), $uploadId);
123  $error = "";
124  $url = tracebackTotalUri();
125  $url = preg_replace("/api\/.*/i", "", $url); // Remove api/v1/report
126  $jobQueueId = $decisionExpoAgent->AgentAdd($jobId, $uploadId, $error, array(), $url);
127  return array($jobId, $jobQueueId, $error);
128  }
129 }
130 
131 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