[TASK] Make sys_file_metadata translatable
[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 * Repository Class as an abstraction layer to sys_file_metadata
36 *
37 * Every access to table sys_file_metadata which is not handled by TCEmain
38 * has to use this Repository class
39 */
40 class MetaDataRepository implements SingletonInterface {
41
42 /**
43 * @var string
44 */
45 protected $tableName = 'sys_file_metadata';
46
47 /**
48 * Wrapper method for getting DatabaseConnection
49 *
50 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
51 */
52 protected function getDatabase() {
53 return $GLOBALS['TYPO3_DB'];
54 }
55
56 /**
57 * Returns array of meta-data properties
58 *
59 * @param File $file
60 * @return array
61 */
62 public function findByFile(File $file) {
63 return $this->findByFileUid($file->getUid());
64 }
65
66 /**
67 * Retrieves metadata for file
68 *
69 * @param int $uid
70 * @return array
71 * @throws \RuntimeException
72 */
73 public function findByFileUid($uid) {
74 $uid = intval($uid);
75 if ($uid <= 0) {
76 throw new \RuntimeException('Metadata can only be retrieved for indexed files.', 1381590731);
77 }
78 $record = $this->getDatabase()->exec_SELECTgetSingleRow('*', $this->tableName, 'file = ' . $uid . $this->getGeneralWhereClause());
79
80 if ($record === FALSE) {
81 $record = $this->createMetaDataRecord($uid);
82 }
83
84 return $record;
85 }
86
87 /**
88 * General Where-Clause which is needed to fetch only language 0 and live record.
89 *
90 * @return string
91 */
92 protected function getGeneralWhereClause() {
93 return ' AND sys_language_uid IN (0,-1) AND pid=0';
94 }
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 }