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