Index: trunk/phase3/maintenance/importDump.php |
— | — | @@ -22,29 +22,76 @@ |
23 | 23 | * @ingroup Maintenance |
24 | 24 | */ |
25 | 25 | |
26 | | -$optionsWithArgs = array( 'report', 'namespaces' ); |
| 26 | +require_once( dirname( __FILE__ ) . '/Maintenance.php' ); |
27 | 27 | |
28 | | -require_once( dirname( __FILE__ ) . '/commandLine.inc' ); |
29 | | - |
30 | 28 | /** |
31 | 29 | * @ingroup Maintenance |
32 | | - * @todo port to Maintenance class |
33 | 30 | */ |
34 | | -class BackupReader { |
| 31 | +class BackupReader extends Maintenance { |
35 | 32 | var $reportingInterval = 100; |
36 | | - var $reporting = true; |
37 | 33 | var $pageCount = 0; |
38 | 34 | var $revCount = 0; |
39 | 35 | var $dryRun = false; |
40 | | - var $debug = false; |
41 | 36 | var $uploads = false; |
42 | 37 | var $imageBasePath = false; |
43 | 38 | var $nsFilter = false; |
44 | 39 | |
45 | 40 | 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; |
46 | 58 | $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 ); |
47 | 69 | } |
48 | 70 | |
| 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 | + |
49 | 96 | function setNsfilter( array $namespaces ) { |
50 | 97 | if ( count( $namespaces ) == 0 ) { |
51 | 98 | $this->nsFilter = false; |
— | — | @@ -62,7 +109,7 @@ |
63 | 110 | if ( strval( $ns ) === $namespace && $wgContLang->getNsText( $ns ) !== false ) { |
64 | 111 | return $ns; |
65 | 112 | } |
66 | | - wfDie( "Unknown namespace text / index specified: $namespace\n" ); |
| 113 | + $this->error( "Unknown namespace text / index specified: $namespace", true ); |
67 | 114 | } |
68 | 115 | |
69 | 116 | private function skippedNamespace( $obj ) { |
— | — | @@ -74,7 +121,7 @@ |
75 | 122 | $ns = $obj->title->getNamespace(); |
76 | 123 | } else { |
77 | 124 | echo wfBacktrace(); |
78 | | - wfDie( "Cannot get namespace of object in " . __METHOD__ . "\n" ); |
| 125 | + $this->error( "Cannot get namespace of object in " . __METHOD__, true ); |
79 | 126 | } |
80 | 127 | return is_array( $this->nsFilter ) && !in_array( $ns, $this->nsFilter ); |
81 | 128 | } |
— | — | @@ -139,7 +186,7 @@ |
140 | 187 | } |
141 | 188 | |
142 | 189 | function showReport() { |
143 | | - if ( $this->reporting ) { |
| 190 | + if ( $this->mQuiet ) { |
144 | 191 | $delta = wfTime() - $this->startTime; |
145 | 192 | if ( $delta ) { |
146 | 193 | $rate = sprintf( "%.2f", $this->pageCount / $delta ); |
— | — | @@ -182,8 +229,7 @@ |
183 | 230 | function importFromStdin() { |
184 | 231 | $file = fopen( 'php://stdin', 'rt' ); |
185 | 232 | if( posix_isatty( $file ) ) { |
186 | | - $this->showHelp(); |
187 | | - exit(); |
| 233 | + $this->maybeHelp( true ); |
188 | 234 | } |
189 | 235 | return $this->importFromHandle( $file ); |
190 | 236 | } |
— | — | @@ -194,7 +240,9 @@ |
195 | 241 | $source = new ImportStreamSource( $handle ); |
196 | 242 | $importer = new WikiImporter( $source ); |
197 | 243 | |
198 | | - $importer->setDebug( $this->debug ); |
| 244 | + if( $this->hasOption( 'debug' ) ) { |
| 245 | + $importer->setDebug( true ); |
| 246 | + } |
199 | 247 | $importer->setPageCallback( array( &$this, 'reportPage' ) ); |
200 | 248 | $this->importCallback = $importer->setRevisionCallback( |
201 | 249 | array( &$this, 'handleRevision' ) ); |
— | — | @@ -215,74 +263,7 @@ |
216 | 264 | |
217 | 265 | return $importer->doImport(); |
218 | 266 | } |
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 | | - } |
249 | 267 | } |
250 | 268 | |
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 ); |