3 * Take page text out of an XML dump file and perform some operation on it.
4 * Used as a base class for CompareParsers and PreprocessDump.
5 * We implement below the simple task of searching inside a dump.
7 * Copyright (C) 2011 Platonides - http://www.mediawiki.org/
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
25 * @ingroup Maintenance
28 require_once( dirname( __FILE__
) . '/Maintenance.php' );
30 abstract class DumpIterator
extends Maintenance
{
35 public function __construct() {
36 parent
::__construct();
37 $this->mDescription
= "Does something with a dump";
38 $this->addOption( 'file', 'File with text to run.', false, true );
39 $this->addOption( 'dump', 'XML dump to execute all revisions.', false, true );
40 $this->addOption( 'from', 'Article from XML dump to start from.', false, true );
43 public function execute() {
44 if (! ( $this->hasOption('file') ^
$this->hasOption('dump') ) ) {
45 $this->error("You must provide a file or dump", true);
48 $this->checkOptions();
50 if ( $this->hasOption('file') ) {
51 $revision = new WikiRevision
;
53 $revision->setText( file_get_contents( $this->getOption( 'file' ) ) );
54 $revision->setTitle( Title
::newFromText( rawurldecode( basename( $this->getOption( 'file' ), '.txt' ) ) ) );
55 $this->handleRevision( $revision );
59 $this->startTime
= wfTime();
61 if ( $this->getOption('dump') == '-' ) {
62 $source = new ImportStreamSource( $this->getStdin() );
64 $this->error("Sorry, I don't support dump filenames yet. Use - and provide it on stdin on the meantime.", true);
66 $importer = new WikiImporter( $source );
68 $importer->setRevisionCallback(
69 array( &$this, 'handleRevision' ) );
71 $this->from
= $this->getOption( 'from', null );
73 $importer->doImport();
77 $delta = wfTime() - $this->startTime
;
78 $this->error( "Done {$this->count} revisions in " . round($delta, 2) . " seconds " );
80 $this->error( round($this->count
/ $delta, 2) . " pages/sec" );
82 # Perform the memory_get_peak_usage() when all the other data has been output so there's no damage if it dies.
83 # It is only available since 5.2.0 (since 5.2.1 if you haven't compiled with --enable-memory-limit)
84 $this->error( "Memory peak usage of " . memory_get_peak_usage() . " bytes\n" );
87 function stripParameters( $text ) {
88 if ( !$this->stripParametersEnabled
) {
91 return preg_replace( '/(<a) [^>]+>/', '$1>', $text );
95 * Callback function for each revision, child classes should override
96 * processRevision instead.
97 * @param $rev Revision
99 public function handleRevision( $rev ) {
100 $title = $rev->getTitle();
102 $this->error( "Got bogus revision with null title!" );
107 if ( isset( $this->from
) ) {
108 if ( $this->from
!= $title )
110 $this->output( "Skipped " . ($this->count
- 1) . " pages\n" );
116 $this->processRevision( $rev );
119 /* Stub function for processing additional options */
120 public function checkOptions() {
124 /* Stub function for giving data about what was computed */
125 public function conclusions() {
129 /* Core function which does whatever the maintenance script is designed to do */
130 abstract public function processRevision( $rev );
133 class SearchDump
extends DumpIterator
{
135 public function __construct() {
136 parent
::__construct();
137 $this->mDescription
= "Runs a regex in the revisions from a dump";
138 $this->addOption( 'regex', 'Searching regex', true, true );
141 public function processRevision( $rev ) {
142 if ( preg_match( $this->getOption( 'regex' ), $rev->getText() ) ) {
143 $this->output( $rev->getTitle() . " matches at edit from " . $rev->getTimestamp() . "\n" );
148 $maintClass = "SearchDump";
149 require_once( RUN_MAINTENANCE_IF_MAIN
);