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 
37  private $uploadDao;
38 
39  public function __construct()
40  {
41  $this->Name = "agent_reuser";
42  $this->Title = _("Reuse of License Clearing");
43  $this->AgentName = "reuser";
44 
45  parent::__construct();
46 
47  $this->uploadDao = $GLOBALS['container']->get('dao.upload');
48  }
49 
55  public function renderContent(&$vars)
56  {
57  $reuserPlugin = plugin_find('plugin_reuser');
58  return $reuserPlugin->renderContent($vars);
59  }
60 
66  public function renderFoot(&$vars)
67  {
68  $reuserPlugin = plugin_find('plugin_reuser');
69  return $reuserPlugin->renderFoot($vars);
70  }
71 
76  public function preInstall()
77  {
78  menu_insert("ParmAgents::" . $this->Title, 0, $this->Name);
79  }
80 
89  public function scheduleAgent($jobId, $uploadId, &$errorMsg, $request)
90  {
91  $reuseUploadPair = explode(',',
92  $request->get(self::UPLOAD_TO_REUSE_SELECTOR_NAME), 2);
93  if (count($reuseUploadPair) == 2) {
94  list($reuseUploadId, $reuseGroupId) = $reuseUploadPair;
95  } else {
96  $errorMsg .= 'no reuse upload id given';
97  return - 1;
98  }
99  $groupId = $request->get('groupId', Auth::getGroupId());
100 
101  $getReuseValue = $request->get('reuseMode') ?: array();
102  $reuserDependencies = array("agent_adj2nest");
103 
104  $reuseMode = UploadDao::REUSE_NONE;
105  foreach ($getReuseValue as $currentReuseValue) {
106  switch ($currentReuseValue) {
107  case 'reuseMain':
108  $reuseMode |= UploadDao::REUSE_MAIN;
109  break;
110  case 'reuseEnhanced':
111  $reuseMode |= UploadDao::REUSE_ENHANCED;
112  break;
113  case 'reuseConf':
114  $reuseMode |= UploadDao::REUSE_CONF;
115  break;
116  case 'reuseCopyright':
117  $reuseMode |= UploadDao::REUSE_COPYRIGHT;
118  break;
119  }
120  }
121 
122  list($agentDeps, $scancodeDeps) = $this->getReuserDependencies($request);
123  $reuserDependencies = array_unique(array_merge($reuserDependencies, $agentDeps));
124  if (!empty($scancodeDeps)) {
125  $reuserDependencies[] = $scancodeDeps;
126  }
127 
128  $this->createPackageLink($uploadId, $reuseUploadId, $groupId, $reuseGroupId,
129  $reuseMode);
130 
131  return $this->doAgentAdd($jobId, $uploadId, $errorMsg,
132  $reuserDependencies, $uploadId, null, $request);
133  }
134 
143  protected function createPackageLink($uploadId, $reuseUploadId, $groupId, $reuseGroupId, $reuseMode=0)
144  {
145  /* @var $packageDao PackageDao */
146  $packageDao = $GLOBALS['container']->get('dao.package');
147  $newUpload = $this->uploadDao->getUpload($uploadId);
148  $uploadForReuse = $this->uploadDao->getUpload($reuseUploadId);
149 
150  $package = $packageDao->findPackageForUpload($reuseUploadId);
151 
152  if ($package === null) {
153  $packageName = StringOperation::getCommonHead($uploadForReuse->getFilename(), $newUpload->getFilename());
154  $package = $packageDao->createPackage($packageName ?: $uploadForReuse->getFilename());
155  $packageDao->addUploadToPackage($reuseUploadId, $package);
156  }
157 
158  $packageDao->addUploadToPackage($uploadId, $package);
159 
160  $this->uploadDao->addReusedUpload($uploadId, $reuseUploadId, $groupId, $reuseGroupId, $reuseMode);
161  }
162 
168  private function getReuserDependencies($request)
169  {
170  $dependencies = array();
171  $scancodeDeps = [];
172  if ($request->get("Check_agent_nomos", false)) {
173  $dependencies[] = "agent_nomos";
174  }
175  if ($request->get("Check_agent_monk", false)) {
176  $dependencies[] = "agent_monk";
177  }
178  if ($request->get("Check_agent_ojo", false)) {
179  $dependencies[] = "agent_ojo";
180  }
181  if ($request->get("Check_agent_ninka", false)) {
182  $dependencies[] = "agent_ninka";
183  }
184  if ($request->get("Check_agent_copyright", false)) {
185  $dependencies[] = "agent_copyright";
186  }
187  if (!empty($request->get("scancodeFlags", []))) {
191  $agentScanCode = plugin_find('agent_scancode');
192  $flags = $request->get('scancodeFlags');
193  $unpackArgs = intval($request->get('scm', 0)) == 1 ? 'I' : '';
194  $scancodeDeps = [
195  "name" => "agent_scancode",
196  "args" => $agentScanCode->getScanCodeArgs($flags, $unpackArgs)
197  ];
198  }
199  return [$dependencies, $scancodeDeps];
200  }
201 }
202 
203 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 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.