FOSSology  4.4.0
Open Source License Compliance by Open Source Software
TreeDao.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
4  SPDX-FileCopyrightText: © 2017 TNG Technology Consulting GmbH
5  Authors: Andreas Würl, Steffen Weber, Maximilian Huber
6 
7  SPDX-License-Identifier: GPL-2.0-only
8 */
9 
10 namespace Fossology\Lib\Dao;
11 
13 use Monolog\Logger;
15 
16 class TreeDao
17 {
19  private $dbManager;
21  private $logger;
22 
23  public function __construct(DbManager $dbManager)
24  {
25  $this->dbManager = $dbManager;
26  $this->logger = new Logger(self::class);
27  }
28 
29  public function getFullPath($itemId, $tableName, $parentId=0, $dropArtifactPrefix=false)
30  {
31  $statementName = __METHOD__.".".$tableName;
32 
33  if ($parentId==$itemId) {
34  return $this->getFullPath($itemId, $tableName);
35  } else if ($parentId > 0) {
36  $params = array($itemId, $parentId);
37  $parentClause = " = $2";
38  $parentLoopCondition = "AND (ut.parent != $2)";
39  $statementName .= ".parent";
40  } else {
41  $params = array($itemId);
42  $parentClause = " IS NULL";
43  $parentLoopCondition = "";
44  }
45 
46  $row = $this->dbManager->getSingleRow(
47  $sql= "
48  WITH RECURSIVE file_tree(uploadtree_pk, parent, ufile_name, path, prev_ufile_mode, artifact_path_prefix, file_path, cycle) AS (
49  SELECT ut.uploadtree_pk, ut.parent, ut.ufile_name,
50  ARRAY[ut.uploadtree_pk],
51  ut.ufile_mode,
52  '',
53  CASE WHEN ut.ufile_mode & (1<<28) = 0 THEN ut.ufile_name ELSE '' END,
54  false
55  FROM $tableName ut
56  WHERE ut.uploadtree_pk = $1
57  UNION ALL
58  SELECT ut.uploadtree_pk, ut.parent, ut.ufile_name,
59  path || ut.uploadtree_pk,
60  ut.ufile_mode,
61  CASE WHEN prev_ufile_mode & (1<<28) = 0
62  THEN
63  CASE WHEN ut.ufile_mode & (1<<28) = 0
64  THEN ''
65  ELSE artifact_path_prefix
66  END
67  ELSE
68  CASE WHEN ut.ufile_mode & (1<<28) = 0
69  THEN ut.ufile_name || '/' || artifact_path_prefix
70  ELSE artifact_path_prefix
71  END
72  END,
73  CASE WHEN (prev_ufile_mode & (1<<28) = 0 and ut.ufile_mode & (1<<28) = 0)
74  THEN ut.ufile_name || '/' || artifact_path_prefix || file_path
75  ELSE file_path
76  END,
77  (ut.uploadtree_pk = ANY(path)) $parentLoopCondition
78  FROM $tableName ut, file_tree ft
79  WHERE ut.uploadtree_pk = ft.parent AND NOT cycle
80  )
81  SELECT artifact_path_prefix, file_path from file_tree WHERE parent $parentClause",
82  $params, $statementName);
83 
84  if (false === $row) {
85  throw new \Exception("could not find path of $itemId:\n$sql--".print_r($params,true));
86  }
87 
88  if (! $dropArtifactPrefix) {
89  return $row['artifact_path_prefix'].$row['file_path'];
90  } else {
91  return $row['file_path'];
92  }
93  }
94 
95  public function getMinimalCoveringItem($uploadId, $tableName)
96  {
97  $statementName = __METHOD__.".".$tableName;
98 
99  $row = $this->dbManager->getSingleRow(
100  "SELECT uploadtree_pk FROM $tableName ut WHERE ut.upload_fk = $1
101  AND NOT EXISTS (
102  SELECT 1 FROM $tableName ut2 WHERE ut2.upload_fk = $1
103  AND NOT (ut2.lft BETWEEN ut.lft AND ut.rgt)
104  AND (ut2.ufile_mode & (3<<28) = 0)
105  )
106  ORDER BY ut.lft DESC LIMIT 1",
107  array($uploadId),
108  $statementName
109  );
110 
111  return $row ? $row['uploadtree_pk'] : 0;
112  }
113 
118  public function getItemHashes($uploadtreeId, $uploadtreeTablename='uploadtree')
119  {
120  $pfile = $this->dbManager->getSingleRow("SELECT pfile.* FROM $uploadtreeTablename, pfile WHERE uploadtree_pk=$1 AND pfile_fk=pfile_pk",
121  array($uploadtreeId), __METHOD__);
122  return array('sha1'=>$pfile['pfile_sha1'],'md5'=>$pfile['pfile_md5'],'sha256'=>$pfile['pfile_sha256']);
123  }
124 
125  public function getRepoPathOfPfile($pfileId, $repo="files")
126  {
127  $pfileRow = $this->dbManager->getSingleRow('SELECT * FROM pfile WHERE pfile_pk=$1',array($pfileId));
128  global $LIBEXECDIR;
129  if (empty($pfileRow['pfile_sha1'])) {
130  return null;
131  }
132  $hash = $pfileRow['pfile_sha1'] . "." . $pfileRow['pfile_md5'] . "." . $pfileRow['pfile_size'];
133  $path = '';
134  exec("$LIBEXECDIR/reppath $repo $hash", $path);
135  return($path[0]);
136  }
137 
143  public function getParentOfItem($itemTreeBounds)
144  {
145  $item = $itemTreeBounds->getItemId();
146  $tableName = $itemTreeBounds->getUploadTreeTableName();
147  $sql = "SELECT realparent FROM $tableName WHERE uploadtree_pk = $1;";
148  $statement = __METHOD__ . ".$tableName";
149  $row = $this->dbManager->getSingleRow($sql, [$item], $statement);
150  return $row['realparent'];
151  }
152 }
getItemHashes($uploadtreeId, $uploadtreeTablename='uploadtree')
Definition: TreeDao.php:118
getParentOfItem($itemTreeBounds)
Definition: TreeDao.php:143
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16