[TASK] Rename table cache_extensions
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Utility / Importer / ExtensionList.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010 Marcus Krause <marcus#exp2010@t3sec.info>
6 * Steffen Kamper <info@sk-typo3.de>
7 * All rights reserved
8 *
9 * This script is part of the TYPO3 project. The TYPO3 project is
10 * free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
17 *
18 * This script is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * This copyright notice MUST APPEAR in all copies of the script!
24 ***************************************************************/
25 /**
26 * Module: Extension manager - Extension list importer
27 *
28 * @author Marcus Krause <marcus#exp2010@t3sec.info>
29 * @author Steffen Kamper <info@sk-typo3.de>
30 */
31
32 /**
33 * Importer object for extension list
34 *
35 * @author Marcus Krause <marcus#exp2010@t3sec.info>
36 * @author Steffen Kamper <info@sk-typo3.de>
37 *
38 * @since 2010-02-10
39 * @package Extension Manager
40 * @subpackage Utility/Importer
41 */
42 class Tx_Extensionmanager_Utility_Importer_ExtensionList implements SplObserver {
43
44 /**
45 * Keeps instance of a XML parser.
46 *
47 * @var Tx_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 /**
101 * Keeps repository UID.
102 *
103 * The UID is necessary for inserting records.
104 *
105 * @var integer
106 */
107 protected $repositoryUid = 1;
108
109 /**
110 * @var Tx_Extensionmanager_Domain_Repository_RepositoryRepository
111 */
112 protected $repositoryRepository;
113
114 /**
115 * @var Tx_Extensionmanager_Domain_Repository_ExtensionRepository
116 */
117 protected $extensionRepository;
118
119 /**
120 * @var Tx_Extensionmanager_Domain_Model_Extension
121 */
122 protected $extensionModel;
123
124 /**
125 * Class constructor.
126 *
127 * Method retrieves and initializes extension XML parser instance.
128 *
129 * @throws Tx_Extensionmanager_Exception_ExtensionManager
130 */
131 public function __construct() {
132 /** @var $objectManager Tx_Extbase_Object_ObjectManager */
133 $this->objectManager = t3lib_div::makeInstance('Tx_Extbase_Object_ObjectManager');
134 $this->repositoryRepository = $this->objectManager->get('Tx_Extensionmanager_Domain_Repository_RepositoryRepository');
135 $this->extensionRepository = $this->objectManager->get('Tx_Extensionmanager_Domain_Repository_ExtensionRepository');
136 $this->extensionModel = $this->objectManager->get('Tx_Extensionmanager_Domain_Model_Extension');
137 // TODO catch parser exception
138 $this->parser = Tx_Extensionmanager_Utility_Parser_XmlParserFactory::getParserInstance('extension');
139 if (is_object($this->parser)) {
140 $this->parser->attach($this);
141 } else {
142 throw new Tx_Extensionmanager_Exception_ExtensionManager(get_class($this) . ': No XML parser available.');
143 }
144 }
145
146 /**
147 * Method initializes parsing of extension.xml.gz file.
148 *
149 * @param string $localExtensionListFile absolute path to extension list xml.gz
150 * @param integer $repositoryUid UID of repository when inserting records into DB
151 * @return integer total number of imported extension versions
152 */
153 public function import($localExtensionListFile, $repositoryUid = NULL) {
154 if (!is_null($repositoryUid) && is_int($repositoryUid)) {
155 $this->repositoryUid = $repositoryUid;
156 }
157 $zlibStream = 'compress.zlib://';
158 $this->sumRecords = 0;
159
160 $this->parser->parseXML($zlibStream . $localExtensionListFile);
161
162 // flush last rows to database if existing
163 if (count($this->arrRows)) {
164 $GLOBALS['TYPO3_DB']->exec_INSERTmultipleRows(
165 'tx_extensionmanager_domain_model_extension',
166 self::$fieldNames,
167 $this->arrRows,
168 self::$fieldIndicesNoQuote
169 );
170 }
171 $extensions = $this->extensionRepository->insertLastVersion($this->repositoryUid);
172 $this->repositoryRepository->updateRepositoryCount($extensions, $this->repositoryUid);
173
174 return $this->sumRecords;
175 }
176
177 /**
178 * Method collects and stores extension version details into the database.
179 *
180 * @param SplSubject &$subject a subject notifying this observer
181 * @return void
182 */
183 protected function loadIntoDatabase(SplSubject &$subject) {
184 // flush every 50 rows to database
185 if ($this->sumRecords !== 0 && $this->sumRecords % 50 === 0) {
186 $GLOBALS['TYPO3_DB']->exec_INSERTmultipleRows(
187 'tx_extensionmanager_domain_model_extension',
188 self::$fieldNames,
189 $this->arrRows,
190 self::$fieldIndicesNoQuote
191 );
192 $this->arrRows = array();
193 }
194 $versionRepresentations = t3lib_utility_VersionNumber::convertVersionStringToArray($subject->getVersion());
195 // order must match that of self::$fieldNamses!
196 $this->arrRows[] = array(
197 $subject->getExtkey(),
198 $subject->getVersion(),
199 $versionRepresentations['version_int'],
200 intval($subject->getAlldownloadcounter()),
201 intval($subject->getDownloadcounter()),
202 !is_null($subject->getTitle()) ? $subject->getTitle() : '',
203 $subject->getOwnerusername(),
204 !is_null($subject->getAuthorname()) ? $subject->getAuthorname() : '',
205 !is_null($subject->getAuthoremail()) ? $subject->getAuthoremail() : '',
206 !is_null($subject->getAuthorcompany()) ? $subject->getAuthorcompany() : '',
207 intval($subject->getLastuploaddate()),
208 $subject->getT3xfilemd5(),
209 $this->repositoryUid,
210 $this->extensionModel->getDefaultState($subject->getState() ? $subject->getState() : ''),
211 intval($subject->getReviewstate()),
212 $this->extensionModel->getDefaultCategory($subject->getCategory() ? $subject->getCategory() : ''),
213 $subject->getDescription() ? $subject->getDescription() : '',
214 $subject->getDependencies() ? $subject->getDependencies() : '',
215 $subject->getUploadcomment() ? $subject->getUploadcomment() : '',
216 );
217 ++$this->sumRecords;
218 }
219
220 /**
221 * Method receives an update from a subject.
222 *
223 * @param SplSubject $subject a subject notifying this observer
224 * @return void
225 */
226 public function update(SplSubject $subject) {
227 if (is_subclass_of($subject, 'Tx_Extensionmanager_Utility_Parser_ExtensionXmlAbstractParser')) {
228 $this->loadIntoDatabase($subject);
229 }
230 }
231 }
232
233 ?>