* Made OuputPage::showPermissionsErrorPage() show a different messages for 'read...
authorAlexandre Emsenhuber <ialex@users.mediawiki.org>
Sat, 5 Nov 2011 19:51:05 +0000 (19:51 +0000)
committerAlexandre Emsenhuber <ialex@users.mediawiki.org>
Sat, 5 Nov 2011 19:51:05 +0000 (19:51 +0000)
* This replaces OuputPage::loginToUse() functionnality, made it simply throw a PermissionsEror exception and updated all calls in core
* Same for the check in SpecialUpload::execute(), EditPage::userNotLoggedInPage() and EditPage::noCreatePermission()
* Throw the same exception in EditPage::attemptSave() whether the user is logged in or not and let OuputPage::showPermissionsErrorPage() decide which message to display
* Replaced call to deprecated OutputPage::blockedPage() in SpecialUpload
* Displayed messages are the same as now, except the title is always "loginreqtitle"
* 'nocreatetitle' and 'uploadnologin' messages are still used by extensions, so I kept them, but the message 'whitelistedittitle' is not used anymore and has been removed

includes/Article.php
includes/EditPage.php
includes/OutputPage.php
includes/Wiki.php
includes/diff/DifferenceEngine.php
includes/specials/SpecialUpload.php
languages/messages/MessagesEn.php
maintenance/language/messages.inc

index b4fa000..0f35661 100644 (file)
@@ -479,11 +479,8 @@ class Article extends Page {
                                        # Another whitelist check in case oldid is altering the title
                                        if ( !$this->getTitle()->userCanRead() ) {
                                                wfDebug( __METHOD__ . ": denied on secondary read check\n" );
-                                               $wgOut->loginToUse();
-                                               $wgOut->output();
-                                               $wgOut->disable();
                                                wfProfileOut( __METHOD__ );
-                                               return;
+                                               throw new PermissionsError( 'read' );
                                        }
 
                                        # Are we looking at an old revision
index efb2c5a..d894e3b 100644 (file)
@@ -2257,21 +2257,11 @@ HTML
 
        /**
         * Produce the stock "please login to edit pages" page
+        *
+        * @deprecated in 1.19; throw an exception directly instead
         */
        function userNotLoggedInPage() {
-               global $wgOut;
-
-               $wgOut->prepareErrorPage( wfMessage( 'whitelistedittitle' ) );
-
-               $loginTitle = SpecialPage::getTitleFor( 'Userlogin' );
-               $loginLink = Linker::linkKnown(
-                       $loginTitle,
-                       wfMsgHtml( 'loginreqlink' ),
-                       array(),
-                       array( 'returnto' => $this->getContextTitle()->getPrefixedText() )
-               );
-               $wgOut->addHTML( wfMessage( 'whitelistedittext' )->rawParams( $loginLink )->parse() );
-               $wgOut->returnToMain( false, $this->getContextTitle() );
+               throw new PermissionsError( 'edit' );
        }
 
        /**
@@ -2281,7 +2271,8 @@ HTML
         * @deprecated in 1.19; throw an exception directly instead
         */
        function noCreatePermission() {
-               throw new MWException( 'nocreatetitle', 'nocreatetext' );
+               $permission = $this->mTitle->isTalkPage() ? 'createtalk' : 'createpage';
+               throw new PermissionsError( $permission );
        }
 
        /**
@@ -2953,15 +2944,10 @@ HTML
                                throw new UserBlockedError( $wgUser->mBlock );
 
                        case self::AS_IMAGE_REDIRECT_ANON:
-                               throw new ErrorPageError( 'uploadnologin', 'uploadnologintext' );
-
                        case self::AS_IMAGE_REDIRECT_LOGGED:
                                throw new PermissionsError( 'upload' );
 
                        case self::AS_READ_ONLY_PAGE_ANON:
-                               $this->userNotLoggedInPage();
-                               return false;
-
                        case self::AS_READ_ONLY_PAGE_LOGGED:
                                throw new PermissionsError( 'edit' );
 
@@ -2972,7 +2958,8 @@ HTML
                                throw new ThrottledError();
 
                        case self::AS_NO_CREATE_PERMISSION:
-                               throw new MWException( 'nocreatetitle', 'nocreatetext' );
+                               $permission = $this->mTitle->isTalkPage() ? 'createtalk' : 'createpage';
+                               throw new PermissionsError( $permission );
 
                }
                return false;
index d11d483..bad955c 100644 (file)
@@ -1979,9 +1979,64 @@ class OutputPage extends ContextSource {
         * @param $action String: action that was denied or null if unknown
         */
        public function showPermissionsErrorPage( $errors, $action = null ) {
-               $this->prepareErrorPage( $this->msg( 'permissionserrors' ) );
+               global $wgGroupPermissions;
+
+               // For some action (read, edit, create and upload), display a "login to do this action"
+               // error if all of the following conditions are met:
+               // 1. the user is not logged in
+               // 2. the only error is insufficient permissions (i.e. no block or something else)
+               // 3. the error can be avoided simply by logging in
+               if ( in_array( $action, array( 'read', 'edit', 'createpage', 'createtalk', 'upload' ) )
+                       && $this->getUser()->isAnon() && count( $errors ) == 1 && isset( $errors[0][0] )
+                       && ( $errors[0][0] == 'badaccess-groups' || $errors[0][0] == 'badaccess-group0' )
+                       && ( ( isset( $wgGroupPermissions['user'][$action] ) && $wgGroupPermissions['user'][$action] )
+                       || ( isset( $wgGroupPermissions['autoconfirmed'][$action] ) && $wgGroupPermissions['autoconfirmed'][$action] ) )
+               ) {
+                       $displayReturnto = null;
+                       $returnto = $this->getTitle();
+                       if ( $action == 'edit' ) {
+                               $msg = 'whitelistedittext';
+                               $displayReturnto = $returnto;
+                       } elseif ( $action == 'createpage' || $action == 'createtalk' ) {
+                               $msg = 'nocreatetext';
+                       } elseif ( $action == 'upload' ) {
+                               $msg = 'uploadnologintext';
+                       } else { # Read
+                               $msg = 'loginreqpagetext';
+                               $displayReturnto = Title::newMainPage();
+                       }
 
-               $this->addWikiText( $this->formatPermissionsErrorMessage( $errors, $action ) );
+                       $query = array();
+                       if ( $returnto ) {
+                               $query['returnto'] = $returnto->getPrefixedText();
+                               $request = $this->getRequest();
+                               if ( !$request->wasPosted() ) {
+                                       $returntoquery = $request->getValues();
+                                       unset( $returntoquery['title'] );
+                                       unset( $returntoquery['returnto'] );
+                                       unset( $returntoquery['returntoquery'] );
+                                       $query['returntoquery'] = wfArrayToCGI( $returntoquery );
+                               }
+                       }
+                       $loginLink = Linker::linkKnown(
+                               SpecialPage::getTitleFor( 'Userlogin' ),
+                               $this->msg( 'loginreqlink' )->escaped(),
+                               array(),
+                               $query
+                       );
+
+                       $this->prepareErrorPage( $this->msg( 'loginreqtitle' ) );
+                       $this->addHTML( $this->msg( $msg )->rawParams( $loginLink )->parse() );
+
+                       # Don't return to a page the user can't read otherwise
+                       # we'll end up in a pointless loop
+                       if ( $displayReturnto && $displayReturnto->userCanRead() ) {
+                               $this->returnToMain( null, $displayReturnto );
+                       }
+               } else {
+                       $this->prepareErrorPage( $this->msg( 'permissionserrors' ) );
+                       $this->addWikiText( $this->formatPermissionsErrorMessage( $errors, $action ) );
+               }
        }
 
        /**
@@ -2008,29 +2063,11 @@ class OutputPage extends ContextSource {
 
        /**
         * Produce the stock "please login to use the wiki" page
+        *
+        * @deprecated in 1.19; throw the exception directly
         */
        public function loginToUse() {
-               if( $this->getUser()->isLoggedIn() ) {
-                       throw new PermissionsError( 'read' );
-               }
-
-               $this->prepareErrorPage( $this->msg( 'loginreqtitle' ), $this->msg( 'errorpagetitle' ) );
-
-               $loginLink = Linker::linkKnown(
-                       SpecialPage::getTitleFor( 'Userlogin' ),
-                       $this->msg( 'loginreqlink' )->escaped(),
-                       array(),
-                       array( 'returnto' => $this->getTitle()->getPrefixedText() )
-               );
-               $this->addHTML( $this->msg( 'loginreqpagetext' )->rawParams( $loginLink )->parse() .
-                       "\n<!--" . $this->getTitle()->getPrefixedUrl() . '-->' );
-
-               # Don't return to the main page if the user can't read it
-               # otherwise we'll end up in a pointless loop
-               $mainPage = Title::newMainPage();
-               if( $mainPage->userCanRead() ) {
-                       $this->returnToMain( null, $mainPage );
-               }
+               throw new PermissionsError( 'read' );
        }
 
        /**
index ddc7f20..d1dee6a 100644 (file)
@@ -161,7 +161,7 @@ class MediaWiki {
                // the Read array in order for the user to see it. (We have to check here to
                // catch special pages etc. We check again in Article::view())
                } elseif ( !$title->userCanRead() ) {
-                       $output->loginToUse();
+                       throw new PermissionsError( 'read' );
                // Interwiki redirects
                } elseif ( $title->getInterwiki() != '' ) {
                        $rdfrom = $request->getVal( 'rdfrom' );
index bb2f4f3..574e285 100644 (file)
@@ -196,11 +196,8 @@ class DifferenceEngine {
 
                # mOldPage might not be set, see below.
                if ( !$this->mNewPage->userCanRead() || ( $this->mOldPage && !$this->mOldPage->userCanRead() ) ) {
-                       $wgOut->loginToUse();
-                       $wgOut->output();
-                       $wgOut->disable();
                        wfProfileOut( __METHOD__ );
-                       return;
+                       throw new PermissionsError( 'read' );
                }
 
                # If external diffs are enabled both globally and for the user,
index 0dc37c1..906ac2c 100644 (file)
@@ -140,8 +140,6 @@ class SpecialUpload extends SpecialPage {
         * Special page entry point
         */
        public function execute( $par ) {
-               global $wgGroupPermissions;
-
                $this->setHeaders();
                $this->outputHeader();
 
@@ -154,19 +152,12 @@ class SpecialUpload extends SpecialPage {
                $user = $this->getUser();
                $permissionRequired = UploadBase::isAllowed( $user );
                if( $permissionRequired !== true ) {
-                       if( !$user->isLoggedIn() && ( $wgGroupPermissions['user']['upload']
-                               || $wgGroupPermissions['autoconfirmed']['upload'] ) ) {
-                               // Custom message if logged-in users without any special rights can upload
-                               throw new ErrorPageError( 'uploadnologin', 'uploadnologintext' );
-                       } else {
-                               throw new PermissionsError( $permissionRequired );
-                       }
+                       throw new PermissionsError( $permissionRequired );
                }
 
                # Check blocks
                if( $user->isBlocked() ) {
-                       $this->getOutput()->blockedPage();
-                       return;
+                       throw new UserBlockedError( $user->mBlock );
                }
 
                # Check whether we actually want to allow changing stuff
index 03b1687..766de9e 100644 (file)
@@ -1298,7 +1298,6 @@ Note that you may not use the "e-mail this user" feature unless you have a valid
 Your current IP address is $3, and the block ID is #$5.
 Please include all above details in any queries you make.',
 'blockednoreason'                  => 'no reason given',
-'whitelistedittitle'               => 'Login required to edit',
 'whitelistedittext'                => 'You have to $1 to edit pages.',
 'confirmedittext'                  => 'You must confirm your e-mail address before editing pages.
 Please set and validate your e-mail address through your [[Special:Preferences|user preferences]].',
index a8e6e01..40aed2d 100644 (file)
@@ -588,7 +588,6 @@ $wgMessageStructure = array(
                'blockedtext',
                'autoblockedtext',
                'blockednoreason',
-               'whitelistedittitle',
                'whitelistedittext',
                'confirmedittext',
                'nosuchsectiontitle',