dumpIterator.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  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.
  6. *
  7. * Copyright © 2011 Platonides
  8. * https://www.mediawiki.org/
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License along
  21. * with this program; if not, write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  23. * http://www.gnu.org/copyleft/gpl.html
  24. *
  25. * @file
  26. * @ingroup Maintenance
  27. */
  28. require_once __DIR__ . '/Maintenance.php';
  29. /**
  30. * Base class for interating over a dump.
  31. *
  32. * @ingroup Maintenance
  33. */
  34. abstract class DumpIterator extends Maintenance {
  35. private $count = 0;
  36. private $startTime;
  37. public function __construct() {
  38. parent::__construct();
  39. $this->addDescription( 'Does something with a dump' );
  40. $this->addOption( 'file', 'File with text to run.', false, true );
  41. $this->addOption( 'dump', 'XML dump to execute all revisions.', false, true );
  42. $this->addOption( 'from', 'Article from XML dump to start from.', false, true );
  43. }
  44. public function execute() {
  45. if ( !( $this->hasOption( 'file' ) ^ $this->hasOption( 'dump' ) ) ) {
  46. $this->error( "You must provide a file or dump", true );
  47. }
  48. $this->checkOptions();
  49. if ( $this->hasOption( 'file' ) ) {
  50. $revision = new WikiRevision( $this->getConfig() );
  51. $revision->setText( file_get_contents( $this->getOption( 'file' ) ) );
  52. $revision->setTitle( Title::newFromText(
  53. rawurldecode( basename( $this->getOption( 'file' ), '.txt' ) )
  54. ) );
  55. $this->handleRevision( $revision );
  56. return;
  57. }
  58. $this->startTime = microtime( true );
  59. if ( $this->getOption( 'dump' ) == '-' ) {
  60. $source = new ImportStreamSource( $this->getStdin() );
  61. } else {
  62. $this->error( "Sorry, I don't support dump filenames yet. "
  63. . "Use - and provide it on stdin on the meantime.", true );
  64. }
  65. $importer = new WikiImporter( $source, $this->getConfig() );
  66. $importer->setRevisionCallback(
  67. [ $this, 'handleRevision' ] );
  68. $this->from = $this->getOption( 'from', null );
  69. $this->count = 0;
  70. $importer->doImport();
  71. $this->conclusions();
  72. $delta = microtime( true ) - $this->startTime;
  73. $this->error( "Done {$this->count} revisions in " . round( $delta, 2 ) . " seconds " );
  74. if ( $delta > 0 ) {
  75. $this->error( round( $this->count / $delta, 2 ) . " pages/sec" );
  76. }
  77. # Perform the memory_get_peak_usage() when all the other data has been
  78. # output so there's no damage if it dies. It is only available since
  79. # 5.2.0 (since 5.2.1 if you haven't compiled with --enable-memory-limit)
  80. $this->error( "Memory peak usage of " . memory_get_peak_usage() . " bytes\n" );
  81. }
  82. public function finalSetup() {
  83. parent::finalSetup();
  84. if ( $this->getDbType() == Maintenance::DB_NONE ) {
  85. global $wgUseDatabaseMessages, $wgLocalisationCacheConf, $wgHooks;
  86. $wgUseDatabaseMessages = false;
  87. $wgLocalisationCacheConf['storeClass'] = 'LCStoreNull';
  88. $wgHooks['InterwikiLoadPrefix'][] = 'DumpIterator::disableInterwikis';
  89. }
  90. }
  91. static function disableInterwikis( $prefix, &$data ) {
  92. # Title::newFromText will check on each namespaced article if it's an interwiki.
  93. # We always answer that it is not.
  94. return false;
  95. }
  96. /**
  97. * Callback function for each revision, child classes should override
  98. * processRevision instead.
  99. * @param Database $rev
  100. */
  101. public function handleRevision( $rev ) {
  102. $title = $rev->getTitle();
  103. if ( !$title ) {
  104. $this->error( "Got bogus revision with null title!" );
  105. return;
  106. }
  107. $this->count++;
  108. if ( isset( $this->from ) ) {
  109. if ( $this->from != $title ) {
  110. return;
  111. }
  112. $this->output( "Skipped " . ( $this->count - 1 ) . " pages\n" );
  113. $this->count = 1;
  114. $this->from = null;
  115. }
  116. $this->processRevision( $rev );
  117. }
  118. /* Stub function for processing additional options */
  119. public function checkOptions() {
  120. return;
  121. }
  122. /* Stub function for giving data about what was computed */
  123. public function conclusions() {
  124. return;
  125. }
  126. /* Core function which does whatever the maintenance script is designed to do */
  127. abstract public function processRevision( $rev );
  128. }
  129. /**
  130. * Maintenance script that runs a regex in the revisions from a dump.
  131. *
  132. * @ingroup Maintenance
  133. */
  134. class SearchDump extends DumpIterator {
  135. public function __construct() {
  136. parent::__construct();
  137. $this->addDescription( 'Runs a regex in the revisions from a dump' );
  138. $this->addOption( 'regex', 'Searching regex', true, true );
  139. }
  140. public function getDbType() {
  141. return Maintenance::DB_NONE;
  142. }
  143. /**
  144. * @param Revision $rev
  145. */
  146. public function processRevision( $rev ) {
  147. if ( preg_match( $this->getOption( 'regex' ), $rev->getContent()->getTextForSearchIndex() ) ) {
  148. $this->output( $rev->getTitle() . " matches at edit from " . $rev->getTimestamp() . "\n" );
  149. }
  150. }
  151. }
  152. $maintClass = "SearchDump";
  153. require_once RUN_MAINTENANCE_IF_MAIN;