https://www.mediawiki.org/wiki/Special:Code/MediaWiki/102112

Revision: 102112
Author:   ialex
Date:     2011-11-05 19:51:05 +0000 (Sat, 05 Nov 2011)
Log Message:
-----------
* Made OuputPage::showPermissionsErrorPage() show a different messages for 
'read', 'edit', 'create' and 'upload' actions to saying "You need to log in to 
do this action" when 1) The user is not logged in 2) The only error is a 
permissions error (no block or something else) and 3) The error can simply be 
avoided by logging in
* 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

Modified Paths:
--------------
    trunk/phase3/includes/Article.php
    trunk/phase3/includes/EditPage.php
    trunk/phase3/includes/OutputPage.php
    trunk/phase3/includes/Wiki.php
    trunk/phase3/includes/diff/DifferenceEngine.php
    trunk/phase3/includes/specials/SpecialUpload.php
    trunk/phase3/languages/messages/MessagesEn.php
    trunk/phase3/maintenance/language/messages.inc

Modified: trunk/phase3/includes/Article.php
===================================================================
--- trunk/phase3/includes/Article.php   2011-11-05 19:48:22 UTC (rev 102111)
+++ trunk/phase3/includes/Article.php   2011-11-05 19:51:05 UTC (rev 102112)
@@ -479,11 +479,8 @@
                                        # 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

Modified: trunk/phase3/includes/EditPage.php
===================================================================
--- trunk/phase3/includes/EditPage.php  2011-11-05 19:48:22 UTC (rev 102111)
+++ trunk/phase3/includes/EditPage.php  2011-11-05 19:51:05 UTC (rev 102112)
@@ -2257,21 +2257,11 @@
 
        /**
         * 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 @@
         * @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 @@
                                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 @@
                                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;

Modified: trunk/phase3/includes/OutputPage.php
===================================================================
--- trunk/phase3/includes/OutputPage.php        2011-11-05 19:48:22 UTC (rev 
102111)
+++ trunk/phase3/includes/OutputPage.php        2011-11-05 19:51:05 UTC (rev 
102112)
@@ -1979,9 +1979,64 @@
         * @param $action String: action that was denied or null if unknown
         */
        public function showPermissionsErrorPage( $errors, $action = null ) {
-               $this->prepareErrorPage( $this->msg( 'permissionserrors' ) );
+               global $wgGroupPermissions;
 
-               $this->addWikiText( $this->formatPermissionsErrorMessage( 
$errors, $action ) );
+               // 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();
+                       }
+
+                       $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 @@
 
        /**
         * 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' );
        }
 
        /**

Modified: trunk/phase3/includes/Wiki.php
===================================================================
--- trunk/phase3/includes/Wiki.php      2011-11-05 19:48:22 UTC (rev 102111)
+++ trunk/phase3/includes/Wiki.php      2011-11-05 19:51:05 UTC (rev 102112)
@@ -161,7 +161,7 @@
                // 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' );

Modified: trunk/phase3/includes/diff/DifferenceEngine.php
===================================================================
--- trunk/phase3/includes/diff/DifferenceEngine.php     2011-11-05 19:48:22 UTC 
(rev 102111)
+++ trunk/phase3/includes/diff/DifferenceEngine.php     2011-11-05 19:51:05 UTC 
(rev 102112)
@@ -196,11 +196,8 @@
 
                # 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,

Modified: trunk/phase3/includes/specials/SpecialUpload.php
===================================================================
--- trunk/phase3/includes/specials/SpecialUpload.php    2011-11-05 19:48:22 UTC 
(rev 102111)
+++ trunk/phase3/includes/specials/SpecialUpload.php    2011-11-05 19:51:05 UTC 
(rev 102112)
@@ -140,8 +140,6 @@
         * Special page entry point
         */
        public function execute( $par ) {
-               global $wgGroupPermissions;
-
                $this->setHeaders();
                $this->outputHeader();
 
@@ -154,19 +152,12 @@
                $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

Modified: trunk/phase3/languages/messages/MessagesEn.php
===================================================================
--- trunk/phase3/languages/messages/MessagesEn.php      2011-11-05 19:48:22 UTC 
(rev 102111)
+++ trunk/phase3/languages/messages/MessagesEn.php      2011-11-05 19:51:05 UTC 
(rev 102112)
@@ -1298,7 +1298,6 @@
 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]].',

Modified: trunk/phase3/maintenance/language/messages.inc
===================================================================
--- trunk/phase3/maintenance/language/messages.inc      2011-11-05 19:48:22 UTC 
(rev 102111)
+++ trunk/phase3/maintenance/language/messages.inc      2011-11-05 19:51:05 UTC 
(rev 102112)
@@ -588,7 +588,6 @@
                'blockedtext',
                'autoblockedtext',
                'blockednoreason',
-               'whitelistedittitle',
                'whitelistedittext',
                'confirmedittext',
                'nosuchsectiontitle',


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to