Enable preprocessDump.php to work without a db.
[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 (C) 2011 Platonides - http://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 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
29
30 class PreprocessDump extends Maintenance {
31
32 private $count = 0;
33 private $startTime;
34
35 /* Variables for dressing up as a parser */
36 public $mTitle = 'PreprocessDump';
37 public $mPPNodeCount = 0;
38
39 public function getStripList() {
40 global $wgParser;
41 return $wgParser->getStripList();
42 }
43
44 public function __construct() {
45 parent::__construct();
46 $this->saveFailed = false;
47 $this->mDescription = "Run a file or dump with a preprocessor";
48 $this->addOption( 'file', 'File with text to run.', false, true );
49 $this->addOption( 'dump', 'XML dump to execute all revisions.', false, true );
50 $this->addOption( 'from', 'Article from XML dump to start from.', false, true );
51 $this->addOption( 'cache', 'Use and populate the preprocessor cache.', false, false );
52 $this->addOption( 'preprocessor', 'Preprocessor to use.', false, false );
53 }
54
55 public function getDbType() {
56 return Maintenance::DB_NONE;
57 }
58
59 public function finalSetup() {
60 parent::finalSetup();
61
62 global $wgUseDatabaseMessages, $wgLocalisationCacheConf, $wgHooks;
63 $wgUseDatabaseMessages = false;
64 $wgLocalisationCacheConf['storeClass'] = 'LCStore_Null';
65 $wgHooks['InterwikiLoadPrefix'][] = 'PreprocessDump::disableInterwikis';
66 }
67
68 static function disableInterwikis( $prefix, &$data ) {
69 # Title::newFromText will check on ach namespaced article if it's an interwiki.
70
71 return false;
72 }
73
74 public function execute() {
75 global $wgParser, $wgParserConf, $wgPreprocessorCacheThreshold;
76
77 if (! ( $this->hasOption( 'file' ) ^ $this->hasOption( 'dump' ) ) ) {
78 $this->error("You must provide a file or dump", true);
79 }
80
81 if ( !$this->hasOption( 'cache' ) ) {
82 $wgPreprocessorCacheThreshold = false;
83 $this->saveFailed = $this->getOption('save-failed');
84 }
85
86 if ( $this->hasOption( 'preprocessor' ) ) {
87 $name = $this->getOption( 'preprocessor' );
88 } elseif ( isset( $wgParserConf['preprocessorClass'] ) ) {
89 $name = $wgParserConf['preprocessorClass'];
90 } else {
91 $name = 'Preprocessor_DOM';
92 }
93
94 $wgParser->firstCallInit();
95 $this->mPreprocessor = new $name( $this );
96
97 if ( $this->hasOption( 'file' ) ) {
98 $revision = new WikiRevision;
99
100 $revision->setText( file_get_contents( $this->getOption( 'file' ) ) );
101 $revision->setTitle( Title::newFromText( rawurldecode( basename( $this->getOption('file'), '.txt' ) ) ) );
102 $this->handleRevision( $revision );
103 return;
104 }
105
106 $this->startTime = wfTime();
107
108 if ( $this->getOption('dump') == '-' ) {
109 $source = new ImportStreamSource( $this->getStdin() );
110 } else {
111 $this->error("Sorry, I don't support dump filenames yet. Use - and provide it on stdin on the meantime.", true);
112 }
113 $importer = new WikiImporter( $source );
114
115 $importer->setRevisionCallback(
116 array( &$this, 'handleRevision' ) );
117
118 $this->from = $this->getOption( 'from', null );
119 $this->count = 0;
120 $importer->doImport();
121
122 $delta = wfTime() - $this->startTime;
123 $this->error( "{$this->count} revisions preprocessed in " . round($delta, 2) . " seconds " );
124 if ($delta > 0)
125 $this->error( round($this->count / $delta, 2) . " pages/sec" );
126
127 # Perform the memory_get_peak_usage() when all the other data has been output so there's no damage if it dies.
128 # It is only available since 5.2.0 (since 5.2.1 if you haven't compiled with --enable-memory-limit)
129 $this->error( "Memory peak usage of " . memory_get_peak_usage() . " bytes\n" );
130 }
131
132 /**
133 * Callback function for each revision, preprocessToObj()
134 * @param $rev Revision
135 */
136 public function handleRevision( $rev ) {
137 $title = $rev->getTitle();
138 if ( !$title ) {
139 $this->error( "Got bogus revision with null title!" );
140 return;
141 }
142
143 $this->count++;
144 if ( isset( $this->from ) ) {
145 if ( $this->from != $title )
146 return;
147 $this->output( "Skipped " . ($this->count - 1) . " pages\n" );
148
149 $this->count = 1;
150 $this->from = null;
151 }
152 try {
153 $this->mPreprocessor->preprocessToObj( $rev->getText(), 0 );
154 }
155 catch(Exception $e) {
156 $this->error("Caught exception " . $e->getMessage() . " in " . $title-> getPrefixedText() );
157 }
158 }
159 }
160
161 $maintClass = "PreprocessDump";
162 require_once( RUN_MAINTENANCE_IF_MAIN );
163