r88555 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r88554‎ | r88555 | r88556 >
Date:23:22, 21 May 2011
Author:demon
Status:ok (Comments)
Tags:
Comment:
Report importDump to subclass Maintenance
Modified paths:
  • /trunk/phase3/maintenance/importDump.php (modified) (history)

Diff [purge]

Index: trunk/phase3/maintenance/importDump.php
@@ -22,29 +22,76 @@
2323 * @ingroup Maintenance
2424 */
2525
26 -$optionsWithArgs = array( 'report', 'namespaces' );
 26+require_once( dirname( __FILE__ ) . '/Maintenance.php' );
2727
28 -require_once( dirname( __FILE__ ) . '/commandLine.inc' );
29 -
3028 /**
3129 * @ingroup Maintenance
32 - * @todo port to Maintenance class
3330 */
34 -class BackupReader {
 31+class BackupReader extends Maintenance {
3532 var $reportingInterval = 100;
36 - var $reporting = true;
3733 var $pageCount = 0;
3834 var $revCount = 0;
3935 var $dryRun = false;
40 - var $debug = false;
4136 var $uploads = false;
4237 var $imageBasePath = false;
4338 var $nsFilter = false;
4439
4540 function __construct() {
 41+ parent::__construct();
 42+ $gz = in_array('compress.zlib', stream_get_wrappers()) ? 'ok' : '(disabled; requires PHP zlib module)';
 43+ $bz2 = in_array('compress.bzip2', stream_get_wrappers()) ? 'ok' : '(disabled; requires PHP bzip2 module)';
 44+
 45+ $this->mDescription = <<<TEXT
 46+This script reads pages from an XML file as produced from Special:Export or
 47+dumpBackup.php, and saves them into the current wiki.
 48+
 49+Compressed XML files may be read directly:
 50+ .gz $gz
 51+ .bz2 $bz2
 52+ .7z (if 7za executable is in PATH)
 53+
 54+Note that for very large data sets, importDump.php may be slow; there are
 55+alternate methods which can be much faster for full site restoration:
 56+<http://www.mediawiki.org/wiki/Manual:Importing_XML_dumps>
 57+TEXT;
4658 $this->stderr = fopen( "php://stderr", "wt" );
 59+ $this->addOption( 'report',
 60+ 'Report position and speed after every n pages processed', false, true );
 61+ $this->addOption( 'namespaces',
 62+ 'Import only the pages from namespaces belonging to the list of ' .
 63+ 'pipe-separated namespace names or namespace indexes', false, true );
 64+ $this->addOption( 'dry-run', 'Parse dump without actually importing pages' );
 65+ $this->addOption( 'debug', 'Output extra verbose debug information' );
 66+ $this->addOption( 'uploads', 'Process file upload data if included (experimental)' );
 67+ $this->addOption( 'image-base-path', 'Import files from a specified path', false, true );
 68+ $this->addArg( 'file', 'Dump file to import [else use stdin]', false );
4769 }
4870
 71+ public function execute() {
 72+ if( wfReadOnly() ) {
 73+ $this->error( "Wiki is in read-only mode; you'll need to disable it for import to work.", true );
 74+ }
 75+
 76+ $this->reportingInterval = intval( $this->getOption( 'report', 100 ) );
 77+ $this->dryRun = $this->hasOption( 'dry-run' );
 78+ $this->uploads = $this->hasOption( 'uploads' ); // experimental!
 79+ if ( $this->hasOption( 'image-base-path' ) ) {
 80+ $this->imageBasePath = $this->getOption( 'image-base-path' );
 81+ }
 82+ if ( $this->hasOption( 'namespaces' ) ) {
 83+ $this->setNsfilter( explode( '|', $this->getOption( 'namespaces' ) ) );
 84+ }
 85+
 86+ if( $this->hasArg() ) {
 87+ $result = $this->importFromFile( $this->getArg() );
 88+ } else {
 89+ $result = $this->importFromStdin();
 90+ }
 91+
 92+ $this->output( "Done!\n" );
 93+ $this->output( "You might want to run rebuildrecentchanges.php to regenerate RecentChanges\n" );
 94+ }
 95+
4996 function setNsfilter( array $namespaces ) {
5097 if ( count( $namespaces ) == 0 ) {
5198 $this->nsFilter = false;
@@ -62,7 +109,7 @@
63110 if ( strval( $ns ) === $namespace && $wgContLang->getNsText( $ns ) !== false ) {
64111 return $ns;
65112 }
66 - wfDie( "Unknown namespace text / index specified: $namespace\n" );
 113+ $this->error( "Unknown namespace text / index specified: $namespace", true );
67114 }
68115
69116 private function skippedNamespace( $obj ) {
@@ -74,7 +121,7 @@
75122 $ns = $obj->title->getNamespace();
76123 } else {
77124 echo wfBacktrace();
78 - wfDie( "Cannot get namespace of object in " . __METHOD__ . "\n" );
 125+ $this->error( "Cannot get namespace of object in " . __METHOD__, true );
79126 }
80127 return is_array( $this->nsFilter ) && !in_array( $ns, $this->nsFilter );
81128 }
@@ -139,7 +186,7 @@
140187 }
141188
142189 function showReport() {
143 - if ( $this->reporting ) {
 190+ if ( $this->mQuiet ) {
144191 $delta = wfTime() - $this->startTime;
145192 if ( $delta ) {
146193 $rate = sprintf( "%.2f", $this->pageCount / $delta );
@@ -182,8 +229,7 @@
183230 function importFromStdin() {
184231 $file = fopen( 'php://stdin', 'rt' );
185232 if( posix_isatty( $file ) ) {
186 - $this->showHelp();
187 - exit();
 233+ $this->maybeHelp( true );
188234 }
189235 return $this->importFromHandle( $file );
190236 }
@@ -194,7 +240,9 @@
195241 $source = new ImportStreamSource( $handle );
196242 $importer = new WikiImporter( $source );
197243
198 - $importer->setDebug( $this->debug );
 244+ if( $this->hasOption( 'debug' ) ) {
 245+ $importer->setDebug( true );
 246+ }
199247 $importer->setPageCallback( array( &$this, 'reportPage' ) );
200248 $this->importCallback = $importer->setRevisionCallback(
201249 array( &$this, 'handleRevision' ) );
@@ -215,74 +263,7 @@
216264
217265 return $importer->doImport();
218266 }
219 -
220 - function showHelp() {
221 - $gz = in_array('compress.zlib', stream_get_wrappers()) ? 'ok' : '(disabled; requires PHP zlib module)';
222 - $bz2 = in_array('compress.bzip2', stream_get_wrappers()) ? 'ok' : '(disabled; requires PHP bzip2 module)';
223 - echo "This script reads pages from an XML file as produced from Special:Export\n";
224 - echo "or dumpBackup.php, and saves them into the current wiki.\n";
225 - echo "\n";
226 - echo "Note that for very large data sets, importDump.php may be slow; there are\n";
227 - echo "alternate methods which can be much faster for full site restoration:\n";
228 - echo "http://www.mediawiki.org/wiki/Manual:Importing_XML_dumps\n";
229 - echo "\n";
230 - echo "Usage: php importDump.php [<options>] [<file>]\n";
231 - echo "If no file is listed, input may be piped from stdin.\n";
232 - echo "\n";
233 - echo "Options:\n";
234 - echo " --quiet Don't dump status reports to stderr.\n";
235 - echo " --report=n Report position and speed after every n pages processed.\n";
236 - echo " --namespaces=a|b|..|z Import only the pages from namespaces belonging to\n";
237 - echo " the list of pipe-separated namespace names or namespace indexes\n";
238 - echo " --dry-run Parse dump without actually importing pages.\n";
239 - echo " --debug Output extra verbose debug information\n";
240 - echo " --uploads Process file upload data if included (experimental)\n";
241 - echo " --image-base-path=path Import files from a specified path\n";
242 - echo "\n";
243 - echo "Compressed XML files may be read directly:\n";
244 - echo " .gz $gz\n";
245 - echo " .bz2 $bz2\n";
246 - echo " .7z (if 7za executable is in PATH)\n";
247 - echo "\n";
248 - }
249267 }
250268
251 -if ( wfReadOnly() ) {
252 - wfDie( "Wiki is in read-only mode; you'll need to disable it for import to work.\n" );
253 -}
254 -
255 -$reader = new BackupReader();
256 -if ( isset( $options['quiet'] ) ) {
257 - $reader->reporting = false;
258 -}
259 -if ( isset( $options['report'] ) ) {
260 - $reader->reportingInterval = intval( $options['report'] );
261 -}
262 -if ( isset( $options['dry-run'] ) ) {
263 - $reader->dryRun = true;
264 -}
265 -if ( isset( $options['debug'] ) ) {
266 - $reader->debug = true;
267 -}
268 -if ( isset( $options['uploads'] ) ) {
269 - $reader->uploads = true; // experimental!
270 -}
271 -if ( isset( $options['image-base-path'] ) ) {
272 - $reader->imageBasePath = $options['image-base-path'];
273 -}
274 -if ( isset( $options['namespaces'] ) ) {
275 - $reader->setNsfilter( explode( '|', $options['namespaces'] ) );
276 -}
277 -
278 -if ( isset( $options['help'] ) ) {
279 - $reader->showHelp();
280 - exit();
281 -} elseif ( isset( $args[0] ) ) {
282 - $result = $reader->importFromFile( $args[0] );
283 -} else {
284 - $result = $reader->importFromStdin();
285 -}
286 -
287 -echo "Done!\n";
288 -echo "You might want to run rebuildrecentchanges.php to regenerate\n";
289 -echo "the recentchanges page.\n";
 269+$maintClass = 'BackupReader';
 270+require_once( RUN_MAINTENANCE_IF_MAIN );

Comments

#Comment by 😂 (talk | contribs)   23:23, 21 May 2011

s/Report/Rewrite/

Status & tagging log