FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
LatestScannerProxy.php
1 <?php
2 /*
3 Copyright (C) 2014, 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 
19 namespace Fossology\Lib\Proxy;
20 
22 {
23  const ARS_SUFFIX = '_ars';
25  private $uploadId;
27  private $columns = 'agent_pk, agent_name';
28 
37  public function __construct($uploadId, $agentNames=array('nomos','monk'), $dbViewName='latest_scanner', $andEnabled = "AND agent_enabled")
38  {
39  if (empty($agentNames)) {
40  throw new \Exception('empty set of scanners');
41  }
42  $this->uploadId = $uploadId;
43  $subqueries = array();
44  foreach ($agentNames as $name) {
45  // NOTE: this query fails if the ars-table is not yet created.
46  $subqueries[] = "SELECT * FROM (SELECT $this->columns FROM $name".self::ARS_SUFFIX.", agent
47  WHERE agent_fk=agent_pk AND upload_fk=$uploadId $andEnabled ORDER BY agent_fk DESC limit 1) latest_$name";
48  }
49  $dbViewQuery = implode(' UNION ',$subqueries);
50  parent::__construct($dbViewQuery, $dbViewName."_".implode("_",$agentNames));
51  }
52 
53  public function materialize()
54  {
55  if (!is_int($this->uploadId)) {
56  throw new \Exception('cannot materialize LatestScannerProxy because upload Id is no number');
57  }
58  parent::materialize();
59  }
60 
64  public function getNameToIdMap()
65  {
66  if (!is_int($this->uploadId)) {
67  throw new \Exception('cannot map LatestScannerProxy because upload Id is no number');
68  }
69  global $container;
70  $dbManager = $container->get('db.manager');
71  $stmt = __METHOD__.".$this->dbViewName";
72  if ($this->materialized) {
73  $stmt .= '.m';
74  $sql = "SELECT * FROM $this->dbViewName";
75  } else {
76  $sql = $this->dbViewQuery;
77  }
78  $map = array();
79 
80  if (! empty($sql)) {
81  $dbManager->prepare($stmt, $sql);
82  $res = $dbManager->execute($stmt, array());
83  while ($row = $dbManager->fetchArray($res)) {
84  $map[$row['agent_name']] = $row['agent_pk'];
85  }
86  $dbManager->freeResult($res);
87  }
88  return $map;
89  }
90 }
__construct($uploadId, $agentNames=array('nomos','monk'), $dbViewName='latest_scanner', $andEnabled="AND agent_enabled")