Fixed bug #7264: Generating extension import list takes very long (credits Michiel...
[Packages/TYPO3.CMS.git] / typo3 / mod / tools / em / class.em_xmlhandler.php
1 <?php
2 /* **************************************************************
3 * Copyright notice
4 *
5 * (c) 2006-2009 Karsten Dambekalns <karsten@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
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 /**
29 * XML handling class for the TYPO3 Extension Manager.
30 *
31 * It contains methods for handling the XML files involved with the EM,
32 * such as the list of extension mirrors and the list of available extensions.
33 *
34 * @author Karsten Dambekalns <karsten@typo3.org>
35 * @package TYPO3
36 * @subpackage EM
37 */
38 class SC_mod_tools_em_xmlhandler {
39
40 /**
41 * Enxtension Manager module
42 *
43 * @var SC_mod_tools_em_index
44 */
45 var $emObj;
46
47 /**
48 * Holds the parsed XML from extensions.xml.gz
49 * @see parseExtensionsXML()
50 *
51 * @var array
52 */
53 var $extXMLResult = array();
54 var $extensionsXML = array();
55 var $reviewStates = null;
56 var $useUnchecked = false;
57 var $useObsolete = false;
58
59 /**
60 * Reduces the entries in $this->extensionsXML to the latest version per extension and removes entries not matching the search parameter
61 *
62 * @param string $search The list of extensions is reduced to entries matching this. If empty, the full list is returned.
63 * @param string $owner If set only extensions of that user are fetched
64 * @param string $order A field to order the result by
65 * @param boolean $allExt If set also unreviewed and obsolete extensions are shown
66 * @param boolean $allVer If set returns all version of an extension, otherwise only the last
67 * @param integer $offset Offset to return result from (goes into LIMIT clause)
68 * @param integer $limit Maximum number of entries to return (goes into LIMIT clause)
69 * @param boolean $exactMatch If set search is done for exact matches of extension keys only
70 * @return void
71 */
72 function searchExtensionsXML($search, $owner='', $order='', $allExt=false, $allVer=false, $offset=0, $limit=500, $exactMatch=false) {
73 $where = '1=1';
74 if ($search && $exactMatch) {
75 $where.= ' AND extkey=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($search, 'cache_extensions');
76 } elseif($search) {
77 $where.= ' AND extkey LIKE \'%'.$GLOBALS['TYPO3_DB']->quoteStr($GLOBALS['TYPO3_DB']->escapeStrForLike($search, 'cache_extensions'), 'cache_extensions').'%\'';
78 }
79 if ($owner) {
80 $where.= ' AND ownerusername='.$GLOBALS['TYPO3_DB']->fullQuoteStr($owner, 'cache_extensions');
81 }
82 if (strlen($owner) || $this->useUnchecked || $allExt) {
83 // show extensions without review or that have passed review
84 $where.= ' AND reviewstate >= 0';
85 } else {
86 // only display extensions that have passed review
87 $where.= ' AND reviewstate > 0';
88 }
89 if (!$this->useObsolete && !$allExt) {
90 $where.= ' AND state!=5'; // 5 == obsolete
91 }
92 switch ($order) {
93 case 'author_company':
94 $forder = 'authorname, authorcompany';
95 break;
96 case 'state':
97 $forder = 'state';
98 break;
99 case 'cat':
100 default:
101 $forder = 'category';
102 break;
103 }
104 $order = $forder.', title';
105 if (!$allVer) {
106 if ($this->useUnchecked) {
107 $where .= ' AND lastversion>0';
108 } else {
109 $where .= ' AND lastreviewedversion>0';
110 }
111 }
112 $this->catArr = array();
113 $idx = 0;
114 foreach ($this->emObj->defaultCategories['cat'] as $catKey => $tmp) {
115 $this->catArr[$idx] = $catKey;
116 $idx++;
117 }
118 $this->stateArr = array();
119 $idx = 0;
120 foreach ($this->emObj->states as $state => $tmp) {
121 $this->stateArr[$idx] = $state;
122 $idx++;
123 }
124
125 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('*', 'cache_extensions', $where, '', $order, $offset.','.$limit);
126 $this->matchingCount = $GLOBALS['TYPO3_DB']->sql_num_rows($res);
127 $this->extensionsXML = array();
128 while ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
129 $row['category'] = $this->catArr[$row['category']];
130 $row['state'] = $this->stateArr[$row['state']];
131
132 if (!is_array($this->extensionsXML[$row['extkey']])) {
133 $this->extensionsXML[$row['extkey']] = array();
134 $this->extensionsXML[$row['extkey']]['downloadcounter'] = $row['alldownloadcounter'];
135 }
136 if (!is_array($this->extensionsXML[$row['extkey']]['versions'])) {
137 $this->extensionsXML[$row['extkey']]['versions'] = array();
138 }
139 $row['dependencies'] = unserialize($row['dependencies']);
140 $this->extensionsXML[$row['extkey']]['versions'][$row['version']] = $row;
141 }
142 $GLOBALS['TYPO3_DB']->sql_free_result($res);
143 }
144
145 /**
146 * Reduces the entries in $this->extensionsXML to the latest version per extension and removes entries not matching the search parameter
147 * The extension key has to be a valid one as search is done for exact matches only.
148 *
149 * @param string $search The list of extensions is reduced to entries with exactely this extension key. If empty, the full list is returned.
150 * @param string $owner If set only extensions of that user are fetched
151 * @param string $order A field to order the result by
152 * @param boolean $allExt If set also unreviewed and obsolete extensions are shown
153 * @param boolean $allVer If set returns all version of an extension, otherwise only the last
154 * @param integer $offset Offset to return result from (goes into LIMIT clause)
155 * @param integer $limit Maximum number of entries to return (goes into LIMIT clause)
156 * @return void
157 */
158 function searchExtensionsXMLExact($search, $owner='', $order='', $allExt=false, $allVer=false, $offset=0, $limit=500) {
159 $this->searchExtensionsXML($search, $owner, $order, $allExt, $allVer, $offset, $limit, true);
160 }
161
162 function countExtensions() {
163 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('extkey', 'cache_extensions', '1=1', 'extkey');
164 $cnt = $GLOBALS['TYPO3_DB']->sql_num_rows($res);
165 $GLOBALS['TYPO3_DB']->sql_free_result($res);
166 return $cnt;
167 }
168
169 /**
170 * Loads the pre-parsed extension list
171 *
172 * @return boolean true on success, false on error
173 */
174 function loadExtensionsXML() {
175 $this->searchExtensionsXML('', '', '', true);
176 }
177
178 /**
179 * Frees the pre-parsed extension list
180 *
181 * @return void
182 */
183 function freeExtensionsXML() {
184 unset($this->extensionsXML);
185 $this->extensionsXML = array();
186 }
187
188 /**
189 * Removes all extension with a certain state from the list
190 *
191 * @param array &$extensions The "versions" subpart of the extension list
192 * @return void
193 */
194 function removeObsolete(&$extensions) {
195 if($this->useObsolete) return;
196
197 reset($extensions);
198 while (list($version, $data) = each($extensions)) {
199 if($data['state']=='obsolete')
200 unset($extensions[$version]);
201 }
202 }
203
204 /**
205 * Returns the reviewstate of a specific extension-key/version
206 *
207 * @param string $extKey
208 * @param string $version: ...
209 * @return integer Review state, if none is set 0 is returned as default.
210 */
211 function getReviewState($extKey, $version) {
212 $where = 'extkey='.$GLOBALS['TYPO3_DB']->fullQuoteStr($extKey, 'cache_extensions').' AND version='.$GLOBALS['TYPO3_DB']->fullQuoteStr($version, 'cache_extensions');
213 $res = $GLOBALS['TYPO3_DB']->exec_SELECTquery('reviewstate', 'cache_extensions', $where);
214 if ($row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc($res)) {
215 return $row['reviewstate'];
216 }
217 $GLOBALS['TYPO3_DB']->sql_free_result($res);
218 return 0;
219 }
220
221 /**
222 * Removes all extension versions from $extensions that have a reviewstate<1, unless explicitly allowed
223 *
224 * @param array &$extensions The "versions" subpart of the extension list
225 * @return void
226 */
227 function checkReviewState(&$extensions) {
228 if ($this->useUnchecked) return;
229
230 reset($extensions);
231 while (list($version, $data) = each($extensions)) {
232 if($data['reviewstate']<1)
233 unset($extensions[$version]);
234 }
235 }
236
237 /**
238 * Removes all extension versions from the list of available extensions that have a reviewstate<1, unless explicitly allowed
239 *
240 * @return void
241 */
242 function checkReviewStateGlobal() {
243 if($this->useUnchecked) return;
244
245 reset($this->extensionsXML);
246 while (list($extkey, $data) = each($this->extensionsXML)) {
247 while (list($version, $vdata) = each($data['versions'])) {
248 if($vdata['reviewstate']<1) unset($this->extensionsXML[$extkey]['versions'][$version]);
249 }
250 if(!count($this->extensionsXML[$extkey]['versions'])) unset($this->extensionsXML[$extkey]);
251 }
252 }
253
254
255 /**
256 * ***************PARSING METHODS***********************
257 */
258 /**
259 * Enter description here...
260 *
261 * @param unknown_type $parser
262 * @param unknown_type $name
263 * @param unknown_type $attrs
264 * @return [type] ...
265 */
266 function startElement($parser, $name, $attrs) {
267 switch($name) {
268 case 'extensions':
269 break;
270 case 'extension':
271 $this->currentExt = $attrs['extensionkey'];
272 break;
273 case 'version':
274 $this->currentVersion = $attrs['version'];
275 $this->extXMLResult[$this->currentExt]['versions'][$this->currentVersion] = array();
276 break;
277 default:
278 $this->currentTag = $name;
279 }
280 }
281
282 /**
283 * Enter description here...
284 *
285 * @param unknown_type $parser
286 * @param unknown_type $name
287 * @return [type] ...
288 */
289 function endElement($parser, $name) {
290 switch($name) {
291 case 'extension':
292 unset($this->currentExt);
293 break;
294 case 'version':
295 unset($this->currentVersion);
296 break;
297 default:
298 unset($this->currentTag);
299 }
300 }
301
302 /**
303 * Enter description here...
304 *
305 * @param unknown_type $parser
306 * @param unknown_type $data
307 * @return [type] ...
308 */
309 function characterData($parser, $data) {
310 if(isset($this->currentTag)) {
311 if(!isset($this->currentVersion) && $this->currentTag == 'downloadcounter') {
312 $this->extXMLResult[$this->currentExt]['downloadcounter'] = trim($data);
313 } elseif($this->currentTag == 'dependencies') {
314 $data = @unserialize($data);
315 if(is_array($data)) {
316 $dep = array();
317 foreach($data as $v) {
318 $dep[$v['kind']][$v['extensionKey']] = $v['versionRange'];
319 }
320 $this->extXMLResult[$this->currentExt]['versions'][$this->currentVersion]['dependencies'] = $dep;
321 }
322 } elseif($this->currentTag == 'reviewstate') {
323 $this->reviewStates[$this->currentExt][$this->currentVersion] = (int)trim($data);
324 $this->extXMLResult[$this->currentExt]['versions'][$this->currentVersion]['reviewstate'] = (int)trim($data);
325 } else {
326 $this->extXMLResult[$this->currentExt]['versions'][$this->currentVersion][$this->currentTag] .= trim($data);
327 }
328 }
329 }
330
331 /**
332 * Parses content of mirrors.xml into a suitable array
333 *
334 * @param string XML data file to parse
335 * @return string HTLML output informing about result
336 */
337 function parseExtensionsXML($filename) {
338
339 $parser = xml_parser_create();
340 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
341 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0);
342 xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, 'utf-8');
343 xml_set_element_handler($parser, array(&$this,'startElement'), array(&$this,'endElement'));
344 xml_set_character_data_handler($parser, array(&$this,'characterData'));
345
346 $fp = gzopen($filename, 'rb');
347 if (!$fp) {
348 $content.= 'Error opening XML extension file "'.$filename.'"';
349 return $content;
350 }
351 $string = gzread($fp, 0xffff); // Read 64KB
352
353 $this->revCatArr = array();
354 $idx = 0;
355 foreach ($this->emObj->defaultCategories['cat'] as $catKey => $tmp) {
356 $this->revCatArr[$catKey] = $idx++;
357 }
358
359 $this->revStateArr = array();
360 $idx = 0;
361 foreach ($this->emObj->states as $state => $tmp) {
362 $this->revStateArr[$state] = $idx++;
363 }
364
365 $GLOBALS['TYPO3_DB']->exec_DELETEquery('cache_extensions', '1=1');
366
367 $extcount = 0;
368 @ini_set('pcre.backtrack_limit', 500000);
369 do {
370 if (preg_match('/.*(<extension\s+extensionkey="[^"]+">.*<\/extension>)/suU', $string, $match)) {
371 // Parse content:
372 if (!xml_parse($parser, $match[0], 0)) {
373 $content.= 'Error in XML parser while decoding extensions XML file. Line '.xml_get_current_line_number($parser).': '.xml_error_string(xml_get_error_code($parser));
374 $error = true;
375 break;
376 }
377 $this->storeXMLResult();
378 $this->extXMLResult = array();
379 $extcount++;
380 $string = substr($string, strlen($match[0]));
381 } elseif(function_exists('preg_last_error') && preg_last_error()) {
382 $errorcodes = array(
383 0 => 'PREG_NO_ERROR',
384 1 => 'PREG_INTERNAL_ERROR',
385 2 => 'PREG_BACKTRACK_LIMIT_ERROR',
386 3 => 'PREG_RECURSION_LIMIT_ERROR',
387 4 => 'PREG_BAD_UTF8_ERROR'
388 );
389 $content.= 'Error in regular expression matching, code: '.$errorcodes[preg_last_error()].'<br />See <a href="http://www.php.net/manual/en/function.preg-last-error.php" target="_blank">http://www.php.net/manual/en/function.preg-last-error.php</a>';
390 $error = true;
391 break;
392 } else {
393 if(gzeof($fp)) break; // Nothing more can be read
394 $string .= gzread($fp, 0xffff); // Read another 64KB
395 }
396 } while (true);
397
398 xml_parser_free($parser);
399 gzclose($fp);
400
401 if(!$error) {
402 $content.= '<p>The extensions list has been updated and now contains '.$extcount.' extension entries.</p>';
403 }
404
405 return $content;
406 }
407
408 function storeXMLResult() {
409 foreach ($this->extXMLResult as $extkey => $extArr) {
410 $max = -1;
411 $maxrev = -1;
412 $last = '';
413 $lastrev = '';
414 $usecat = '';
415 $usetitle = '';
416 $usestate = '';
417 $useauthorcompany = '';
418 $useauthorname = '';
419 $verArr = array();
420 foreach ($extArr['versions'] as $version => $vArr) {
421 $iv = $this->emObj->makeVersion($version, 'int');
422 if ($vArr['title']&&!$usetitle) {
423 $usetitle = $vArr['title'];
424 }
425 if ($vArr['state']&&!$usestate) {
426 $usestate = $vArr['state'];
427 }
428 if ($vArr['authorcompany']&&!$useauthorcompany) {
429 $useauthorcompany = $vArr['authorcompany'];
430 }
431 if ($vArr['authorname']&&!$useauthorname) {
432 $useauthorname = $vArr['authorname'];
433 }
434 $verArr[$version] = $iv;
435 if ($iv>$max) {
436 $max = $iv;
437 $last = $version;
438 if ($vArr['title']) {
439 $usetitle = $vArr['title'];
440 }
441 if ($vArr['state']) {
442 $usestate = $vArr['state'];
443 }
444 if ($vArr['authorcompany']) {
445 $useauthorcompany = $vArr['authorcompany'];
446 }
447 if ($vArr['authorname']) {
448 $useauthorname = $vArr['authorname'];
449 }
450 $usecat = $vArr['category'];
451 }
452 if ($vArr['reviewstate'] && ($iv>$maxrev)) {
453 $maxrev = $iv;
454 $lastrev = $version;
455 }
456 }
457 if (!strlen($usecat)) {
458 $usecat = 4; // Extensions without a category end up in "misc"
459 } else {
460 if (isset($this->revCatArr[$usecat])) {
461 $usecat = $this->revCatArr[$usecat];
462 } else {
463 $usecat = 4; // Extensions without a category end up in "misc"
464 }
465 }
466 if (isset($this->revStateArr[$usestate])) {
467 $usestate = $this->revCatArr[$usestate];
468 } else {
469 $usestate = 999; // Extensions without a category end up in "misc"
470 }
471 foreach ($extArr['versions'] as $version => $vArr) {
472 $vArr['version'] = $version;
473 $vArr['intversion'] = $verArr[$version];
474 $vArr['extkey'] = $extkey;
475 $vArr['alldownloadcounter'] = $extArr['downloadcounter'];
476 $vArr['dependencies'] = serialize($vArr['dependencies']);
477 $vArr['category'] = $usecat;
478 $vArr['title'] = $usetitle;
479 if ($version==$last) {
480 $vArr['lastversion'] = 1;
481 }
482 if ($version==$lastrev) {
483 $vArr['lastreviewedversion'] = 1;
484 }
485 $vArr['state'] = isset($this->revStateArr[$vArr['state']])?$this->revStateArr[$vArr['state']]:$usestate; // 999 = not set category
486 $GLOBALS['TYPO3_DB']->exec_INSERTquery('cache_extensions', $vArr);
487 }
488 }
489 }
490
491 /**
492 * Parses content of mirrors.xml into a suitable array
493 *
494 * @param string $string: XML data to parse
495 * @return string HTLML output informing about result
496 */
497 function parseMirrorsXML($string) {
498 global $TYPO3_CONF_VARS;
499
500 // Create parser:
501 $parser = xml_parser_create();
502 $vals = array();
503 $index = array();
504
505 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
506 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0);
507
508 $preg_result = array();
509 preg_match('/^[[:space:]]*<\?xml[^>]*encoding[[:space:]]*=[[:space:]]*"([^"]*)"/',substr($string,0,200),$preg_result);
510 $theCharset = $preg_result[1] ? $preg_result[1] : ($TYPO3_CONF_VARS['BE']['forceCharset'] ? $TYPO3_CONF_VARS['BE']['forceCharset'] : 'iso-8859-1');
511 xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, $theCharset); // us-ascii / utf-8 / iso-8859-1
512
513 // Parse content:
514 xml_parse_into_struct($parser, $string, $vals, $index);
515
516 // If error, return error message:
517 if (xml_get_error_code($parser)) {
518 $line = xml_get_current_line_number($parser);
519 $error = xml_error_string(xml_get_error_code($parser));
520 xml_parser_free($parser);
521 return 'Error in XML parser while decoding mirrors XML file. Line '.$line.': '.$error;
522 } else {
523 // Init vars:
524 $stack = array(array());
525 $stacktop = 0;
526 $mirrornumber = 0;
527 $current=array();
528 $tagName = '';
529 $documentTag = '';
530
531 // Traverse the parsed XML structure:
532 foreach($vals as $val) {
533
534 // First, process the tag-name (which is used in both cases, whether "complete" or "close")
535 $tagName = ($val['tag']=='mirror' && $val['type']=='open') ? '__plh' : $val['tag'];
536 if (!$documentTag) $documentTag = $tagName;
537
538 // Setting tag-values, manage stack:
539 switch($val['type']) {
540 case 'open': // If open tag it means there is an array stored in sub-elements. Therefore increase the stackpointer and reset the accumulation array:
541 $current[$tagName] = array(); // Setting blank place holder
542 $stack[$stacktop++] = $current;
543 $current = array();
544 break;
545 case 'close': // If the tag is "close" then it is an array which is closing and we decrease the stack pointer.
546 $oldCurrent = $current;
547 $current = $stack[--$stacktop];
548 end($current); // Going to the end of array to get placeholder key, key($current), and fill in array next:
549 if($tagName=='mirror') {
550 unset($current['__plh']);
551 $current[$oldCurrent['host']] = $oldCurrent;
552 } else {
553 $current[key($current)] = $oldCurrent;
554 }
555 unset($oldCurrent);
556 break;
557 case 'complete': // If "complete", then it's a value. If the attribute "base64" is set, then decode the value, otherwise just set it.
558 $current[$tagName] = (string)$val['value']; // Had to cast it as a string - otherwise it would be evaluate false if tested with isset()!!
559 break;
560 }
561 }
562 return $current[$tagName];
563 }
564 }
565
566 /**
567 * Parses content of *-l10n.xml into a suitable array
568 *
569 * @param string $string: XML data to parse
570 * @return array Array representation of XML data
571 */
572 function parseL10nXML($string) {
573 // Create parser:
574 $parser = xml_parser_create();
575 $vals = array();
576 $index = array();
577
578 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
579 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0);
580
581 // Parse content:
582 xml_parse_into_struct($parser, $string, $vals, $index);
583
584 // If error, return error message:
585 if (xml_get_error_code($parser)) {
586 $line = xml_get_current_line_number($parser);
587 $error = xml_error_string(xml_get_error_code($parser));
588 debug($error);
589 xml_parser_free($parser);
590 return 'Error in XML parser while decoding l10n XML file. Line '.$line.': '.$error;
591 } else {
592 // Init vars:
593 $stack = array(array());
594 $stacktop = 0;
595 $mirrornumber = 0;
596 $current=array();
597 $tagName = '';
598 $documentTag = '';
599
600 // Traverse the parsed XML structure:
601 foreach($vals as $val) {
602
603 // First, process the tag-name (which is used in both cases, whether "complete" or "close")
604 $tagName = ($val['tag']=='languagepack' && $val['type']=='open') ? $val['attributes']['language'] : $val['tag'];
605 if (!$documentTag) $documentTag = $tagName;
606
607 // Setting tag-values, manage stack:
608 switch($val['type']) {
609 case 'open': // If open tag it means there is an array stored in sub-elements. Therefore increase the stackpointer and reset the accumulation array:
610 $current[$tagName] = array(); // Setting blank place holder
611 $stack[$stacktop++] = $current;
612 $current = array();
613 break;
614 case 'close': // If the tag is "close" then it is an array which is closing and we decrease the stack pointer.
615 $oldCurrent = $current;
616 $current = $stack[--$stacktop];
617 end($current); // Going to the end of array to get placeholder key, key($current), and fill in array next:
618 $current[key($current)] = $oldCurrent;
619 unset($oldCurrent);
620 break;
621 case 'complete': // If "complete", then it's a value. If the attribute "base64" is set, then decode the value, otherwise just set it.
622 $current[$tagName] = (string)$val['value']; // Had to cast it as a string - otherwise it would be evaluate false if tested with isset()!!
623 break;
624 }
625 }
626 return $current[$tagName];
627 }
628 }
629 }
630
631 ?>