MediaWiki 1.43.1
dumpIterator.php
Go to the documentation of this file.
1<?php
36
37// @codeCoverageIgnoreStart
38require_once __DIR__ . '/Maintenance.php';
39// @codeCoverageIgnoreEnd
40
46abstract class DumpIterator extends Maintenance {
48 private $count = 0;
50 private $startTime;
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
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
177 public function checkOptions() {
178 }
179
183 public function conclusions() {
184 }
185
191 abstract public function processRevision( WikiRevision $rev );
192}
193
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
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;
223require_once RUN_MAINTENANCE_IF_MAIN;
224// @codeCoverageIgnoreEnd
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
array $params
The job parameters.
Base class for iterating over a dump.
static disableInterwikis( $prefix, &$data)
processRevision(WikiRevision $rev)
Core function which does whatever the maintenance script is designed to do.
conclusions()
Stub function for giving data about what was computed.
checkOptions()
Stub function for processing additional options.
handleRevision( $rev)
Callback function for each revision, child classes should override processRevision instead.
finalSetup(SettingsBuilder $settingsBuilder)
Imports a XML dump from a file (either from file upload, files on disk, or HTTP)
A content handler knows how do deal with a specific type of content on a wiki page.
A class containing constants representing the names of configuration variables.
Represents an authority that has all permissions.
Value object representing a content slot associated with a page revision.
Builder class for constructing a Config object from a set of sources during bootstrap.
putConfigValues(array $values)
Sets the value of multiple config variables.
Represents a title within MediaWiki.
Definition Title.php:78
internal since 1.36
Definition User.php:94
Maintenance script that runs a regex in the revisions from a dump.
processRevision(WikiRevision $rev)
Represents a revision, log entry or upload during the import process.
getContent( $role=SlotRecord::MAIN)
$wgHooks
Config variable stub for the Hooks setting, for use by phpdoc and IDEs.
$maintClass
$source