phpcs: More require/include is not a function
[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 © 2011 Platonides
8 * http://www.mediawiki.org/
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 *
25 * @file
26 * @ingroup Maintenance
27 */
28
29 require_once __DIR__ . '/Maintenance.php';
30
31 /**
32 * Base class for interating over a dump.
33 *
34 * @ingroup Maintenance
35 */
36 abstract class DumpIterator extends Maintenance {
37
38 private $count = 0;
39 private $startTime;
40
41 public function __construct() {
42 parent::__construct();
43 $this->mDescription = "Does something with a dump";
44 $this->addOption( 'file', 'File with text to run.', false, true );
45 $this->addOption( 'dump', 'XML dump to execute all revisions.', false, true );
46 $this->addOption( 'from', 'Article from XML dump to start from.', false, true );
47 }
48
49 public function execute() {
50 if ( !( $this->hasOption( 'file' ) ^ $this->hasOption( 'dump' ) ) ) {
51 $this->error( "You must provide a file or dump", true );
52 }
53
54 $this->checkOptions();
55
56 if ( $this->hasOption( 'file' ) ) {
57 $revision = new WikiRevision;
58
59 $revision->setText( file_get_contents( $this->getOption( 'file' ) ) );
60 $revision->setTitle( Title::newFromText( rawurldecode( basename( $this->getOption( 'file' ), '.txt' ) ) ) );
61 $this->handleRevision( $revision );
62 return;
63 }
64
65 $this->startTime = microtime( true );
66
67 if ( $this->getOption( 'dump' ) == '-' ) {
68 $source = new ImportStreamSource( $this->getStdin() );
69 } else {
70 $this->error( "Sorry, I don't support dump filenames yet. Use - and provide it on stdin on the meantime.", true );
71 }
72 $importer = new WikiImporter( $source );
73
74 $importer->setRevisionCallback(
75 array( &$this, 'handleRevision' ) );
76
77 $this->from = $this->getOption( 'from', null );
78 $this->count = 0;
79 $importer->doImport();
80
81 $this->conclusions();
82
83 $delta = microtime( true ) - $this->startTime;
84 $this->error( "Done {$this->count} revisions in " . round( $delta, 2 ) . " seconds " );
85 if ( $delta > 0 ) {
86 $this->error( round( $this->count / $delta, 2 ) . " pages/sec" );
87 }
88
89 # Perform the memory_get_peak_usage() when all the other data has been output so there's no damage if it dies.
90 # It is only available since 5.2.0 (since 5.2.1 if you haven't compiled with --enable-memory-limit)
91 $this->error( "Memory peak usage of " . memory_get_peak_usage() . " bytes\n" );
92 }
93
94 public function finalSetup() {
95 parent::finalSetup();
96
97 if ( $this->getDbType() == Maintenance::DB_NONE ) {
98 global $wgUseDatabaseMessages, $wgLocalisationCacheConf, $wgHooks;
99 $wgUseDatabaseMessages = false;
100 $wgLocalisationCacheConf['storeClass'] = 'LCStore_Null';
101 $wgHooks['InterwikiLoadPrefix'][] = 'DumpIterator::disableInterwikis';
102 }
103 }
104
105 static function disableInterwikis( $prefix, &$data ) {
106 # Title::newFromText will check on each namespaced article if it's an interwiki.
107 # We always answer that it is not.
108
109 return false;
110 }
111
112 /**
113 * Callback function for each revision, child classes should override
114 * processRevision instead.
115 * @param $rev Revision
116 */
117 public function handleRevision( $rev ) {
118 $title = $rev->getTitle();
119 if ( !$title ) {
120 $this->error( "Got bogus revision with null title!" );
121 return;
122 }
123
124 $this->count++;
125 if ( isset( $this->from ) ) {
126 if ( $this->from != $title ) {
127 return;
128 }
129 $this->output( "Skipped " . ( $this->count - 1 ) . " pages\n" );
130
131 $this->count = 1;
132 $this->from = null;
133 }
134
135 $this->processRevision( $rev );
136 }
137
138 /* Stub function for processing additional options */
139 public function checkOptions() {
140 return;
141 }
142
143 /* Stub function for giving data about what was computed */
144 public function conclusions() {
145 return;
146 }
147
148 /* Core function which does whatever the maintenance script is designed to do */
149 abstract public function processRevision( $rev );
150 }
151
152 /**
153 * Maintenance script that runs a regex in the revisions from a dump.
154 *
155 * @ingroup Maintenance
156 */
157 class SearchDump extends DumpIterator {
158
159 public function __construct() {
160 parent::__construct();
161 $this->mDescription = "Runs a regex in the revisions from a dump";
162 $this->addOption( 'regex', 'Searching regex', true, true );
163 }
164
165 public function getDbType() {
166 return Maintenance::DB_NONE;
167 }
168
169 /**
170 * @param $rev Revision
171 */
172 public function processRevision( $rev ) {
173 if ( preg_match( $this->getOption( 'regex' ), $rev->getContent()->getTextForSearchIndex() ) ) {
174 $this->output( $rev->getTitle() . " matches at edit from " . $rev->getTimestamp() . "\n" );
175 }
176 }
177 }
178
179 $maintClass = "SearchDump";
180 require_once RUN_MAINTENANCE_IF_MAIN;