* Added callback to send notices from WikiImporter and use it in ImportReporter so...
authorAlexandre Emsenhuber <ialex@users.mediawiki.org>
Fri, 6 Jan 2012 14:21:16 +0000 (14:21 +0000)
committerAlexandre Emsenhuber <ialex@users.mediawiki.org>
Fri, 6 Jan 2012 14:21:16 +0000 (14:21 +0000)
* Early abort on invalid title in ImportReporter::reportPage() since a notice has already been sent
* Localised message saying the title is invalid

includes/Import.php
includes/specials/SpecialImport.php
languages/messages/MessagesEn.php
maintenance/language/messages.inc

index c79b963..94c77b8 100644 (file)
@@ -34,7 +34,7 @@ class WikiImporter {
        private $reader = null;
        private $mLogItemCallback, $mUploadCallback, $mRevisionCallback, $mPageCallback;
        private $mSiteInfoCallback, $mTargetNamespace, $mPageOutCallback;
-       private $mDebug;
+       private $mNoticeCallback, $mDebug;
        private $mImportUploads, $mImageBasePath;
        private $mNoUpdates = false;
 
@@ -75,13 +75,14 @@ class WikiImporter {
                wfDebug( "IMPORT: $data\n" );
        }
 
-       private function notice( $data ) {
-               global $wgCommandLineMode;
-               if( $wgCommandLineMode ) {
-                       print "$data\n";
-               } else {
-                       global $wgOut;
-                       $wgOut->addHTML( "<li>" . htmlspecialchars( $data ) . "</li>\n" );
+       private function notice( $msg /*, $param, ...*/ ) {
+               $params = func_get_args();
+               array_shift( $params );
+
+               if ( is_callable( $this->mNoticeCallback ) ) {
+                       call_user_func( $this->mNoticeCallback, $msg, $params );
+               } else { # No ImportReporter -> CLI
+                       echo wfMessage( $msg, $params )->text() . "\n";
                }
        }
 
@@ -101,6 +102,16 @@ class WikiImporter {
                $this->mNoUpdates = $noupdates;
        }
 
+       /**
+        * Set a callback that displays notice messages
+        *
+        * @param $callback callback
+        * @return callback
+        */
+       public function setNoticeCallback( $callback ) {
+               return wfSetVar( $this->mNoticeCallback, $callback );
+       }
+
        /**
         * Sets the action to perform as each new page in the stream is reached.
         * @param $callback callback
@@ -780,21 +791,21 @@ class WikiImporter {
 
                if( is_null( $title ) ) {
                        # Invalid page title? Ignore the page
-                       $this->notice( "Skipping invalid page title '$workTitle'" );
+                       $this->notice( 'import-error-invalid', $workTitle );
                        return false;
                } elseif( $title->isExternal() ) {
-                       $this->notice( wfMessage( 'import-error-interwiki', $title->getText() )->text() );
+                       $this->notice( 'import-error-interwiki', $title->getPrefixedText() );
                        return false;
                } elseif( !$title->canExist() ) {
-                       $this->notice( wfMessage( 'import-error-special', $title->getText() )->text() );
+                       $this->notice( 'import-error-special', $title->getPrefixedText() );
                        return false;
                } elseif( !$title->userCan( 'edit' ) && !$wgCommandLineMode ) {
                        # Do not import if the importing wiki user cannot edit this page
-                       $this->notice( wfMessage( 'import-error-edit', $title->getText() )->text() );
+                       $this->notice( 'import-error-edit', $title->getPrefixedText() );
                        return false;
                } elseif( !$title->exists() && !$title->userCan( 'create' ) && !$wgCommandLineMode ) {
                        # Do not import if the importing wiki user cannot create this page
-                       $this->notice( wfMessage( 'import-error-create', $title->getText() )->text() );
+                       $this->notice( 'import-error-create', $title->getPrefixedText() );
                        return false;
                }
 
index e8c5762..a2380fb 100644 (file)
@@ -321,6 +321,7 @@ class ImportReporter extends ContextSource {
                                $importer->setPageOutCallback( array( $this, 'reportPage' ) );
                $this->mOriginalLogCallback =
                        $importer->setLogItemCallback( array( $this, 'reportLogItem' ) );
+               $importer->setNoticeCallback( array( $this, 'reportNotice' ) );
                $this->mPageCount = 0;
                $this->mIsUpload = $upload;
                $this->mInterwiki = $interwiki;
@@ -331,6 +332,10 @@ class ImportReporter extends ContextSource {
                $this->getOutput()->addHTML( "<ul>\n" );
        }
 
+       function reportNotice( $msg, array $params ) {
+               $this->getOutput()->addHTML( Html::element( 'li', array(), $this->msg( $msg, $params )->text() ) );
+       }
+
        function reportLogItem( /* ... */ ) {
                $this->mLogItemCount++;
                if ( is_callable( $this->mOriginalLogCallback ) ) {
@@ -352,6 +357,11 @@ class ImportReporter extends ContextSource {
                $args = func_get_args();
                call_user_func_array( $this->mOriginalPageOutCallback, $args );
 
+               if ( $title === null ) {
+                       # Invalid or non-importable title; a notice is already displayed
+                       return;
+               }
+
                $this->mPageCount++;
 
                $localCount = $this->getLanguage()->formatNum( $successCount );
index b9963b6..a8fd6e2 100644 (file)
@@ -3400,6 +3400,7 @@ Please try again.',
 'import-error-create'        => 'Page "$1" is not imported because you are not allowed to create it.',
 'import-error-interwiki'     => 'Page "$1" is not imported because its name is reserved for external linking (interwiki).',
 'import-error-special'       => 'Page "$1" is not imported because it belongs to a special namespace that does not allow pages.',
+'import-error-invalid' => 'Page "$1" is not imported because its name is invalid.',
 
 # Import log
 'importlogpage'                    => 'Import log',
index 3d1f403..b2295d3 100644 (file)
@@ -2355,6 +2355,7 @@ $wgMessageStructure = array(
                'import-error-create',
                'import-error-interwiki',
                'import-error-special',
+               'import-error-invalid',
        ),
        'importlog' => array(
                'importlogpage',