1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) Enalean, 2014. All Rights Reserved. |
4
|
|
|
* |
5
|
|
|
* This file is a part of Tuleap. |
6
|
|
|
* |
7
|
|
|
* Tuleap is free software; you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU General Public License as published by |
9
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* Tuleap is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License |
18
|
|
|
* along with Tuleap. If not, see <http://www.gnu.org/licenses/>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
class ArtifactAttachmentXMLZipper implements ArtifactAttachmentXMLExporter { |
22
|
|
|
|
23
|
|
|
/** @var ArtifactXMLNodeHelper */ |
24
|
|
|
private $node_helper; |
25
|
|
|
|
26
|
|
|
/** @var ZipArchive */ |
27
|
|
|
private $archive; |
28
|
|
|
|
29
|
|
|
/** @var Boolean */ |
30
|
|
|
private $skip_files = false; |
31
|
|
|
|
32
|
|
|
/** @var ArtifactXMLExporterDao */ |
33
|
|
|
private $dao; |
34
|
|
|
|
35
|
|
|
public function __construct(ArtifactXMLNodeHelper $node_helper, ArtifactXMLExporterDao $dao, ZipArchive $archive, $skip_files) { |
36
|
|
|
$this->node_helper = $node_helper; |
37
|
|
|
$this->dao = $dao; |
38
|
|
|
$this->archive = $archive; |
39
|
|
|
$this->skip_files = $skip_files; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function addFilesToArtifact(DOMElement $artifact_node, $artifact_type_id, $artifact_id) { |
43
|
|
|
$dar = $this->dao->searchFilesForArtifact($artifact_id); |
44
|
|
|
if (count($dar)) { |
45
|
|
|
$this->archive->addEmptyDir(ArtifactXMLExporter::ARCHIVE_DATA_DIR); |
46
|
|
|
} |
47
|
|
|
foreach($dar as $row) { |
|
|
|
|
48
|
|
|
$xml_file_id = ArtifactAttachmentFieldXMLExporter::XML_FILE_PREFIX.$row['id']; |
49
|
|
|
$path_in_archive = $this->getFilePathInArchive($xml_file_id); |
50
|
|
|
if ($this->skip_files) { |
51
|
|
|
$this->archive->addFromString($path_in_archive, ''); |
52
|
|
|
} else { |
53
|
|
|
$this->archive->addFile( |
54
|
|
|
$this->getFilePathOnServer($artifact_type_id, $row['id']), |
55
|
|
|
$path_in_archive |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
$file = $this->node_helper->createElement('file'); |
59
|
|
|
$file->setAttribute('id', $xml_file_id); |
60
|
|
|
$file->appendChild($this->node_helper->getNodeWithValue('filename', $row['filename'])); |
61
|
|
|
$file->appendChild($this->node_helper->getNodeWithValue('path', $this->getFilePathInArchive($xml_file_id))); |
62
|
|
|
$file->appendChild($this->node_helper->getNodeWithValue('filesize', $row['filesize'])); |
63
|
|
|
$file->appendChild($this->node_helper->getNodeWithValue('filetype', $row['filetype'])); |
64
|
|
|
$file->appendChild($this->node_helper->getNodeWithValue('description', $row['description'])); |
65
|
|
|
$artifact_node->appendChild($file); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function getFilePathOnServer($artifact_type_id, $attachment_id) { |
70
|
|
|
return ArtifactFile::getPathOnFilesystemByArtifactTypeId($artifact_type_id, $attachment_id); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function getFilePathInArchive($xml_file_id) { |
74
|
|
|
return ArtifactXMLExporter::ARCHIVE_DATA_DIR.DIRECTORY_SEPARATOR.'Artifact'.$xml_file_id; |
75
|
|
|
} |
76
|
|
|
} |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.