Completed
Push — master ( 11d1bd...ce0e70 )
by Fabien
54:57
created
Classes/Hook/DataHandlerHook.php 1 patch
Indentation   +309 added lines, -309 removed lines patch added patch discarded remove patch
@@ -26,313 +26,313 @@
 block discarded – undo
26 26
  */
27 27
 class DataHandlerHook
28 28
 {
29
-    /**
30
-     * Store indexed file before the Data Handler start "working".
31
-     *
32
-     * @var array
33
-     */
34
-    protected $beforeDataHandlerProcessFileIdentifiers = [];
35
-
36
-    /**
37
-     * Store indexed file after the Data Handler has done its job.
38
-     *
39
-     * @var array
40
-     */
41
-    protected $afterDataHandlerProcessFileIdentifiers = [];
42
-
43
-    /**
44
-     * Internal key for the Cache Manager.
45
-     *
46
-     * @var string
47
-     */
48
-    protected $registerKey = 'media-hook-elementsToKeepTrack';
49
-
50
-    /**
51
-     * First procedures to launch before all operations in DataHandler.
52
-     *
53
-     * Feed variable $this->beforeDataHandlerProcessFileIdentifiers
54
-     *
55
-     * @param DataHandler $caller TCEMain Object
56
-     * @return void
57
-     */
58
-    public function processDatamap_beforeStart(DataHandler $caller)
59
-    {
60
-        // Use a register to keep track of files.
61
-        // It is required according to TCEMain behaviour which register "elements to be deleted".
62
-        // Those element must not be forgotten.
63
-        $this->initializeFileRegister();
64
-        $this->registerFilesToKeepTrack();
65
-
66
-        foreach ($caller->datamap as $tableName => $configuration) {
67
-            $id = key($configuration);
68
-            if (!MathUtility::canBeInterpretedAsInteger($id)) {
69
-                continue;
70
-            }
71
-
72
-            /** @var $refIndexObj \TYPO3\CMS\Core\Database\ReferenceIndex */
73
-            $refIndexObj = GeneralUtility::makeInstance(ReferenceIndex::class);
74
-            if (BackendUtility::isTableWorkspaceEnabled($tableName)) {
75
-                $refIndexObj->setWorkspaceId($caller->BE_USER->workspace);
76
-            }
77
-            $indexes = $refIndexObj->updateRefIndexTable($tableName, $id);
78
-
79
-            // Make sure $index is an array.
80
-            if (!is_array($indexes)) {
81
-                $indexes = [];
82
-            }
83
-
84
-            try {
85
-                $fileIdentifiers = $this->lookForFiles($indexes);
86
-                $this->addBeforeDataHandlerProcessFileIdentifiers($fileIdentifiers);
87
-            } catch (\Exception $e) {
88
-                // do nothing
89
-            }
90
-        }
91
-    }
92
-
93
-    /**
94
-     * Last procedures to launch after all operations in DataHandler.
95
-     *
96
-     * Process field "number_of_references" which may require updates.
97
-     *
98
-     * @param DataHandler $caller TCEMain Object
99
-     * @return void
100
-     */
101
-    public function processDatamap_afterAllOperations(DataHandler $caller)
102
-    {
103
-        // First collect files which have been involved.
104
-        foreach ($caller->datamap as $tableName => $configuration) {
105
-            $id = key($configuration);
106
-
107
-            /** @var $refIndexObj \TYPO3\CMS\Core\Database\ReferenceIndex */
108
-            $refIndexObj = GeneralUtility::makeInstance(ReferenceIndex::class);
109
-            if (BackendUtility::isTableWorkspaceEnabled($tableName)) {
110
-                $refIndexObj->setWorkspaceId($caller->BE_USER->workspace);
111
-            }
112
-            $indexes = $refIndexObj->updateRefIndexTable($tableName, $id);
113
-
114
-            // Make sure $index is an array.
115
-            if (!is_array($indexes)) {
116
-                $indexes = [];
117
-            }
118
-
119
-            $fileIdentifiers = $this->lookForFiles($indexes);
120
-            $this->addAfterDataHandlerProcessFileIdentifiers($fileIdentifiers);
121
-        }
122
-
123
-        // After collecting files, update the column "number_of_references".
124
-        foreach ($this->getFileToProcess() as $fileIdentifier) {
125
-            try {
126
-                $file = $this->getResourceFactory()->getFileObject($fileIdentifier);
127
-                $numberOfReferences = $this->getFileReferenceService()->countTotalReferences($file);
128
-
129
-                $values = array(
130
-                    'number_of_references' => $numberOfReferences
131
-                );
132
-                $this->getDataService()->update('sys_file', $values, ['uid' => $file->getUid()]);
133
-            } catch (FileDoesNotExistException $fileDoesNotExistException) {
134
-                // Do nothing here. A file that does not exist needs no update.
135
-                // See https://github.com/fabarea/media/issues/159 for more information.
136
-            }
137
-        }
138
-    }
139
-
140
-    /**
141
-     * @return void
142
-     */
143
-    protected function initializeFileRegister()
144
-    {
145
-        $items = $this->getMemoryCache()->get($this->registerKey);
146
-        if (!is_array($items)) {
147
-            $this->getMemoryCache()->set($this->registerKey, []);
148
-        }
149
-    }
150
-
151
-    /**
152
-     * @return void
153
-     */
154
-    protected function registerFilesToKeepTrack()
155
-    {
156
-        $fileIdentifiers = [];
157
-        $elementsToBeDeleted = $this->getMemoryCache()->get('core-t3lib_TCEmain-elementsToBeDeleted');
158
-        if (is_array($elementsToBeDeleted)) {
159
-            foreach ($elementsToBeDeleted as $tableName => $element) {
160
-                if ($tableName === 'sys_file_reference') {
161
-                    $fileReferenceIdentifier = key($element);
162
-                    if ($element[$fileReferenceIdentifier] === true) {
163
-                        $fileIdentifier = $this->findFileByFileReference($fileReferenceIdentifier);
164
-                        $fileIdentifiers[] = $fileIdentifier;
165
-                    }
166
-                }
167
-            }
168
-        }
169
-
170
-        // Put back in the memory cache the value.
171
-        $items = $this->getMemoryCache()->get($this->registerKey);
172
-        $mergedItems = array_merge($items, $fileIdentifiers);
173
-        $this->getMemoryCache()->set($this->registerKey, $mergedItems);
174
-    }
175
-
176
-    /**
177
-     * @return array
178
-     */
179
-    protected function getRegisteredFiles()
180
-    {
181
-        $files = $this->getMemoryCache()->get($this->registerKey);
182
-        return $files;
183
-    }
184
-
185
-    /**
186
-     * Look for file which are within the reference index.
187
-     *
188
-     * @return array
189
-     */
190
-    protected function getFileToProcess()
191
-    {
192
-        $fileIdentifiers = array_merge(
193
-            $this->beforeDataHandlerProcessFileIdentifiers,
194
-            $this->afterDataHandlerProcessFileIdentifiers,
195
-            $this->getRegisteredFiles()
196
-        );
197
-        return array_unique($fileIdentifiers);
198
-    }
199
-
200
-    /**
201
-     * @param array $fileIdentifiers
202
-     * @return void
203
-     */
204
-    protected function addBeforeDataHandlerProcessFileIdentifiers(array $fileIdentifiers)
205
-    {
206
-        $this->beforeDataHandlerProcessFileIdentifiers = array_merge($this->beforeDataHandlerProcessFileIdentifiers, $fileIdentifiers);
207
-    }
208
-
209
-    /**
210
-     * @param array $fileIdentifiers
211
-     * @return void
212
-     */
213
-    protected function addAfterDataHandlerProcessFileIdentifiers(array $fileIdentifiers)
214
-    {
215
-        $this->afterDataHandlerProcessFileIdentifiers = array_merge($this->afterDataHandlerProcessFileIdentifiers, $fileIdentifiers);
216
-    }
217
-
218
-    /**
219
-     * Look for file which are within the reference index.
220
-     *
221
-     * @param array $indexes
222
-     * @return array
223
-     */
224
-    protected function lookForFiles(array $indexes)
225
-    {
226
-        $fileIdentifiers = [];
227
-        if (isset($indexes['relations'])) {
228
-            foreach ($indexes['relations'] as $index) {
229
-                if (is_array($index)) {
230
-                    if ($this->isSoftReferenceImage($index)) {
231
-                        $fileIdentifiers[] = $index['ref_uid'];
232
-                    } elseif ($this->isSoftReferenceLink($index)) {
233
-                        $fileIdentifiers[] = $index['ref_uid'];
234
-                    } elseif ($this->isFileReference($index)) {
235
-                        $fileIdentifiers[] = $this->findFileByFileReference($index['ref_uid']);
236
-                    }
237
-                }
238
-            }
239
-        }
240
-
241
-        return $fileIdentifiers;
242
-    }
243
-
244
-    /**
245
-     * @param array $index
246
-     * @return bool
247
-     */
248
-    public function isFileReference(array $index)
249
-    {
250
-        return $index['ref_table'] === 'sys_file_reference';
251
-    }
252
-
253
-    /**
254
-     * @param array $index
255
-     * @return bool
256
-     */
257
-    public function isSoftReferenceLink(array $index)
258
-    {
259
-        return $index['softref_key'] === 'typolink_tag' && $index['ref_table'] === 'sys_file';
260
-    }
261
-
262
-    /**
263
-     * @param array $index
264
-     * @return bool
265
-     */
266
-    public function isSoftReferenceImage(array $index)
267
-    {
268
-        return $index['softref_key'] === 'rtehtmlarea_images' && $index['ref_table'] === 'sys_file';
269
-    }
270
-
271
-    /**
272
-     * Retrieve the File identifier.
273
-     *
274
-     * @param $fileReferenceIdentifier
275
-     * @return int
276
-     * @throws \Exception
277
-     */
278
-    protected function findFileByFileReference($fileReferenceIdentifier)
279
-    {
280
-        $record = $this->getDataService()->getRecord(
281
-            'sys_file_reference',
282
-            [
283
-                'uid' => $fileReferenceIdentifier
284
-            ],
285
-            [
286
-                DeletedRestriction::class
287
-            ]
288
-        );
289
-
290
-        if (empty($record)) {
291
-            throw new \Exception('There is something broken with the File References. Consider updating the Reference Index.', 1408619796);
292
-        }
293
-
294
-        $fileIdentifier = $record['uid_local'];
295
-        return $fileIdentifier;
296
-    }
297
-
298
-    /**
299
-     * @return FileReferenceService|object
300
-     */
301
-    protected function getFileReferenceService()
302
-    {
303
-        return GeneralUtility::makeInstance(FileReferenceService::class);
304
-    }
305
-
306
-    /**
307
-     * Gets an instance of the memory cache.
308
-     *
309
-     * @return VariableFrontend
310
-     */
311
-    protected function getMemoryCache()
312
-    {
313
-        return $this->getCacheManager()->getCache('cache_runtime');
314
-    }
315
-
316
-    /**
317
-     * Create and returns an instance of the CacheManager
318
-     *
319
-     * @return CacheManager|object
320
-     */
321
-    protected function getCacheManager()
322
-    {
323
-        return GeneralUtility::makeInstance(CacheManager::class);
324
-    }
325
-
326
-    /**
327
-     * @return object|DataService
328
-     */
329
-    protected function getDataService(): DataService
330
-    {
331
-        return GeneralUtility::makeInstance(DataService::class);
332
-    }
333
-
334
-    protected function getResourceFactory(): ResourceFactory
335
-    {
336
-        return GeneralUtility::makeInstance(ResourceFactory::class);
337
-    }
29
+	/**
30
+	 * Store indexed file before the Data Handler start "working".
31
+	 *
32
+	 * @var array
33
+	 */
34
+	protected $beforeDataHandlerProcessFileIdentifiers = [];
35
+
36
+	/**
37
+	 * Store indexed file after the Data Handler has done its job.
38
+	 *
39
+	 * @var array
40
+	 */
41
+	protected $afterDataHandlerProcessFileIdentifiers = [];
42
+
43
+	/**
44
+	 * Internal key for the Cache Manager.
45
+	 *
46
+	 * @var string
47
+	 */
48
+	protected $registerKey = 'media-hook-elementsToKeepTrack';
49
+
50
+	/**
51
+	 * First procedures to launch before all operations in DataHandler.
52
+	 *
53
+	 * Feed variable $this->beforeDataHandlerProcessFileIdentifiers
54
+	 *
55
+	 * @param DataHandler $caller TCEMain Object
56
+	 * @return void
57
+	 */
58
+	public function processDatamap_beforeStart(DataHandler $caller)
59
+	{
60
+		// Use a register to keep track of files.
61
+		// It is required according to TCEMain behaviour which register "elements to be deleted".
62
+		// Those element must not be forgotten.
63
+		$this->initializeFileRegister();
64
+		$this->registerFilesToKeepTrack();
65
+
66
+		foreach ($caller->datamap as $tableName => $configuration) {
67
+			$id = key($configuration);
68
+			if (!MathUtility::canBeInterpretedAsInteger($id)) {
69
+				continue;
70
+			}
71
+
72
+			/** @var $refIndexObj \TYPO3\CMS\Core\Database\ReferenceIndex */
73
+			$refIndexObj = GeneralUtility::makeInstance(ReferenceIndex::class);
74
+			if (BackendUtility::isTableWorkspaceEnabled($tableName)) {
75
+				$refIndexObj->setWorkspaceId($caller->BE_USER->workspace);
76
+			}
77
+			$indexes = $refIndexObj->updateRefIndexTable($tableName, $id);
78
+
79
+			// Make sure $index is an array.
80
+			if (!is_array($indexes)) {
81
+				$indexes = [];
82
+			}
83
+
84
+			try {
85
+				$fileIdentifiers = $this->lookForFiles($indexes);
86
+				$this->addBeforeDataHandlerProcessFileIdentifiers($fileIdentifiers);
87
+			} catch (\Exception $e) {
88
+				// do nothing
89
+			}
90
+		}
91
+	}
92
+
93
+	/**
94
+	 * Last procedures to launch after all operations in DataHandler.
95
+	 *
96
+	 * Process field "number_of_references" which may require updates.
97
+	 *
98
+	 * @param DataHandler $caller TCEMain Object
99
+	 * @return void
100
+	 */
101
+	public function processDatamap_afterAllOperations(DataHandler $caller)
102
+	{
103
+		// First collect files which have been involved.
104
+		foreach ($caller->datamap as $tableName => $configuration) {
105
+			$id = key($configuration);
106
+
107
+			/** @var $refIndexObj \TYPO3\CMS\Core\Database\ReferenceIndex */
108
+			$refIndexObj = GeneralUtility::makeInstance(ReferenceIndex::class);
109
+			if (BackendUtility::isTableWorkspaceEnabled($tableName)) {
110
+				$refIndexObj->setWorkspaceId($caller->BE_USER->workspace);
111
+			}
112
+			$indexes = $refIndexObj->updateRefIndexTable($tableName, $id);
113
+
114
+			// Make sure $index is an array.
115
+			if (!is_array($indexes)) {
116
+				$indexes = [];
117
+			}
118
+
119
+			$fileIdentifiers = $this->lookForFiles($indexes);
120
+			$this->addAfterDataHandlerProcessFileIdentifiers($fileIdentifiers);
121
+		}
122
+
123
+		// After collecting files, update the column "number_of_references".
124
+		foreach ($this->getFileToProcess() as $fileIdentifier) {
125
+			try {
126
+				$file = $this->getResourceFactory()->getFileObject($fileIdentifier);
127
+				$numberOfReferences = $this->getFileReferenceService()->countTotalReferences($file);
128
+
129
+				$values = array(
130
+					'number_of_references' => $numberOfReferences
131
+				);
132
+				$this->getDataService()->update('sys_file', $values, ['uid' => $file->getUid()]);
133
+			} catch (FileDoesNotExistException $fileDoesNotExistException) {
134
+				// Do nothing here. A file that does not exist needs no update.
135
+				// See https://github.com/fabarea/media/issues/159 for more information.
136
+			}
137
+		}
138
+	}
139
+
140
+	/**
141
+	 * @return void
142
+	 */
143
+	protected function initializeFileRegister()
144
+	{
145
+		$items = $this->getMemoryCache()->get($this->registerKey);
146
+		if (!is_array($items)) {
147
+			$this->getMemoryCache()->set($this->registerKey, []);
148
+		}
149
+	}
150
+
151
+	/**
152
+	 * @return void
153
+	 */
154
+	protected function registerFilesToKeepTrack()
155
+	{
156
+		$fileIdentifiers = [];
157
+		$elementsToBeDeleted = $this->getMemoryCache()->get('core-t3lib_TCEmain-elementsToBeDeleted');
158
+		if (is_array($elementsToBeDeleted)) {
159
+			foreach ($elementsToBeDeleted as $tableName => $element) {
160
+				if ($tableName === 'sys_file_reference') {
161
+					$fileReferenceIdentifier = key($element);
162
+					if ($element[$fileReferenceIdentifier] === true) {
163
+						$fileIdentifier = $this->findFileByFileReference($fileReferenceIdentifier);
164
+						$fileIdentifiers[] = $fileIdentifier;
165
+					}
166
+				}
167
+			}
168
+		}
169
+
170
+		// Put back in the memory cache the value.
171
+		$items = $this->getMemoryCache()->get($this->registerKey);
172
+		$mergedItems = array_merge($items, $fileIdentifiers);
173
+		$this->getMemoryCache()->set($this->registerKey, $mergedItems);
174
+	}
175
+
176
+	/**
177
+	 * @return array
178
+	 */
179
+	protected function getRegisteredFiles()
180
+	{
181
+		$files = $this->getMemoryCache()->get($this->registerKey);
182
+		return $files;
183
+	}
184
+
185
+	/**
186
+	 * Look for file which are within the reference index.
187
+	 *
188
+	 * @return array
189
+	 */
190
+	protected function getFileToProcess()
191
+	{
192
+		$fileIdentifiers = array_merge(
193
+			$this->beforeDataHandlerProcessFileIdentifiers,
194
+			$this->afterDataHandlerProcessFileIdentifiers,
195
+			$this->getRegisteredFiles()
196
+		);
197
+		return array_unique($fileIdentifiers);
198
+	}
199
+
200
+	/**
201
+	 * @param array $fileIdentifiers
202
+	 * @return void
203
+	 */
204
+	protected function addBeforeDataHandlerProcessFileIdentifiers(array $fileIdentifiers)
205
+	{
206
+		$this->beforeDataHandlerProcessFileIdentifiers = array_merge($this->beforeDataHandlerProcessFileIdentifiers, $fileIdentifiers);
207
+	}
208
+
209
+	/**
210
+	 * @param array $fileIdentifiers
211
+	 * @return void
212
+	 */
213
+	protected function addAfterDataHandlerProcessFileIdentifiers(array $fileIdentifiers)
214
+	{
215
+		$this->afterDataHandlerProcessFileIdentifiers = array_merge($this->afterDataHandlerProcessFileIdentifiers, $fileIdentifiers);
216
+	}
217
+
218
+	/**
219
+	 * Look for file which are within the reference index.
220
+	 *
221
+	 * @param array $indexes
222
+	 * @return array
223
+	 */
224
+	protected function lookForFiles(array $indexes)
225
+	{
226
+		$fileIdentifiers = [];
227
+		if (isset($indexes['relations'])) {
228
+			foreach ($indexes['relations'] as $index) {
229
+				if (is_array($index)) {
230
+					if ($this->isSoftReferenceImage($index)) {
231
+						$fileIdentifiers[] = $index['ref_uid'];
232
+					} elseif ($this->isSoftReferenceLink($index)) {
233
+						$fileIdentifiers[] = $index['ref_uid'];
234
+					} elseif ($this->isFileReference($index)) {
235
+						$fileIdentifiers[] = $this->findFileByFileReference($index['ref_uid']);
236
+					}
237
+				}
238
+			}
239
+		}
240
+
241
+		return $fileIdentifiers;
242
+	}
243
+
244
+	/**
245
+	 * @param array $index
246
+	 * @return bool
247
+	 */
248
+	public function isFileReference(array $index)
249
+	{
250
+		return $index['ref_table'] === 'sys_file_reference';
251
+	}
252
+
253
+	/**
254
+	 * @param array $index
255
+	 * @return bool
256
+	 */
257
+	public function isSoftReferenceLink(array $index)
258
+	{
259
+		return $index['softref_key'] === 'typolink_tag' && $index['ref_table'] === 'sys_file';
260
+	}
261
+
262
+	/**
263
+	 * @param array $index
264
+	 * @return bool
265
+	 */
266
+	public function isSoftReferenceImage(array $index)
267
+	{
268
+		return $index['softref_key'] === 'rtehtmlarea_images' && $index['ref_table'] === 'sys_file';
269
+	}
270
+
271
+	/**
272
+	 * Retrieve the File identifier.
273
+	 *
274
+	 * @param $fileReferenceIdentifier
275
+	 * @return int
276
+	 * @throws \Exception
277
+	 */
278
+	protected function findFileByFileReference($fileReferenceIdentifier)
279
+	{
280
+		$record = $this->getDataService()->getRecord(
281
+			'sys_file_reference',
282
+			[
283
+				'uid' => $fileReferenceIdentifier
284
+			],
285
+			[
286
+				DeletedRestriction::class
287
+			]
288
+		);
289
+
290
+		if (empty($record)) {
291
+			throw new \Exception('There is something broken with the File References. Consider updating the Reference Index.', 1408619796);
292
+		}
293
+
294
+		$fileIdentifier = $record['uid_local'];
295
+		return $fileIdentifier;
296
+	}
297
+
298
+	/**
299
+	 * @return FileReferenceService|object
300
+	 */
301
+	protected function getFileReferenceService()
302
+	{
303
+		return GeneralUtility::makeInstance(FileReferenceService::class);
304
+	}
305
+
306
+	/**
307
+	 * Gets an instance of the memory cache.
308
+	 *
309
+	 * @return VariableFrontend
310
+	 */
311
+	protected function getMemoryCache()
312
+	{
313
+		return $this->getCacheManager()->getCache('cache_runtime');
314
+	}
315
+
316
+	/**
317
+	 * Create and returns an instance of the CacheManager
318
+	 *
319
+	 * @return CacheManager|object
320
+	 */
321
+	protected function getCacheManager()
322
+	{
323
+		return GeneralUtility::makeInstance(CacheManager::class);
324
+	}
325
+
326
+	/**
327
+	 * @return object|DataService
328
+	 */
329
+	protected function getDataService(): DataService
330
+	{
331
+		return GeneralUtility::makeInstance(DataService::class);
332
+	}
333
+
334
+	protected function getResourceFactory(): ResourceFactory
335
+	{
336
+		return GeneralUtility::makeInstance(ResourceFactory::class);
337
+	}
338 338
 }
Please login to merge, or discard this patch.