Cleanup: Updated copyright comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / em / classes / import / class.tx_em_import_extensionlistimporter.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2010-2011 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 * class.tx_em_import_extensionlistimporter.php
27 *
28 * Module: Extension manager - Extension list importer
29 *
30 * $Id: class.tx_em_import_extensionlistimporter.php 2016 2010-03-14 04:01:47Z mkrause $
31 *
32 * @author Marcus Krause <marcus#exp2010@t3sec.info>
33 * @author Steffen Kamper <info@sk-typo3.de>
34 */
35
36 /**
37 * Importer object for extension list
38 *
39 * @author Marcus Krause <marcus#exp2010@t3sec.info>
40 * @author Steffen Kamper <info@sk-typo3.de>
41 *
42 * @since 2010-02-10
43 * @package TYPO3
44 * @subpackage EM
45 */
46 class tx_em_Import_ExtensionListImporter implements SplObserver {
47
48 /**
49 * Keeps instance of a XML parser.
50 *
51 * @var tx_em_Parser_ExtensionXmlAbstractParser
52 */
53 protected $parser;
54
55 /**
56 * Keeps number of processed version records.
57 *
58 * @var integer
59 */
60 protected $sumRecords = 0;
61
62 /**
63 * Keeps record values to be inserted into database.
64 *
65 * @var array
66 */
67 protected $arrRows = array();
68
69 /**
70 * Keeps fieldnames of cache_extension table.
71 *
72 * @var array
73 */
74 static protected $fieldNames = array(
75 'extkey',
76 'version',
77 'intversion',
78 'alldownloadcounter',
79 'downloadcounter',
80 'title',
81 'ownerusername',
82 'authorname',
83 'authoremail',
84 'authorcompany',
85 'lastuploaddate',
86 't3xfilemd5',
87 'repository',
88 'state',
89 'reviewstate',
90 'category',
91 'description',
92 'dependencies',
93 'uploadcomment',
94 //'lastversion',
95 //'lastreviewedversion'
96 );
97
98 /**
99 * Keeps indexes of fields that should not be quoted.
100 *
101 * @var array
102 */
103 static protected $fieldIndicesNoQuote = array(2, 3, 4, 10, 12, 13, 14, 15);
104
105
106 /**
107 * Keeps repository UID.
108 *
109 * The UID is necessary for inserting records.
110 *
111 * @var integer
112 */
113 protected $repositoryUID = 1;
114
115
116 /**
117 * Class constructor.
118 *
119 * Method retrieves and initializes extension XML parser instance.
120 *
121 * @access public
122 * @return void
123 * @throws tx_em_XmlException in case no valid parser instance is available
124 */
125 function __construct() {
126 // TODO catch parser exception
127 $this->parser = tx_em_Parser_XmlParserFactory::getParserInstance('extension');
128 if (is_object($this->parser)) {
129 $this->parser->attach($this);
130 } else {
131 throw new tx_em_XmlException(get_class($this) . ': ' . 'No XML parser available.');
132 }
133 }
134
135 /**
136 * Method initializes parsing of extension.xml.gz file.
137 *
138 * @access public
139 * @param string $localExtListFile absolute path to (gzipped) local extension list xml file
140 * @param integer $repositoryUID UID of repository to be used when inserting records into DB
141 * @return integer total number of imported extension versions
142 */
143 public function import($localExtListFile, $repositoryUID = NULL) {
144 if (!is_null($repositoryUID) && is_int($repositoryUID)) {
145 $this->repositoryUID = $repositoryUID;
146 }
147 $zlibStream = 'compress.zlib://';
148 $this->sumRecords = 0;
149
150 $this->parser->parseXML($zlibStream . $localExtListFile);
151
152 // flush last rows to database if existing
153 if (count($this->arrRows)) {
154 $GLOBALS['TYPO3_DB']->exec_INSERTmultipleRows(
155 'cache_extensions',
156 self::$fieldNames,
157 $this->arrRows,
158 self::$fieldIndicesNoQuote
159 );
160 }
161 $extensions = tx_em_Database::insertLastVersion($this->repositoryUID);
162 tx_em_Database::updateRepositoryCount($extensions, $this->repositoryUID);
163
164 return $this->sumRecords;
165 }
166
167 /**
168 * Method collects and stores extension version details into the database.
169 *
170 * @access protected
171 * @param SplSubject $subject a subject notifying this observer
172 * @return void
173 */
174 protected function loadIntoDB(SplSubject &$subject) {
175 // flush every 50 rows to database
176 if ($this->sumRecords !== 0 && $this->sumRecords % 50 === 0) {
177 $GLOBALS['TYPO3_DB']->exec_INSERTmultipleRows(
178 'cache_extensions',
179 self::$fieldNames,
180 $this->arrRows,
181 self::$fieldIndicesNoQuote
182 );
183 $this->arrRows = array();
184 }
185 // order must match that of self::$fieldNamses!
186 $this->arrRows[] = array(
187 $subject->getExtkey(),
188 $subject->getVersion(),
189 tx_em_Tools::makeVersion($subject->getVersion(), 'int'),
190 intval($subject->getAlldownloadcounter()),
191 intval($subject->getDownloadcounter()),
192 !is_null($subject->getTitle()) ? $subject->getTitle() : '',
193 $subject->getOwnerusername(),
194 !is_null($subject->getAuthorname()) ? $subject->getAuthorname() : '',
195 !is_null($subject->getAuthoremail()) ? $subject->getAuthoremail() : '',
196 !is_null($subject->getAuthorcompany()) ? $subject->getAuthorcompany() : '',
197 intval($subject->getLastuploaddate()),
198 $subject->getT3xfilemd5(),
199 $this->repositoryUID,
200 tx_em_Tools::getDefaultState($subject->getState() ? $subject->getState() : ''),
201 intval($subject->getReviewstate()),
202 tx_em_Tools::getDefaultCategory($subject->getCategory() ? $subject->getCategory() : ''),
203 $subject->getDescription() ? $subject->getDescription() : '',
204 $subject->getDependencies() ? $subject->getDependencies() : '',
205 $subject->getUploadcomment() ? $subject->getUploadcomment() : '',
206 );
207 ++$this->sumRecords;
208 }
209
210 /**
211 * Method receives an update from a subject.
212 *
213 * @access public
214 * @param SplSubject $subject a subject notifying this observer
215 * @return void
216 */
217 public function update(SplSubject $subject) {
218 if (is_subclass_of($subject, 'tx_em_Parser_ExtensionXmlAbstractParser')) {
219 $this->loadIntoDB($subject);
220 }
221 }
222 }
223
224 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/sysext/em/classes/import/class.tx_em_import_extensionlistimporter.php'])) {
225 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['typo3/sysext/em/classes/import/class.tx_em_import_extensionlistimporter.php']);
226 }
227
228 ?>