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