MediaWiki  master
dumpIterator.php
Go to the documentation of this file.
1 <?php
33 
34 require_once __DIR__ . '/Maintenance.php';
35 
41 abstract class DumpIterator extends Maintenance {
43  private $count = 0;
45  private $startTime;
47  private $from;
48 
49  public function __construct() {
50  parent::__construct();
51  $this->addDescription( 'Does something with a dump' );
52  $this->addOption( 'file', 'File with text to run.', false, true );
53  $this->addOption( 'dump', 'XML dump to execute all revisions.', false, true );
54  $this->addOption( 'from', 'Article from XML dump to start from.', false, true );
55  }
56 
57  public function execute() {
58  if ( !( $this->hasOption( 'file' ) xor $this->hasOption( 'dump' ) ) ) {
59  $this->fatalError( "You must provide a file or dump" );
60  }
61 
62  $this->checkOptions();
63 
64  if ( $this->hasOption( 'file' ) ) {
65  $file = $this->getOption( 'file' );
66  $revision = new WikiRevision();
67  $text = file_get_contents( $file );
68  $title = Title::newFromText( rawurldecode( basename( $file, '.txt' ) ) );
69  $revision->setTitle( $title );
70  $content = ContentHandler::makeContent( $text, $title );
71  $revision->setContent( SlotRecord::MAIN, $content );
72 
73  $this->from = false;
74  $this->handleRevision( $revision );
75 
76  return;
77  }
78 
79  $this->startTime = microtime( true );
80 
81  if ( $this->getOption( 'dump' ) == '-' ) {
82  $source = new ImportStreamSource( $this->getStdin() );
83  } else {
84  $this->fatalError( "Sorry, I don't support dump filenames yet. "
85  . "Use - and provide it on stdin on the meantime." );
86  }
87 
88  $importer = $this->getServiceContainer()
89  ->getWikiImporterFactory()
90  ->getWikiImporter( $source );
91 
92  $importer->setRevisionCallback(
93  [ $this, 'handleRevision' ] );
94  $importer->setNoticeCallback( static function ( $msg, $params ) {
95  echo wfMessage( $msg, $params )->text() . "\n";
96  } );
97 
98  $this->from = $this->getOption( 'from', null );
99  $this->count = 0;
100  $importer->doImport();
101 
102  $this->conclusions();
103 
104  $delta = microtime( true ) - $this->startTime;
105  $this->error( "Done {$this->count} revisions in " . round( $delta, 2 ) . " seconds " );
106  if ( $delta > 0 ) {
107  $this->error( round( $this->count / $delta, 2 ) . " pages/sec" );
108  }
109 
110  # Perform the memory_get_peak_usage() when all the other data has been
111  # output so there's no damage if it dies. It is only available since
112  # 5.2.0 (since 5.2.1 if you haven't compiled with --enable-memory-limit)
113  $this->error( "Memory peak usage of " . memory_get_peak_usage() . " bytes\n" );
114  }
115 
116  public function finalSetup( SettingsBuilder $settingsBuilder = null ) {
117  parent::finalSetup( $settingsBuilder );
118 
119  if ( $this->getDbType() == Maintenance::DB_NONE ) {
120  // TODO: Allow hooks to be registered via SettingsBuilder as well!
121  // This matches the idea of unifying SettingsBuilder with ExtensionRegistry.
122  // phpcs:disable MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgHooks
123  global $wgHooks;
124  $wgHooks['InterwikiLoadPrefix'][] = 'DumpIterator::disableInterwikis';
125 
126  $settingsBuilder->putConfigValues( [
127  MainConfigNames::UseDatabaseMessages => false,
128  MainConfigNames::LocalisationCacheConf => [ 'storeClass' => LCStoreNull::class ],
129  ] );
130  }
131  }
132 
133  public static function disableInterwikis( $prefix, &$data ) {
134  # Title::newFromText will check on each namespaced article if it's an interwiki.
135  # We always answer that it is not.
136 
137  return false;
138  }
139 
145  public function handleRevision( $rev ) {
146  $title = $rev->getTitle();
147  if ( !$title ) {
148  $this->error( "Got bogus revision with null title!" );
149 
150  return;
151  }
152 
153  $this->count++;
154  if ( $this->from !== false ) {
155  if ( $this->from != $title ) {
156  return;
157  }
158  $this->output( "Skipped " . ( $this->count - 1 ) . " pages\n" );
159 
160  $this->count = 1;
161  $this->from = null;
162  }
163 
164  $this->processRevision( $rev );
165  }
166 
170  public function checkOptions() {
171  }
172 
176  public function conclusions() {
177  }
178 
184  abstract public function processRevision( WikiRevision $rev );
185 }
186 
192 class SearchDump extends DumpIterator {
193 
194  public function __construct() {
195  parent::__construct();
196  $this->addDescription( 'Runs a regex in the revisions from a dump' );
197  $this->addOption( 'regex', 'Searching regex', true, true );
198  }
199 
200  public function getDbType() {
201  return Maintenance::DB_NONE;
202  }
203 
207  public function processRevision( WikiRevision $rev ) {
208  if ( preg_match( $this->getOption( 'regex' ), $rev->getContent()->getTextForSearchIndex() ) ) {
209  $this->output( $rev->getTitle() . " matches at edit from " . $rev->getTimestamp() . "\n" );
210  }
211  }
212 }
213 
214 $maintClass = SearchDump::class;
215 require_once RUN_MAINTENANCE_IF_MAIN;
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
static makeContent( $text, Title $title=null, $modelId=null, $format=null)
Convenience function for creating a Content object from a given textual representation.
Base class for iterating over a dump.
static disableInterwikis( $prefix, &$data)
__construct()
Default constructor.
execute()
Do the actual work.
finalSetup(SettingsBuilder $settingsBuilder=null)
Handle some last-minute setup here.
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.
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...
Definition: Maintenance.php:66
error( $err, $die=0)
Throw an error to the user.
const DB_NONE
Constants for DB access type.
Definition: Maintenance.php:71
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.
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40
Builder class for constructing a Config object from a set of sources during bootstrap.
Represents a title within MediaWiki.
Definition: Title.php:76
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
$content
Definition: router.php:76
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42