32 require_once(dirname(dirname(__FILE__)) .
"/common-dir.php");
38 const REUSE_ENHANCED = 2;
40 const REUSE_CONF = 16;
47 private $permissionDao;
52 $this->logger = $logger;
53 $this->permissionDao = $uploadPermissionDao;
62 public function getUploadEntry($uploadTreeId, $uploadTreeTableName =
"uploadtree")
64 $stmt = __METHOD__ .
".$uploadTreeTableName";
65 $uploadEntry = $this->
dbManager->getSingleRow(
"SELECT * FROM $uploadTreeTableName WHERE uploadtree_pk = $1",
66 array($uploadTreeId), $stmt);
68 $uploadEntry[
'tablename'] = $uploadTreeTableName;
82 $stmt = __METHOD__ .
".$uploadTreeTableName";
83 $uploadEntry = $this->
dbManager->getSingleRow(
"SELECT uploadtree_pk " .
84 "FROM $uploadTreeTableName " .
85 "WHERE upload_fk = $1 AND pfile_fk = $2",
86 array($uploadFk, $pfileFk), $stmt);
87 return intval($uploadEntry[
'uploadtree_pk']);
97 $row = $this->
dbManager->getSingleRow(
"SELECT * FROM upload WHERE upload_pk = $1",
98 array($uploadId), $stmt);
110 $uploadEntryData = $this->
getUploadEntry($itemId, $uploadTreeTableName);
133 if ($uploadTreeTableName === null) {
137 $stmt = __METHOD__ .
".$uploadTreeTableName";
138 $parameters = array();
139 $uploadCondition = $this->handleUploadIdForTable($uploadTreeTableName, $uploadId, $parameters);
141 $uploadEntryData = $this->
dbManager->getSingleRow(
"SELECT * FROM $uploadTreeTableName 147 return $uploadEntryData ? $this->
createItemTreeBounds($uploadEntryData, $uploadTreeTableName) :
false;
158 $stmt = __METHOD__ .
".$uploadTreeTableName";
159 $parameters = array($itemTreeBounds->
getLeft(), $itemTreeBounds->
getRight());
160 $uploadCondition = $this->handleUploadIdForTable($uploadTreeTableName, $itemTreeBounds->
getUploadId(), $parameters);
162 $row = $this->
dbManager->getSingleRow(
"SELECT count(*) as count FROM $uploadTreeTableName 163 WHERE lft BETWEEN $1 AND $2 165 AND ((ufile_mode & (3<<28))=0) 168 $fileCount = intval($row[
"count"]);
172 private function handleUploadIdForTable($uploadTreeTableName, $uploadId, &$parameters)
174 if ($uploadTreeTableName ===
"uploadtree" || $uploadTreeTableName ===
"uploadtree_a") {
175 $parameters[] = $uploadId;
176 return " AND upload_fk = $" . count($parameters) .
" ";
185 public function getStatusTypeMap()
189 $uploadStatus = $container->get(
'upload_status.types');
190 return $uploadStatus->getMap();
198 if ($this->isAccessible($uploadId, $groupId)) {
199 $row = $this->
dbManager->getSingleRow(
"SELECT status_fk FROM upload_clearing WHERE upload_fk = $1", array($uploadId));
200 if (
false === $row) {
201 throw new \Exception(
"cannot find uploadId=$uploadId");
203 return $row[
'status_fk'];
205 throw new \Exception(
"permission denied");
219 if (!empty($uploadId)) {
220 $statementName = __METHOD__;
222 "SELECT uploadtree_tablename FROM upload WHERE upload_pk=$1",
226 if (!empty($row[
'uploadtree_tablename'])) {
227 return $row[
'uploadtree_tablename'];
255 const NOT_FOUND = null;
267 $originItem = $this->
getUploadEntry($itemId, $uploadTreeTableName);
268 $originLft = $originItem[
'lft'];
270 $options[UploadTreeProxy::OPT_ITEM_FILTER] =
" AND ut.ufile_mode & (3<<28) = 0";
271 $uploadTreeViewName =
'items2care';
273 if ($direction == self::DIR_FWD) {
274 $uploadTreeViewName .=
'fwd';
275 $options[UploadTreeProxy::OPT_ITEM_FILTER] .=
" AND lft>$1";
278 $uploadTreeViewName .=
'bwd';
279 $options[UploadTreeProxy::OPT_ITEM_FILTER] .=
" AND lft<$1";
283 $uploadTreeView =
new UploadTreeProxy($uploadId, $options, $uploadTreeTableName, $uploadTreeViewName);
284 $statementName = __METHOD__ .
".$uploadTreeViewName.";
285 $query = $uploadTreeView->getDbViewQuery().
" ORDER BY lft $order";
287 $newItemRow = $this->
dbManager->getSingleRow(
"$query LIMIT 1", array($originLft), $statementName);
289 return $this->
createItem($newItemRow, $uploadTreeTableName);
291 return self::NOT_FOUND;
303 $statementname = __METHOD__ . $uploadTreeTableName;
305 $parent = $this->
dbManager->getSingleRow(
306 "SELECT uploadtree_pk 307 FROM $uploadTreeTableName 308 WHERE upload_fk=$1 AND parent IS NULL", array($uploadId), $statementname);
309 if (
false === $parent) {
310 throw new \Exception(
"Missing upload tree parent for upload");
312 return $parent[
'uploadtree_pk'];
315 public function getLeftAndRight($uploadtreeID, $uploadTreeTableName =
"uploadtree")
317 $statementName = __METHOD__ . $uploadTreeTableName;
318 $leftRight = $this->
dbManager->getSingleRow(
319 "SELECT lft,rgt FROM $uploadTreeTableName WHERE uploadtree_pk = $1",
320 array($uploadtreeID), $statementName
323 return array($leftRight[
'lft'], $leftRight[
'rgt']);
333 $sql =
"SELECT count(*) FROM " . $uploadTreeView->
getDbViewName() .
" WHERE lft BETWEEN $1 AND $2";
334 $result = $this->
dbManager->getSingleRow($sql
335 , array($itemTreeBounds->
getLeft(), $itemTreeBounds->
getRight()), __METHOD__ . $uploadTreeView->
asCTE());
336 $output = $result[
'count'];
346 public function getContainedItems(
ItemTreeBounds $itemTreeBounds, $addCondition =
"", $addParameters = array())
350 $statementName = __METHOD__ .
".$uploadTreeTableName";
352 $view =
new UploadTreeViewProxy($itemTreeBounds, array(UploadTreeViewProxy::CONDITION_PLAIN_FILES));
354 $this->
dbManager->prepare($statementName,
355 $view->asCTE() .
" SELECT * FROM " . $view->getDbViewName() .
" ut ".
356 ($addCondition ?
" WHERE " . $addCondition :
''));
357 $res = $this->
dbManager->execute($statementName, $addParameters);
360 while ($row = $this->
dbManager->fetchArray($res)) {
361 $items[] = $this->
createItem($row, $uploadTreeTableName);
374 public function addReusedUpload($uploadId, $reusedUploadId, $groupId, $reusedGroupId, $reuseMode=0)
376 $this->
dbManager->insertTableRow(
'upload_reuse',
377 array(
'upload_fk'=>$uploadId,
'group_fk'=> $groupId,
'reused_upload_fk'=>$reusedUploadId,
'reused_group_fk'=>$reusedGroupId,
'reuse_mode'=>$reuseMode));
387 $statementName = __METHOD__;
389 $this->
dbManager->prepare($statementName,
390 "SELECT reused_upload_fk, reused_group_fk, reuse_mode FROM upload_reuse WHERE upload_fk = $1 AND group_fk=$2");
391 $res = $this->
dbManager->execute($statementName, array($uploadId, $groupId));
392 $reusedPairs = $this->
dbManager->fetchAll($res);
402 protected function createItem($uploadEntry, $uploadTreeTableName)
405 intval($uploadEntry[
'uploadtree_pk']),
406 $uploadTreeTableName,
407 intval($uploadEntry[
'upload_fk']),
408 intval($uploadEntry[
'lft']), intval($uploadEntry[
'rgt']));
410 $parent = $uploadEntry[
'parent'];
412 $itemTreeBounds, $parent !== null ? intval($parent) : null, intval($uploadEntry[
'pfile_fk']), intval($uploadEntry[
'ufile_mode']), $uploadEntry[
'ufile_name']
425 if ($uploadEntryData ===
false) {
426 throw new Exception(
"did not find uploadTreeId in $uploadTreeTableName");
428 return new ItemTreeBounds(intval($uploadEntryData[
'uploadtree_pk']), $uploadTreeTableName, intval($uploadEntryData[
'upload_fk']), intval($uploadEntryData[
'lft']), intval($uploadEntryData[
'rgt']));
439 $sql =
"SELECT count(*) FROM ".$itemTreeBounds->getUploadTreeTableName().
" ut " 440 .
"WHERE ut.upload_fk=$1";
443 $stmt = __METHOD__.
'.parent';
444 $params[] = $itemTreeBounds->
getItemId();
445 $sql .=
" AND ut.ufile_mode & (1<<28) = 0 AND ut.realparent = $2";
447 $params[] = $itemTreeBounds->
getLeft();
448 $params[] = $itemTreeBounds->
getRight();
449 $sql .=
" AND ut.ufile_mode & (3<<28) = 0 AND (ut.lft BETWEEN $2 AND $3)";
452 $descendants = $this->
dbManager->getSingleRow($sql,$params);
453 return $descendants[
'count'];
457 public function isAccessible($uploadId, $groupId)
459 return $this->permissionDao->isAccessible($uploadId, $groupId);
462 public function isEditable($uploadId, $groupId)
464 return $this->permissionDao->isEditable($uploadId, $groupId);
467 public function makeAccessibleToGroup($uploadId, $groupId, $perm=null)
469 $this->permissionDao->makeAccessibleToGroup($uploadId, $groupId, $perm);
472 public function makeAccessibleToAllGroupsOf($uploadId, $userId, $perm=null)
474 $this->permissionDao->makeAccessibleToAllGroupsOf($uploadId, $userId, $perm);
483 $pfile = $this->
dbManager->getSingleRow(
'SELECT pfile.* FROM upload, pfile WHERE upload_pk=$1 AND pfile_fk=pfile_pk',
484 array($uploadId), __METHOD__);
485 return array(
'sha1'=>$pfile[
'pfile_sha1'],
'md5'=>$pfile[
'pfile_md5'],
'sha256'=>$pfile[
'pfile_sha256']);
496 $sqlChildrenOf =
"SELECT COUNT(*) FROM $uploadtreeTablename s 497 WHERE ufile_mode&(1<<28)=0 and s.upload_fk=$2 AND s.realparent=";
498 $sql=
"WITH RECURSIVE item_path (item_id,num_children,depth,ufile_mode,ufile_name) AS ( 499 SELECT uploadtree_pk item_id, ($sqlChildrenOf $1) num_children, 0 depth, ufile_mode, ufile_name 500 FROM $uploadtreeTablename WHERE upload_fk=$2 AND uploadtree_pk=$1 502 SELECT uploadtree_pk item_id, ($sqlChildrenOf ut.uploadtree_pk) num_children, 503 item_path.depth+1 depth, ut.ufile_mode, item_path.ufile_name||'/'||ut.ufile_name ufile_name 504 FROM $uploadtreeTablename ut INNER JOIN item_path ON item_id=ut.realparent 505 WHERE upload_fk=$2 AND ut.ufile_mode&(1<<28)=0 AND num_children<2 507 SELECT * FROM item_path WHERE num_children!=1 OR ufile_mode&(1<<29)=0 ORDER BY depth DESC LIMIT 1";
508 return $this->
dbManager->getSingleRow($sql,array($itemId, $uploadId),__METHOD__.$uploadtreeTablename);
519 $itemRow = $this->
getFatItemArray($itemId,$uploadId,$uploadtreeTablename);
520 return $itemRow[
'item_id'];
530 $statementName = __METHOD__ .
'.' . $uploadTreeTableName;
533 $param[] = $itemTreeBounds->
getLeft();
534 $param[] = $itemTreeBounds->
getRight();
535 $condition =
" lft BETWEEN $1 AND $2";
536 $condition .=
" AND (ufile_mode & (1<<28)) = 0";
538 if (
'uploadtree_a' == $uploadTreeTableName) {
540 $condition .=
" AND upload_fk=$".count($param);
544 SELECT ufile_name, uploadtree_pk, lft, rgt, ufile_mode, 545 pfile_pk, pfile_md5, pfile_sha1, pfile_sha256 546 FROM $uploadTreeTableName 548 ON pfile_fk = pfile_pk 553 $this->
dbManager->prepare($statementName, $sql);
554 $result = $this->
dbManager->execute($statementName, $param);
555 $pfilePerFileName = array();
557 $row = $this->
dbManager->fetchArray($result);
558 $pathStack = array($row[
'ufile_name']);
559 $rgtStack = array($row[
'rgt']);
560 $lastLft = $row[
'lft'];
561 $this->addToPFilePerFileName($pfilePerFileName, $pathStack, $row);
562 while ($row = $this->
dbManager->fetchArray($result)) {
563 if ($row[
'lft'] < $lastLft) {
567 $this->updateStackState($pathStack, $rgtStack, $lastLft, $row);
568 $this->addToPFilePerFileName($pfilePerFileName, $pathStack, $row);
571 return $pfilePerFileName;
574 private function updateStackState(&$pathStack, &$rgtStack, &$lastLft, $row)
576 if ($row[
'lft'] >= $lastLft) {
577 while (count($rgtStack) > 0 && $row[
'lft'] > $rgtStack[count($rgtStack)-1]) {
578 array_pop($pathStack);
579 array_pop($rgtStack);
581 if ($row[
'lft'] > $lastLft) {
582 array_push($pathStack, $row[
'ufile_name']);
583 array_push($rgtStack, $row[
'rgt']);
584 $lastLft = $row[
'lft'];
589 private function addToPFilePerFileName(&$pfilePerFileName, $pathStack, $row)
591 if (($row[
'ufile_mode'] & (1 << 29)) == 0) {
592 $path = implode($pathStack,
'/');
593 $pfilePerFileName[$path][
'pfile_pk'] = $row[
'pfile_pk'];
594 $pfilePerFileName[$path][
'uploadtree_pk'] = $row[
'uploadtree_pk'];
595 $pfilePerFileName[$path][
'md5'] = $row[
'pfile_md5'];
596 $pfilePerFileName[$path][
'sha1'] = $row[
'pfile_sha1'];
597 $pfilePerFileName[$path][
'sha256'] = $row[
'pfile_sha256'];
609 $statementName = __METHOD__ .
'.' . $uploadTreeTableName;
612 $param[] = $itemTreeBounds->
getLeft();
613 $param[] = $itemTreeBounds->
getRight();
614 $condition =
" lft BETWEEN $1 AND $2";
615 $condition .=
" AND (ufile_mode & (1<<28)) = 0";
617 if (
'uploadtree_a' == $uploadTreeTableName) {
619 $condition .=
" AND upload_fk=$".count($param);
621 $condition .=
" AND pfile_$hashAlgo IS NOT NULL";
624 SELECT pfile_fk, uploadtree_pk, ufile_mode, pfile_$hashAlgo as hash 625 FROM $uploadTreeTableName 627 ON pfile_fk = pfile_pk 632 $this->
dbManager->prepare($statementName, $sql);
633 $result = $this->
dbManager->execute($statementName, $param);
635 $pfilePerHashAlgo = array();
636 while ($row = $this->
dbManager->fetchArray($result)) {
637 if (($row[
'ufile_mode']&(1<<29)) == 0) {
638 $pfilePerHashAlgo[strtolower($row[
'hash'])][] = array(
'pfile_pk' => $row[
'pfile_fk'],
639 'uploadtree_pk' => $row[
'uploadtree_pk']);
643 return $pfilePerHashAlgo;
650 public function getReportInfo($uploadId)
653 $sql =
"SELECT * FROM report_info WHERE upload_fk = $1";
654 $row = $this->
dbManager->getSingleRow($sql, array($uploadId), $stmt);
658 $stmt = __METHOD__.
'ifempty';
659 $sql =
"INSERT INTO report_info (upload_fk) VALUES ($1) RETURNING *";
660 $row = $this->
dbManager->getSingleRow($sql, array($uploadId), $stmt);
673 $stmt = __METHOD__.
"getUploadHashesFromPfileId";
674 $sql =
"SELECT * FROM pfile WHERE pfile_pk = $1";
675 $row = $this->
dbManager->getSingleRow($sql, array($pfilePk), $stmt);
677 return [
"sha1" => $row[
"pfile_sha1"],
"md5" => $row[
"pfile_md5"],
"sha256" => $row[
"pfile_sha256"]];
688 $sql =
"SELECT exists(SELECT 1 FROM report_info WHERE upload_fk = $1 LIMIT 1)::int";
689 $row = $this->
dbManager->getSingleRow($sql, array($reusedUploadId), $stmt);
691 if ($row[
'exists']) {
692 $stmt = __METHOD__.
"CopyinsertReportConfReuse";
694 "INSERT INTO report_info( 695 upload_fk, ri_ga_checkbox_selection, ri_spdx_selection, 696 ri_excluded_obligations, ri_reviewed, ri_footer, ri_report_rel, 697 ri_community, ri_component, ri_version, ri_release_date, 698 ri_sw360_link, ri_general_assesment, ri_ga_additional, ri_ga_risk) 699 SELECT $1, ri_ga_checkbox_selection, ri_spdx_selection, 700 ri_excluded_obligations, ri_reviewed, ri_footer, ri_report_rel, 701 ri_community, ri_component, ri_version, ri_release_date, 702 ri_sw360_link, ri_general_assesment, ri_ga_additional, ri_ga_risk 703 FROM report_info WHERE upload_fk = $2" 705 $this->
dbManager->freeResult($this->
dbManager->execute($stmt, array($uploadId, $reusedUploadId)));
getUploadtreeTableName($uploadId)
Get the uploadtree table name for this upload_pk If upload_pk does not exist, return "uploadtree"...
getReusedUpload($uploadId, $groupId)
insertReportConfReuse($uploadId, $reusedUploadId)
countNonArtifactDescendants(ItemTreeBounds $itemTreeBounds, $isFlat=true)
getPFileDataPerFileName(ItemTreeBounds $itemTreeBounds)
getFatItemArray($itemId, $uploadId, $uploadtreeTablename)
getPFilesDataPerHashAlgo(ItemTreeBounds $itemTreeBounds, $hashAlgo="sha1")
getUploadHashes($uploadId)
static createFromTable($row)
getUploadParent($uploadId)
getNextItem($uploadId, $itemId, $options=null)
addReusedUpload($uploadId, $reusedUploadId, $groupId, $reusedGroupId, $reuseMode=0)
getUploadEntry($uploadTreeId, $uploadTreeTableName="uploadtree")
getUploadtreeIdFromPfile($uploadFk, $pfileFk)
fo_dbManager * dbManager
fo_dbManager object
asCTE()
Common Table Expressions.
getPreviousItem($uploadId, $itemId, $options=null)
getItemTreeBounds($itemId, $uploadTreeTableName="uploadtree")
createItem($uploadEntry, $uploadTreeTableName)
getItemTreeBoundsFromUploadId($uploadTreeId, $uploadId)
createItemTreeBounds($uploadEntryData, $uploadTreeTableName)
getStatus($uploadId, $groupId)
unused function
getParentItemBounds($uploadId, $uploadTreeTableName=null)
getItemByDirection($uploadId, $itemId, $direction, $options)
countPlainFiles(ItemTreeBounds $itemTreeBounds)
getFatItemId($itemId, $uploadId, $uploadtreeTablename)
getUploadHashesFromPfileId($pfilePk)
Get Pfile hashes from the pfile id.