[TASK] FrontendContentAdapterService processes record repeatedly
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Resource / Service / FrontendContentAdapterService.php
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Service;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011-2013 Ingmar Schlecht <ingmar@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26 /**
27 * Tslib content adapter to modify $row array ($cObj->data[]) for backwards compatibility
28 *
29 * @author Ingmar Schlecht <ingmar@typo3.org>
30 * @license http://www.gnu.org/copyleft/gpl.html
31 */
32 class FrontendContentAdapterService {
33
34 /**
35 * The name of the table
36 *
37 * @var string
38 */
39 static protected $migrateFields = array(
40 'tt_content' => array(
41 'image' => array(
42 'paths' => 'image',
43 'titleTexts' => 'titleText',
44 'captions' => 'imagecaption',
45 'links' => 'image_link',
46 'alternativeTexts' => 'altText',
47 '__typeMatch' => array(
48 'typeField' => 'CType',
49 'types' => array('image', 'textpic'),
50 )
51 ),
52 'media' => array(
53 'paths' => 'media',
54 'captions' => 'imagecaption',
55 '__typeMatch' => array(
56 'typeField' => 'CType',
57 'types' => array('uploads'),
58 )
59 )
60 ),
61 'pages' => array(
62 'media' => array(
63 'paths' => 'media'
64 )
65 )
66 );
67
68 /**
69 * @var array
70 */
71 protected static $migrationCache = array();
72
73 /**
74 * Modifies the DB row in the CONTENT cObj of tslib_content for supplying
75 * backwards compatibility for some file fields which have switched to using
76 * the new File API instead of the old uploads/ folder for storing files.
77 *
78 * This method is called by the render() method of \TYPO3\CMS\Frontend\ContentObject\ContentContentObject
79 *
80 * @param array $row typically an array, but can also be null (in extensions or e.g. FLUID viewhelpers)
81 * @param string $table the database table where the record is from
82 * @throws \RuntimeException
83 * @return void
84 */
85 static public function modifyDBRow(&$row, $table) {
86 // Only consider records with uid set, that have
87 // not been processed yet ("migrated")
88 if (!isset($row['uid']) || isset($row['_MIGRATED']) && $row['_MIGRATED'] === TRUE) {
89 return;
90 }
91 // Only consider records of table pages and tt_content
92 if ($table !== 'pages' && $table !== 'tt_content') {
93 return;
94 }
95 // Use cached result, if available
96 if (!empty(static::$migrationCache[$table][$row['uid']])) {
97 $row = static::$migrationCache[$table][$row['uid']];
98 return;
99 }
100 // Process fields and execute "migration"
101 if (!isset(static::$migrationCache[$table])) {
102 static::$migrationCache[$table] = array();
103 }
104 foreach (static::$migrateFields[$table] as $migrateFieldName => $oldFieldNames) {
105 if (isset($row[$migrateFieldName]) && self::fieldIsInType($migrateFieldName, $table, $row)) {
106 /** @var $fileRepository \TYPO3\CMS\Core\Resource\FileRepository */
107 $fileRepository = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Resource\\FileRepository');
108 if ($table === 'pages' && isset($row['_LOCALIZED_UID']) && intval($row['sys_language_uid']) > 0) {
109 $table = 'pages_language_overlay';
110 }
111 $files = $fileRepository->findByRelation($table, $migrateFieldName, isset($row['_LOCALIZED_UID']) ? intval($row['_LOCALIZED_UID']) : intval($row['uid']));
112 $fileFieldContents = array(
113 'paths' => array(),
114 'titleTexts' => array(),
115 'captions' => array(),
116 'links' => array(),
117 'alternativeTexts' => array(),
118 $migrateFieldName . '_fileUids' => array(),
119 $migrateFieldName . '_fileReferenceUids' => array(),
120 );
121 $oldFieldNames[$migrateFieldName . '_fileUids'] = $migrateFieldName . '_fileUids';
122 $oldFieldNames[$migrateFieldName . '_fileReferenceUids'] = $migrateFieldName . '_fileReferenceUids';
123
124 foreach ($files as $file) {
125 /** @var $file \TYPO3\CMS\Core\Resource\FileReference */
126 $fileProperties = $file->getProperties();
127 $fileFieldContents['paths'][] = '../../' . $file->getPublicUrl();
128 $fileFieldContents['titleTexts'][] = $fileProperties['title'];
129 $fileFieldContents['captions'][] = $fileProperties['description'];
130 $fileFieldContents['links'][] = $fileProperties['link'];
131 $fileFieldContents['alternativeTexts'][] = $fileProperties['alternative'];
132 $fileFieldContents[$migrateFieldName . '_fileUids'][] = $file->getOriginalFile()->getUid();
133 $fileFieldContents[$migrateFieldName . '_fileReferenceUids'][] = $file->getUid();
134 }
135 foreach ($oldFieldNames as $oldFieldType => $oldFieldName) {
136 if ($oldFieldType === '__typeMatch') {
137 continue;
138 }
139 if ($oldFieldType === 'paths' || substr($oldFieldType, -9) == '_fileUids' || substr($oldFieldType, -18) == '_fileReferenceUids') {
140 // For paths and uids, make comma separated list
141 $fieldContents = implode(',', $fileFieldContents[$oldFieldType]);
142 } else {
143 // For all other fields, separate by newline
144 $fieldContents = implode(chr(10), $fileFieldContents[$oldFieldType]);
145 }
146 $row[$oldFieldName] = $fieldContents;
147 }
148 }
149 }
150
151 $row['_MIGRATED'] = TRUE;
152 static::$migrationCache[$table][$row['uid']] = $row;
153 }
154
155 /**
156 * Checks whether field is in type
157 *
158 * @param string $fieldName
159 * @param string $table
160 * @param array $row
161 * @return boolean
162 */
163 static protected function fieldIsInType($fieldName, $table, array $row) {
164 $fieldConfiguration = static::$migrateFields[$table][$fieldName];
165 if (empty($fieldConfiguration['__typeMatch'])) {
166 return TRUE;
167 } else {
168 return in_array(
169 $row[$fieldConfiguration['__typeMatch']['typeField']],
170 $fieldConfiguration['__typeMatch']['types']
171 );
172 }
173 }
174 }