FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
agent-reuser.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 
25 namespace Fossology\Reuser;
26 
33 
34 include_once(dirname(__DIR__) . "/agent/version.php");
35 
41 {
42  const UPLOAD_TO_REUSE_SELECTOR_NAME = 'uploadToReuse';
43 
47  private $uploadDao;
48 
49  public function __construct()
50  {
51  $this->Name = "agent_reuser";
52  $this->Title = _("Reuse of License Clearing");
53  $this->AgentName = "reuser";
54 
55  parent::__construct();
56 
57  $this->uploadDao = $GLOBALS['container']->get('dao.upload');
58  }
59 
65  public function renderContent(&$vars)
66  {
67  $reuserPlugin = plugin_find('plugin_reuser');
68  return $reuserPlugin->renderContent($vars);
69  }
70 
76  public function renderFoot(&$vars)
77  {
78  $reuserPlugin = plugin_find('plugin_reuser');
79  return $reuserPlugin->renderFoot($vars);
80  }
81 
86  public function preInstall()
87  {
88  menu_insert("ParmAgents::" . $this->Title, 0, $this->Name);
89  }
90 
99  public function scheduleAgent($jobId, $uploadId, &$errorMsg, $request)
100  {
101  $reuseUploadPair = explode(',',
102  $request->get(self::UPLOAD_TO_REUSE_SELECTOR_NAME), 2);
103  if (count($reuseUploadPair) == 2) {
104  list($reuseUploadId, $reuseGroupId) = $reuseUploadPair;
105  } else {
106  $errorMsg .= 'no reuse upload id given';
107  return - 1;
108  }
109  $groupId = $request->get('groupId', Auth::getGroupId());
110 
111  $getReuseValue = $request->get('reuseMode') ?: array();
112  $reuserDependencies = array("agent_adj2nest");
113 
114  $reuseMode = UploadDao::REUSE_NONE;
115  foreach ($getReuseValue as $currentReuseValue) {
116  switch ($currentReuseValue) {
117  case 'reuseMain':
118  $reuseMode |= UploadDao::REUSE_MAIN;
119  break;
120  case 'reuseEnhanced':
121  $reuseMode |= UploadDao::REUSE_ENHANCED;
122  break;
123  case 'reuseConf':
124  $reuseMode |= UploadDao::REUSE_CONF;
125  break;
126  }
127  }
128 
129  $reuserDependencies = array_merge($reuserDependencies,
130  $this->getReuserDependencies($request));
131 
132  $this->createPackageLink($uploadId, $reuseUploadId, $groupId, $reuseGroupId,
133  $reuseMode);
134 
135  return $this->doAgentAdd($jobId, $uploadId, $errorMsg,
136  array_unique($reuserDependencies), $uploadId);
137  }
138 
147  protected function createPackageLink($uploadId, $reuseUploadId, $groupId, $reuseGroupId, $reuseMode=0)
148  {
149  /* @var $packageDao PackageDao */
150  $packageDao = $GLOBALS['container']->get('dao.package');
151  $newUpload = $this->uploadDao->getUpload($uploadId);
152  $uploadForReuse = $this->uploadDao->getUpload($reuseUploadId);
153 
154  $package = $packageDao->findPackageForUpload($reuseUploadId);
155 
156  if ($package === null) {
157  $packageName = StringOperation::getCommonHead($uploadForReuse->getFilename(), $newUpload->getFilename());
158  $package = $packageDao->createPackage($packageName ?: $uploadForReuse->getFilename());
159  $packageDao->addUploadToPackage($reuseUploadId, $package);
160  }
161 
162  $packageDao->addUploadToPackage($uploadId, $package);
163 
164  $this->uploadDao->addReusedUpload($uploadId, $reuseUploadId, $groupId, $reuseGroupId, $reuseMode);
165  }
166 
172  private function getReuserDependencies($request)
173  {
174  $dependencies = array();
175  if ($request->get("Check_agent_nomos", false)) {
176  $dependencies[] = "agent_nomos";
177  }
178  if ($request->get("Check_agent_monk", false)) {
179  $dependencies[] = "agent_monk";
180  }
181  if ($request->get("Check_agent_ojo", false)) {
182  $dependencies[] = "agent_ojo";
183  }
184  if ($request->get("Check_agent_ninka", false)) {
185  $dependencies[] = "agent_ninka";
186  }
187  return $dependencies;
188  }
189 }
190 
191 register_plugin(new ReuserAgentPlugin());
UI element for reuser during Uploading new package.
renderFoot(&$vars)
Render footer twig templates for plugin_reuser.
createPackageLink($uploadId, $reuseUploadId, $groupId, $reuseGroupId, $reuseMode=0)
Create links between old and new upload.
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
const UPLOAD_TO_REUSE_SELECTOR_NAME
Form element name for main license to reuse.
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...
static getGroupId()
Get the current user&#39;s group id.
Definition: Auth.php:78
renderContent(&$vars)
Render twig templates for plugin_reuser.
scheduleAgent($jobId, $uploadId, &$errorMsg, $request)
Get parameters from request and add to job queue.
doAgentAdd($jobId, $uploadId, &$errorMsg, $dependencies, $jqargs="", $jq_cmd_args=null)