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