MediaWiki  REL1_24
dumpIterator.php
Go to the documentation of this file.
00001 <?php
00029 require_once __DIR__ . '/Maintenance.php';
00030 
00036 abstract class DumpIterator extends Maintenance {
00037 
00038     private $count = 0;
00039     private $startTime;
00040 
00041     public function __construct() {
00042         parent::__construct();
00043         $this->mDescription = "Does something with a dump";
00044         $this->addOption( 'file', 'File with text to run.', false, true );
00045         $this->addOption( 'dump', 'XML dump to execute all revisions.', false, true );
00046         $this->addOption( 'from', 'Article from XML dump to start from.', false, true );
00047     }
00048 
00049     public function execute() {
00050         if ( !( $this->hasOption( 'file' ) ^ $this->hasOption( 'dump' ) ) ) {
00051             $this->error( "You must provide a file or dump", true );
00052         }
00053 
00054         $this->checkOptions();
00055 
00056         if ( $this->hasOption( 'file' ) ) {
00057             $revision = new WikiRevision;
00058 
00059             $revision->setText( file_get_contents( $this->getOption( 'file' ) ) );
00060             $revision->setTitle( Title::newFromText(
00061                 rawurldecode( basename( $this->getOption( 'file' ), '.txt' ) )
00062             ) );
00063             $this->handleRevision( $revision );
00064 
00065             return;
00066         }
00067 
00068         $this->startTime = microtime( true );
00069 
00070         if ( $this->getOption( 'dump' ) == '-' ) {
00071             $source = new ImportStreamSource( $this->getStdin() );
00072         } else {
00073             $this->error( "Sorry, I don't support dump filenames yet. "
00074                 . "Use - and provide it on stdin on the meantime.", true );
00075         }
00076         $importer = new WikiImporter( $source );
00077 
00078         $importer->setRevisionCallback(
00079             array( &$this, 'handleRevision' ) );
00080 
00081         $this->from = $this->getOption( 'from', null );
00082         $this->count = 0;
00083         $importer->doImport();
00084 
00085         $this->conclusions();
00086 
00087         $delta = microtime( true ) - $this->startTime;
00088         $this->error( "Done {$this->count} revisions in " . round( $delta, 2 ) . " seconds " );
00089         if ( $delta > 0 ) {
00090             $this->error( round( $this->count / $delta, 2 ) . " pages/sec" );
00091         }
00092 
00093         # Perform the memory_get_peak_usage() when all the other data has been
00094         # output so there's no damage if it dies. It is only available since
00095         # 5.2.0 (since 5.2.1 if you haven't compiled with --enable-memory-limit)
00096         $this->error( "Memory peak usage of " . memory_get_peak_usage() . " bytes\n" );
00097     }
00098 
00099     public function finalSetup() {
00100         parent::finalSetup();
00101 
00102         if ( $this->getDbType() == Maintenance::DB_NONE ) {
00103             global $wgUseDatabaseMessages, $wgLocalisationCacheConf, $wgHooks;
00104             $wgUseDatabaseMessages = false;
00105             $wgLocalisationCacheConf['storeClass'] = 'LCStoreNull';
00106             $wgHooks['InterwikiLoadPrefix'][] = 'DumpIterator::disableInterwikis';
00107         }
00108     }
00109 
00110     static function disableInterwikis( $prefix, &$data ) {
00111         # Title::newFromText will check on each namespaced article if it's an interwiki.
00112         # We always answer that it is not.
00113 
00114         return false;
00115     }
00116 
00122     public function handleRevision( $rev ) {
00123         $title = $rev->getTitle();
00124         if ( !$title ) {
00125             $this->error( "Got bogus revision with null title!" );
00126 
00127             return;
00128         }
00129 
00130         $this->count++;
00131         if ( isset( $this->from ) ) {
00132             if ( $this->from != $title ) {
00133                 return;
00134             }
00135             $this->output( "Skipped " . ( $this->count - 1 ) . " pages\n" );
00136 
00137             $this->count = 1;
00138             $this->from = null;
00139         }
00140 
00141         $this->processRevision( $rev );
00142     }
00143 
00144     /* Stub function for processing additional options */
00145     public function checkOptions() {
00146         return;
00147     }
00148 
00149     /* Stub function for giving data about what was computed */
00150     public function conclusions() {
00151         return;
00152     }
00153 
00154     /* Core function which does whatever the maintenance script is designed to do */
00155     abstract public function processRevision( $rev );
00156 }
00157 
00163 class SearchDump extends DumpIterator {
00164 
00165     public function __construct() {
00166         parent::__construct();
00167         $this->mDescription = "Runs a regex in the revisions from a dump";
00168         $this->addOption( 'regex', 'Searching regex', true, true );
00169     }
00170 
00171     public function getDbType() {
00172         return Maintenance::DB_NONE;
00173     }
00174 
00178     public function processRevision( $rev ) {
00179         if ( preg_match( $this->getOption( 'regex' ), $rev->getContent()->getTextForSearchIndex() ) ) {
00180             $this->output( $rev->getTitle() . " matches at edit from " . $rev->getTimestamp() . "\n" );
00181         }
00182     }
00183 }
00184 
00185 $maintClass = "SearchDump";
00186 require_once RUN_MAINTENANCE_IF_MAIN;