|
1
|
|
|
<?php |
|
2
|
|
|
namespace Fab\Media\Index; |
|
3
|
|
|
|
|
4
|
|
|
/* |
|
5
|
|
|
* This file is part of the Fab/Media project under GPLv2 or later. |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please read the |
|
8
|
|
|
* LICENSE.md file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
use Fab\Vidi\Service\DataService; |
|
12
|
|
|
use TYPO3\CMS\Core\Resource\File; |
|
13
|
|
|
use TYPO3\CMS\Core\Resource\Index\ExtractorRegistry; |
|
14
|
|
|
use TYPO3\CMS\Core\Resource\Index\FileIndexRepository; |
|
15
|
|
|
use TYPO3\CMS\Core\Resource\Index\MetaDataRepository; |
|
16
|
|
|
use TYPO3\CMS\Core\Resource\ResourceStorage; |
|
17
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
18
|
|
|
use Fab\Media\Utility\ConfigurationUtility; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Service dealing with Indexing in the context of Media. |
|
22
|
|
|
*/ |
|
23
|
|
|
class MediaIndexer |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var ResourceStorage |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $storage = null; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param ResourceStorage $storage |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(ResourceStorage $storage) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->storage = $storage; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param \TYPO3\CMS\Core\Resource\File $file |
|
41
|
|
|
* @return $this |
|
42
|
|
|
*/ |
|
43
|
|
|
public function updateIndex(File $file) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->getCoreIndexer()->updateIndexEntry($file); |
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param \TYPO3\CMS\Core\Resource\File $file |
|
51
|
|
|
* @return $this |
|
52
|
|
|
*/ |
|
53
|
|
|
public function extractMetadata(File $file) |
|
54
|
|
|
{ |
|
55
|
|
|
|
|
56
|
|
|
$extractionServices = $this->getExtractorRegistry()->getExtractorsWithDriverSupport($this->storage->getDriverType()); |
|
57
|
|
|
|
|
58
|
|
|
$newMetaData = array( |
|
59
|
|
|
0 => $file->_getMetaData() |
|
60
|
|
|
); |
|
61
|
|
|
foreach ($extractionServices as $service) { |
|
62
|
|
|
if ($service->canProcess($file)) { |
|
63
|
|
|
$newMetaData[$service->getPriority()] = $service->extractMetaData($file, $newMetaData); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
ksort($newMetaData); |
|
68
|
|
|
$metaData = []; |
|
69
|
|
|
foreach ($newMetaData as $data) { |
|
70
|
|
|
$metaData = array_merge($metaData, $data); |
|
71
|
|
|
} |
|
72
|
|
|
$file->_updateMetaDataProperties($metaData); |
|
73
|
|
|
$this->getMetaDataRepository()->update($file->getUid(), $metaData); |
|
74
|
|
|
$this->getFileIndexRepository()->updateIndexingTime($file->getUid()); |
|
75
|
|
|
|
|
76
|
|
|
return $this; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param \TYPO3\CMS\Core\Resource\File $file |
|
81
|
|
|
* @return $this |
|
82
|
|
|
*/ |
|
83
|
|
|
public function applyDefaultCategories(File $file) |
|
84
|
|
|
{ |
|
85
|
|
|
|
|
86
|
|
|
$categoryList = ConfigurationUtility::getInstance()->get('default_categories'); |
|
87
|
|
|
$categories = GeneralUtility::trimExplode(',', $categoryList, true); |
|
88
|
|
|
|
|
89
|
|
|
foreach ($categories as $category) { |
|
90
|
|
|
$values = array( |
|
91
|
|
|
'uid_local' => $category, |
|
92
|
|
|
'uid_foreign' => $this->getFileMetadataIdentifier($file), |
|
93
|
|
|
'tablenames' => 'sys_file_metadata', |
|
94
|
|
|
'fieldname' => 'categories', |
|
95
|
|
|
); |
|
96
|
|
|
$this->getDataService()->insert('sys_category_record_mm', $values); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$metaData['categories'] = count($categories); |
|
|
|
|
|
|
100
|
|
|
$file->_updateMetaDataProperties($metaData); |
|
101
|
|
|
$this->getMetaDataRepository()->update($file->getUid(), $metaData); |
|
102
|
|
|
$this->getFileIndexRepository()->updateIndexingTime($file->getUid()); |
|
103
|
|
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Retrieve the file metadata uid which is different from the file uid. |
|
108
|
|
|
* |
|
109
|
|
|
* @param \TYPO3\CMS\Core\Resource\File $file |
|
110
|
|
|
* @return int |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function getFileMetadataIdentifier(File $file) |
|
113
|
|
|
{ |
|
114
|
|
|
$metadataProperties = $file->_getMetaData(); |
|
115
|
|
|
return isset($metadataProperties['_ORIG_uid']) ? (int)$metadataProperties['_ORIG_uid'] : (int)$metadataProperties['uid']; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Returns an instance of the FileIndexRepository |
|
121
|
|
|
* |
|
122
|
|
|
* @return FileIndexRepository |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function getFileIndexRepository() |
|
125
|
|
|
{ |
|
126
|
|
|
return FileIndexRepository::getInstance(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Returns an instance of the FileIndexRepository |
|
131
|
|
|
* |
|
132
|
|
|
* @return MetaDataRepository |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function getMetaDataRepository() |
|
135
|
|
|
{ |
|
136
|
|
|
return MetaDataRepository::getInstance(); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Returns an instance of the FileIndexRepository |
|
141
|
|
|
* |
|
142
|
|
|
* @return ExtractorRegistry |
|
143
|
|
|
*/ |
|
144
|
|
|
protected function getExtractorRegistry() |
|
145
|
|
|
{ |
|
146
|
|
|
return ExtractorRegistry::getInstance(); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return object|DataService |
|
151
|
|
|
*/ |
|
152
|
|
|
protected function getDataService(): DataService |
|
153
|
|
|
{ |
|
154
|
|
|
return GeneralUtility::makeInstance(DataService::class); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @return \TYPO3\CMS\Core\Resource\Index\Indexer|object |
|
159
|
|
|
*/ |
|
160
|
|
|
protected function getCoreIndexer() |
|
161
|
|
|
{ |
|
162
|
|
|
return GeneralUtility::makeInstance(\TYPO3\CMS\Core\Resource\Index\Indexer::class, $this->storage); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
} |
|
166
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.