FOSSology  4.4.0
Open Source License Compliance by Open Source Software
LicenseViewProxy.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 CANDIDATE_PREFIX = 'candidatePrefix';
13  const OPT_COLUMNS = 'columns';
15  private $groupId;
17  private $allColumns = array('rf_pk', 'rf_spdx_id', 'rf_shortname', 'rf_text',
18  'rf_url', 'rf_add_date', 'rf_copyleft', 'rf_fullname', 'rf_notes',
19  'marydone', 'rf_active', 'rf_text_updatable', 'rf_md5', 'rf_detector_type',
20  'rf_source', 'group_fk');
21 
27  public function __construct($groupId, $options=array(), $dbViewName='license_all')
28  {
29  $this->groupId = $groupId;
30  if ($groupId == 0) {
31  $dbViewQuery = $this->queryOnlyLicenseRef($options);
32  parent::__construct($dbViewQuery, $dbViewName);
33  return;
34  }
35  $dbViewQuery = $this->queryLicenseCandidate($options);
36  if (! array_key_exists('diff', $options)) {
37  $dbViewQuery .= " UNION ".$this->queryOnlyLicenseRef($options);
38  }
39  parent::__construct($dbViewQuery, $dbViewName);
40  }
41 
42 
43  private function queryLicenseCandidate($options)
44  {
45  $columns = array_key_exists(self::OPT_COLUMNS, $options) ? $options[self::OPT_COLUMNS] : $this->allColumns;
46  if (array_key_exists(self::CANDIDATE_PREFIX, $options)) {
47  $shortnameId = array_search('rf_shortname',$columns);
48  if ($shortnameId !== false) {
49  $columns[$shortnameId] = "'". pg_escape_string($options[self::CANDIDATE_PREFIX]). '\'||rf_shortname AS rf_shortname';
50  }
51  }
52  $gluedColumns = implode(',', $columns);
53  $dbViewQuery = "SELECT $gluedColumns FROM license_candidate WHERE group_fk=$this->groupId";
54  if (array_key_exists('extraCondition', $options)) {
55  $dbViewQuery .= " AND $options[extraCondition]";
56  }
57  return $dbViewQuery;
58  }
59 
60  private function queryOnlyLicenseRef($options)
61  {
62  $columns = array_key_exists(self::OPT_COLUMNS, $options) ? $options[self::OPT_COLUMNS] : $this->allColumns;
63  $groupFkPos = array_search('group_fk',$columns);
64  if ($groupFkPos) {
65  $columns[$groupFkPos] = '0 AS group_fk';
66  }
67  $gluedColumns = implode(',', $columns);
68  $dbViewQuery = "SELECT $gluedColumns FROM ONLY license_ref";
69  if (array_key_exists('extraCondition', $options)) {
70  $dbViewQuery .= " WHERE $options[extraCondition]";
71  }
72  return $dbViewQuery;
73  }
74 }
__construct($groupId, $options=array(), $dbViewName='license_all')