[TASK] Merge submodule linkvalidator into core
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / ProcessedFileRepository.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource;
3 use \TYPO3\CMS\Core\Utility;
4
5 /***************************************************************
6 * Copyright notice
7 *
8 * (c) 2012-2013 Benjamin Mack <benni@typo3.org>
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 * A copy is found in the textfile GPL.txt and important notices to the license
20 * from the author is found in LICENSE.txt distributed with these scripts.
21 *
22 *
23 * This script is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * This copyright notice MUST APPEAR in all copies of the script!
29 ***************************************************************/
30 /**
31 * Repository for accessing files
32 * it also serves as the public API for the indexing part of files in general
33 *
34 * @author Benjamin Mack <benni@typo3.org>
35 * @author Ingmar Schlecht <ingmar@typo3.org>
36 */
37 class ProcessedFileRepository extends AbstractRepository {
38
39 /**
40 * The main object type of this class. In some cases (fileReference) this
41 * repository can also return FileReference objects, implementing the
42 * common FileInterface.
43 *
44 * @var string
45 */
46 protected $objectType = 'TYPO3\\CMS\\Core\\Resource\\ProcessedFile';
47
48 /**
49 * Main File object storage table. Note that this repository also works on
50 * the sys_file_reference table when returning FileReference objects.
51 *
52 * @var string
53 */
54 protected $table = 'sys_file_processedfile';
55
56 /**
57 * @var ResourceFactory
58 */
59 protected $resourceFactory;
60
61 /**
62 * @var \TYPO3\CMS\Core\Database\DatabaseConnection
63 */
64 protected $databaseConnection;
65
66 /**
67 * Creates this object.
68 */
69 public function __construct() {
70 $this->resourceFactory = Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\ResourceFactory');
71 $this->databaseConnection = $GLOBALS['TYPO3_DB'];
72 }
73
74 /**
75 * Creates a ProcessedFile object from a file object and a processing configuration
76 *
77 * @param FileInterface $originalFile
78 * @param string $taskType
79 * @param array $configuration
80 * @return ProcessedFile
81 */
82 public function createNewProcessedFileObject(FileInterface $originalFile, $taskType, array $configuration) {
83 return Utility\GeneralUtility::makeInstance(
84 $this->objectType,
85 $originalFile,
86 $taskType,
87 $configuration
88 );
89 }
90
91 /**
92 * @param array $databaseRow
93 * @return ProcessedFile
94 */
95 protected function createDomainObject(array $databaseRow) {
96 $originalFile = $this->resourceFactory->getFileObject(intval($databaseRow['original']));
97 $originalFile->setStorage($this->resourceFactory->getStorageObject($originalFile->getProperty('storage')));
98 $taskType = $databaseRow['task_type'];
99 $configuration = unserialize($databaseRow['configuration']);
100
101 return Utility\GeneralUtility::makeInstance(
102 $this->objectType,
103 $originalFile,
104 $taskType,
105 $configuration,
106 $databaseRow
107 );
108 }
109
110 /**
111 * Adds a processedfile object in the database
112 *
113 * @param ProcessedFile $processedFile
114 * @return void
115 */
116 public function add($processedFile) {
117 if ($processedFile->isPersisted()) {
118 $this->update($processedFile);
119 } else {
120 $insertFields = $processedFile->toArray();
121 $insertFields['crdate'] = $insertFields['tstamp'] = time();
122 $insertFields = $this->cleanUnavailableColumns($insertFields);
123 $this->databaseConnection->exec_INSERTquery($this->table, $insertFields);
124 $uid = $this->databaseConnection->sql_insert_id();
125 $processedFile->updateProperties(array('uid' => $uid));
126 }
127 }
128
129 /**
130 * Updates an existing file object in the database
131 *
132 * @param ProcessedFile $processedFile
133 * @return void
134 */
135 public function update($processedFile) {
136 if ($processedFile->isPersisted()) {
137 $uid = intval($processedFile->getUid());
138 $updateFields = $this->cleanUnavailableColumns($processedFile->toArray());
139 $updateFields['tstamp'] = time();
140 $this->databaseConnection->exec_UPDATEquery($this->table, 'uid=' . intval($uid), $updateFields);
141 }
142 }
143
144 /**
145 * @param \TYPO3\CMS\Core\Resource\File|\TYPO3\CMS\Core\Resource\FileInterface $file
146 * @param string $taskType The task that should be executed on the file
147 * @param array $configuration
148 *
149 * @return ProcessedFile
150 */
151 public function findOneByOriginalFileAndTaskTypeAndConfiguration(FileInterface $file, $taskType, array $configuration) {
152 $databaseRow = $this->databaseConnection->exec_SELECTgetSingleRow(
153 '*',
154 $this->table,
155 'original=' . intval($file->getUid()) .
156 ' AND task_type=' . $this->databaseConnection->fullQuoteStr($taskType, $this->table) .
157 ' AND configuration=' . $this->databaseConnection->fullQuoteStr(serialize($configuration), $this->table)
158 );
159
160 if (is_array($databaseRow)) {
161 $processedFile = $this->createDomainObject($databaseRow);
162 } else {
163 $processedFile = $this->createNewProcessedFileObject($file, $taskType, $configuration);
164 }
165 return $processedFile;
166 }
167
168 /**
169 * Removes all array keys which cannot be persisted
170 *
171 * @param array $data
172 *
173 * @return array
174 */
175 protected function cleanUnavailableColumns(array $data) {
176 return array_intersect_key($data, $this->databaseConnection->admin_get_fields($this->table));
177 }
178 }
179
180
181 ?>