FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
TreeDao.php
1 <?php
2 /*
3 Copyright (C) 2014-2015, Siemens AG
4 Copyright (C) 2017 TNG Technology Consulting GmbH
5 Authors: Andreas Würl, Steffen Weber, Maximilian Huber
6 
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 version 2 as published by the Free Software Foundation.
10 
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20 
21 namespace Fossology\Lib\Dao;
22 
24 use Monolog\Logger;
25 
26 class TreeDao
27 {
29  private $dbManager;
31  private $logger;
32 
33  public function __construct(DbManager $dbManager)
34  {
35  $this->dbManager = $dbManager;
36  $this->logger = new Logger(self::class);
37  }
38 
39  public function getFullPath($itemId, $tableName, $parentId=0, $dropArtifactPrefix=false)
40  {
41  $statementName = __METHOD__.".".$tableName;
42 
43  if ($parentId==$itemId) {
44  return $this->getFullPath($itemId, $tableName);
45  } else if ($parentId > 0) {
46  $params = array($itemId, $parentId);
47  $parentClause = " = $2";
48  $parentLoopCondition = "AND (ut.parent != $2)";
49  $statementName .= ".parent";
50  } else {
51  $params = array($itemId);
52  $parentClause = " IS NULL";
53  $parentLoopCondition = "";
54  }
55 
56  $row = $this->dbManager->getSingleRow(
57  $sql= "
58  WITH RECURSIVE file_tree(uploadtree_pk, parent, ufile_name, path, prev_ufile_mode, artifact_path_prefix, file_path, cycle) AS (
59  SELECT ut.uploadtree_pk, ut.parent, ut.ufile_name,
60  ARRAY[ut.uploadtree_pk],
61  ut.ufile_mode,
62  '',
63  CASE WHEN ut.ufile_mode & (1<<28) = 0 THEN ut.ufile_name ELSE '' END,
64  false
65  FROM $tableName ut
66  WHERE ut.uploadtree_pk = $1
67  UNION ALL
68  SELECT ut.uploadtree_pk, ut.parent, ut.ufile_name,
69  path || ut.uploadtree_pk,
70  ut.ufile_mode,
71  CASE WHEN prev_ufile_mode & (1<<28) = 0
72  THEN
73  CASE WHEN ut.ufile_mode & (1<<28) = 0
74  THEN ''
75  ELSE artifact_path_prefix
76  END
77  ELSE
78  CASE WHEN ut.ufile_mode & (1<<28) = 0
79  THEN ut.ufile_name || '/' || artifact_path_prefix
80  ELSE artifact_path_prefix
81  END
82  END,
83  CASE WHEN (prev_ufile_mode & (1<<28) = 0 and ut.ufile_mode & (1<<28) = 0)
84  THEN ut.ufile_name || '/' || artifact_path_prefix || file_path
85  ELSE file_path
86  END,
87  (ut.uploadtree_pk = ANY(path)) $parentLoopCondition
88  FROM $tableName ut, file_tree ft
89  WHERE ut.uploadtree_pk = ft.parent AND NOT cycle
90  )
91  SELECT artifact_path_prefix, file_path from file_tree WHERE parent $parentClause",
92  $params, $statementName);
93 
94  if (false === $row) {
95  throw new \Exception("could not find path of $itemId:\n$sql--".print_r($params,true));
96  }
97 
98  if (! $dropArtifactPrefix) {
99  return $row['artifact_path_prefix'].$row['file_path'];
100  } else {
101  return $row['file_path'];
102  }
103  }
104 
105  public function getMinimalCoveringItem($uploadId, $tableName)
106  {
107  $statementName = __METHOD__.".".$tableName;
108 
109  $row = $this->dbManager->getSingleRow(
110  "SELECT uploadtree_pk FROM $tableName ut WHERE ut.upload_fk = $1
111  AND NOT EXISTS (
112  SELECT 1 FROM $tableName ut2 WHERE ut2.upload_fk = $1
113  AND NOT (ut2.lft BETWEEN ut.lft AND ut.rgt)
114  AND (ut2.ufile_mode & (3<<28) = 0)
115  )
116  ORDER BY ut.lft DESC LIMIT 1",
117  array($uploadId),
118  $statementName
119  );
120 
121  return $row ? $row['uploadtree_pk'] : 0;
122  }
123 
128  public function getItemHashes($uploadtreeId, $uploadtreeTablename='uploadtree')
129  {
130  $pfile = $this->dbManager->getSingleRow("SELECT pfile.* FROM $uploadtreeTablename, pfile WHERE uploadtree_pk=$1 AND pfile_fk=pfile_pk",
131  array($uploadtreeId), __METHOD__);
132  return array('sha1'=>$pfile['pfile_sha1'],'md5'=>$pfile['pfile_md5'],'sha256'=>$pfile['pfile_sha256']);
133  }
134 
135  public function getRepoPathOfPfile($pfileId, $repo="files")
136  {
137  $pfileRow = $this->dbManager->getSingleRow('SELECT * FROM pfile WHERE pfile_pk=$1',array($pfileId));
138  global $LIBEXECDIR;
139  if (empty($pfileRow['pfile_sha1'])) {
140  return null;
141  }
142  $hash = $pfileRow['pfile_sha1'] . "." . $pfileRow['pfile_md5'] . "." . $pfileRow['pfile_size'];
143  $path = '';
144  exec("$LIBEXECDIR/reppath $repo $hash", $path);
145  return($path[0]);
146  }
147 }
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
getItemHashes($uploadtreeId, $uploadtreeTablename='uploadtree')
Definition: TreeDao.php:128