MediaWiki master
dumpIterator.php
Go to the documentation of this file.
1<?php
35
36require_once __DIR__ . '/Maintenance.php';
37
43abstract class DumpIterator extends Maintenance {
45 private $count = 0;
47 private $startTime;
49 private $from;
50
51 public function __construct() {
52 parent::__construct();
53 $this->addDescription( 'Does something with a dump' );
54 $this->addOption( 'file', 'File with text to run.', false, true );
55 $this->addOption( 'dump', 'XML dump to execute all revisions.', false, true );
56 $this->addOption( 'from', 'Article from XML dump to start from.', false, true );
57 }
58
59 public function execute() {
60 if ( !( $this->hasOption( 'file' ) xor $this->hasOption( 'dump' ) ) ) {
61 $this->fatalError( "You must provide a file or dump" );
62 }
63
64 $this->checkOptions();
65
66 if ( $this->hasOption( 'file' ) ) {
67 $file = $this->getOption( 'file' );
68 $revision = new WikiRevision();
69 $text = file_get_contents( $file );
70 $title = Title::newFromText( rawurldecode( basename( $file, '.txt' ) ) );
71 $revision->setTitle( $title );
72 $content = ContentHandler::makeContent( $text, $title );
73 $revision->setContent( SlotRecord::MAIN, $content );
74
75 $this->from = false;
76 $this->handleRevision( $revision );
77
78 return;
79 }
80
81 $this->startTime = microtime( true );
82
83 if ( $this->getOption( 'dump' ) == '-' ) {
84 $source = new ImportStreamSource( $this->getStdin() );
85 } else {
86 $this->fatalError( "Sorry, I don't support dump filenames yet. "
87 . "Use - and provide it on stdin on the meantime." );
88 }
89
90 $user = User::newSystemUser( User::MAINTENANCE_SCRIPT_USER, [ 'steal' => true ] );
91
92 $importer = $this->getServiceContainer()
93 ->getWikiImporterFactory()
94 ->getWikiImporter( $source, new UltimateAuthority( $user ) );
95
96 $importer->setRevisionCallback(
97 [ $this, 'handleRevision' ] );
98 $importer->setNoticeCallback( static function ( $msg, $params ) {
99 echo wfMessage( $msg, $params )->text() . "\n";
100 } );
101
102 $this->from = $this->getOption( 'from', null );
103 $this->count = 0;
104 $importer->doImport();
105
106 $this->conclusions();
107
108 $delta = microtime( true ) - $this->startTime;
109 $this->error( "Done {$this->count} revisions in " . round( $delta, 2 ) . " seconds " );
110 if ( $delta > 0 ) {
111 $this->error( round( $this->count / $delta, 2 ) . " pages/sec" );
112 }
113
114 # Perform the memory_get_peak_usage() when all the other data has been
115 # output so there's no damage if it dies. It is only available since
116 # 5.2.0 (since 5.2.1 if you haven't compiled with --enable-memory-limit)
117 $this->error( "Memory peak usage of " . memory_get_peak_usage() . " bytes\n" );
118 }
119
120 public function finalSetup( SettingsBuilder $settingsBuilder ) {
121 parent::finalSetup( $settingsBuilder );
122
123 if ( $this->getDbType() == Maintenance::DB_NONE ) {
124 // TODO: Allow hooks to be registered via SettingsBuilder as well!
125 // This matches the idea of unifying SettingsBuilder with ExtensionRegistry.
126 // phpcs:disable MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgHooks
127 global $wgHooks;
128 $wgHooks['InterwikiLoadPrefix'][] = 'DumpIterator::disableInterwikis';
129
130 $settingsBuilder->putConfigValues( [
131 MainConfigNames::UseDatabaseMessages => false,
132 MainConfigNames::LocalisationCacheConf => [ 'storeClass' => LCStoreNull::class ],
133 ] );
134 }
135 }
136
137 public static function disableInterwikis( $prefix, &$data ) {
138 # Title::newFromText will check on each namespaced article if it's an interwiki.
139 # We always answer that it is not.
140
141 return false;
142 }
143
149 public function handleRevision( $rev ) {
150 $title = $rev->getTitle();
151 if ( !$title ) {
152 $this->error( "Got bogus revision with null title!" );
153
154 return;
155 }
156
157 $this->count++;
158 if ( $this->from !== false ) {
159 if ( $this->from != $title ) {
160 return;
161 }
162 $this->output( "Skipped " . ( $this->count - 1 ) . " pages\n" );
163
164 $this->count = 1;
165 $this->from = null;
166 }
167
168 $this->processRevision( $rev );
169 }
170
174 public function checkOptions() {
175 }
176
180 public function conclusions() {
181 }
182
188 abstract public function processRevision( WikiRevision $rev );
189}
190
197
198 public function __construct() {
199 parent::__construct();
200 $this->addDescription( 'Runs a regex in the revisions from a dump' );
201 $this->addOption( 'regex', 'Searching regex', true, true );
202 }
203
204 public function getDbType() {
206 }
207
211 public function processRevision( WikiRevision $rev ) {
212 if ( preg_match( $this->getOption( 'regex' ), $rev->getContent()->getTextForSearchIndex() ) ) {
213 $this->output( $rev->getTitle() . " matches at edit from " . $rev->getTimestamp() . "\n" );
214 }
215 }
216}
217
218$maintClass = SearchDump::class;
219require_once RUN_MAINTENANCE_IF_MAIN;
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)
__construct()
Default constructor.
execute()
Do the actual work.
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)
Handle some last-minute setup here.
Imports a XML dump from a file (either from file upload, files on disk, or HTTP)
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
error( $err, $die=0)
Throw an error to the user.
const DB_NONE
Constants for DB access type.
output( $out, $channel=null)
Throw some output to the user.
getStdin( $len=null)
Return input from stdin.
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
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:79
internal since 1.36
Definition User.php:93
Maintenance script that runs a regex in the revisions from a dump.
__construct()
Default constructor.
processRevision(WikiRevision $rev)
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
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