Refactor the common code of compareParsers.php and preprocessDump.php into a dumpIter...
[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 function stripParameters( $text ) {
88 if ( !$this->stripParametersEnabled ) {
89 return $text;
90 }
91 return preg_replace( '/(<a) [^>]+>/', '$1>', $text );
92 }
93
94 /**
95 * Callback function for each revision, child classes should override
96 * processRevision instead.
97 * @param $rev Revision
98 */
99 public function handleRevision( $rev ) {
100 $title = $rev->getTitle();
101 if ( !$title ) {
102 $this->error( "Got bogus revision with null title!" );
103 return;
104 }
105
106 $this->count++;
107 if ( isset( $this->from ) ) {
108 if ( $this->from != $title )
109 return;
110 $this->output( "Skipped " . ($this->count - 1) . " pages\n" );
111
112 $this->count = 1;
113 $this->from = null;
114 }
115
116 $this->processRevision( $rev );
117 }
118
119 /* Stub function for processing additional options */
120 public function checkOptions() {
121 return;
122 }
123
124 /* Stub function for giving data about what was computed */
125 public function conclusions() {
126 return;
127 }
128
129 /* Core function which does whatever the maintenance script is designed to do */
130 abstract public function processRevision( $rev );
131 }
132
133 class SearchDump extends DumpIterator {
134
135 public function __construct() {
136 parent::__construct();
137 $this->mDescription = "Runs a regex in the revisions from a dump";
138 $this->addOption( 'regex', 'Searching regex', true, true );
139 }
140
141 public function processRevision( $rev ) {
142 if ( preg_match( $this->getOption( 'regex' ), $rev->getText() ) ) {
143 $this->output( $rev->getTitle() . " matches at edit from " . $rev->getTimestamp() . "\n" );
144 }
145 }
146 }
147
148 $maintClass = "SearchDump";
149 require_once( RUN_MAINTENANCE_IF_MAIN );