Don't load all languages just to check whether message is known.
[mediawiki.git] / maintenance / dumpIterator.php
blob470bc56eb2a39f8aa527e9b3d9f42498ae3c9233
1 <?php
2 /**
3 * Take page text out of an XML dump file and perform some operation on it.
4 * Used as a base class for CompareParsers and PreprocessDump.
5 * We implement below the simple task of searching inside a dump.
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 abstract class DumpIterator extends Maintenance {
32 private $count = 0;
33 private $startTime;
35 public function __construct() {
36 parent::__construct();
37 $this->mDescription = "Does something with a dump";
38 $this->addOption( 'file', 'File with text to run.', false, true );
39 $this->addOption( 'dump', 'XML dump to execute all revisions.', false, true );
40 $this->addOption( 'from', 'Article from XML dump to start from.', false, true );
43 public function execute() {
44 if (! ( $this->hasOption('file') ^ $this->hasOption('dump') ) ) {
45 $this->error("You must provide a file or dump", true);
48 $this->checkOptions();
50 if ( $this->hasOption('file') ) {
51 $revision = new WikiRevision;
53 $revision->setText( file_get_contents( $this->getOption( 'file' ) ) );
54 $revision->setTitle( Title::newFromText( rawurldecode( basename( $this->getOption( 'file' ), '.txt' ) ) ) );
55 $this->handleRevision( $revision );
56 return;
59 $this->startTime = wfTime();
61 if ( $this->getOption('dump') == '-' ) {
62 $source = new ImportStreamSource( $this->getStdin() );
63 } else {
64 $this->error("Sorry, I don't support dump filenames yet. Use - and provide it on stdin on the meantime.", true);
66 $importer = new WikiImporter( $source );
68 $importer->setRevisionCallback(
69 array( &$this, 'handleRevision' ) );
71 $this->from = $this->getOption( 'from', null );
72 $this->count = 0;
73 $importer->doImport();
75 $this->conclusions();
77 $delta = wfTime() - $this->startTime;
78 $this->error( "Done {$this->count} revisions in " . round($delta, 2) . " seconds " );
79 if ($delta > 0)
80 $this->error( round($this->count / $delta, 2) . " pages/sec" );
82 # Perform the memory_get_peak_usage() when all the other data has been output so there's no damage if it dies.
83 # It is only available since 5.2.0 (since 5.2.1 if you haven't compiled with --enable-memory-limit)
84 $this->error( "Memory peak usage of " . memory_get_peak_usage() . " bytes\n" );
87 public function finalSetup() {
88 parent::finalSetup();
90 if ( $this->getDbType() == Maintenance::DB_NONE ) {
91 global $wgUseDatabaseMessages, $wgLocalisationCacheConf, $wgHooks;
92 $wgUseDatabaseMessages = false;
93 $wgLocalisationCacheConf['storeClass'] = 'LCStore_Null';
94 $wgHooks['InterwikiLoadPrefix'][] = 'DumpIterator::disableInterwikis';
98 static function disableInterwikis( $prefix, &$data ) {
99 # Title::newFromText will check on each namespaced article if it's an interwiki.
100 # We always answer that it is not.
102 return false;
106 * Callback function for each revision, child classes should override
107 * processRevision instead.
108 * @param $rev Revision
110 public function handleRevision( $rev ) {
111 $title = $rev->getTitle();
112 if ( !$title ) {
113 $this->error( "Got bogus revision with null title!" );
114 return;
117 $this->count++;
118 if ( isset( $this->from ) ) {
119 if ( $this->from != $title )
120 return;
121 $this->output( "Skipped " . ($this->count - 1) . " pages\n" );
123 $this->count = 1;
124 $this->from = null;
127 $this->processRevision( $rev );
130 /* Stub function for processing additional options */
131 public function checkOptions() {
132 return;
135 /* Stub function for giving data about what was computed */
136 public function conclusions() {
137 return;
140 /* Core function which does whatever the maintenance script is designed to do */
141 abstract public function processRevision( $rev );
144 class SearchDump extends DumpIterator {
146 public function __construct() {
147 parent::__construct();
148 $this->mDescription = "Runs a regex in the revisions from a dump";
149 $this->addOption( 'regex', 'Searching regex', true, true );
152 public function getDbType() {
153 return Maintenance::DB_NONE;
157 * @param $rev Revision
159 public function processRevision( $rev ) {
160 if ( preg_match( $this->getOption( 'regex' ), $rev->getText() ) ) {
161 $this->output( $rev->getTitle() . " matches at edit from " . $rev->getTimestamp() . "\n" );
166 $maintClass = "SearchDump";
167 require_once( RUN_MAINTENANCE_IF_MAIN );