FOSSology  4.4.0
Open Source License Compliance by Open Source Software
CopyrightLister.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2012 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
14 namespace Fossology\Lib\Util;
15 
21 
23 {
25  private $dbManager;
27  private $uploadDao;
29  private $treeDao;
31  private $copyrightDao;
32 
33  private $includeContainer = FALSE;
34  private $excludingCopyright = -1;
35  private $includingCopyright = -1;
37  private $type = "";
38  private $agentId;
39 
40  function __construct()
41  {
42  global $container;
43  $this->dbManager = $container->get('db.manager');
44  $this->uploadDao = $container->get('dao.upload');
45  $this->treeDao = $container->get('dao.tree');
46  $this->copyrightDao = $container->get('dao.copyright');
47  }
48 
49  public function setContainerInclusion($includeContainer)
50  {
51  $this->includeContainer = $includeContainer;
52  }
53 
54  public function setExcludingCopyright($excludingCopyright)
55  {
56  $this->excludingCopyright = $excludingCopyright;
57  }
58 
59  public function setIncludingCopyright($includingCopyright)
60  {
61  $this->includingCopyright = $includingCopyright;
62  }
63 
64  public function setType($type)
65  {
66  $this->type = $type;
67  }
68 
73  public function getCopyrightList($itemId, $uploadId)
74  {
75  if (empty($itemId)) {
76  $itemId = $this->uploadDao->getUploadParent($uploadId);
77  }
78  if (!$this->selectAgentId($uploadId)) {
79  echo 'no valid copyright agent found';
80  return;
81  }
82  $uploadtree_tablename = $this->uploadDao->getUploadtreeTableName($uploadId);
83  $toprow = $this->uploadDao->getItemTreeBounds($itemId,$uploadtree_tablename);
84 
85  $extraWhere = 'agent_fk='.$this->agentId.' AND lft>'.$toprow->getLeft().' AND rgt<'.$toprow->getRight();
86  $allCopyrightEntries = $this->copyrightDao->getAllEntries('copyright', $uploadId, $uploadtree_tablename,
87  empty($this->type)||$this->type=='all' ? null : $this->type, false, null, $extraWhere);
88 
89  $modeMask = empty($this->includeContainer) ? (3<<28) : (1<<28);
90  $sql = "SELECT uploadtree_pk, ufile_name, lft, rgt FROM $uploadtree_tablename
91  WHERE upload_fk=$1 AND lft>$2 AND rgt<$3 AND (ufile_mode & $4) = 0
92  ORDER BY uploadtree_pk";
93  $this->dbManager->prepare($outerStmt=__METHOD__.'.loopThroughAllRecordsInTree',$sql);
94  $outerresult = $this->dbManager->execute($outerStmt,array($toprow->getUploadId(),$toprow->getLeft(),$toprow->getRight(),$modeMask));
95  while ($row = $this->dbManager->fetchArray($outerresult)) {
96  $this->printRow($row,$uploadtree_tablename, $allCopyrightEntries); //$this->uploadDao->getParentItemBounds($uploadId)->getItemId());
97  }
98  $this->dbManager->freeResult($outerresult);
99  }
100 
105  private function selectAgentId($uploadId)
106  {
107  global $container;
108  /* @var $agentDao AgentDao */
109  $agentDao = $container->get('dao.agent');
110  $agentRec = $agentDao->agentARSList($tableName="copyright_ars", $uploadId, 1);
111 
112  if ($agentRec === false) {
113  echo _("No data available \n");
114  return false;
115  }
116  $this->agentId = $agentRec[0]["agent_fk"];
117  return true;
118  }
119 
123  private function printRow($row,$uploadtree_tablename, &$allCopyrightEntries, $parentId=0)
124  {
125  $filepath = $this->treeDao->getFullPath($row['uploadtree_pk'], $uploadtree_tablename, $parentId);
126 
127  $copyrightArray = array();
128  foreach ($allCopyrightEntries as $entry) {
129  if ($entry['uploadtree_pk'] == $row['uploadtree_pk']) {
130  $copyrightArray[] = $entry['content'];
131  }
132  }
133  $copyright = implode(', ', $copyrightArray);
134 
136  if (-1 != $this->includingCopyright && -1 != $this->excludingCopyright && !empty($this->includingCopyright) &&
137  !empty($this->excludingCopyright)) {
138  if (empty($copyright) || stristr($copyright, $this->includingCopyright) ||
139  stristr($copyright, $this->excludingCopyright)) {
140  return;
141  }
142  } else if (
144  ! (-1 == $this->includingCopyright && -1 == $this->excludingCopyright) &&
146  ! (empty($this->includingCopyright) && empty($this->excludingCopyright)) &&
148  ! (empty($this->includingCopyright) && empty($copyright)) &&
150  ! (empty($this->excludingCopyright) && !empty($copyright)) &&
152  ! (-1 != $this->includingCopyright && !empty($this->includingCopyright) && !empty($copyright) && stristr($copyright, $this->includingCopyright)) &&
154  ! (-1 != $this->excludingCopyright && !empty($this->excludingCopyright) && !empty($copyright) && !stristr($copyright, $this->excludingCopyright))) {
155  return;
156  }
157  print ("$filepath: $copyright\n");
158  }
159 }
printRow($row, $uploadtree_tablename, &$allCopyrightEntries, $parentId=0)
write out text in format 'filepath: copyright list'
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16