[BUGFIX] Adapt IndexerService to new table structures
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Index / MetaDataRepository.php
1 <?php
2
3 namespace TYPO3\CMS\Core\Resource\Index;
4
5 /***************************************************************
6 * Copyright notice
7 *
8 * (c) 2013 Steffen Ritter <steffen.ritter@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 use TYPO3\CMS\Core\Resource\File;
32 use TYPO3\CMS\Core\SingletonInterface;
33
34
35 /**
36 * Repository Class as an abstraction layer to sys_file_metadata
37 *
38 * Every access to table sys_file_metadata which is not handled by TCEmain
39 * has to use this Repository class
40 */
41 class MetaDataRepository implements SingletonInterface {
42
43 /**
44 * @var string
45 */
46 protected $tableName = 'sys_file_metadata';
47
48 /**
49 * Wrapper method for getting DatabaseConnection
50 *
51 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
52 */
53 protected function getDatabase() {
54 return $GLOBALS['TYPO3_DB'];
55 }
56
57 /**
58 * Returns array of meta-data properties
59 *
60 * @param File $file
61 * @return array
62 */
63 public function findByFile(File $file) {
64 return $this->findByFileUid($file->getUid());
65 }
66
67 /**
68 * Retrieves metadata for file
69 *
70 * @param int $uid
71 * @return array
72 * @throws \RuntimeException
73 */
74 public function findByFileUid($uid) {
75 $uid = intval($uid);
76 if ($uid <= 0) {
77 throw new \RuntimeException('Metadata can only be retrieved for indexed files.', 1381590731);
78 }
79 $record = $this->getDatabase()->exec_SELECTgetSingleRow('*', $this->tableName, 'file = ' . $uid);
80
81 if ($record === FALSE) {
82 $record = $this->createMetaDataRecord($uid);
83 }
84
85 return $record;
86 }
87
88 /**
89 * General Where-Clause which is needed to fetch only language 0 and live record.
90 *
91 * @return string
92 */
93 protected function getGeneralWhereClause() {
94 return ' AND 1=1';
95 }
96 /**
97 * Create empty
98 *
99 * @param int $fileUid
100 * @return array
101 */
102 protected function createMetaDataRecord($fileUid) {
103 $emptyRecord = array(
104 'file' => intval($fileUid),
105 'pid' => 0,
106 'crdate' => $GLOBALS['EXEC_TIME'],
107 'tstamp' => $GLOBALS['EXEC_TIME'],
108 'cruser_id' => TYPO3_MODE == 'BE' ? $GLOBALS['BE_USER']->user['uid'] : 0
109 );
110 $this->getDatabase()->exec_INSERTquery($this->tableName, $emptyRecord);
111 $record = $emptyRecord;
112 $record['uid'] = $this->getDatabase()->sql_insert_id();
113
114 return $record;
115 }
116
117 /**
118 * Updates the metadata record in the database
119 *
120 * @internal
121 * @param int $fileUid the file uid to update
122 * @param array $data Data to update
123 */
124 public function update($fileUid, array $data) {
125 $updateRow = array_intersect_key($data, $this->getDatabase()->admin_get_fields($this->tableName));
126 if (array_key_exists('uid', $data)) {
127 unset($data['uid']);
128 }
129 $row = $this->findByFileUid($fileUid);
130 if (count($updateRow) > 0) {
131 $updateRow['tstamp'] = time();
132 $this->getDatabase()->exec_UPDATEquery($this->tableName, 'uid = ' . intval($row['uid']), $updateRow);
133 }
134 }
135 }