[ Index ] |
PHP Cross Reference of MediaWiki-1.24.0 |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Import XML dump files into the current wiki. 4 * 5 * Copyright © 2005 Brion Vibber <[email protected]> 6 * https://www.mediawiki.org/ 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License along 19 * with this program; if not, write to the Free Software Foundation, Inc., 20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 21 * http://www.gnu.org/copyleft/gpl.html 22 * 23 * @file 24 * @ingroup Maintenance 25 */ 26 27 require_once __DIR__ . '/Maintenance.php'; 28 29 /** 30 * Maintenance script that imports XML dump files into the current wiki. 31 * 32 * @ingroup Maintenance 33 */ 34 class BackupReader extends Maintenance { 35 public $reportingInterval = 100; 36 public $pageCount = 0; 37 public $revCount = 0; 38 public $dryRun = false; 39 public $uploads = false; 40 public $imageBasePath = false; 41 public $nsFilter = false; 42 43 function __construct() { 44 parent::__construct(); 45 $gz = in_array( 'compress.zlib', stream_get_wrappers() ) 46 ? 'ok' 47 : '(disabled; requires PHP zlib module)'; 48 $bz2 = in_array( 'compress.bzip2', stream_get_wrappers() ) 49 ? 'ok' 50 : '(disabled; requires PHP bzip2 module)'; 51 52 $this->mDescription = <<<TEXT 53 This script reads pages from an XML file as produced from Special:Export or 54 dumpBackup.php, and saves them into the current wiki. 55 56 Compressed XML files may be read directly: 57 .gz $gz 58 .bz2 $bz2 59 .7z (if 7za executable is in PATH) 60 61 Note that for very large data sets, importDump.php may be slow; there are 62 alternate methods which can be much faster for full site restoration: 63 <https://www.mediawiki.org/wiki/Manual:Importing_XML_dumps> 64 TEXT; 65 $this->stderr = fopen( "php://stderr", "wt" ); 66 $this->addOption( 'report', 67 'Report position and speed after every n pages processed', false, true ); 68 $this->addOption( 'namespaces', 69 'Import only the pages from namespaces belonging to the list of ' . 70 'pipe-separated namespace names or namespace indexes', false, true ); 71 $this->addOption( 'dry-run', 'Parse dump without actually importing pages' ); 72 $this->addOption( 'debug', 'Output extra verbose debug information' ); 73 $this->addOption( 'uploads', 'Process file upload data if included (experimental)' ); 74 $this->addOption( 75 'no-updates', 76 'Disable link table updates. Is faster but leaves the wiki in an inconsistent state' 77 ); 78 $this->addOption( 'image-base-path', 'Import files from a specified path', false, true ); 79 $this->addArg( 'file', 'Dump file to import [else use stdin]', false ); 80 } 81 82 public function execute() { 83 if ( wfReadOnly() ) { 84 $this->error( "Wiki is in read-only mode; you'll need to disable it for import to work.", true ); 85 } 86 87 $this->reportingInterval = intval( $this->getOption( 'report', 100 ) ); 88 if ( !$this->reportingInterval ) { 89 $this->reportingInterval = 100; // avoid division by zero 90 } 91 92 $this->dryRun = $this->hasOption( 'dry-run' ); 93 $this->uploads = $this->hasOption( 'uploads' ); // experimental! 94 if ( $this->hasOption( 'image-base-path' ) ) { 95 $this->imageBasePath = $this->getOption( 'image-base-path' ); 96 } 97 if ( $this->hasOption( 'namespaces' ) ) { 98 $this->setNsfilter( explode( '|', $this->getOption( 'namespaces' ) ) ); 99 } 100 101 if ( $this->hasArg() ) { 102 $this->importFromFile( $this->getArg() ); 103 } else { 104 $this->importFromStdin(); 105 } 106 107 $this->output( "Done!\n" ); 108 $this->output( "You might want to run rebuildrecentchanges.php to regenerate RecentChanges\n" ); 109 } 110 111 function setNsfilter( array $namespaces ) { 112 if ( count( $namespaces ) == 0 ) { 113 $this->nsFilter = false; 114 115 return; 116 } 117 $this->nsFilter = array_unique( array_map( array( $this, 'getNsIndex' ), $namespaces ) ); 118 } 119 120 private function getNsIndex( $namespace ) { 121 global $wgContLang; 122 if ( ( $result = $wgContLang->getNsIndex( $namespace ) ) !== false ) { 123 return $result; 124 } 125 $ns = intval( $namespace ); 126 if ( strval( $ns ) === $namespace && $wgContLang->getNsText( $ns ) !== false ) { 127 return $ns; 128 } 129 $this->error( "Unknown namespace text / index specified: $namespace", true ); 130 } 131 132 /** 133 * @param Title|Revision $obj 134 * @return bool 135 */ 136 private function skippedNamespace( $obj ) { 137 if ( $obj instanceof Title ) { 138 $ns = $obj->getNamespace(); 139 } elseif ( $obj instanceof Revision ) { 140 $ns = $obj->getTitle()->getNamespace(); 141 } elseif ( $obj instanceof WikiRevision ) { 142 $ns = $obj->title->getNamespace(); 143 } else { 144 throw new MWException( "Cannot get namespace of object in " . __METHOD__ ); 145 } 146 147 return is_array( $this->nsFilter ) && !in_array( $ns, $this->nsFilter ); 148 } 149 150 function reportPage( $page ) { 151 $this->pageCount++; 152 } 153 154 /** 155 * @param Revision $rev 156 */ 157 function handleRevision( $rev ) { 158 $title = $rev->getTitle(); 159 if ( !$title ) { 160 $this->progress( "Got bogus revision with null title!" ); 161 162 return; 163 } 164 165 if ( $this->skippedNamespace( $title ) ) { 166 return; 167 } 168 169 $this->revCount++; 170 $this->report(); 171 172 if ( !$this->dryRun ) { 173 call_user_func( $this->importCallback, $rev ); 174 } 175 } 176 177 /** 178 * @param Revision $revision 179 * @return bool 180 */ 181 function handleUpload( $revision ) { 182 if ( $this->uploads ) { 183 if ( $this->skippedNamespace( $revision ) ) { 184 return false; 185 } 186 $this->uploadCount++; 187 // $this->report(); 188 $this->progress( "upload: " . $revision->getFilename() ); 189 190 if ( !$this->dryRun ) { 191 // bluuuh hack 192 // call_user_func( $this->uploadCallback, $revision ); 193 $dbw = wfGetDB( DB_MASTER ); 194 195 return $dbw->deadlockLoop( array( $revision, 'importUpload' ) ); 196 } 197 } 198 199 return false; 200 } 201 202 function handleLogItem( $rev ) { 203 if ( $this->skippedNamespace( $rev ) ) { 204 return; 205 } 206 $this->revCount++; 207 $this->report(); 208 209 if ( !$this->dryRun ) { 210 call_user_func( $this->logItemCallback, $rev ); 211 } 212 } 213 214 function report( $final = false ) { 215 if ( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) { 216 $this->showReport(); 217 } 218 } 219 220 function showReport() { 221 if ( !$this->mQuiet ) { 222 $delta = microtime( true ) - $this->startTime; 223 if ( $delta ) { 224 $rate = sprintf( "%.2f", $this->pageCount / $delta ); 225 $revrate = sprintf( "%.2f", $this->revCount / $delta ); 226 } else { 227 $rate = '-'; 228 $revrate = '-'; 229 } 230 # Logs dumps don't have page tallies 231 if ( $this->pageCount ) { 232 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" ); 233 } else { 234 $this->progress( "$this->revCount ($revrate revs/sec)" ); 235 } 236 } 237 wfWaitForSlaves(); 238 // XXX: Don't let deferred jobs array get absurdly large (bug 24375) 239 DeferredUpdates::doUpdates( 'commit' ); 240 } 241 242 function progress( $string ) { 243 fwrite( $this->stderr, $string . "\n" ); 244 } 245 246 function importFromFile( $filename ) { 247 if ( preg_match( '/\.gz$/', $filename ) ) { 248 $filename = 'compress.zlib://' . $filename; 249 } elseif ( preg_match( '/\.bz2$/', $filename ) ) { 250 $filename = 'compress.bzip2://' . $filename; 251 } elseif ( preg_match( '/\.7z$/', $filename ) ) { 252 $filename = 'mediawiki.compress.7z://' . $filename; 253 } 254 255 $file = fopen( $filename, 'rt' ); 256 257 return $this->importFromHandle( $file ); 258 } 259 260 function importFromStdin() { 261 $file = fopen( 'php://stdin', 'rt' ); 262 if ( self::posix_isatty( $file ) ) { 263 $this->maybeHelp( true ); 264 } 265 266 return $this->importFromHandle( $file ); 267 } 268 269 function importFromHandle( $handle ) { 270 $this->startTime = microtime( true ); 271 272 $source = new ImportStreamSource( $handle ); 273 $importer = new WikiImporter( $source ); 274 275 if ( $this->hasOption( 'debug' ) ) { 276 $importer->setDebug( true ); 277 } 278 if ( $this->hasOption( 'no-updates' ) ) { 279 $importer->setNoUpdates( true ); 280 } 281 $importer->setPageCallback( array( &$this, 'reportPage' ) ); 282 $this->importCallback = $importer->setRevisionCallback( 283 array( &$this, 'handleRevision' ) ); 284 $this->uploadCallback = $importer->setUploadCallback( 285 array( &$this, 'handleUpload' ) ); 286 $this->logItemCallback = $importer->setLogItemCallback( 287 array( &$this, 'handleLogItem' ) ); 288 if ( $this->uploads ) { 289 $importer->setImportUploads( true ); 290 } 291 if ( $this->imageBasePath ) { 292 $importer->setImageBasePath( $this->imageBasePath ); 293 } 294 295 if ( $this->dryRun ) { 296 $importer->setPageOutCallback( null ); 297 } 298 299 return $importer->doImport(); 300 } 301 } 302 303 $maintClass = 'BackupReader'; 304 require_once RUN_MAINTENANCE_IF_MAIN;
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 14:03:12 2014 | Cross-referenced by PHPXref 0.7.1 |