FOSSology  4.4.0
Open Source License Compliance by Open Source Software
SoftwareHeritageDao.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2019 Sandip Kumar Bhuyan
4  Author: Sandip Kumar Bhuyan<sandipbhuyan@gmail.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\Lib\Dao;
10 
12 use Monolog\Logger;
13 
19 {
20 
21  const SWH_STATUS_OK = 200;
22  const SWH_BAD_REQUEST = 400;
23  const SWH_NOT_FOUND = 404;
24  const SWH_RATELIMIT_EXCEED = 429;
25 
27  private $dbManager;
29  private $logger;
31  private $uploadDao;
32 
33  public function __construct(DbManager $dbManager, Logger $logger,UploadDao $uploadDao)
34  {
35  $this->dbManager = $dbManager;
36  $this->logger = $logger;
37  $this->uploadDao = $uploadDao;
38  }
39 
45  public function getSoftwareHeritagePfileFk($uploadId)
46  {
47  $uploadTreeTableName = $this->uploadDao->getUploadtreeTableName($uploadId);
48  $stmt = __METHOD__.$uploadTreeTableName;
49  $sql = "SELECT DISTINCT(SWH.pfile_fk) FROM $uploadTreeTableName UT
50  INNER JOIN software_heritage SWH ON SWH.pfile_fk = UT.pfile_fk
51  WHERE UT.upload_fk = $1";
52  return $this->dbManager->getRows($sql,array($uploadId),$stmt);
53  }
54 
55 
62  public function setSoftwareHeritageDetails($pfileId, $licenseDetails, $status)
63  {
64  if (!empty($this->dbManager->insertTableRow('software_heritage',['pfile_fk' => $pfileId, 'swh_shortnames' => $licenseDetails, 'swh_status' => $status]))) {
65  return true;
66  }
67  return false;
68  }
69 
75  public function getSoftwareHetiageRecord($pfileId)
76  {
77  $stmt = __METHOD__ . "getSoftwareHeritageRecord";
78  $row = $this->dbManager->getSingleRow(
79  "SELECT swh_shortnames, swh_status FROM software_heritage WHERE pfile_fk = $1",
80  array($pfileId), $stmt);
81  if (empty($row)) {
82  $row = [
83  'swh_status' => null,
84  'swh_shortnames' => null
85  ];
86  }
87  $img = '<img alt="done" src="images/red.png" class="icon-small"/>';
88  if (self::SWH_STATUS_OK == $row['swh_status']) {
89  $img = '<img alt="done" src="images/green.png" class="icon-small"/>';
90  }
91  return ["license" => $row['swh_shortnames'], "img" => $img];
92  }
93 }
getSoftwareHetiageRecord($pfileId)
Get a record from Software Heritage schema from the PfileId.
getSoftwareHeritagePfileFk($uploadId)
Get all the pfile_fk stored in software heritage table.
setSoftwareHeritageDetails($pfileId, $licenseDetails, $status)
Store a record of Software Heritage license info in table.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16