FOSSology  4.4.0
Open Source License Compliance by Open Source Software
SpashtDao.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2019 Vivek Kumar
4  Author: Vivek Kumar<vvksindia@gmail.com>
5  SPDX-FileCopyrightText: © 2022 Siemens AG
6 
7  SPDX-License-Identifier: GPL-2.0-only
8 */
9 
10 namespace Fossology\Lib\Dao;
11 
17 use Monolog\Logger;
19 
24 class SpashtDao
25 {
27  private $dbManager;
28 
33  function __construct(DbManager $dbManager, Logger $logger)
34  {
35  $this->dbManager = $dbManager;
36  $this->logger = $logger;
37  }
38 
46  public function addComponentRevision($coordinate, $uploadID)
47  {
48  $statement = __METHOD__.".AddingNewRevision";
49 
50  $keys = "spasht_type,spasht_provider,spasht_namespace,spasht_name," .
51  "spasht_revision,upload_fk";
52 
53  $params = [
54  $coordinate->getType(),
55  $coordinate->getProvider(),
56  $coordinate->getNamespace(),
57  $coordinate->getName(),
58  $coordinate->getRevision(),
59  $uploadID
60  ];
61 
62  return $this->dbManager->insertInto("spasht", $keys, $params, $statement,
63  "spasht_pk");
64  }
65 
73  public function alterComponentRevision($coordinate, $uploadID)
74  {
75  $assocParams = [
76  "spasht_type" => $coordinate->getType(),
77  "spasht_provider" => $coordinate->getProvider(),
78  "spasht_namespace" => $coordinate->getNamespace(),
79  "spasht_name" => $coordinate->getName(),
80  "spasht_revision" => $coordinate->getRevision()
81  ];
82 
83  $tableName = "spasht";
84  $primaryColumn = 'upload_fk';
85 
86  $this->dbManager->updateTableRow($tableName, $assocParams, $primaryColumn,
87  $uploadID, __METHOD__ . ".updateCoordinates");
88  return $uploadID;
89  }
90 
96  public function getComponent($uploadID)
97  {
98  $statement = __METHOD__.".CheckUpload";
99 
100  $params = [ $uploadID ];
101 
102  $sql = "SELECT * FROM spasht ".
103  "WHERE upload_fk = $1";
104 
105  $row = $this->dbManager->getSingleRow($sql, $params, $statement);
106  if (empty($row)) {
107  return null;
108  }
109  return $this->rowToCoordinate($row);
110  }
111 
117  public function getSpashtArs($uploadID)
118  {
119  $statement = __METHOD__.".CheckUpload";
120 
121  $params = [ $uploadID ];
122 
123  $sql = "SELECT * FROM spasht_ars ".
124  "WHERE upload_fk = $1 ORDER BY ars_pk DESC;";
125 
126  $row = $this->dbManager->getSingleRow($sql, $params, $statement);
127  return ($row);
128  }
129 
137  public function updateCopyright($item, $hash, $content, $action='')
138  {
139  $itemTable = $item->getUploadTreeTableName();
140  $stmt = __METHOD__ . "$itemTable";
141  $params = array($hash, $item->getLeft(), $item->getRight());
142 
143  if ($action == "delete") {
144  $setSql = "is_enabled='false'";
145  $stmt .= '.delete';
146  } else if ($action == "rollback") {
147  $setSql = "is_enabled='true'";
148  $stmt .= '.rollback';
149  } else {
150  $setSql = "textfinding = $4, hash = md5($4), is_enabled='true'";
151  $params[] = StringOperation::replaceUnicodeControlChar($content);
152  }
153 
154  $sql = "UPDATE copyright_spasht AS cpr SET $setSql
155  FROM copyright_spasht as cp
156  INNER JOIN $itemTable AS ut ON cp.pfile_fk = ut.pfile_fk
157  WHERE cpr.copyright_spasht_pk = cp.copyright_spasht_pk
158  AND cp.hash = $1
159  AND ( ut.lft BETWEEN $2 AND $3 )";
160  if ('uploadtree_a' == $item->getUploadTreeTableName()) {
161  $params[] = $item->getUploadId();
162  $sql .= " AND ut.upload_fk=$".count($params);
163  $stmt .= '.upload';
164  }
165  $this->dbManager->getSingleRow($sql, $params, $stmt);
166  }
167 
173  private function rowToCoordinate($row)
174  {
175  $obj = [
176  'type' => $row['spasht_type'],
177  'provider' => $row['spasht_provider'],
178  'namespace' => $row['spasht_namespace'],
179  'name' => $row['spasht_name'],
180  'revision' => $row['spasht_revision']
181  ];
182  return new Coordinate($obj);
183  }
184 
197  public function getCoordinateFromCompId($uploadId)
198  {
199  $sql = "SELECT ri_component_type, ri_component_id " .
200  "FROM report_info WHERE upload_fk = $1;";
201  $compRow = $this->dbManager->getSingleRow($sql, [$uploadId]);
202  if (
203  empty($compRow) || empty($compRow['ri_component_id'])
204  || $compRow['ri_component_id'] == "NA"
205  || (
206  $compRow['ri_component_type'] != ComponentType::PURL
207  && $compRow['ri_component_type'] != ComponentType::PACKAGEURL
208  )
209  ) {
210  return null;
211  }
212  $componentId = $compRow['ri_component_id'];
213  $purl = PurlOperations::fromString($componentId);
214  if ($purl["scheme"] != "pkg") {
215  return null;
216  }
217  return new Coordinate([
218  "type" => $purl["type"],
219  "provider" => $purl["type"],
220  "namespace" => $purl["namespace"],
221  "name" => $purl["name"],
222  "revision" => $purl["version"]
223  ]);
224  }
225 }
alterComponentRevision($coordinate, $uploadID)
Definition: SpashtDao.php:73
__construct(DbManager $dbManager, Logger $logger)
Definition: SpashtDao.php:33
addComponentRevision($coordinate, $uploadID)
Definition: SpashtDao.php:46
updateCopyright($item, $hash, $content, $action='')
Definition: SpashtDao.php:137
getCoordinateFromCompId($uploadId)
Get ClearlyDefined Coordinate if component id is a pURL.
Definition: SpashtDao.php:197
static replaceUnicodeControlChar($input, $replace="")
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16