Merge "Provide command to adjust phpunit.xml for code coverage"
[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 use MediaWiki\Storage\RevisionRecord;
30
31 require_once __DIR__ . '/dumpIterator.php';
32
33 /**
34 * Maintenance script that takes page text out of an XML dump file and
35 * preprocesses it to obj.
36 *
37 * @ingroup Maintenance
38 */
39 class PreprocessDump extends DumpIterator {
40
41 /* Variables for dressing up as a parser */
42 public $mTitle = 'PreprocessDump';
43 public $mPPNodeCount = 0;
44 /** @var Preprocessor */
45 public $mPreprocessor;
46
47 public function getStripList() {
48 $parser = MediaWikiServices::getInstance()->getParser();
49
50 return $parser->getStripList();
51 }
52
53 public function __construct() {
54 parent::__construct();
55 $this->addOption( 'cache', 'Use and populate the preprocessor cache.', false, false );
56 $this->addOption( 'preprocessor', 'Preprocessor to use.', false, false );
57 }
58
59 public function getDbType() {
60 return Maintenance::DB_NONE;
61 }
62
63 public function checkOptions() {
64 global $wgParserConf, $wgPreprocessorCacheThreshold;
65
66 if ( !$this->hasOption( 'cache' ) ) {
67 $wgPreprocessorCacheThreshold = false;
68 }
69
70 if ( $this->hasOption( 'preprocessor' ) ) {
71 $name = $this->getOption( 'preprocessor' );
72 } elseif ( isset( $wgParserConf['preprocessorClass'] ) ) {
73 $name = $wgParserConf['preprocessorClass'];
74 } else {
75 $name = Preprocessor_DOM::class;
76 }
77
78 $parser = MediaWikiServices::getInstance()->getParser();
79 $parser->firstCallInit();
80 $this->mPreprocessor = new $name( $parser );
81 }
82
83 /**
84 * Callback function for each revision, preprocessToObj()
85 * @param Revision $rev
86 */
87 public function processRevision( $rev ) {
88 $content = $rev->getContent( RevisionRecord::RAW );
89
90 if ( $content->getModel() !== CONTENT_MODEL_WIKITEXT ) {
91 return;
92 }
93 /** @var WikitextContent $content */
94 '@phan-var WikitextContent $content';
95
96 try {
97 $this->mPreprocessor->preprocessToObj( strval( $content->getText() ), 0 );
98 } catch ( Exception $e ) {
99 $this->error( "Caught exception " . $e->getMessage() . " in "
100 . $rev->getTitle()->getPrefixedText() );
101 }
102 }
103 }
104
105 $maintClass = PreprocessDump::class;
106 require_once RUN_MAINTENANCE_IF_MAIN;