‪TYPO3CMS  ‪main
ExtensionXmlParser.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
21 
30 class ‪ExtensionXmlParser implements \SplSubject
31 {
37  protected array ‪$observers = [];
38 
42  protected string ‪$elementData = '';
43 
47  protected string ‪$authorcompany = '';
48  protected string ‪$authoremail = '';
49  protected string ‪$authorname = '';
50  protected string ‪$category = '';
51  protected string ‪$dependencies = '';
52  protected string ‪$description = '';
53  protected int ‪$extensionDownloadCounter = 0;
54  protected string ‪$extensionKey = '';
55  protected int ‪$lastuploaddate = 0;
56  protected string ‪$ownerusername = '';
57  protected int ‪$reviewstate = 0;
58  protected string ‪$state = '';
59  protected string ‪$t3xfilemd5 = '';
60  protected string ‪$title = '';
61  protected string ‪$uploadcomment = '';
62  protected string ‪$version = '';
63  protected int ‪$versionDownloadCounter = 0;
64  protected string ‪$documentationLink = '';
65  protected string ‪$distributionImage = '';
66  protected string ‪$distributionWelcomeImage = '';
67 
68  public function ‪__construct()
69  {
70  if (!extension_loaded('xml')) {
71  throw new \RuntimeException('PHP extension "xml" not loaded', 1622148496);
72  }
73  }
74 
81  public function ‪parseXml($file): void
82  {
84  ‪$parser = xml_parser_create();
85  xml_set_object(‪$parser, $this);
86 
87  // keep original character case of XML document
88  xml_parser_set_option(‪$parser, XML_OPTION_CASE_FOLDING, 0);
89  xml_parser_set_option(‪$parser, XML_OPTION_SKIP_WHITE, 0);
90  xml_parser_set_option(‪$parser, XML_OPTION_TARGET_ENCODING, 'utf-8');
91  xml_set_element_handler(‪$parser, [$this, 'startElement'], [$this, 'endElement']);
92  xml_set_character_data_handler(‪$parser, [$this, 'characterData']);
93  if (!($fp = fopen($file, 'r'))) {
95  }
96  while ($data = fread($fp, 4096)) {
97  if (!xml_parse(‪$parser, $data, feof($fp))) {
98  throw $this->‪createXmlErrorException(‪$parser, $file);
99  }
100  }
101  xml_parser_free(‪$parser);
102  }
103 
105  {
106  return new ‪ExtensionManagerException(sprintf('Unable to open file resource %s.', $file), 1342640689);
107  }
108 
110  {
111  return new ‪ExtensionManagerException(
112  sprintf(
113  'XML error %s in line %u of file resource %s.',
114  xml_error_string(xml_get_error_code(‪$parser)),
115  xml_get_current_line_number(‪$parser),
116  $file
117  ),
118  1342640703
119  );
120  }
121 
129  protected function ‪startElement(‪$parser, $elementName, $attrs)
130  {
131  switch ($elementName) {
132  case 'extension':
133  $this->extensionKey = $attrs['extensionkey'];
134  break;
135  case 'version':
136  $this->version = $attrs['version'];
137  break;
138  default:
139  $this->elementData = '';
140  }
141  }
142 
149  protected function ‪endElement(‪$parser, $elementName)
150  {
151  switch ($elementName) {
152  case 'extension':
153  $this->‪resetProperties(true);
154  break;
155  case 'version':
156  $this->‪notify();
157  $this->‪resetProperties();
158  break;
159  case 'downloadcounter':
160  // downloadcounter can be a child node of extension or version
161  if ($this->version === '') {
162  $this->extensionDownloadCounter = (int)$this->elementData;
163  } else {
164  $this->versionDownloadCounter = (int)$this->elementData;
165  }
166  break;
167  case 'title':
168  $this->title = ‪$this->elementData;
169  break;
170  case 'description':
172  break;
173  case 'state':
174  $this->state = ‪$this->elementData;
175  break;
176  case 'reviewstate':
177  $this->reviewstate = (int)$this->elementData;
178  break;
179  case 'category':
180  $this->category = ‪$this->elementData;
181  break;
182  case 'lastuploaddate':
183  $this->lastuploaddate = (int)$this->elementData;
184  break;
185  case 'uploadcomment':
186  $this->uploadcomment = ‪$this->elementData;
187  break;
188  case 'dependencies':
189  $newDependencies = [];
190  $dependenciesArray = unserialize($this->elementData, ['allowed_classes' => false]);
191  if (is_array($dependenciesArray)) {
192  foreach ($dependenciesArray as ‪$version) {
193  if (!empty(‪$version['kind']) && !empty(‪$version['extensionKey'])) {
194  $newDependencies[‪$version['kind']][‪$version['extensionKey']] = ‪$version['versionRange'];
195  }
196  }
197  }
198  $this->dependencies = serialize($newDependencies);
199  break;
200  case 'authorname':
201  $this->authorname = ‪$this->elementData;
202  break;
203  case 'authoremail':
204  $this->authoremail = ‪$this->elementData;
205  break;
206  case 'authorcompany':
207  $this->authorcompany = ‪$this->elementData;
208  break;
209  case 'ownerusername':
210  $this->ownerusername = ‪$this->elementData;
211  break;
212  case 't3xfilemd5':
213  $this->t3xfilemd5 = ‪$this->elementData;
214  break;
215  case 'documentation_link':
216  $this->documentationLink = ‪$this->elementData;
217  break;
218  case 'distributionImage':
219  if (preg_match('/^https:\/\/extensions\.typo3\.org[a-zA-Z0-9._\/]+Distribution\.png$/', $this->elementData)) {
220  $this->distributionImage = ‪$this->elementData;
221  }
222  break;
223  case 'distributionImageWelcome':
224  if (preg_match('/^https:\/\/extensions\.typo3\.org[a-zA-Z0-9._\/]+DistributionWelcome\.png$/', $this->elementData)) {
225  $this->distributionWelcomeImage = ‪$this->elementData;
226  }
227  break;
228  }
229  }
230 
236  protected function ‪resetProperties($resetAll = false): void
237  {
238  // Resetting at least class property "version" is mandatory as we need to do some magic in
239  // regards to an extension's and version's child node "downloadcounter"
240  $this->version = $this->authorcompany = $this->authorname = $this->authoremail = $this->category = $this->dependencies = $this->state = '';
241  $this->‪description = $this->ownerusername = $this->t3xfilemd5 = $this->title = $this->uploadcomment = $this->documentationLink = $this->distributionImage = $this->distributionWelcomeImage = '';
242  $this->lastuploaddate = $this->reviewstate = $this->versionDownloadCounter = 0;
243  if ($resetAll) {
244  $this->extensionKey = '';
245  $this->extensionDownloadCounter = 0;
246  }
247  }
248 
255  protected function ‪characterData(‪$parser, string $data)
256  {
257  $this->elementData .= $data;
258  }
259 
267  public function ‪attach(\SplObserver $observer): void
268  {
269  $this->observers[] = $observer;
270  }
271 
277  public function ‪detach(\SplObserver $observer): void
278  {
279  $key = array_search($observer, $this->observers, true);
280  if ($key !== false) {
281  unset($this->observers[$key]);
282  }
283  }
284 
288  public function ‪notify(): void
289  {
290  foreach ($this->observers as $observer) {
291  $observer->update($this);
292  }
293  }
294 
298  public function ‪getAlldownloadcounter(): int
299  {
301  }
302 
306  public function ‪getAuthorcompany(): string
307  {
309  }
310 
314  public function ‪getAuthoremail(): string
315  {
316  return ‪$this->authoremail;
317  }
318 
322  public function ‪getAuthorname(): string
323  {
324  return ‪$this->authorname;
325  }
326 
330  public function ‪getCategory(): string
331  {
332  return ‪$this->category;
333  }
334 
338  public function ‪getDependencies(): string
339  {
340  return ‪$this->dependencies;
341  }
342 
346  public function ‪getDescription(): string
347  {
348  return ‪$this->description;
349  }
350 
354  public function ‪getDownloadcounter(): int
355  {
357  }
358 
362  public function ‪getExtkey(): string
363  {
364  return ‪$this->extensionKey;
365  }
366 
370  public function ‪getLastuploaddate(): int
371  {
373  }
374 
378  public function ‪getOwnerusername(): string
379  {
381  }
382 
386  public function ‪getReviewstate(): int
387  {
388  return ‪$this->reviewstate;
389  }
390 
394  public function ‪getState(): string
395  {
396  return ‪$this->state;
397  }
398 
402  public function ‪getT3xfilemd5(): string
403  {
404  return ‪$this->t3xfilemd5;
405  }
406 
410  public function ‪getTitle(): string
411  {
412  return ‪$this->title;
413  }
414 
418  public function ‪getUploadcomment(): string
419  {
421  }
422 
426  public function ‪getVersion(): string
427  {
428  return ‪$this->version;
429  }
430 
434  public function ‪isValidVersionNumber(): bool
435  {
436  // Validate the version number, see `isValidVersionNumber` in TER API
437  return (bool)preg_match('/^(0|[1-9]\d{0,2})\.(0|[1-9]\d{0,2})\.(0|[1-9]\d{0,2})$/', $this->version);
438  }
439 
443  public function ‪getDocumentationLink(): string
444  {
446  }
447 
451  public function ‪getDistributionImage(): string
452  {
454  }
455 
459  public function ‪getDistributionWelcomeImage(): string
460  {
462  }
463 }
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\$elementData
‪string $elementData
Definition: ExtensionXmlParser.php:42
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\$category
‪string $category
Definition: ExtensionXmlParser.php:50
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\parseXml
‪parseXml($file)
Definition: ExtensionXmlParser.php:81
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\getExtkey
‪getExtkey()
Definition: ExtensionXmlParser.php:362
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\$extensionDownloadCounter
‪int $extensionDownloadCounter
Definition: ExtensionXmlParser.php:53
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\$lastuploaddate
‪int $lastuploaddate
Definition: ExtensionXmlParser.php:55
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\$extensionKey
‪string $extensionKey
Definition: ExtensionXmlParser.php:54
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\attach
‪attach(\SplObserver $observer)
Definition: ExtensionXmlParser.php:267
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\getT3xfilemd5
‪getT3xfilemd5()
Definition: ExtensionXmlParser.php:402
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\getCategory
‪getCategory()
Definition: ExtensionXmlParser.php:330
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\getVersion
‪getVersion()
Definition: ExtensionXmlParser.php:426
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\getDownloadcounter
‪getDownloadcounter()
Definition: ExtensionXmlParser.php:354
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\$authorcompany
‪string $authorcompany
Definition: ExtensionXmlParser.php:47
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\getAlldownloadcounter
‪getAlldownloadcounter()
Definition: ExtensionXmlParser.php:298
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\getState
‪getState()
Definition: ExtensionXmlParser.php:394
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\$reviewstate
‪int $reviewstate
Definition: ExtensionXmlParser.php:57
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\detach
‪detach(\SplObserver $observer)
Definition: ExtensionXmlParser.php:277
‪$parser
‪$parser
Definition: annotationChecker.php:103
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\getAuthorname
‪getAuthorname()
Definition: ExtensionXmlParser.php:322
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\$observers
‪array $observers
Definition: ExtensionXmlParser.php:37
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\$description
‪string $description
Definition: ExtensionXmlParser.php:52
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\$documentationLink
‪string $documentationLink
Definition: ExtensionXmlParser.php:64
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\getDependencies
‪getDependencies()
Definition: ExtensionXmlParser.php:338
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\getOwnerusername
‪getOwnerusername()
Definition: ExtensionXmlParser.php:378
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\$ownerusername
‪string $ownerusername
Definition: ExtensionXmlParser.php:56
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\$version
‪string $version
Definition: ExtensionXmlParser.php:62
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\getDescription
‪getDescription()
Definition: ExtensionXmlParser.php:346
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\$dependencies
‪string $dependencies
Definition: ExtensionXmlParser.php:51
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\$versionDownloadCounter
‪int $versionDownloadCounter
Definition: ExtensionXmlParser.php:63
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\__construct
‪__construct()
Definition: ExtensionXmlParser.php:68
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\getAuthoremail
‪getAuthoremail()
Definition: ExtensionXmlParser.php:314
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\endElement
‪endElement($parser, $elementName)
Definition: ExtensionXmlParser.php:149
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\getTitle
‪getTitle()
Definition: ExtensionXmlParser.php:410
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\getUploadcomment
‪getUploadcomment()
Definition: ExtensionXmlParser.php:418
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\getReviewstate
‪getReviewstate()
Definition: ExtensionXmlParser.php:386
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\notify
‪notify()
Definition: ExtensionXmlParser.php:288
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\startElement
‪startElement($parser, $elementName, $attrs)
Definition: ExtensionXmlParser.php:129
‪TYPO3\CMS\Extensionmanager\Parser
Definition: ExtensionXmlParser.php:18
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\$distributionWelcomeImage
‪string $distributionWelcomeImage
Definition: ExtensionXmlParser.php:66
‪TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
Definition: ExtensionManagerException.php:25
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\getAuthorcompany
‪getAuthorcompany()
Definition: ExtensionXmlParser.php:306
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\createXmlErrorException
‪createXmlErrorException($parser, string $file)
Definition: ExtensionXmlParser.php:109
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\resetProperties
‪resetProperties($resetAll=false)
Definition: ExtensionXmlParser.php:236
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\$state
‪string $state
Definition: ExtensionXmlParser.php:58
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\getDistributionImage
‪getDistributionImage()
Definition: ExtensionXmlParser.php:451
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\$authorname
‪string $authorname
Definition: ExtensionXmlParser.php:49
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\isValidVersionNumber
‪isValidVersionNumber()
Definition: ExtensionXmlParser.php:434
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\characterData
‪characterData($parser, string $data)
Definition: ExtensionXmlParser.php:255
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser
Definition: ExtensionXmlParser.php:31
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\createUnableToOpenFileResourceException
‪createUnableToOpenFileResourceException(string $file)
Definition: ExtensionXmlParser.php:104
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\$uploadcomment
‪string $uploadcomment
Definition: ExtensionXmlParser.php:61
‪TYPO3\CMS\Redirects\Message\description
‪identifier description
Definition: RedirectWasHitMessage.php:32
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\$authoremail
‪string $authoremail
Definition: ExtensionXmlParser.php:48
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\$title
‪string $title
Definition: ExtensionXmlParser.php:60
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\getLastuploaddate
‪getLastuploaddate()
Definition: ExtensionXmlParser.php:370
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\getDistributionWelcomeImage
‪getDistributionWelcomeImage()
Definition: ExtensionXmlParser.php:459
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\getDocumentationLink
‪getDocumentationLink()
Definition: ExtensionXmlParser.php:443
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\$distributionImage
‪string $distributionImage
Definition: ExtensionXmlParser.php:65
‪TYPO3\CMS\Extensionmanager\Parser\ExtensionXmlParser\$t3xfilemd5
‪string $t3xfilemd5
Definition: ExtensionXmlParser.php:59