Change usages of $wgUser->getSkin() in special pages to use $this->getSkin()
[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( 'image-base-path', 'Import files from a specified path', false, true );
67 $this->addArg( 'file', 'Dump file to import [else use stdin]', false );
68 }
69
70 public function execute() {
71 if( wfReadOnly() ) {
72 $this->error( "Wiki is in read-only mode; you'll need to disable it for import to work.", true );
73 }
74
75 $this->reportingInterval = intval( $this->getOption( 'report', 100 ) );
76 $this->dryRun = $this->hasOption( 'dry-run' );
77 $this->uploads = $this->hasOption( 'uploads' ); // experimental!
78 if ( $this->hasOption( 'image-base-path' ) ) {
79 $this->imageBasePath = $this->getOption( 'image-base-path' );
80 }
81 if ( $this->hasOption( 'namespaces' ) ) {
82 $this->setNsfilter( explode( '|', $this->getOption( 'namespaces' ) ) );
83 }
84
85 if( $this->hasArg() ) {
86 $this->importFromFile( $this->getArg() );
87 } else {
88 $this->importFromStdin();
89 }
90
91 $this->output( "Done!\n" );
92 $this->output( "You might want to run rebuildrecentchanges.php to regenerate RecentChanges\n" );
93 }
94
95 function setNsfilter( array $namespaces ) {
96 if ( count( $namespaces ) == 0 ) {
97 $this->nsFilter = false;
98 return;
99 }
100 $this->nsFilter = array_unique( array_map( array( $this, 'getNsIndex' ), $namespaces ) );
101 }
102
103 private function getNsIndex( $namespace ) {
104 global $wgContLang;
105 if ( ( $result = $wgContLang->getNsIndex( $namespace ) ) !== false ) {
106 return $result;
107 }
108 $ns = intval( $namespace );
109 if ( strval( $ns ) === $namespace && $wgContLang->getNsText( $ns ) !== false ) {
110 return $ns;
111 }
112 $this->error( "Unknown namespace text / index specified: $namespace", true );
113 }
114
115 private function skippedNamespace( $obj ) {
116 if ( $obj instanceof Title ) {
117 $ns = $obj->getNamespace();
118 } elseif ( $obj instanceof Revision ) {
119 $ns = $obj->getTitle()->getNamespace();
120 } elseif ( $obj instanceof WikiRevision ) {
121 $ns = $obj->title->getNamespace();
122 } else {
123 echo wfBacktrace();
124 $this->error( "Cannot get namespace of object in " . __METHOD__, true );
125 }
126 return is_array( $this->nsFilter ) && !in_array( $ns, $this->nsFilter );
127 }
128
129 function reportPage( $page ) {
130 $this->pageCount++;
131 }
132
133 function handleRevision( $rev ) {
134 $title = $rev->getTitle();
135 if ( !$title ) {
136 $this->progress( "Got bogus revision with null title!" );
137 return;
138 }
139
140 if ( $this->skippedNamespace( $title ) ) {
141 return;
142 }
143
144 $this->revCount++;
145 $this->report();
146
147 if ( !$this->dryRun ) {
148 call_user_func( $this->importCallback, $rev );
149 }
150 }
151
152 function handleUpload( $revision ) {
153 if ( $this->uploads ) {
154 if ( $this->skippedNamespace( $revision ) ) {
155 return;
156 }
157 $this->uploadCount++;
158 // $this->report();
159 $this->progress( "upload: " . $revision->getFilename() );
160
161 if ( !$this->dryRun ) {
162 // bluuuh hack
163 // call_user_func( $this->uploadCallback, $revision );
164 $dbw = wfGetDB( DB_MASTER );
165 return $dbw->deadlockLoop( array( $revision, 'importUpload' ) );
166 }
167 }
168 }
169
170 function handleLogItem( $rev ) {
171 if ( $this->skippedNamespace( $rev ) ) {
172 return;
173 }
174 $this->revCount++;
175 $this->report();
176
177 if ( !$this->dryRun ) {
178 call_user_func( $this->logItemCallback, $rev );
179 }
180 }
181
182 function report( $final = false ) {
183 if ( $final xor ( $this->pageCount % $this->reportingInterval == 0 ) ) {
184 $this->showReport();
185 }
186 }
187
188 function showReport() {
189 if ( $this->mQuiet ) {
190 $delta = wfTime() - $this->startTime;
191 if ( $delta ) {
192 $rate = sprintf( "%.2f", $this->pageCount / $delta );
193 $revrate = sprintf( "%.2f", $this->revCount / $delta );
194 } else {
195 $rate = '-';
196 $revrate = '-';
197 }
198 # Logs dumps don't have page tallies
199 if ( $this->pageCount ) {
200 $this->progress( "$this->pageCount ($rate pages/sec $revrate revs/sec)" );
201 } else {
202 $this->progress( "$this->revCount ($revrate revs/sec)" );
203 }
204 }
205 wfWaitForSlaves();
206 // XXX: Don't let deferred jobs array get absurdly large (bug 24375)
207 wfDoUpdates( 'commit' );
208 }
209
210 function progress( $string ) {
211 fwrite( $this->stderr, $string . "\n" );
212 }
213
214 function importFromFile( $filename ) {
215 if ( preg_match( '/\.gz$/', $filename ) ) {
216 $filename = 'compress.zlib://' . $filename;
217 }
218 elseif ( preg_match( '/\.bz2$/', $filename ) ) {
219 $filename = 'compress.bzip2://' . $filename;
220 }
221 elseif ( preg_match( '/\.7z$/', $filename ) ) {
222 $filename = 'mediawiki.compress.7z://' . $filename;
223 }
224
225 $file = fopen( $filename, 'rt' );
226 return $this->importFromHandle( $file );
227 }
228
229 function importFromStdin() {
230 $file = fopen( 'php://stdin', 'rt' );
231 if( posix_isatty( $file ) ) {
232 $this->maybeHelp( true );
233 }
234 return $this->importFromHandle( $file );
235 }
236
237 function importFromHandle( $handle ) {
238 $this->startTime = wfTime();
239
240 $source = new ImportStreamSource( $handle );
241 $importer = new WikiImporter( $source );
242
243 if( $this->hasOption( 'debug' ) ) {
244 $importer->setDebug( true );
245 }
246 $importer->setPageCallback( array( &$this, 'reportPage' ) );
247 $this->importCallback = $importer->setRevisionCallback(
248 array( &$this, 'handleRevision' ) );
249 $this->uploadCallback = $importer->setUploadCallback(
250 array( &$this, 'handleUpload' ) );
251 $this->logItemCallback = $importer->setLogItemCallback(
252 array( &$this, 'handleLogItem' ) );
253 if ( $this->uploads ) {
254 $importer->setImportUploads( true );
255 }
256 if ( $this->imageBasePath ) {
257 $importer->setImageBasePath( $this->imageBasePath );
258 }
259
260 if ( $this->dryRun ) {
261 $importer->setPageOutCallback( null );
262 }
263
264 return $importer->doImport();
265 }
266 }
267
268 $maintClass = 'BackupReader';
269 require_once( RUN_MAINTENANCE_IF_MAIN );