FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
LicenseMap.php
1 <?php
2 /*
3 Copyright (C) 2014-2015, 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 
20 
24 
30 {
31  const CONCLUSION = 1;
32  const TRIVIAL = 2;
33  const FAMILY = 3;
34  const REPORT = 4;
35 
37  private $dbManager;
39  private $usageId;
41  private $groupId;
43  private $map = array();
44 
52  public function __construct(DbManager $dbManager, $groupId, $usageId=null, $full=false)
53  {
54  $this->usageId = $usageId?:self::CONCLUSION;
55  $this->groupId = $groupId;
56  $this->dbManager = $dbManager;
57  if ($this->usageId == self::TRIVIAL && !$full) {
58  return;
59  }
60  $licenseView = new LicenseViewProxy($groupId);
61  if ($full) {
62  $query = $licenseView->asCTE()
63  .' SELECT distinct on(rf_pk) rf_pk rf_fk, rf_shortname parent_shortname, rf_parent FROM (
64  SELECT r1.rf_pk, r2.rf_shortname, usage, rf_parent FROM '.$licenseView->getDbViewName()
65  .' r1 inner join license_map on usage=$1 and rf_fk=r1.rf_pk
66  left join license_ref r2 on rf_parent=r2.rf_pk
67  UNION
68  SELECT rf_pk, rf_shortname, -1 usage, rf_pk rf_parent from '.$licenseView->getDbViewName()
69  .') full_map ORDER BY rf_pk,usage DESC';
70 
71  $stmt = __METHOD__.".$this->usageId,$groupId,full";
72  } else {
73  $query = $licenseView->asCTE()
74  .' SELECT rf_fk, rf_shortname parent_shortname, rf_parent FROM license_map, '.$licenseView->getDbViewName()
75  .' WHERE rf_pk=rf_parent AND rf_fk!=rf_parent AND usage=$1';
76  $stmt = __METHOD__.".$this->usageId,$groupId";
77  }
78  $dbManager->prepare($stmt,$query);
79  $res = $dbManager->execute($stmt,array($this->usageId));
80  while ($row = $dbManager->fetchArray($res)) {
81  $this->map[$row['rf_fk']] = $row;
82  }
83  $dbManager->freeResult($res);
84  }
85 
91  public function getProjectedId($licenseId)
92  {
93  if (array_key_exists($licenseId, $this->map)) {
94  return $this->map[$licenseId]['rf_parent'];
95  }
96  return $licenseId;
97  }
98 
107  public function getProjectedShortname($licenseId, $defaultName=null)
108  {
109  if (array_key_exists($licenseId, $this->map)) {
110  return $this->map[$licenseId]['parent_shortname'];
111  }
112  return $defaultName;
113  }
114 
119  public function getUsage()
120  {
121  return $this->usageId;
122  }
123 
128  public function getGroupId()
129  {
130  return $this->groupId;
131  }
132 
137  public function getTopLevelLicenseRefs()
138  {
139  $licenseView = new LicenseViewProxy($this->groupId,
140  array('columns'=>array('rf_pk','rf_shortname','rf_fullname')),
141  'license_visible');
142  $query = $licenseView->asCTE()
143  .' SELECT rf_pk, rf_shortname, rf_fullname FROM '.$licenseView->getDbViewName()
144  .' LEFT JOIN license_map ON rf_pk=rf_fk AND rf_fk!=rf_parent AND usage=$1'
145  .' WHERE license_map_pk IS NULL';
146  $stmt = __METHOD__.".$this->usageId,$this->groupId";
147  $this->dbManager->prepare($stmt,$query);
148  $res = $this->dbManager->execute($stmt,array($this->usageId));
149  $topLevel = array();
150  while ($row = $this->dbManager->fetchArray($res)) {
151  $topLevel[$row['rf_pk']] = new LicenseRef($row['rf_pk'],$row['rf_shortname'],$row['rf_fullname']);
152  }
153  return $topLevel;
154  }
155 
160  public static function getMappedLicenseRefView($usageExpr='$1')
161  {
162  return "SELECT bot.rf_pk rf_origin, top.rf_pk, top.rf_shortname, top.rf_fullname FROM ONLY license_ref bot "
163  ."LEFT JOIN license_map ON bot.rf_pk=rf_fk AND usage=$usageExpr "
164  ."INNER JOIN license_ref top ON rf_parent=top.rf_pk OR rf_parent IS NULL AND bot.rf_pk=top.rf_pk";
165  }
166 
173  public function getObligationsForLicenseRef($license_ref, $candidate = false)
174  {
175  $tableName = $candidate ? "obligation_candidate_map" : "obligation_map";
176  $sql = "SELECT distinct(ob_fk) FROM $tableName WHERE rf_fk = $1;";
177  $ob_fks = $this->dbManager->getRows($sql, [$license_ref],
178  __METHOD__ . $tableName);
179  $returnVal = [];
180  foreach ($ob_fks as $row) {
181  $returnVal[] = $row['ob_fk'];
182  }
183  return $returnVal;
184  }
185 }
__construct(DbManager $dbManager, $groupId, $usageId=null, $full=false)
Definition: LicenseMap.php:52
prepare($statementName, $sqlStatement)
Wrapper class for license map.
Definition: LicenseMap.php:29
Contains business rules for FOSSology.
getProjectedId($licenseId)
For a given license id, get the projected id.
Definition: LicenseMap.php:91
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
execute($statementName, $params=array())
static getMappedLicenseRefView($usageExpr='$1')
Query to get license map view along with license ref.
Definition: LicenseMap.php:160
getObligationsForLicenseRef($license_ref, $candidate=false)
Get all Obligations attached with given license ref.
Definition: LicenseMap.php:173
getProjectedShortname($licenseId, $defaultName=null)
For a given license id, get the projected shortname.
Definition: LicenseMap.php:107