FOSSology  4.4.0
Open Source License Compliance by Open Source Software
agent-reuser.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 
14 namespace Fossology\Reuser;
15 
22 use Symfony\Component\HttpFoundation\Request;
23 
24 include_once(dirname(__DIR__) . "/agent/version.php");
25 
31 {
32  const UPLOAD_TO_REUSE_SELECTOR_NAME = 'uploadToReuse';
33  const REUSE_MODE = 'reuseMode';
34 
38  private $uploadDao;
39 
40  public function __construct()
41  {
42  $this->Name = "agent_reuser";
43  $this->Title = _("Reuse of License Clearing");
44  $this->AgentName = "reuser";
45 
46  parent::__construct();
47 
48  $this->uploadDao = $GLOBALS['container']->get('dao.upload');
49  }
50 
56  public function renderContent(&$vars)
57  {
58  $reuserPlugin = plugin_find('plugin_reuser');
59  return $reuserPlugin->renderContent($vars);
60  }
61 
67  public function renderFoot(&$vars)
68  {
69  $reuserPlugin = plugin_find('plugin_reuser');
70  return $reuserPlugin->renderFoot($vars);
71  }
72 
77  public function preInstall()
78  {
79  menu_insert("ParmAgents::" . $this->Title, 0, $this->Name);
80  }
81 
90  public function scheduleAgent($jobId, $uploadId, &$errorMsg, $request)
91  {
92  $reuseUploadPair = explode(',',
93  $request->get(self::UPLOAD_TO_REUSE_SELECTOR_NAME), 2);
94  if (count($reuseUploadPair) == 2) {
95  list($reuseUploadId, $reuseGroupId) = $reuseUploadPair;
96  } else {
97  $errorMsg .= 'no reuse upload id given';
98  return - 1;
99  }
100  $groupId = $request->get('groupId', Auth::getGroupId());
101 
102  $getReuseValue = $request->get(self::REUSE_MODE) ?: array();
103  $reuserDependencies = array("agent_adj2nest");
104 
105  $reuseMode = UploadDao::REUSE_NONE;
106  foreach ($getReuseValue as $currentReuseValue) {
107  switch ($currentReuseValue) {
108  case 'reuseMain':
109  $reuseMode |= UploadDao::REUSE_MAIN;
110  break;
111  case 'reuseEnhanced':
112  $reuseMode |= UploadDao::REUSE_ENHANCED;
113  break;
114  case 'reuseConf':
115  $reuseMode |= UploadDao::REUSE_CONF;
116  break;
117  case 'reuseCopyright':
118  $reuseMode |= UploadDao::REUSE_COPYRIGHT;
119  break;
120  }
121  }
122 
123  list($agentDeps, $scancodeDeps) = $this->getReuserDependencies($request);
124  $reuserDependencies = array_unique(array_merge($reuserDependencies, $agentDeps));
125  if (!empty($scancodeDeps)) {
126  $reuserDependencies[] = $scancodeDeps;
127  }
128 
129  $this->createPackageLink($uploadId, $reuseUploadId, $groupId, $reuseGroupId,
130  $reuseMode);
131 
132  return $this->doAgentAdd($jobId, $uploadId, $errorMsg,
133  $reuserDependencies, $uploadId, null, $request);
134  }
135 
144  protected function createPackageLink($uploadId, $reuseUploadId, $groupId, $reuseGroupId, $reuseMode=0)
145  {
146  /* @var $packageDao PackageDao */
147  $packageDao = $GLOBALS['container']->get('dao.package');
148  $newUpload = $this->uploadDao->getUpload($uploadId);
149  $uploadForReuse = $this->uploadDao->getUpload($reuseUploadId);
150 
151  $package = $packageDao->findPackageForUpload($reuseUploadId);
152 
153  if ($package === null) {
154  $packageName = StringOperation::getCommonHead($uploadForReuse->getFilename(), $newUpload->getFilename());
155  $package = $packageDao->createPackage($packageName ?: $uploadForReuse->getFilename());
156  $packageDao->addUploadToPackage($reuseUploadId, $package);
157  }
158 
159  $packageDao->addUploadToPackage($uploadId, $package);
160 
161  $this->uploadDao->addReusedUpload($uploadId, $reuseUploadId, $groupId, $reuseGroupId, $reuseMode);
162  }
163 
169  private function getReuserDependencies($request)
170  {
171  $dependencies = array();
172  $scancodeDeps = [];
173  if ($request->get("Check_agent_nomos", false)) {
174  $dependencies[] = "agent_nomos";
175  }
176  if ($request->get("Check_agent_monk", false)) {
177  $dependencies[] = "agent_monk";
178  }
179  if ($request->get("Check_agent_ojo", false)) {
180  $dependencies[] = "agent_ojo";
181  }
182  if ($request->get("Check_agent_ninka", false)) {
183  $dependencies[] = "agent_ninka";
184  }
185  if ($request->get("Check_agent_copyright", false)) {
186  $dependencies[] = "agent_copyright";
187  }
188  if (!empty($request->get("scancodeFlags", []))) {
192  $agentScanCode = plugin_find('agent_scancode');
193  $flags = $request->get('scancodeFlags');
194  $unpackArgs = intval($request->get('scm', 0)) == 1 ? 'I' : '';
195  $scancodeDeps = [
196  "name" => "agent_scancode",
197  "args" => $agentScanCode->getScanCodeArgs($flags, $unpackArgs)
198  ];
199  }
200  return [$dependencies, $scancodeDeps];
201  }
202 }
203 
204 register_plugin(new ReuserAgentPlugin());
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
static getGroupId()
Get the current user's group id.
Definition: Auth.php:80
doAgentAdd($jobId, $uploadId, &$errorMsg, $dependencies, $jqargs="", $jq_cmd_args=null, $request=null)
UI element for reuser during Uploading new package.
scheduleAgent($jobId, $uploadId, &$errorMsg, $request)
Get parameters from request and add to job queue.
renderContent(&$vars)
Render twig templates for plugin_reuser.
const REUSE_MODE
Form element name for main license to reuse.
const UPLOAD_TO_REUSE_SELECTOR_NAME
Form element name for main license to reuse.
createPackageLink($uploadId, $reuseUploadId, $groupId, $reuseGroupId, $reuseMode=0)
Create links between old and new upload.
renderFoot(&$vars)
Render footer twig templates for plugin_reuser.
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.
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.