MediaWiki  master
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  private $count = 0;
38  private $startTime;
40  private $from;
41 
42  public function __construct() {
43  parent::__construct();
44  $this->addDescription( 'Does something with a dump' );
45  $this->addOption( 'file', 'File with text to run.', false, true );
46  $this->addOption( 'dump', 'XML dump to execute all revisions.', false, true );
47  $this->addOption( 'from', 'Article from XML dump to start from.', false, true );
48  }
49 
50  public function execute() {
51  if ( !( $this->hasOption( 'file' ) xor $this->hasOption( 'dump' ) ) ) {
52  $this->fatalError( "You must provide a file or dump" );
53  }
54 
55  $this->checkOptions();
56 
57  if ( $this->hasOption( 'file' ) ) {
58  $revision = new WikiRevision( $this->getConfig() );
59 
60  $revision->setText( file_get_contents( $this->getOption( 'file' ) ) );
61  $revision->setTitle( Title::newFromText(
62  rawurldecode( basename( $this->getOption( 'file' ), '.txt' ) )
63  ) );
64  $this->from = false;
65  $this->handleRevision( $revision );
66 
67  return;
68  }
69 
70  $this->startTime = microtime( true );
71 
72  if ( $this->getOption( 'dump' ) == '-' ) {
73  $source = new ImportStreamSource( $this->getStdin() );
74  } else {
75  $this->fatalError( "Sorry, I don't support dump filenames yet. "
76  . "Use - and provide it on stdin on the meantime." );
77  }
78  $importer = new WikiImporter( $source, $this->getConfig() );
79 
80  $importer->setRevisionCallback(
81  [ $this, 'handleRevision' ] );
82  $importer->setNoticeCallback( function ( $msg, $params ) {
83  echo wfMessage( $msg, $params )->text() . "\n";
84  } );
85 
86  $this->from = $this->getOption( 'from', null );
87  $this->count = 0;
88  $importer->doImport();
89 
90  $this->conclusions();
91 
92  $delta = microtime( true ) - $this->startTime;
93  $this->error( "Done {$this->count} revisions in " . round( $delta, 2 ) . " seconds " );
94  if ( $delta > 0 ) {
95  $this->error( round( $this->count / $delta, 2 ) . " pages/sec" );
96  }
97 
98  # Perform the memory_get_peak_usage() when all the other data has been
99  # output so there's no damage if it dies. It is only available since
100  # 5.2.0 (since 5.2.1 if you haven't compiled with --enable-memory-limit)
101  $this->error( "Memory peak usage of " . memory_get_peak_usage() . " bytes\n" );
102  }
103 
104  public function finalSetup() {
105  parent::finalSetup();
106 
107  if ( $this->getDbType() == Maintenance::DB_NONE ) {
109  $wgUseDatabaseMessages = false;
110  $wgLocalisationCacheConf['storeClass'] = LCStoreNull::class;
111  $wgHooks['InterwikiLoadPrefix'][] = 'DumpIterator::disableInterwikis';
112  }
113  }
114 
115  public static function disableInterwikis( $prefix, &$data ) {
116  # Title::newFromText will check on each namespaced article if it's an interwiki.
117  # We always answer that it is not.
118 
119  return false;
120  }
121 
127  public function handleRevision( $rev ) {
128  $title = $rev->getTitle();
129  if ( !$title ) {
130  $this->error( "Got bogus revision with null title!" );
131 
132  return;
133  }
134 
135  $this->count++;
136  if ( $this->from !== false ) {
137  if ( $this->from != $title ) {
138  return;
139  }
140  $this->output( "Skipped " . ( $this->count - 1 ) . " pages\n" );
141 
142  $this->count = 1;
143  $this->from = null;
144  }
145 
146  $this->processRevision( $rev );
147  }
148 
152  public function checkOptions() {
153  }
154 
158  public function conclusions() {
159  }
160 
166  abstract public function processRevision( WikiRevision $rev );
167 }
168 
174 class SearchDump extends DumpIterator {
175 
176  public function __construct() {
177  parent::__construct();
178  $this->addDescription( 'Runs a regex in the revisions from a dump' );
179  $this->addOption( 'regex', 'Searching regex', true, true );
180  }
181 
182  public function getDbType() {
183  return Maintenance::DB_NONE;
184  }
185 
189  public function processRevision( WikiRevision $rev ) {
190  if ( preg_match( $this->getOption( 'regex' ), $rev->getContent()->getTextForSearchIndex() ) ) {
191  $this->output( $rev->getTitle() . " matches at edit from " . $rev->getTimestamp() . "\n" );
192  }
193  }
194 }
195 
196 $maintClass = SearchDump::class;
197 require_once RUN_MAINTENANCE_IF_MAIN;
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
WikiImporter
XML file reader for the page data importer.
Definition: WikiImporter.php:37
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:328
DumpIterator\__construct
__construct()
Default constructor.
Definition: dumpIterator.php:42
Maintenance\getStdin
getStdin( $len=null)
Return input from stdin.
Definition: Maintenance.php:406
Maintenance\getDbType
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
Definition: Maintenance.php:547
DumpIterator\handleRevision
handleRevision( $rev)
Callback function for each revision, child classes should override processRevision instead.
Definition: dumpIterator.php:127
SearchDump\getDbType
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
Definition: dumpIterator.php:182
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:487
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:327
$maintClass
$maintClass
Definition: dumpIterator.php:196
WikiRevision\getTitle
getTitle()
Definition: WikiRevision.php:428
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1220
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:55
ImportStreamSource
Imports a XML dump from a file (either from file upload, files on disk, or HTTP)
Definition: ImportStreamSource.php:32
DumpIterator\execute
execute()
Do the actual work.
Definition: dumpIterator.php:50
DumpIterator\$startTime
$startTime
Definition: dumpIterator.php:38
$wgHooks
$wgHooks
Global list of hooks.
Definition: DefaultSettings.php:7929
Maintenance\getConfig
getConfig()
Definition: Maintenance.php:596
SearchDump
Maintenance script that runs a regex in the revisions from a dump.
Definition: dumpIterator.php:174
WikiRevision\getTimestamp
getTimestamp()
Definition: WikiRevision.php:444
DumpIterator\$count
$count
Definition: dumpIterator.php:37
SearchDump\__construct
__construct()
Default constructor.
Definition: dumpIterator.php:176
$wgUseDatabaseMessages
$wgUseDatabaseMessages
Translation using MediaWiki: namespace.
Definition: DefaultSettings.php:3232
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:245
$title
$title
Definition: testCompression.php:38
$wgLocalisationCacheConf
$wgLocalisationCacheConf
Localisation cache configuration.
Definition: DefaultSettings.php:2745
WikiRevision\getContent
getContent( $role=SlotRecord::MAIN)
Definition: WikiRevision.php:493
DumpIterator\checkOptions
checkOptions()
Stub function for processing additional options.
Definition: dumpIterator.php:152
SearchDump\processRevision
processRevision(WikiRevision $rev)
Definition: dumpIterator.php:189
DumpIterator\processRevision
processRevision(WikiRevision $rev)
Core function which does whatever the maintenance script is designed to do.
DumpIterator\disableInterwikis
static disableInterwikis( $prefix, &$data)
Definition: dumpIterator.php:115
Maintenance\DB_NONE
const DB_NONE
Constants for DB access type.
Definition: Maintenance.php:60
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:281
WikiRevision
Represents a revision, log entry or upload during the import process.
Definition: WikiRevision.php:39
$source
$source
Definition: mwdoc-filter.php:34
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:463
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:104
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option exists.
Definition: Maintenance.php:266
DumpIterator\$from
string bool null $from
Definition: dumpIterator.php:40
DumpIterator\conclusions
conclusions()
Stub function for giving data about what was computed.
Definition: dumpIterator.php:158