Merge "Installer: output css correctly when session errors occur"
[lhc/web/wiklou.git] / includes / specials / SpecialImport.php
1 <?php
2 /**
3 * Implements Special:Import
4 *
5 * Copyright © 2003,2005 Brion Vibber <brion@pobox.com>
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 SpecialPage
25 */
26
27 /**
28 * MediaWiki page data importer
29 *
30 * @ingroup SpecialPage
31 */
32 class SpecialImport extends SpecialPage {
33 private $interwiki = false;
34 private $namespace;
35 private $rootpage = '';
36 private $frompage = '';
37 private $logcomment = false;
38 private $history = true;
39 private $includeTemplates = false;
40 private $pageLinkDepth;
41
42 /**
43 * Constructor
44 */
45 public function __construct() {
46 parent::__construct( 'Import', 'import' );
47 global $wgImportTargetNamespace;
48 $this->namespace = $wgImportTargetNamespace;
49 }
50
51 /**
52 * Execute
53 */
54 function execute( $par ) {
55 $this->setHeaders();
56 $this->outputHeader();
57
58 $user = $this->getUser();
59 if ( !$user->isAllowedAny( 'import', 'importupload' ) ) {
60 throw new PermissionsError( 'import' );
61 }
62
63 # @todo Allow Title::getUserPermissionsErrors() to take an array
64 # @todo FIXME: Title::checkSpecialsAndNSPermissions() has a very wierd expectation of what
65 # getUserPermissionsErrors() might actually be used for, hence the 'ns-specialprotected'
66 $errors = wfMergeErrorArrays(
67 $this->getPageTitle()->getUserPermissionsErrors(
68 'import', $user, true,
69 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
70 ),
71 $this->getPageTitle()->getUserPermissionsErrors(
72 'importupload', $user, true,
73 array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
74 )
75 );
76
77 if ( $errors ) {
78 throw new PermissionsError( 'import', $errors );
79 }
80
81 $this->checkReadOnly();
82
83 $request = $this->getRequest();
84 if ( $request->wasPosted() && $request->getVal( 'action' ) == 'submit' ) {
85 $this->doImport();
86 }
87 $this->showForm();
88 }
89
90 /**
91 * Do the actual import
92 */
93 private function doImport() {
94 global $wgImportSources, $wgExportMaxLinkDepth;
95
96 $isUpload = false;
97 $request = $this->getRequest();
98 $this->namespace = $request->getIntOrNull( 'namespace' );
99 $sourceName = $request->getVal( "source" );
100
101 $this->logcomment = $request->getText( 'log-comment' );
102 $this->pageLinkDepth = $wgExportMaxLinkDepth == 0
103 ? 0
104 : $request->getIntOrNull( 'pagelink-depth' );
105 $this->rootpage = $request->getText( 'rootpage' );
106
107 $user = $this->getUser();
108 if ( !$user->matchEditToken( $request->getVal( 'editToken' ) ) ) {
109 $source = Status::newFatal( 'import-token-mismatch' );
110 } elseif ( $sourceName == 'upload' ) {
111 $isUpload = true;
112 if ( $user->isAllowed( 'importupload' ) ) {
113 $source = ImportStreamSource::newFromUpload( "xmlimport" );
114 } else {
115 throw new PermissionsError( 'importupload' );
116 }
117 } elseif ( $sourceName == "interwiki" ) {
118 if ( !$user->isAllowed( 'import' ) ) {
119 throw new PermissionsError( 'import' );
120 }
121 $this->interwiki = $request->getVal( 'interwiki' );
122 if ( !in_array( $this->interwiki, $wgImportSources ) ) {
123 $source = Status::newFatal( "import-invalid-interwiki" );
124 } else {
125 $this->history = $request->getCheck( 'interwikiHistory' );
126 $this->frompage = $request->getText( "frompage" );
127 $this->includeTemplates = $request->getCheck( 'interwikiTemplates' );
128 $source = ImportStreamSource::newFromInterwiki(
129 $this->interwiki,
130 $this->frompage,
131 $this->history,
132 $this->includeTemplates,
133 $this->pageLinkDepth );
134 }
135 } else {
136 $source = Status::newFatal( "importunknownsource" );
137 }
138
139 $out = $this->getOutput();
140 if ( !$source->isGood() ) {
141 $out->wrapWikiMsg(
142 "<p class=\"error\">\n$1\n</p>",
143 array( 'importfailed', $source->getWikiText() )
144 );
145 } else {
146 $importer = new WikiImporter( $source->value );
147 if ( !is_null( $this->namespace ) ) {
148 $importer->setTargetNamespace( $this->namespace );
149 }
150 if ( !is_null( $this->rootpage ) ) {
151 $statusRootPage = $importer->setTargetRootPage( $this->rootpage );
152 if ( !$statusRootPage->isGood() ) {
153 $out->wrapWikiMsg(
154 "<p class=\"error\">\n$1\n</p>",
155 array(
156 'import-options-wrong',
157 $statusRootPage->getWikiText(),
158 count( $statusRootPage->getErrorsArray() )
159 )
160 );
161
162 return;
163 }
164 }
165
166 $out->addWikiMsg( "importstart" );
167
168 $reporter = new ImportReporter(
169 $importer,
170 $isUpload,
171 $this->interwiki,
172 $this->logcomment
173 );
174 $reporter->setContext( $this->getContext() );
175 $exception = false;
176
177 $reporter->open();
178 try {
179 $importer->doImport();
180 } catch ( MWException $e ) {
181 $exception = $e;
182 }
183 $result = $reporter->close();
184
185 if ( $exception ) {
186 # No source or XML parse error
187 $out->wrapWikiMsg(
188 "<p class=\"error\">\n$1\n</p>",
189 array( 'importfailed', $exception->getMessage() )
190 );
191 } elseif ( !$result->isGood() ) {
192 # Zero revisions
193 $out->wrapWikiMsg(
194 "<p class=\"error\">\n$1\n</p>",
195 array( 'importfailed', $result->getWikiText() )
196 );
197 } else {
198 # Success!
199 $out->addWikiMsg( 'importsuccess' );
200 }
201 $out->addHTML( '<hr />' );
202 }
203 }
204
205 private function showForm() {
206 global $wgImportSources, $wgExportMaxLinkDepth;
207
208 $action = $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) );
209 $user = $this->getUser();
210 $out = $this->getOutput();
211
212 if ( $user->isAllowed( 'importupload' ) ) {
213 $out->addHTML(
214 Xml::fieldset( $this->msg( 'import-upload' )->text() ) .
215 Xml::openElement(
216 'form',
217 array(
218 'enctype' => 'multipart/form-data',
219 'method' => 'post',
220 'action' => $action,
221 'id' => 'mw-import-upload-form'
222 )
223 ) .
224 $this->msg( 'importtext' )->parseAsBlock() .
225 Html::hidden( 'action', 'submit' ) .
226 Html::hidden( 'source', 'upload' ) .
227 Xml::openElement( 'table', array( 'id' => 'mw-import-table-upload' ) ) .
228 "<tr>
229 <td class='mw-label'>" .
230 Xml::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
231 "</td>
232 <td class='mw-input'>" .
233 Html::input( 'xmlimport', '', 'file', array( 'id' => 'xmlimport' ) ) . ' ' .
234 "</td>
235 </tr>
236 <tr>
237 <td class='mw-label'>" .
238 Xml::label( $this->msg( 'import-comment' )->text(), 'mw-import-comment' ) .
239 "</td>
240 <td class='mw-input'>" .
241 Xml::input( 'log-comment', 50, '',
242 array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
243 "</td>
244 </tr>
245 <tr>
246 <td class='mw-label'>" .
247 Xml::label(
248 $this->msg( 'import-interwiki-rootpage' )->text(),
249 'mw-interwiki-rootpage-upload'
250 ) .
251 "</td>
252 <td class='mw-input'>" .
253 Xml::input( 'rootpage', 50, $this->rootpage,
254 array( 'id' => 'mw-interwiki-rootpage-upload', 'type' => 'text' ) ) . ' ' .
255 "</td>
256 </tr>
257 <tr>
258 <td></td>
259 <td class='mw-submit'>" .
260 Xml::submitButton( $this->msg( 'uploadbtn' )->text() ) .
261 "</td>
262 </tr>" .
263 Xml::closeElement( 'table' ) .
264 Html::hidden( 'editToken', $user->getEditToken() ) .
265 Xml::closeElement( 'form' ) .
266 Xml::closeElement( 'fieldset' )
267 );
268 } else {
269 if ( empty( $wgImportSources ) ) {
270 $out->addWikiMsg( 'importnosources' );
271 }
272 }
273
274 if ( $user->isAllowed( 'import' ) && !empty( $wgImportSources ) ) {
275 # Show input field for import depth only if $wgExportMaxLinkDepth > 0
276 $importDepth = '';
277 if ( $wgExportMaxLinkDepth > 0 ) {
278 $importDepth = "<tr>
279 <td class='mw-label'>" .
280 $this->msg( 'export-pagelinks' )->parse() .
281 "</td>
282 <td class='mw-input'>" .
283 Xml::input( 'pagelink-depth', 3, 0 ) .
284 "</td>
285 </tr>";
286 }
287
288 $out->addHTML(
289 Xml::fieldset( $this->msg( 'importinterwiki' )->text() ) .
290 Xml::openElement(
291 'form',
292 array(
293 'method' => 'post',
294 'action' => $action,
295 'id' => 'mw-import-interwiki-form'
296 )
297 ) .
298 $this->msg( 'import-interwiki-text' )->parseAsBlock() .
299 Html::hidden( 'action', 'submit' ) .
300 Html::hidden( 'source', 'interwiki' ) .
301 Html::hidden( 'editToken', $user->getEditToken() ) .
302 Xml::openElement( 'table', array( 'id' => 'mw-import-table-interwiki' ) ) .
303 "<tr>
304 <td class='mw-label'>" .
305 Xml::label( $this->msg( 'import-interwiki-source' )->text(), 'interwiki' ) .
306 "</td>
307 <td class='mw-input'>" .
308 Xml::openElement(
309 'select',
310 array( 'name' => 'interwiki', 'id' => 'interwiki' )
311 )
312 );
313
314 foreach ( $wgImportSources as $prefix ) {
315 $selected = ( $this->interwiki === $prefix ) ? ' selected="selected"' : '';
316 $out->addHTML( Xml::option( $prefix, $prefix, $selected ) );
317 }
318
319 $out->addHTML(
320 Xml::closeElement( 'select' ) .
321 Xml::input( 'frompage', 50, $this->frompage, array( 'id' => 'frompage' ) ) .
322 "</td>
323 </tr>
324 <tr>
325 <td>
326 </td>
327 <td class='mw-input'>" .
328 Xml::checkLabel(
329 $this->msg( 'import-interwiki-history' )->text(),
330 'interwikiHistory',
331 'interwikiHistory',
332 $this->history
333 ) .
334 "</td>
335 </tr>
336 <tr>
337 <td>
338 </td>
339 <td class='mw-input'>" .
340 Xml::checkLabel(
341 $this->msg( 'import-interwiki-templates' )->text(),
342 'interwikiTemplates',
343 'interwikiTemplates',
344 $this->includeTemplates
345 ) .
346 "</td>
347 </tr>
348 $importDepth
349 <tr>
350 <td class='mw-label'>" .
351 Xml::label( $this->msg( 'import-interwiki-namespace' )->text(), 'namespace' ) .
352 "</td>
353 <td class='mw-input'>" .
354 Html::namespaceSelector(
355 array(
356 'selected' => $this->namespace,
357 'all' => '',
358 ), array(
359 'name' => 'namespace',
360 'id' => 'namespace',
361 'class' => 'namespaceselector',
362 )
363 ) .
364 "</td>
365 </tr>
366 <tr>
367 <td class='mw-label'>" .
368 Xml::label( $this->msg( 'import-comment' )->text(), 'mw-interwiki-comment' ) .
369 "</td>
370 <td class='mw-input'>" .
371 Xml::input( 'log-comment', 50, '',
372 array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
373 "</td>
374 </tr>
375 <tr>
376 <td class='mw-label'>" .
377 Xml::label(
378 $this->msg( 'import-interwiki-rootpage' )->text(),
379 'mw-interwiki-rootpage-interwiki'
380 ) .
381 "</td>
382 <td class='mw-input'>" .
383 Xml::input( 'rootpage', 50, $this->rootpage,
384 array( 'id' => 'mw-interwiki-rootpage-interwiki', 'type' => 'text' ) ) . ' ' .
385 "</td>
386 </tr>
387 <tr>
388 <td>
389 </td>
390 <td class='mw-submit'>" .
391 Xml::submitButton(
392 $this->msg( 'import-interwiki-submit' )->text(),
393 Linker::tooltipAndAccesskeyAttribs( 'import' )
394 ) .
395 "</td>
396 </tr>" .
397 Xml::closeElement( 'table' ) .
398 Xml::closeElement( 'form' ) .
399 Xml::closeElement( 'fieldset' )
400 );
401 }
402 }
403
404 protected function getGroupName() {
405 return 'pagetools';
406 }
407 }
408
409 /**
410 * Reporting callback
411 * @ingroup SpecialPage
412 */
413 class ImportReporter extends ContextSource {
414 private $reason = false;
415 private $mOriginalLogCallback = null;
416 private $mOriginalPageOutCallback = null;
417 private $mLogItemCount = 0;
418
419 /**
420 * @param WikiImporter $importer
421 * @param bool $upload
422 * @param string $interwiki
423 * @param string|bool $reason
424 */
425 function __construct( $importer, $upload, $interwiki, $reason = false ) {
426 $this->mOriginalPageOutCallback =
427 $importer->setPageOutCallback( array( $this, 'reportPage' ) );
428 $this->mOriginalLogCallback =
429 $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
430 $importer->setNoticeCallback( array( $this, 'reportNotice' ) );
431 $this->mPageCount = 0;
432 $this->mIsUpload = $upload;
433 $this->mInterwiki = $interwiki;
434 $this->reason = $reason;
435 }
436
437 function open() {
438 $this->getOutput()->addHTML( "<ul>\n" );
439 }
440
441 function reportNotice( $msg, array $params ) {
442 $this->getOutput()->addHTML(
443 Html::element( 'li', array(), $this->msg( $msg, $params )->text() )
444 );
445 }
446
447 function reportLogItem( /* ... */ ) {
448 $this->mLogItemCount++;
449 if ( is_callable( $this->mOriginalLogCallback ) ) {
450 call_user_func_array( $this->mOriginalLogCallback, func_get_args() );
451 }
452 }
453
454 /**
455 * @param Title $title
456 * @param Title $origTitle
457 * @param int $revisionCount
458 * @param int $successCount
459 * @param array $pageInfo
460 * @return void
461 */
462 function reportPage( $title, $origTitle, $revisionCount, $successCount, $pageInfo ) {
463 $args = func_get_args();
464 call_user_func_array( $this->mOriginalPageOutCallback, $args );
465
466 if ( $title === null ) {
467 # Invalid or non-importable title; a notice is already displayed
468 return;
469 }
470
471 $this->mPageCount++;
472
473 if ( $successCount > 0 ) {
474 $this->getOutput()->addHTML(
475 "<li>" . Linker::linkKnown( $title ) . " " .
476 $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
477 "</li>\n"
478 );
479
480 $log = new LogPage( 'import' );
481 if ( $this->mIsUpload ) {
482 $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
483 $successCount )->inContentLanguage()->text();
484 if ( $this->reason ) {
485 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
486 . $this->reason;
487 }
488 $log->addEntry( 'upload', $title, $detail, array(), $this->getUser() );
489 } else {
490 $interwiki = '[[:' . $this->mInterwiki . ':' .
491 $origTitle->getPrefixedText() . ']]';
492 $detail = $this->msg( 'import-logentry-interwiki-detail' )->numParams(
493 $successCount )->params( $interwiki )->inContentLanguage()->text();
494 if ( $this->reason ) {
495 $detail .= $this->msg( 'colon-separator' )->inContentLanguage()->text()
496 . $this->reason;
497 }
498 $log->addEntry( 'interwiki', $title, $detail, array(), $this->getUser() );
499 }
500
501 $comment = $detail; // quick
502 $dbw = wfGetDB( DB_MASTER );
503 $latest = $title->getLatestRevID();
504 $nullRevision = Revision::newNullRevision(
505 $dbw,
506 $title->getArticleID(),
507 $comment,
508 true,
509 $this->getUser()
510 );
511
512 if ( !is_null( $nullRevision ) ) {
513 $nullRevision->insertOn( $dbw );
514 $page = WikiPage::factory( $title );
515 # Update page record
516 $page->updateRevisionOn( $dbw, $nullRevision );
517 wfRunHooks(
518 'NewRevisionFromEditComplete',
519 array( $page, $nullRevision, $latest, $this->getUser() )
520 );
521 }
522 } else {
523 $this->getOutput()->addHTML( "<li>" . Linker::linkKnown( $title ) . " " .
524 $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
525 }
526 }
527
528 function close() {
529 $out = $this->getOutput();
530 if ( $this->mLogItemCount > 0 ) {
531 $msg = $this->msg( 'imported-log-entries' )->numParams( $this->mLogItemCount )->parse();
532 $out->addHTML( Xml::tags( 'li', null, $msg ) );
533 } elseif ( $this->mPageCount == 0 && $this->mLogItemCount == 0 ) {
534 $out->addHTML( "</ul>\n" );
535
536 return Status::newFatal( 'importnopages' );
537 }
538 $out->addHTML( "</ul>\n" );
539
540 return Status::newGood( $this->mPageCount );
541 }
542 }