2 /***************************************************************
5 * (c) 2010 Marcus Krause <marcus#exp2010@t3sec.info>
6 * Steffen Kamper <info@sk-typo3.de>
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.
15 * The GNU General Public License can be found at
16 * http://www.gnu.org/copyleft/gpl.html.
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.
23 * This copyright notice MUST APPEAR in all copies of the script!
24 ***************************************************************/
26 * Module: Extension manager - Extension list importer
28 * @author Marcus Krause <marcus#exp2010@t3sec.info>
29 * @author Steffen Kamper <info@sk-typo3.de>
33 * Importer object for extension list
35 * @author Marcus Krause <marcus#exp2010@t3sec.info>
36 * @author Steffen Kamper <info@sk-typo3.de>
39 * @package Extension Manager
40 * @subpackage Utility/Importer
42 class Tx_Extensionmanager_Utility_Importer_ExtensionList
implements SplObserver
{
45 * Keeps instance of a XML parser.
47 * @var Tx_Extensionmanager_Utility_Parser_ExtensionXmlAbstractParser
52 * Keeps number of processed version records.
56 protected $sumRecords = 0;
59 * Keeps record values to be inserted into database.
63 protected $arrRows = array();
66 * Keeps fieldnames of cache_extension table.
70 static protected $fieldNames = array(
93 * Keeps indexes of fields that should not be quoted.
97 static protected $fieldIndicesNoQuote = array(2, 3, 4, 10, 12, 13, 14, 15);
101 * Keeps repository UID.
103 * The UID is necessary for inserting records.
107 protected $repositoryUid = 1;
110 * @var Tx_Extensionmanager_Domain_Repository_RepositoryRepository
112 protected $repositoryRepository;
115 * @var Tx_Extensionmanager_Domain_Repository_ExtensionRepository
117 protected $extensionRepository;
120 * @var Tx_Extensionmanager_Domain_Model_Extension
122 protected $extensionModel;
127 * Method retrieves and initializes extension XML parser instance.
129 * @throws Tx_Extensionmanager_Exception_ExtensionManager
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);
142 throw new Tx_Extensionmanager_Exception_ExtensionManager(get_class($this) . ': No XML parser available.');
147 * Method initializes parsing of extension.xml.gz file.
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
153 public function import($localExtensionListFile, $repositoryUid = NULL) {
154 if (!is_null($repositoryUid) && is_int($repositoryUid)) {
155 $this->repositoryUid
= $repositoryUid;
157 $zlibStream = 'compress.zlib://';
158 $this->sumRecords
= 0;
160 $this->parser
->parseXML($zlibStream . $localExtensionListFile);
162 // flush last rows to database if existing
163 if (count($this->arrRows
)) {
164 $GLOBALS['TYPO3_DB']->exec_INSERTmultipleRows(
168 self
::$fieldIndicesNoQuote
171 $extensions = $this->extensionRepository
->insertLastVersion($this->repositoryUid
);
172 $this->repositoryRepository
->updateRepositoryCount($extensions, $this->repositoryUid
);
174 return $this->sumRecords
;
178 * Method collects and stores extension version details into the database.
180 * @param SplSubject &$subject a subject notifying this observer
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(
190 self
::$fieldIndicesNoQuote
192 $this->arrRows
= array();
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() : '',
221 * Method receives an update from a subject.
223 * @param SplSubject $subject a subject notifying this observer
226 public function update(SplSubject
$subject) {
227 if (is_subclass_of($subject, 'Tx_Extensionmanager_Utility_Parser_ExtensionXmlAbstractParser')) {
228 $this->loadIntoDatabase($subject);