Revert r64853 (add $wgLogAutocreatedAccounts to enable/disable account autocreation...
[mediawiki.git] / maintenance / preprocessDump.php
blob27d11dcda06ca6ff388c44e13b0acfe27502d9d7
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.
7 * Copyright (C) 2011 Platonides - http://www.mediawiki.org/
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.
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.
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
24 * @file
25 * @ingroup Maintenance
28 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
30 class PreprocessDump extends Maintenance {
32 private $count = 0;
33 private $startTime;
35 /* Variables for dressing up as a parser */
36 public $mTitle = 'PreprocessDump';
37 public $mPPNodeCount = 0;
39 public function getStripList() {
40 global $wgParser;
41 return $wgParser->getStripList();
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 );
55 public function getDbType() {
56 return Maintenance::DB_NONE;
59 public function finalSetup() {
60 parent::finalSetup();
62 global $wgUseDatabaseMessages, $wgLocalisationCacheConf, $wgHooks;
63 $wgUseDatabaseMessages = false;
64 $wgLocalisationCacheConf['storeClass'] = 'LCStore_Null';
65 $wgHooks['InterwikiLoadPrefix'][] = 'PreprocessDump::disableInterwikis';
68 static function disableInterwikis( $prefix, &$data ) {
69 # Title::newFromText will check on each namespaced article if it's an interwiki.
70 # We always answer that it is not.
72 return false;
75 public function execute() {
76 global $wgParser, $wgParserConf, $wgPreprocessorCacheThreshold;
78 if (! ( $this->hasOption( 'file' ) ^ $this->hasOption( 'dump' ) ) ) {
79 $this->error("You must provide a file or dump", true);
82 if ( !$this->hasOption( 'cache' ) ) {
83 $wgPreprocessorCacheThreshold = false;
84 $this->saveFailed = $this->getOption('save-failed');
87 if ( $this->hasOption( 'preprocessor' ) ) {
88 $name = $this->getOption( 'preprocessor' );
89 } elseif ( isset( $wgParserConf['preprocessorClass'] ) ) {
90 $name = $wgParserConf['preprocessorClass'];
91 } else {
92 $name = 'Preprocessor_DOM';
95 $wgParser->firstCallInit();
96 $this->mPreprocessor = new $name( $this );
98 if ( $this->hasOption( 'file' ) ) {
99 $revision = new WikiRevision;
101 $revision->setText( file_get_contents( $this->getOption( 'file' ) ) );
102 $revision->setTitle( Title::newFromText( rawurldecode( basename( $this->getOption('file'), '.txt' ) ) ) );
103 $this->handleRevision( $revision );
104 return;
107 $this->startTime = wfTime();
109 if ( $this->getOption('dump') == '-' ) {
110 $source = new ImportStreamSource( $this->getStdin() );
111 } else {
112 $this->error("Sorry, I don't support dump filenames yet. Use - and provide it on stdin on the meantime.", true);
114 $importer = new WikiImporter( $source );
116 $importer->setRevisionCallback(
117 array( &$this, 'handleRevision' ) );
119 $this->from = $this->getOption( 'from', null );
120 $this->count = 0;
121 $importer->doImport();
123 $delta = wfTime() - $this->startTime;
124 $this->error( "{$this->count} revisions preprocessed in " . round($delta, 2) . " seconds " );
125 if ($delta > 0)
126 $this->error( round($this->count / $delta, 2) . " pages/sec" );
128 # Perform the memory_get_peak_usage() when all the other data has been output so there's no damage if it dies.
129 # It is only available since 5.2.0 (since 5.2.1 if you haven't compiled with --enable-memory-limit)
130 $this->error( "Memory peak usage of " . memory_get_peak_usage() . " bytes\n" );
134 * Callback function for each revision, preprocessToObj()
135 * @param $rev Revision
137 public function handleRevision( $rev ) {
138 $title = $rev->getTitle();
139 if ( !$title ) {
140 $this->error( "Got bogus revision with null title!" );
141 return;
144 $this->count++;
145 if ( isset( $this->from ) ) {
146 if ( $this->from != $title )
147 return;
148 $this->output( "Skipped " . ($this->count - 1) . " pages\n" );
150 $this->count = 1;
151 $this->from = null;
153 try {
154 $this->mPreprocessor->preprocessToObj( $rev->getText(), 0 );
156 catch(Exception $e) {
157 $this->error("Caught exception " . $e->getMessage() . " in " . $title-> getPrefixedText() );
162 $maintClass = "PreprocessDump";
163 require_once( RUN_MAINTENANCE_IF_MAIN );