Merge "Do not suppress php notices in SpecialPageFatalTest"
[lhc/web/wiklou.git] / maintenance / preprocessDump.php
1 <?php
2 /**
3 * Take page text out of an XML dump file and preprocess it to obj.
4 * It may be useful for getting preprocessor statistics or filling the
5 * preprocessor cache.
6 *
7 * Copyright © 2011 Platonides - https://www.mediawiki.org/
8 *
9 * This program is 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 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 * @ingroup Maintenance
26 */
27
28 use MediaWiki\MediaWikiServices;
29
30 require_once __DIR__ . '/dumpIterator.php';
31
32 /**
33 * Maintenance script that takes page text out of an XML dump file and
34 * preprocesses it to obj.
35 *
36 * @ingroup Maintenance
37 */
38 class PreprocessDump extends DumpIterator {
39
40 /* Variables for dressing up as a parser */
41 public $mTitle = 'PreprocessDump';
42 public $mPPNodeCount = 0;
43
44 public function getStripList() {
45 $parser = MediaWikiServices::getInstance()->getParser();
46
47 return $parser->getStripList();
48 }
49
50 public function __construct() {
51 parent::__construct();
52 $this->addOption( 'cache', 'Use and populate the preprocessor cache.', false, false );
53 $this->addOption( 'preprocessor', 'Preprocessor to use.', false, false );
54 }
55
56 public function getDbType() {
57 return Maintenance::DB_NONE;
58 }
59
60 public function checkOptions() {
61 global $wgParser, $wgParserConf, $wgPreprocessorCacheThreshold;
62
63 if ( !$this->hasOption( 'cache' ) ) {
64 $wgPreprocessorCacheThreshold = false;
65 }
66
67 if ( $this->hasOption( 'preprocessor' ) ) {
68 $name = $this->getOption( 'preprocessor' );
69 } elseif ( isset( $wgParserConf['preprocessorClass'] ) ) {
70 $name = $wgParserConf['preprocessorClass'];
71 } else {
72 $name = Preprocessor_DOM::class;
73 }
74
75 $wgParser->firstCallInit();
76 $this->mPreprocessor = new $name( $this );
77 }
78
79 /**
80 * Callback function for each revision, preprocessToObj()
81 * @param Revision $rev
82 */
83 public function processRevision( $rev ) {
84 $content = $rev->getContent( Revision::RAW );
85
86 if ( $content->getModel() !== CONTENT_MODEL_WIKITEXT ) {
87 return;
88 }
89
90 try {
91 $this->mPreprocessor->preprocessToObj( strval( $content->getText() ), 0 );
92 } catch ( Exception $e ) {
93 $this->error( "Caught exception " . $e->getMessage() . " in "
94 . $rev->getTitle()->getPrefixedText() );
95 }
96 }
97 }
98
99 $maintClass = PreprocessDump::class;
100 require_once RUN_MAINTENANCE_IF_MAIN;