Merge "Show redirect fragments on Special:ListRedirects"
[lhc/web/wiklou.git] / includes / specials / SpecialImport.php
index 0574dbc..beb454d 100644 (file)
@@ -24,6 +24,8 @@
  * @ingroup SpecialPage
  */
 
+use MediaWiki\MediaWikiServices;
+
 /**
  * MediaWiki page data importer
  *
@@ -44,9 +46,6 @@ class SpecialImport extends SpecialPage {
        private $pageLinkDepth;
        private $importSources;
 
-       /**
-        * Constructor
-        */
        public function __construct() {
                parent::__construct( 'Import', 'import' );
        }
@@ -72,7 +71,7 @@ class SpecialImport extends SpecialPage {
                $this->getOutput()->addModules( 'mediawiki.special.import' );
 
                $this->importSources = $this->getConfig()->get( 'ImportSources' );
-               Hooks::run( 'ImportSources', array( &$this->importSources ) );
+               Hooks::run( 'ImportSources', [ &$this->importSources ] );
 
                $user = $this->getUser();
                if ( !$user->isAllowedAny( 'import', 'importupload' ) ) {
@@ -85,11 +84,11 @@ class SpecialImport extends SpecialPage {
                $errors = wfMergeErrorArrays(
                        $this->getPageTitle()->getUserPermissionsErrors(
                                'import', $user, true,
-                               array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
+                               [ 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' ]
                        ),
                        $this->getPageTitle()->getUserPermissionsErrors(
                                'importupload', $user, true,
-                               array( 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' )
+                               [ 'ns-specialprotected', 'badaccess-group0', 'badaccess-groups' ]
                        )
                );
 
@@ -174,10 +173,8 @@ class SpecialImport extends SpecialPage {
 
                $out = $this->getOutput();
                if ( !$source->isGood() ) {
-                       $out->wrapWikiMsg(
-                               "<p class=\"error\">\n$1\n</p>",
-                               array( 'importfailed', $source->getWikiText() )
-                       );
+                       $out->addWikiText( "<p class=\"error\">\n" .
+                               $this->msg( 'importfailed', $source->getWikiText() )->parse() . "\n</p>" );
                } else {
                        $importer = new WikiImporter( $source->value, $this->getConfig() );
                        if ( !is_null( $this->namespace ) ) {
@@ -187,11 +184,11 @@ class SpecialImport extends SpecialPage {
                                if ( !$statusRootPage->isGood() ) {
                                        $out->wrapWikiMsg(
                                                "<p class=\"error\">\n$1\n</p>",
-                                               array(
+                                               [
                                                        'import-options-wrong',
                                                        $statusRootPage->getWikiText(),
                                                        count( $statusRootPage->getErrorsArray() )
-                                               )
+                                               ]
                                        );
 
                                        return;
@@ -221,13 +218,13 @@ class SpecialImport extends SpecialPage {
                                # No source or XML parse error
                                $out->wrapWikiMsg(
                                        "<p class=\"error\">\n$1\n</p>",
-                                       array( 'importfailed', $exception->getMessage() )
+                                       [ 'importfailed', $exception->getMessage() ]
                                );
                        } elseif ( !$result->isGood() ) {
                                # Zero revisions
                                $out->wrapWikiMsg(
                                        "<p class=\"error\">\n$1\n</p>",
-                                       array( 'importfailed', $result->getWikiText() )
+                                       [ 'importfailed', $result->getWikiText() ]
                                );
                        } else {
                                # Success!
@@ -271,16 +268,16 @@ class SpecialImport extends SpecialPage {
                                                        !is_null( $defaultNamespace ) )
                                        ) . ' ' .
                                        Html::namespaceSelector(
-                                               array(
+                                               [
                                                        'selected' => ( $isSameSourceAsBefore ?
                                                                $this->namespace :
                                                                ( $defaultNamespace || '' ) ),
-                                               ), array(
+                                               ], [
                                                        'name' => "namespace",
                                                        // mw-import-namespace-interwiki, mw-import-namespace-upload
                                                        'id' => "mw-import-namespace-$sourceName",
                                                        'class' => 'namespaceselector',
-                                               )
+                                               ]
                                        ) .
                                        "</td>
                                </tr>
@@ -298,20 +295,20 @@ class SpecialImport extends SpecialPage {
                                        ) . ' ' .
                                        Xml::input( 'rootpage', 50,
                                                ( $isSameSourceAsBefore ? $this->rootpage : '' ),
-                                               array(
+                                               [
                                                        // Should be "mw-import-rootpage-...", but we keep this inaccurate
                                                        // ID for legacy reasons
                                                        // mw-interwiki-rootpage-interwiki, mw-interwiki-rootpage-upload
                                                        'id' => "mw-interwiki-rootpage-$sourceName",
                                                        'type' => 'text'
-                                               )
+                                               ]
                                        ) . ' ' .
                                        "</td>
                                </tr>";
        }
 
        private function showForm() {
-               $action = $this->getPageTitle()->getLocalURL( array( 'action' => 'submit' ) );
+               $action = $this->getPageTitle()->getLocalURL( [ 'action' => 'submit' ] );
                $user = $this->getUser();
                $out = $this->getOutput();
                $this->addHelpLink( '//meta.wikimedia.org/wiki/Special:MyLanguage/Help:Import', true );
@@ -322,23 +319,23 @@ class SpecialImport extends SpecialPage {
                                Xml::fieldset( $this->msg( 'import-upload' )->text() ) .
                                        Xml::openElement(
                                                'form',
-                                               array(
+                                               [
                                                        'enctype' => 'multipart/form-data',
                                                        'method' => 'post',
                                                        'action' => $action,
                                                        'id' => 'mw-import-upload-form'
-                                               )
+                                               ]
                                        ) .
                                        $this->msg( 'importtext' )->parseAsBlock() .
                                        Html::hidden( 'action', 'submit' ) .
                                        Html::hidden( 'source', 'upload' ) .
-                                       Xml::openElement( 'table', array( 'id' => 'mw-import-table-upload' ) ) .
+                                       Xml::openElement( 'table', [ 'id' => 'mw-import-table-upload' ] ) .
                                        "<tr>
                                        <td class='mw-label'>" .
                                        Xml::label( $this->msg( 'import-upload-filename' )->text(), 'xmlimport' ) .
                                        "</td>
                                        <td class='mw-input'>" .
-                                       Html::input( 'xmlimport', '', 'file', array( 'id' => 'xmlimport' ) ) . ' ' .
+                                       Html::input( 'xmlimport', '', 'file', [ 'id' => 'xmlimport' ] ) . ' ' .
                                        "</td>
                                </tr>
                                <tr>
@@ -348,7 +345,7 @@ class SpecialImport extends SpecialPage {
                                        <td class='mw-input'>" .
                                        Xml::input( 'log-comment', 50,
                                                ( $this->sourceName === 'upload' ? $this->logcomment : '' ),
-                                               array( 'id' => 'mw-import-comment', 'type' => 'text' ) ) . ' ' .
+                                               [ 'id' => 'mw-import-comment', 'type' => 'text' ] ) . ' ' .
                                        "</td>
                                </tr>
                                $mappingSelection
@@ -388,17 +385,17 @@ class SpecialImport extends SpecialPage {
                                Xml::fieldset( $this->msg( 'importinterwiki' )->text() ) .
                                        Xml::openElement(
                                                'form',
-                                               array(
+                                               [
                                                        'method' => 'post',
                                                        'action' => $action,
                                                        'id' => 'mw-import-interwiki-form'
-                                               )
+                                               ]
                                        ) .
                                        $this->msg( 'import-interwiki-text' )->parseAsBlock() .
                                        Html::hidden( 'action', 'submit' ) .
                                        Html::hidden( 'source', 'interwiki' ) .
                                        Html::hidden( 'editToken', $user->getEditToken() ) .
-                                       Xml::openElement( 'table', array( 'id' => 'mw-import-table-interwiki' ) ) .
+                                       Xml::openElement( 'table', [ 'id' => 'mw-import-table-interwiki' ] ) .
                                        "<tr>
                                        <td class='mw-label'>" .
                                        Xml::label( $this->msg( 'import-interwiki-sourcewiki' )->text(), 'interwiki' ) .
@@ -406,7 +403,7 @@ class SpecialImport extends SpecialPage {
                                        <td class='mw-input'>" .
                                        Xml::openElement(
                                                'select',
-                                               array( 'name' => 'interwiki', 'id' => 'interwiki' )
+                                               [ 'name' => 'interwiki', 'id' => 'interwiki' ]
                                        )
                        );
 
@@ -418,9 +415,9 @@ class SpecialImport extends SpecialPage {
                                        $needSubprojectField = true;
                                }
 
-                               $attribs = array(
+                               $attribs = [
                                        'value' => $key,
-                               );
+                               ];
                                if ( is_array( $value ) ) {
                                        $attribs['data-subprojects'] = implode( ' ', $value );
                                }
@@ -438,11 +435,11 @@ class SpecialImport extends SpecialPage {
                                $out->addHTML(
                                        Xml::openElement(
                                                'select',
-                                               array( 'name' => 'subproject', 'id' => 'subproject' )
+                                               [ 'name' => 'subproject', 'id' => 'subproject' ]
                                        )
                                );
 
-                               $subprojectsToAdd = array();
+                               $subprojectsToAdd = [];
                                foreach ( $this->importSources as $key => $value ) {
                                        if ( is_array( $value ) ) {
                                                $subprojectsToAdd = array_merge( $subprojectsToAdd, $value );
@@ -467,7 +464,7 @@ class SpecialImport extends SpecialPage {
                                        Xml::label( $this->msg( 'import-interwiki-sourcepage' )->text(), 'frompage' ) .
                                        "</td>
                                        <td class='mw-input'>" .
-                                       Xml::input( 'frompage', 50, $this->frompage, array( 'id' => 'frompage' ) ) .
+                                       Xml::input( 'frompage', 50, $this->frompage, [ 'id' => 'frompage' ] ) .
                                        "</td>
                                </tr>
                                <tr>
@@ -502,7 +499,7 @@ class SpecialImport extends SpecialPage {
                                        <td class='mw-input'>" .
                                        Xml::input( 'log-comment', 50,
                                                ( $this->sourceName === 'interwiki' ? $this->logcomment : '' ),
-                                               array( 'id' => 'mw-interwiki-comment', 'type' => 'text' ) ) . ' ' .
+                                               [ 'id' => 'mw-interwiki-comment', 'type' => 'text' ] ) . ' ' .
                                        "</td>
                                </tr>
                                $mappingSelection
@@ -534,6 +531,7 @@ class SpecialImport extends SpecialPage {
  */
 class ImportReporter extends ContextSource {
        private $reason = false;
+       private $logTags = [];
        private $mOriginalLogCallback = null;
        private $mOriginalPageOutCallback = null;
        private $mLogItemCount = 0;
@@ -546,23 +544,33 @@ class ImportReporter extends ContextSource {
         */
        function __construct( $importer, $upload, $interwiki, $reason = false ) {
                $this->mOriginalPageOutCallback =
-                       $importer->setPageOutCallback( array( $this, 'reportPage' ) );
+                       $importer->setPageOutCallback( [ $this, 'reportPage' ] );
                $this->mOriginalLogCallback =
-                       $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
-               $importer->setNoticeCallback( array( $this, 'reportNotice' ) );
+                       $importer->setLogItemCallback( [ $this, 'reportLogItem' ] );
+               $importer->setNoticeCallback( [ $this, 'reportNotice' ] );
                $this->mPageCount = 0;
                $this->mIsUpload = $upload;
                $this->mInterwiki = $interwiki;
                $this->reason = $reason;
        }
 
+       /**
+        * Sets change tags to apply to the import log entry and null revision.
+        *
+        * @param array $tags
+        * @since 1.29
+        */
+       public function setChangeTags( array $tags ) {
+               $this->logTags = $tags;
+       }
+
        function open() {
                $this->getOutput()->addHTML( "<ul>\n" );
        }
 
        function reportNotice( $msg, array $params ) {
                $this->getOutput()->addHTML(
-                       Html::element( 'li', array(), $this->msg( $msg, $params )->text() )
+                       Html::element( 'li', [], $this->msg( $msg, $params )->text() )
                );
        }
 
@@ -581,7 +589,7 @@ class ImportReporter extends ContextSource {
         * @param array $pageInfo
         * @return void
         */
-       function reportPage( $title, $foreignTitle, $revisionCount,
+       public function reportPage( $title, $foreignTitle, $revisionCount,
                        $successCount, $pageInfo ) {
                $args = func_get_args();
                call_user_func_array( $this->mOriginalPageOutCallback, $args );
@@ -592,15 +600,19 @@ class ImportReporter extends ContextSource {
                }
 
                $this->mPageCount++;
-
+               $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
                if ( $successCount > 0 ) {
+                       // <bdi> prevents jumbling of the versions count
+                       // in RTL wikis in case the page title is LTR
                        $this->getOutput()->addHTML(
-                               "<li>" . Linker::linkKnown( $title ) . " " .
+                               "<li>" . $linkRenderer->makeLink( $title ) . " " .
+                                       "<bdi>" .
                                        $this->msg( 'import-revision-count' )->numParams( $successCount )->escaped() .
+                                       "</bdi>" .
                                        "</li>\n"
                        );
 
-                       $logParams = array( '4:number:count' => $successCount );
+                       $logParams = [ '4:number:count' => $successCount ];
                        if ( $this->mIsUpload ) {
                                $detail = $this->msg( 'import-logentry-upload-detail' )->numParams(
                                        $successCount )->inContentLanguage()->text();
@@ -608,7 +620,7 @@ class ImportReporter extends ContextSource {
                        } else {
                                $pageTitle = $foreignTitle->getFullText();
                                $fullInterwikiPrefix = $this->mInterwiki;
-                               Hooks::run( 'ImportLogInterwikiLink', array( &$fullInterwikiPrefix, &$pageTitle ) );
+                               Hooks::run( 'ImportLogInterwikiLink', [ &$fullInterwikiPrefix, &$pageTitle ] );
 
                                $interwikiTitleStr = $fullInterwikiPrefix . ':' . $pageTitle;
                                $interwiki = '[[:' . $interwikiTitleStr . ']]';
@@ -622,14 +634,6 @@ class ImportReporter extends ContextSource {
                                        . $this->reason;
                        }
 
-                       $logEntry = new ManualLogEntry( 'import', $action );
-                       $logEntry->setTarget( $title );
-                       $logEntry->setComment( $this->reason );
-                       $logEntry->setPerformer( $this->getUser() );
-                       $logEntry->setParameters( $logParams );
-                       $logid = $logEntry->insert();
-                       $logEntry->publish( $logid );
-
                        $comment = $detail; // quick
                        $dbw = wfGetDB( DB_MASTER );
                        $latest = $title->getLatestRevID();
@@ -641,18 +645,35 @@ class ImportReporter extends ContextSource {
                                $this->getUser()
                        );
 
+                       $nullRevId = null;
                        if ( !is_null( $nullRevision ) ) {
-                               $nullRevision->insertOn( $dbw );
+                               $nullRevId = $nullRevision->insertOn( $dbw );
                                $page = WikiPage::factory( $title );
                                # Update page record
                                $page->updateRevisionOn( $dbw, $nullRevision );
                                Hooks::run(
                                        'NewRevisionFromEditComplete',
-                                       array( $page, $nullRevision, $latest, $this->getUser() )
+                                       [ $page, $nullRevision, $latest, $this->getUser() ]
                                );
                        }
+
+                       // Create the import log entry
+                       $logEntry = new ManualLogEntry( 'import', $action );
+                       $logEntry->setTarget( $title );
+                       $logEntry->setComment( $this->reason );
+                       $logEntry->setPerformer( $this->getUser() );
+                       $logEntry->setParameters( $logParams );
+                       $logid = $logEntry->insert();
+                       if ( count( $this->logTags ) ) {
+                               $logEntry->setTags( $this->logTags );
+                       }
+                       // Make sure the null revision will be tagged as well
+                       $logEntry->setAssociatedRevId( $nullRevId );
+
+                       $logEntry->publish( $logid );
+
                } else {
-                       $this->getOutput()->addHTML( "<li>" . Linker::linkKnown( $title ) . " " .
+                       $this->getOutput()->addHTML( "<li>" . $linkRenderer->makeKnownLink( $title ) . " " .
                                $this->msg( 'import-nonewrevisions' )->escaped() . "</li>\n" );
                }
        }