d764dd4f640ad5b089632522010c17e1d235e230
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Utility / Importer / ExtensionListUtility.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Utility\Importer;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010 Marcus Krause <marcus#exp2010@t3sec.info>
8 * Steffen Kamper <info@sk-typo3.de>
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Module: Extension manager - Extension list importer
29 *
30 * @author Marcus Krause <marcus#exp2010@t3sec.info>
31 * @author Steffen Kamper <info@sk-typo3.de>
32 */
33 /**
34 * Importer object for extension list
35 *
36 * @author Marcus Krause <marcus#exp2010@t3sec.info>
37 * @author Steffen Kamper <info@sk-typo3.de>
38 * @since 2010-02-10
39 * @package Extension Manager
40 * @subpackage Utility/Importer
41 */
42 class ExtensionListUtility implements SplObserver {
43
44 /**
45 * Keeps instance of a XML parser.
46 *
47 * @var \TYPO3\CMS\Extensionmanager\Utility\Parser\ExtensionXmlAbstractParser
48 */
49 protected $parser;
50
51 /**
52 * Keeps number of processed version records.
53 *
54 * @var integer
55 */
56 protected $sumRecords = 0;
57
58 /**
59 * Keeps record values to be inserted into database.
60 *
61 * @var array
62 */
63 protected $arrRows = array();
64
65 /**
66 * Keeps fieldnames of tx_extensionmanager_domain_model_extension table.
67 *
68 * @var array
69 */
70 static protected $fieldNames = array(
71 'extkey',
72 'version',
73 'intversion',
74 'alldownloadcounter',
75 'downloadcounter',
76 'title',
77 'ownerusername',
78 'authorname',
79 'authoremail',
80 'authorcompany',
81 'lastuploaddate',
82 't3xfilemd5',
83 'repository',
84 'state',
85 'reviewstate',
86 'category',
87 'description',
88 'dependencies',
89 'uploadcomment'
90 );
91
92 /**
93 * Keeps indexes of fields that should not be quoted.
94 *
95 * @var array
96 */
97 static protected $fieldIndicesNoQuote = array(2, 3, 4, 10, 12, 13, 14, 15);
98
99 /**
100 * Keeps repository UID.
101 *
102 * The UID is necessary for inserting records.
103 *
104 * @var integer
105 */
106 protected $repositoryUid = 1;
107
108 /**
109 * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\RepositoryRepository
110 */
111 protected $repositoryRepository;
112
113 /**
114 * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository
115 */
116 protected $extensionRepository;
117
118 /**
119 * @var \TYPO3\CMS\Extensionmanager\Domain\Model\Extension
120 */
121 protected $extensionModel;
122
123 /**
124 * Class constructor.
125 *
126 * Method retrieves and initializes extension XML parser instance.
127 *
128 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
129 */
130 public function __construct() {
131 /** @var $objectManager \TYPO3\CMS\Extbase\Object\ObjectManager */
132 $this->objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');
133 $this->repositoryRepository = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Domain\\Repository\\RepositoryRepository');
134 $this->extensionRepository = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Domain\\Repository\\ExtensionRepository');
135 $this->extensionModel = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Domain\\Model\\Extension');
136 // TODO catch parser exception
137 $this->parser = \TYPO3\CMS\Extensionmanager\Utility\Parser\XmlParserFactory::getParserInstance('extension');
138 if (is_object($this->parser)) {
139 $this->parser->attach($this);
140 } else {
141 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(get_class($this) . ': No XML parser available.');
142 }
143 }
144
145 /**
146 * Method initializes parsing of extension.xml.gz file.
147 *
148 * @param string $localExtensionListFile absolute path to extension list xml.gz
149 * @param integer $repositoryUid UID of repository when inserting records into DB
150 * @return integer total number of imported extension versions
151 */
152 public function import($localExtensionListFile, $repositoryUid = NULL) {
153 if (!is_null($repositoryUid) && is_int($repositoryUid)) {
154 $this->repositoryUid = $repositoryUid;
155 }
156 $zlibStream = 'compress.zlib://';
157 $this->sumRecords = 0;
158 $this->parser->parseXML($zlibStream . $localExtensionListFile);
159 // flush last rows to database if existing
160 if (count($this->arrRows)) {
161 $GLOBALS['TYPO3_DB']->exec_INSERTmultipleRows('tx_extensionmanager_domain_model_extension', self::$fieldNames, $this->arrRows, self::$fieldIndicesNoQuote);
162 }
163 $extensions = $this->extensionRepository->insertLastVersion($this->repositoryUid);
164 $this->repositoryRepository->updateRepositoryCount($extensions, $this->repositoryUid);
165 return $this->sumRecords;
166 }
167
168 /**
169 * Method collects and stores extension version details into the database.
170 *
171 * @param SplSubject &$subject a subject notifying this observer
172 * @return void
173 */
174 protected function loadIntoDatabase(\SplSubject &$subject) {
175 // flush every 50 rows to database
176 if ($this->sumRecords !== 0 && $this->sumRecords % 50 === 0) {
177 $GLOBALS['TYPO3_DB']->exec_INSERTmultipleRows('tx_extensionmanager_domain_model_extension', self::$fieldNames, $this->arrRows, self::$fieldIndicesNoQuote);
178 $this->arrRows = array();
179 }
180 $versionRepresentations = \TYPO3\CMS\Core\Utility\VersionNumberUtility::convertVersionStringToArray($subject->getVersion());
181 // order must match that of self::$fieldNamses!
182 $this->arrRows[] = array(
183 $subject->getExtkey(),
184 $subject->getVersion(),
185 $versionRepresentations['version_int'],
186 intval($subject->getAlldownloadcounter()),
187 intval($subject->getDownloadcounter()),
188 !is_null($subject->getTitle()) ? $subject->getTitle() : '',
189 $subject->getOwnerusername(),
190 !is_null($subject->getAuthorname()) ? $subject->getAuthorname() : '',
191 !is_null($subject->getAuthoremail()) ? $subject->getAuthoremail() : '',
192 !is_null($subject->getAuthorcompany()) ? $subject->getAuthorcompany() : '',
193 intval($subject->getLastuploaddate()),
194 $subject->getT3xfilemd5(),
195 $this->repositoryUid,
196 $this->extensionModel->getDefaultState($subject->getState() ? $subject->getState() : ''),
197 intval($subject->getReviewstate()),
198 $this->extensionModel->getDefaultCategory($subject->getCategory() ? $subject->getCategory() : ''),
199 $subject->getDescription() ? $subject->getDescription() : '',
200 $subject->getDependencies() ? $subject->getDependencies() : '',
201 $subject->getUploadcomment() ? $subject->getUploadcomment() : ''
202 );
203 ++$this->sumRecords;
204 }
205
206 /**
207 * Method receives an update from a subject.
208 *
209 * @param SplSubject $subject a subject notifying this observer
210 * @return void
211 */
212 public function update(\SplSubject $subject) {
213 if (is_subclass_of($subject, 'TYPO3\\CMS\\Extensionmanager\\Utility\\Parser\\ExtensionXmlAbstractParser')) {
214 $this->loadIntoDatabase($subject);
215 }
216 }
217
218 }
219
220
221 ?>