Debian Codegraph @

maintenance/dumpIterator.php

raw symbols SCIP
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.
5 * We implement below the simple task of searching inside a dump.
6 *
7 * Copyright © 2011 Platonides
8 * https://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 use MediaWiki\Content\ContentHandler;
30 use MediaWiki\MainConfigNames;
31 use MediaWiki\Permissions\UltimateAuthority;
32 use MediaWiki\Revision\SlotRecord;
33 use MediaWiki\Settings\SettingsBuilder;
34 use MediaWiki\Title\Title;
35 use MediaWiki\User\User;
36
37 // @codeCoverageIgnoreStart
38 require_once __DIR__ . '/Maintenance.php';
39 // @codeCoverageIgnoreEnd
40
41 /**
42 * Base class for iterating over a dump.
43 *
44 * @ingroup Maintenance
45 */
46 abstract class DumpIterator extends Maintenance {
47 /** @var int */
48 private $count = 0;
49 /** @var float */
50 private $startTime;
51 /** @var string|null|false */
52 private $from;
53
54 public function __construct() {
55 parent::__construct();
56 $this->addDescription( 'Does something with a dump' );
57 $this->addOption( 'file', 'File with text to run.', false, true );
58 $this->addOption( 'dump', 'XML dump to execute all revisions.', false, true );
59 $this->addOption( 'from', 'Article from XML dump to start from.', false, true );
60 }
61
62 public function execute() {
63 if ( !( $this->hasOption( 'file' ) xor $this->hasOption( 'dump' ) ) ) {
64 $this->fatalError( "You must provide a file or dump" );
65 }
66
67 $this->checkOptions();
68
69 if ( $this->hasOption( 'file' ) ) {
70 $file = $this->getOption( 'file' );
71 $revision = new WikiRevision();
72 $text = file_get_contents( $file );
73 $title = Title::newFromText( rawurldecode( basename( $file, '.txt' ) ) );
74 $revision->setTitle( $title );
75 $content = ContentHandler::makeContent( $text, $title );
76 $revision->setContent( SlotRecord::MAIN, $content );
77
78 $this->from = false;
79 $this->handleRevision( $revision );
80
81 return;
82 }
83
84 $this->startTime = microtime( true );
85
86 if ( $this->getOption( 'dump' ) == '-' ) {
87 $source = new ImportStreamSource( $this->getStdin() );
88 } else {
89 $this->fatalError( "Sorry, I don't support dump filenames yet. "
90 . "Use - and provide it on stdin on the meantime." );
91 }
92
93 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
94
95 $importer = $this->getServiceContainer()
96 ->getWikiImporterFactory()
97 ->getWikiImporter( $source, new UltimateAuthority( $user ) );
98
99 $importer->setRevisionCallback(
100 [ $this, 'handleRevision' ] );
101 $importer->setNoticeCallback( static function ( $msg, $params ) {
102 echo wfMessage( $msg, $params )->text() . "\n";
103 } );
104
105 $this->from = $this->getOption( 'from', null );
106 $this->count = 0;
107 $importer->doImport();
108
109 $this->conclusions();
110
111 $delta = microtime( true ) - $this->startTime;
112 $this->error( "Done {$this->count} revisions in " . round( $delta, 2 ) . " seconds " );
113 if ( $delta > 0 ) {
114 $this->error( round( $this->count / $delta, 2 ) . " pages/sec" );
115 }
116
117 # Perform the memory_get_peak_usage() when all the other data has been
118 # output so there's no damage if it dies. It is only available since
119 # 5.2.0 (since 5.2.1 if you haven't compiled with --enable-memory-limit)
120 $this->error( "Memory peak usage of " . memory_get_peak_usage() . " bytes\n" );
121 }
122
123 public function finalSetup( SettingsBuilder $settingsBuilder ) {
124 parent::finalSetup( $settingsBuilder );
125
126 if ( $this->getDbType() == Maintenance::DB_NONE ) {
127 // TODO: Allow hooks to be registered via SettingsBuilder as well!
128 // This matches the idea of unifying SettingsBuilder with ExtensionRegistry.
129 // phpcs:disable MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgHooks
130 global $wgHooks;
131 $wgHooks['InterwikiLoadPrefix'][] = 'DumpIterator::disableInterwikis';
132
133 $settingsBuilder->putConfigValues( [
134 MainConfigNames::UseDatabaseMessages => false,
135 MainConfigNames::LocalisationCacheConf => [ 'storeClass' => LCStoreNull::class ],
136 ] );
137 }
138 }
139
140 public static function disableInterwikis( $prefix, &$data ) {
141 # Title::newFromText will check on each namespaced article if it's an interwiki.
142 # We always answer that it is not.
143
144 return false;
145 }
146
147 /**
148 * Callback function for each revision, child classes should override
149 * processRevision instead.
150 * @param WikiRevision $rev
151 */
152 public function handleRevision( $rev ) {
153 $title = $rev->getTitle();
154 if ( !$title ) {
155 $this->error( "Got bogus revision with null title!" );
156
157 return;
158 }
159
160 $this->count++;
161 if ( $this->from !== false ) {
162 if ( $this->from != $title ) {
163 return;
164 }
165 $this->output( "Skipped " . ( $this->count - 1 ) . " pages\n" );
166
167 $this->count = 1;
168 $this->from = null;
169 }
170
171 $this->processRevision( $rev );
172 }
173
174 /**
175 * Stub function for processing additional options
176 */
177 public function checkOptions() {
178 }
179
180 /**
181 * Stub function for giving data about what was computed
182 */
183 public function conclusions() {
184 }
185
186 /**
187 * Core function which does whatever the maintenance script is designed to do
188 *
189 * @param WikiRevision $rev
190 */
191 abstract public function processRevision( WikiRevision $rev );
192 }
193
194 /**
195 * Maintenance script that runs a regex in the revisions from a dump.
196 *
197 * @ingroup Maintenance
198 */
199 class SearchDump extends DumpIterator {
200
201 public function __construct() {
202 parent::__construct();
203 $this->addDescription( 'Runs a regex in the revisions from a dump' );
204 $this->addOption( 'regex', 'Searching regex', true, true );
205 }
206
207 public function getDbType() {
208 return Maintenance::DB_NONE;
209 }
210
211 /**
212 * @param WikiRevision $rev
213 */
214 public function processRevision( WikiRevision $rev ) {
215 if ( preg_match( $this->getOption( 'regex' ), $rev->getContent()->getTextForSearchIndex() ) ) {
216 $this->output( $rev->getTitle() . " matches at edit from " . $rev->getTimestamp() . "\n" );
217 }
218 }
219 }
220
221 // @codeCoverageIgnoreStart
222 $maintClass = SearchDump::class;
223 require_once RUN_MAINTENANCE_IF_MAIN;
224 // @codeCoverageIgnoreEnd