Add unit test for bug 32888
[lhc/web/wiklou.git] / maintenance / dumpIterator.php
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.
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 abstract class DumpIterator extends Maintenance {
31
32 private $count = 0;
33 private $startTime;
34
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 );
41 }
42
43 public function execute() {
44 if (! ( $this->hasOption('file') ^ $this->hasOption('dump') ) ) {
45 $this->error("You must provide a file or dump", true);
46 }
47
48 $this->checkOptions();
49
50 if ( $this->hasOption('file') ) {
51 $revision = new WikiRevision;
52
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;
57 }
58
59 $this->startTime = wfTime();
60
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);
65 }
66 $importer = new WikiImporter( $source );
67
68 $importer->setRevisionCallback(
69 array( &$this, 'handleRevision' ) );
70
71 $this->from = $this->getOption( 'from', null );
72 $this->count = 0;
73 $importer->doImport();
74
75 $this->conclusions();
76
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" );
81
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" );
85 }
86
87 public function finalSetup() {
88 parent::finalSetup();
89
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';
95 }
96 }
97
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.
101
102 return false;
103 }
104
105 /**
106 * Callback function for each revision, child classes should override
107 * processRevision instead.
108 * @param $rev Revision
109 */
110 public function handleRevision( $rev ) {
111 $title = $rev->getTitle();
112 if ( !$title ) {
113 $this->error( "Got bogus revision with null title!" );
114 return;
115 }
116
117 $this->count++;
118 if ( isset( $this->from ) ) {
119 if ( $this->from != $title )
120 return;
121 $this->output( "Skipped " . ($this->count - 1) . " pages\n" );
122
123 $this->count = 1;
124 $this->from = null;
125 }
126
127 $this->processRevision( $rev );
128 }
129
130 /* Stub function for processing additional options */
131 public function checkOptions() {
132 return;
133 }
134
135 /* Stub function for giving data about what was computed */
136 public function conclusions() {
137 return;
138 }
139
140 /* Core function which does whatever the maintenance script is designed to do */
141 abstract public function processRevision( $rev );
142 }
143
144 class SearchDump extends DumpIterator {
145
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 );
150 }
151
152 public function getDbType() {
153 return Maintenance::DB_NONE;
154 }
155
156 /**
157 * @param $rev Revision
158 */
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" );
162 }
163 }
164 }
165
166 $maintClass = "SearchDump";
167 require_once( RUN_MAINTENANCE_IF_MAIN );