/
ExtensionListUtility.php
279 lines (259 loc) · 9.62 KB
/
ExtensionListUtility.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace TYPO3\CMS\Extensionmanager\Utility\Importer;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Database\Platform\PlatformInformation;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extensionmanager\Domain\Model\Extension;
use TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository;
use TYPO3\CMS\Extensionmanager\Domain\Repository\RepositoryRepository;
use TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException;
use TYPO3\CMS\Extensionmanager\Utility\Parser\AbstractExtensionXmlParser;
use TYPO3\CMS\Extensionmanager\Utility\Parser\XmlParserFactory;
/**
* Importer object for extension list
* @internal This class is a specific ExtensionManager implementation and is not part of the Public TYPO3 API.
*/
class ExtensionListUtility implements \SplObserver
{
/**
* Keeps instance of a XML parser.
*
* @var AbstractExtensionXmlParser
*/
protected $parser;
/**
* Keeps number of processed version records.
*
* @var int
*/
protected $sumRecords = 0;
/**
* Keeps record values to be inserted into database.
*
* @var array
*/
protected $arrRows = [];
/**
* Keeps fieldnames of tx_extensionmanager_domain_model_extension table.
*
* @var array
*/
protected static $fieldNames = [
'extension_key',
'version',
'integer_version',
'current_version',
'alldownloadcounter',
'downloadcounter',
'title',
'ownerusername',
'author_name',
'author_email',
'authorcompany',
'last_updated',
'md5hash',
'repository',
'state',
'review_state',
'category',
'description',
'serialized_dependencies',
'update_comment',
'documentation_link'
];
/**
* Table name to be used to store extension models.
*
* @var string
*/
protected static $tableName = 'tx_extensionmanager_domain_model_extension';
/**
* Maximum of rows that can be used in a bulk insert for the current
* database platform.
*
* @var int
*/
protected $maxRowsPerChunk = 50;
/**
* Keeps repository UID.
*
* The UID is necessary for inserting records.
*
* @var int
*/
protected $repositoryUid = 1;
/**
* @var \TYPO3\CMS\Extensionmanager\Domain\Repository\RepositoryRepository
*/
protected $repositoryRepository;
/**
* @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository
*/
protected $extensionRepository;
/**
* @var \TYPO3\CMS\Extensionmanager\Domain\Model\Extension
*/
protected $extensionModel;
/**
* @var \TYPO3\CMS\Extbase\Object\ObjectManager
*/
protected $objectManager;
/**
* Only import extensions newer than this date (timestamp),
* see constructor
*
* @var int
*/
protected $minimumDateToImport;
/**
* Class constructor.
*
* Method retrieves and initializes extension XML parser instance.
*
* @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
*/
public function __construct()
{
/** @var \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager */
$this->objectManager = GeneralUtility::makeInstance(ObjectManager::class);
$this->repositoryRepository = $this->objectManager->get(RepositoryRepository::class);
$this->extensionRepository = $this->objectManager->get(ExtensionRepository::class);
$this->extensionModel = $this->objectManager->get(Extension::class);
// @todo catch parser exception
$this->parser = XmlParserFactory::getParserInstance('extension');
if (is_object($this->parser)) {
$this->parser->attach($this);
} else {
throw new ExtensionManagerException(
static::class . ': No XML parser available.',
1476108717
);
}
$connection = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable(self::$tableName);
$maxBindParameters = PlatformInformation::getMaxBindParameters(
$connection->getDatabasePlatform()
);
$countOfBindParamsPerRow = count(self::$fieldNames);
// flush at least chunks of 50 elements - in case the currently used
// database platform does not support that, the threshold is lowered
$this->maxRowsPerChunk = min(
$this->maxRowsPerChunk,
floor($maxBindParameters / $countOfBindParamsPerRow)
);
// Only import extensions that are compatible with 7.6 or higher.
// TER only allows to publish extensions with compatibility if the TYPO3 version has been released
// And 7.6 was released on 10th of November 2015.
// This effectively reduces the number of extensions imported into this TYPO3 installation
// by more than 70%. As long as the extensions.xml from TER includes these files, we need to "hack" this
// within TYPO3 Core.
// For TYPO3 v11.0, this date could be set to 2018-10-02 (v9 LTS release).
// Also see https://decisions.typo3.org/t/reduce-size-of-extension-manager-db-table/329/
$this->minimumDateToImport = strtotime('2017-04-04T00:00:00+00:00');
}
/**
* Method initializes parsing of extension.xml.gz file.
*
* @param string $localExtensionListFile absolute path to extension list xml.gz
* @param int $repositoryUid UID of repository when inserting records into DB
* @return int total number of imported extension versions
*/
public function import($localExtensionListFile, $repositoryUid = null)
{
if ($repositoryUid !== null && is_int($repositoryUid)) {
$this->repositoryUid = $repositoryUid;
}
$zlibStream = 'compress.zlib://';
$this->sumRecords = 0;
$this->parser->parseXml($zlibStream . $localExtensionListFile);
// flush last rows to database if existing
if (!empty($this->arrRows)) {
GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('tx_extensionmanager_domain_model_extension')
->bulkInsert(
'tx_extensionmanager_domain_model_extension',
$this->arrRows,
self::$fieldNames
);
}
$extensions = $this->extensionRepository->insertLastVersion($this->repositoryUid);
$this->repositoryRepository->updateRepositoryCount($extensions, $this->repositoryUid);
return $this->sumRecords;
}
/**
* Method collects and stores extension version details into the database.
*
* @param AbstractExtensionXmlParser $subject a subject notifying this observer
*/
protected function loadIntoDatabase(AbstractExtensionXmlParser &$subject)
{
if ($this->sumRecords !== 0 && $this->sumRecords % $this->maxRowsPerChunk === 0) {
GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable(self::$tableName)
->bulkInsert(
self::$tableName,
$this->arrRows,
self::$fieldNames
);
$this->arrRows = [];
}
if (!$subject->isValidVersionNumber()) {
// Skip in case extension version is not valid
return;
}
$versionRepresentations = VersionNumberUtility::convertVersionStringToArray($subject->getVersion());
// order must match that of self::$fieldNames!
$this->arrRows[] = [
$subject->getExtkey(),
$subject->getVersion(),
$versionRepresentations['version_int'],
// initialize current_version, correct value computed later:
0,
(int)$subject->getAlldownloadcounter(),
(int)$subject->getDownloadcounter(),
$subject->getTitle() ?? '',
$subject->getOwnerusername(),
$subject->getAuthorname() ?? '',
$subject->getAuthoremail() ?? '',
$subject->getAuthorcompany() ?? '',
(int)$subject->getLastuploaddate(),
$subject->getT3xfilemd5(),
$this->repositoryUid,
$this->extensionModel->getDefaultState($subject->getState() ?: ''),
(int)$subject->getReviewstate(),
$this->extensionModel->getCategoryIndexFromStringOrNumber($subject->getCategory() ?: ''),
$subject->getDescription() ?: '',
$subject->getDependencies() ?: '',
$subject->getUploadcomment() ?: '',
$subject->getDocumentationLink() ?: '',
];
++$this->sumRecords;
}
/**
* Method receives an update from a subject.
*
* @param \SplSubject $subject a subject notifying this observer
*/
public function update(\SplSubject $subject)
{
if (is_subclass_of($subject, AbstractExtensionXmlParser::class)) {
if ((int)$subject->getLastuploaddate() > $this->minimumDateToImport) {
$this->loadIntoDatabase($subject);
}
}
}
}