MediaWiki  1.33.0
dumpIterator.php
Go to the documentation of this file.
1 <?php
29 require_once __DIR__ . '/Maintenance.php';
30 
36 abstract class DumpIterator extends Maintenance {
37 
38  private $count = 0;
39  private $startTime;
40 
41  public function __construct() {
42  parent::__construct();
43  $this->addDescription( '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->fatalError( "You must provide a file or dump" );
52  }
53 
54  $this->checkOptions();
55 
56  if ( $this->hasOption( 'file' ) ) {
57  $revision = new WikiRevision( $this->getConfig() );
58 
59  $revision->setText( file_get_contents( $this->getOption( 'file' ) ) );
60  $revision->setTitle( Title::newFromText(
61  rawurldecode( basename( $this->getOption( 'file' ), '.txt' ) )
62  ) );
63  $this->handleRevision( $revision );
64 
65  return;
66  }
67 
68  $this->startTime = microtime( true );
69 
70  if ( $this->getOption( 'dump' ) == '-' ) {
71  $source = new ImportStreamSource( $this->getStdin() );
72  } else {
73  $this->fatalError( "Sorry, I don't support dump filenames yet. "
74  . "Use - and provide it on stdin on the meantime." );
75  }
76  $importer = new WikiImporter( $source, $this->getConfig() );
77 
78  $importer->setRevisionCallback(
79  [ $this, 'handleRevision' ] );
80  $importer->setNoticeCallback( function ( $msg, $params ) {
81  echo wfMessage( $msg, $params )->text() . "\n";
82  } );
83 
84  $this->from = $this->getOption( 'from', null );
85  $this->count = 0;
86  $importer->doImport();
87 
88  $this->conclusions();
89 
90  $delta = microtime( true ) - $this->startTime;
91  $this->error( "Done {$this->count} revisions in " . round( $delta, 2 ) . " seconds " );
92  if ( $delta > 0 ) {
93  $this->error( round( $this->count / $delta, 2 ) . " pages/sec" );
94  }
95 
96  # Perform the memory_get_peak_usage() when all the other data has been
97  # output so there's no damage if it dies. It is only available since
98  # 5.2.0 (since 5.2.1 if you haven't compiled with --enable-memory-limit)
99  $this->error( "Memory peak usage of " . memory_get_peak_usage() . " bytes\n" );
100  }
101 
102  public function finalSetup() {
103  parent::finalSetup();
104 
105  if ( $this->getDbType() == Maintenance::DB_NONE ) {
107  $wgUseDatabaseMessages = false;
109  $wgHooks['InterwikiLoadPrefix'][] = 'DumpIterator::disableInterwikis';
110  }
111  }
112 
113  static function disableInterwikis( $prefix, &$data ) {
114  # Title::newFromText will check on each namespaced article if it's an interwiki.
115  # We always answer that it is not.
116 
117  return false;
118  }
119 
125  public function handleRevision( $rev ) {
126  $title = $rev->getTitle();
127  if ( !$title ) {
128  $this->error( "Got bogus revision with null title!" );
129 
130  return;
131  }
132 
133  $this->count++;
134  if ( isset( $this->from ) ) {
135  if ( $this->from != $title ) {
136  return;
137  }
138  $this->output( "Skipped " . ( $this->count - 1 ) . " pages\n" );
139 
140  $this->count = 1;
141  $this->from = null;
142  }
143 
144  $this->processRevision( $rev );
145  }
146 
147  /* Stub function for processing additional options */
148  public function checkOptions() {
149  }
150 
151  /* Stub function for giving data about what was computed */
152  public function conclusions() {
153  }
154 
155  /* Core function which does whatever the maintenance script is designed to do */
156  abstract public function processRevision( $rev );
157 }
158 
164 class SearchDump extends DumpIterator {
165 
166  public function __construct() {
167  parent::__construct();
168  $this->addDescription( 'Runs a regex in the revisions from a dump' );
169  $this->addOption( 'regex', 'Searching regex', true, true );
170  }
171 
172  public function getDbType() {
173  return Maintenance::DB_NONE;
174  }
175 
179  public function processRevision( $rev ) {
180  if ( preg_match( $this->getOption( 'regex' ), $rev->getContent()->getTextForSearchIndex() ) ) {
181  $this->output( $rev->getTitle() . " matches at edit from " . $rev->getTimestamp() . "\n" );
182  }
183  }
184 }
185 
187 require_once RUN_MAINTENANCE_IF_MAIN;
WikiImporter
XML file reader for the page data importer.
Definition: WikiImporter.php:35
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:306
DumpIterator\__construct
__construct()
Default constructor.
Definition: dumpIterator.php:41
Maintenance\getStdin
getStdin( $len=null)
Return input from stdin.
Definition: Maintenance.php:407
Maintenance\getDbType
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
Definition: Maintenance.php:544
DumpIterator\handleRevision
handleRevision( $rev)
Callback function for each revision, child classes should override processRevision instead.
Definition: dumpIterator.php:125
SearchDump\getDbType
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
Definition: dumpIterator.php:172
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:485
captcha-old.count
count
Definition: captcha-old.py:249
SearchDump\processRevision
processRevision( $rev)
Definition: dumpIterator.php:179
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
$maintClass
$maintClass
Definition: dumpIterator.php:186
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$params
$params
Definition: styleTest.css.php:44
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
ImportStreamSource
Imports a XML dump from a file (either from file upload, files on disk, or HTTP)
Definition: ImportStreamSource.php:32
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
DumpIterator\execute
execute()
Do the actual work.
Definition: dumpIterator.php:49
DumpIterator\$startTime
$startTime
Definition: dumpIterator.php:39
DumpIterator\processRevision
processRevision( $rev)
$data
$data
Utility to generate mapping file used in mw.Title (phpCharToUpper.json)
Definition: generatePhpCharToUpperMappings.php:13
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
Maintenance\getConfig
getConfig()
Definition: Maintenance.php:594
SearchDump
Maintenance script that runs a regex in the revisions from a dump.
Definition: dumpIterator.php:164
DumpIterator\$count
$count
Definition: dumpIterator.php:38
SearchDump\__construct
__construct()
Default constructor.
Definition: dumpIterator.php:166
$wgUseDatabaseMessages
$wgUseDatabaseMessages
Translation using MediaWiki: namespace.
Definition: DefaultSettings.php:3070
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:248
$wgLocalisationCacheConf
$wgLocalisationCacheConf
Localisation cache configuration.
Definition: DefaultSettings.php:2587
DumpIterator\checkOptions
checkOptions()
Definition: dumpIterator.php:148
DumpIterator\disableInterwikis
static disableInterwikis( $prefix, &$data)
Definition: dumpIterator.php:113
Maintenance\DB_NONE
const DB_NONE
Constants for DB access type.
Definition: Maintenance.php:77
DumpIterator
Base class for interating over a dump.
Definition: dumpIterator.php:36
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:283
WikiRevision
Represents a revision, log entry or upload during the import process.
Definition: WikiRevision.php:37
$wgHooks
$wgHooks['ArticleShow'][]
Definition: hooks.txt:108
$rev
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition: hooks.txt:1769
from
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control direct or to cause the direction or management of such whether by contract or including but not limited to software source documentation and configuration files Object form shall mean any form resulting from mechanical transformation or translation of a Source including but not limited to compiled object generated and conversions to other media types Work shall mean the work of whether in Source or Object made available under the as indicated by a copyright notice that is included in or attached to the whether in Source or Object that is based or other modifications as a an original work of authorship For the purposes of this Derivative Works shall not include works that remain separable from
Definition: APACHE-LICENSE-2.0.txt:43
$source
$source
Definition: mwdoc-filter.php:46
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:462
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
DumpIterator\finalSetup
finalSetup()
Handle some last-minute setup here.
Definition: dumpIterator.php:102
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:269
wfMessage
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
DumpIterator\conclusions
conclusions()
Definition: dumpIterator.php:152