MediaWiki REL1_30
dumpIterator.php
Go to the documentation of this file.
1<?php
29require_once __DIR__ . '/Maintenance.php';
30
36abstract 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->error( "You must provide a file or dump", true );
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->error( "Sorry, I don't support dump filenames yet. "
74 . "Use - and provide it on stdin on the meantime.", true );
75 }
76 $importer = new WikiImporter( $source, $this->getConfig() );
77
78 $importer->setRevisionCallback(
79 [ $this, 'handleRevision' ] );
80
81 $this->from = $this->getOption( 'from', null );
82 $this->count = 0;
83 $importer->doImport();
84
85 $this->conclusions();
86
87 $delta = microtime( true ) - $this->startTime;
88 $this->error( "Done {$this->count} revisions in " . round( $delta, 2 ) . " seconds " );
89 if ( $delta > 0 ) {
90 $this->error( round( $this->count / $delta, 2 ) . " pages/sec" );
91 }
92
93 # Perform the memory_get_peak_usage() when all the other data has been
94 # output so there's no damage if it dies. It is only available since
95 # 5.2.0 (since 5.2.1 if you haven't compiled with --enable-memory-limit)
96 $this->error( "Memory peak usage of " . memory_get_peak_usage() . " bytes\n" );
97 }
98
99 public function finalSetup() {
100 parent::finalSetup();
101
102 if ( $this->getDbType() == Maintenance::DB_NONE ) {
105 $wgLocalisationCacheConf['storeClass'] = 'LCStoreNull';
106 $wgHooks['InterwikiLoadPrefix'][] = 'DumpIterator::disableInterwikis';
107 }
108 }
109
110 static function disableInterwikis( $prefix, &$data ) {
111 # Title::newFromText will check on each namespaced article if it's an interwiki.
112 # We always answer that it is not.
113
114 return false;
115 }
116
122 public function handleRevision( $rev ) {
123 $title = $rev->getTitle();
124 if ( !$title ) {
125 $this->error( "Got bogus revision with null title!" );
126
127 return;
128 }
129
130 $this->count++;
131 if ( isset( $this->from ) ) {
132 if ( $this->from != $title ) {
133 return;
134 }
135 $this->output( "Skipped " . ( $this->count - 1 ) . " pages\n" );
136
137 $this->count = 1;
138 $this->from = null;
139 }
140
141 $this->processRevision( $rev );
142 }
143
144 /* Stub function for processing additional options */
145 public function checkOptions() {
146 return;
147 }
148
149 /* Stub function for giving data about what was computed */
150 public function conclusions() {
151 return;
152 }
153
154 /* Core function which does whatever the maintenance script is designed to do */
155 abstract public function processRevision( $rev );
156}
157
164
165 public function __construct() {
166 parent::__construct();
167 $this->addDescription( 'Runs a regex in the revisions from a dump' );
168 $this->addOption( 'regex', 'Searching regex', true, true );
169 }
170
171 public function getDbType() {
173 }
174
178 public function processRevision( $rev ) {
179 if ( preg_match( $this->getOption( 'regex' ), $rev->getContent()->getTextForSearchIndex() ) ) {
180 $this->output( $rev->getTitle() . " matches at edit from " . $rev->getTimestamp() . "\n" );
181 }
182 }
183}
184
185$maintClass = "SearchDump";
186require_once RUN_MAINTENANCE_IF_MAIN;
$wgUseDatabaseMessages
Translation using MediaWiki: namespace.
$wgLocalisationCacheConf
Localisation cache configuration.
Base class for interating over a dump.
static disableInterwikis( $prefix, &$data)
__construct()
Default constructor.
execute()
Do the actual work.
processRevision( $rev)
finalSetup()
Handle some last-minute setup here.
handleRevision( $rev)
Callback function for each revision, child classes should override processRevision instead.
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...
const DB_NONE
Constants for DB access type.
getStdin( $len=null)
Return input from stdin.
hasOption( $name)
Checks to see if a particular param exists.
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.
Maintenance script that runs a regex in the revisions from a dump.
__construct()
Default constructor.
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
processRevision( $rev)
XML file reader for the page data importer.
Represents a revision, log entry or upload during the import process.
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling output() to send it all. It could be easily changed to send incrementally if that becomes useful
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults error
Definition hooks.txt:2581
$wgHooks['ArticleShow'][]
Definition hooks.txt:108
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:1760
$maintClass
require_once RUN_MAINTENANCE_IF_MAIN
$source