FOSSology  4.4.0
Open Source License Compliance by Open Source Software
LatestScannerProxy.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\Lib\Proxy;
9 
11 {
12  const ARS_SUFFIX = '_ars';
14  private $uploadId;
16  private $columns = 'agent_pk, agent_name';
17 
26  public function __construct($uploadId, $agentNames=array('nomos','monk'), $dbViewName='latest_scanner', $andEnabled = "AND agent_enabled")
27  {
28  if (empty($agentNames)) {
29  throw new \Exception('empty set of scanners');
30  }
31  $this->uploadId = $uploadId;
32  $subqueries = array();
33  foreach ($agentNames as $name) {
34  // NOTE: this query fails if the ars-table is not yet created.
35  $subqueries[] = "SELECT * FROM (SELECT $this->columns FROM $name".self::ARS_SUFFIX.", agent
36  WHERE agent_fk=agent_pk AND upload_fk=$uploadId $andEnabled ORDER BY agent_fk DESC limit 1) latest_$name";
37  }
38  $dbViewQuery = implode(' UNION ',$subqueries);
39  parent::__construct($dbViewQuery, $dbViewName."_".implode("_",$agentNames));
40  }
41 
42  public function materialize()
43  {
44  if (!is_int($this->uploadId)) {
45  throw new \Exception('cannot materialize LatestScannerProxy because upload Id is no number');
46  }
47  parent::materialize();
48  }
49 
53  public function getNameToIdMap()
54  {
55  if (!is_int($this->uploadId)) {
56  throw new \Exception('cannot map LatestScannerProxy because upload Id is no number');
57  }
58  global $container;
59  $dbManager = $container->get('db.manager');
60  $stmt = __METHOD__.".$this->dbViewName";
61  if ($this->materialized) {
62  $stmt .= '.m';
63  $sql = "SELECT * FROM $this->dbViewName";
64  } else {
65  $sql = $this->dbViewQuery;
66  }
67  $map = array();
68 
69  if (! empty($sql)) {
70  $dbManager->prepare($stmt, $sql);
71  $res = $dbManager->execute($stmt, array());
72  while ($row = $dbManager->fetchArray($res)) {
73  $map[$row['agent_name']] = $row['agent_pk'];
74  }
75  $dbManager->freeResult($res);
76  }
77  return $map;
78  }
79 }
__construct($uploadId, $agentNames=array('nomos', 'monk'), $dbViewName='latest_scanner', $andEnabled="AND agent_enabled")