From d83c2a431c2ac478ae4c19b05eb49e7c296b3cc9 Mon Sep 17 00:00:00 2001 From: Alexandre Emsenhuber Date: Fri, 4 Nov 2011 18:35:25 +0000 Subject: [PATCH] Some cleanup to EditPage: * Added displayPermissionsError() to show the permissions error page with the content if there's content to show or it will throw an normal exception if there's nothing to display; this restores a functionnality lost at some point and added new message 'viewyourtext' that is displayed when the user made changes to the content (e.g. when the block happens while the user is editing the page) * Marked EditPage::readOnlyPage() as deprecated since displayPermissionsError() is now used. This also means that OutputPage::readOnlyPage() is no longer used to display such error pages (but still have to keep that functionnality since it's used by extensions, same for EditPage::readOnlyPage(), *sigh*) * Removed blockedPage()'s display of content since it's nearly never called (a blocked user would trigger the path mentionned above, not the check in internalAttemptSave()) and also removed 'blockedoriginalsource' and 'blockededitsource' messages that are no longer used * Use OutputPage::prepareErrorPage() to prepare error pages instead of doing all that stuff directly * Moved noCreatePermission() near other error-related functions * Throw an exception directly in attemptSave() instead of calling deprecated methods and marked noCreatePermission() and blockedPage() as deprecated --- includes/EditPage.php | 187 +++++++++++++++++------------ languages/messages/MessagesEn.php | 3 +- languages/messages/MessagesQqq.php | 1 + maintenance/language/messages.inc | 3 +- 4 files changed, 115 insertions(+), 79 deletions(-) diff --git a/includes/EditPage.php b/includes/EditPage.php index 0c808ef82b..efb2c5a7fa 100644 --- a/includes/EditPage.php +++ b/includes/EditPage.php @@ -403,27 +403,6 @@ class EditPage { $this->preview = true; } - $wgOut->addModules( array( 'mediawiki.action.edit' ) ); - - if ( $wgUser->getOption( 'uselivepreview', false ) ) { - $wgOut->addModules( 'mediawiki.legacy.preview' ); - } - // Bug #19334: textarea jumps when editing articles in IE8 - $wgOut->addStyle( 'common/IE80Fixes.css', 'screen', 'IE 8' ); - - $permErrors = $this->getEditPermissionErrors(); - if ( $permErrors ) { - // Auto-block user's IP if the account was "hard" blocked - $wgUser->spreadAnyEditBlock(); - - wfDebug( __METHOD__ . ": User can't edit\n" ); - $content = $this->getContent( null ); - $content = $content === '' ? null : $content; - $this->readOnlyPage( $content, true, $permErrors, 'edit' ); - wfProfileOut( __METHOD__ ); - return; - } - if ( $this->save ) { $this->formtype = 'save'; } elseif ( $this->preview ) { @@ -439,6 +418,26 @@ class EditPage { } } + $permErrors = $this->getEditPermissionErrors(); + if ( $permErrors ) { + wfDebug( __METHOD__ . ": User can't edit\n" ); + // Auto-block user's IP if the account was "hard" blocked + $wgUser->spreadAnyEditBlock(); + + $this->displayPermissionsError( $permErrors ); + + wfProfileOut( __METHOD__ ); + return; + } + + $wgOut->addModules( array( 'mediawiki.action.edit' ) ); + + if ( $wgUser->getOption( 'uselivepreview', false ) ) { + $wgOut->addModules( 'mediawiki.legacy.preview' ); + } + // Bug #19334: textarea jumps when editing articles in IE8 + $wgOut->addStyle( 'common/IE80Fixes.css', 'screen', 'IE 8' ); + wfProfileIn( __METHOD__."-business-end" ); $this->isConflict = false; @@ -539,10 +538,69 @@ class EditPage { return $permErrors; } + /** + * Display a permissions error page, like OutputPage::showPermissionsErrorPage(), + * but with the following differences: + * - If redlink=1, the user will be redirect to the page + * - If there is content to display or the error occurs while either saving, + * previewing or showing the difference, it will be a + * "View source for ..." page displaying the source code after the error message. + * + * @since 1.19 + * @param $permErrors Array of permissions errors, as returned by + * Title::getUserPermissionsErrors(). + */ + protected function displayPermissionsError( array $permErrors ) { + global $wgRequest, $wgOut; + + if ( $wgRequest->getBool( 'redlink' ) ) { + // The edit page was reached via a red link. + // Redirect to the article page and let them click the edit tab if + // they really want a permission error. + $wgOut->redirect( $this->mTitle->getFullUrl() ); + return; + } + + $content = $this->getContent(); + + # Use the normal message if there's nothing to display + if ( $this->firsttime && $content === '' ) { + $action = $this->mTitle->exists() ? 'edit' : + ( $permission = $this->mTitle->isTalkPage() ? 'createtalk' : 'createpage' ); + throw new PermissionsError( $action, $permErrors ); + } + + $wgOut->setPageTitle( wfMessage( 'viewsource' ) ); + $wgOut->setSubtitle( + wfMessage( 'viewsourcefor', Linker::linkKnown( $this->mTitle ) )->text() + ); + $wgOut->addWikiText( $wgOut->formatPermissionsErrorMessage( $permErrors, 'edit' ) ); + $wgOut->addHTML( "
\n" ); + + # If the user made changes, preserve them when showing the markup + # (This happens when a user is blocked during edit, for instance) + if ( !$this->firsttime ) { + $content = $this->textbox1; + $wgOut->addWikiMsg( 'viewyourtext' ); + } else { + $wgOut->addWikiMsg( 'viewsourcetext' ); + } + + $this->showTextbox( $content, 'wpTextbox1', array( 'readonly' ) ); + + $wgOut->addHTML( Html::rawElement( 'div', array( 'class' => 'templatesUsed' ), + Linker::formatTemplates( $this->getTemplates() ) ) ); + + if ( $this->mTitle->exists() ) { + $wgOut->returnToMain( null, $this->mTitle ); + } + } + /** * Show a read-only error * Parameters are the same as OutputPage:readOnlyPage() * Redirect to the article page if redlink=1 + * @deprecated in 1.19; use displayPermissionsError() instead */ function readOnlyPage( $source = null, $protected = false, $reasons = array(), $action = null ) { global $wgRequest, $wgOut; @@ -2188,26 +2246,13 @@ HTML /** * Call the stock "user is blocked" page + * + * @deprecated in 1.19; throw an exception directly instead */ function blockedPage() { - global $wgOut; - $wgOut->blockedPage( false ); # Standard block notice on the top, don't 'return' - - # If the user made changes, preserve them when showing the markup - # (This happens when a user is blocked during edit, for instance) - $first = $this->firsttime || ( !$this->save && $this->textbox1 == '' ); - if ( $first ) { - $source = $this->mTitle->exists() ? $this->getContent() : false; - } else { - $source = $this->textbox1; - } + global $wgUser; - # Spit out the source or the user's modified version - if ( $source !== false ) { - $wgOut->addHTML( '
' ); - $wgOut->addWikiMsg( $first ? 'blockedoriginalsource' : 'blockededitsource', $this->mTitle->getPrefixedText() ); - $this->showTextbox1( array( 'readonly' ), $source ); - } + throw new UserBlockedError( $wgUser->mBlock ); } /** @@ -2216,6 +2261,8 @@ HTML function userNotLoggedInPage() { global $wgOut; + $wgOut->prepareErrorPage( wfMessage( 'whitelistedittitle' ) ); + $loginTitle = SpecialPage::getTitleFor( 'Userlogin' ); $loginLink = Linker::linkKnown( $loginTitle, @@ -2223,15 +2270,20 @@ HTML array(), array( 'returnto' => $this->getContextTitle()->getPrefixedText() ) ); - - $wgOut->setPageTitle( wfMessage( 'whitelistedittitle' ) ); - $wgOut->setRobotPolicy( 'noindex,nofollow' ); - $wgOut->setArticleRelated( false ); - $wgOut->addHTML( wfMessage( 'whitelistedittext' )->rawParams( $loginLink )->parse() ); $wgOut->returnToMain( false, $this->getContextTitle() ); } + /** + * Show an error page saying to the user that he has insufficient permissions + * to create a new page + * + * @deprecated in 1.19; throw an exception directly instead + */ + function noCreatePermission() { + throw new MWException( 'nocreatetitle', 'nocreatetext' ); + } + /** * Creates a basic error page which informs the user that * they have attempted to edit a nonexistent section. @@ -2239,9 +2291,7 @@ HTML function noSuchSectionPage() { global $wgOut; - $wgOut->setPageTitle( wfMessage( 'nosuchsectiontitle' ) ); - $wgOut->setRobotPolicy( 'noindex,nofollow' ); - $wgOut->setArticleRelated( false ); + $wgOut->prepareErrorPage( wfMessage( 'nosuchsectiontitle' ) ); $res = wfMsgExt( 'nosuchsectiontext', 'parse', $this->section ); wfRunHooks( 'EditPageNoSuchSection', array( &$this, &$res ) ); @@ -2259,9 +2309,7 @@ HTML static function spamPage( $match = false ) { global $wgOut, $wgTitle; - $wgOut->setPageTitle( wfMessage( 'spamprotectiontitle' ) ); - $wgOut->setRobotPolicy( 'noindex,nofollow' ); - $wgOut->setArticleRelated( false ); + $wgOut->prepareErrorPage( wfMessage( 'spamprotectiontitle' ) ); $wgOut->addHTML( '
' ); $wgOut->addWikiMsg( 'spamprotectiontext' ); @@ -2282,9 +2330,7 @@ HTML global $wgOut; $this->textbox2 = $this->textbox1; - $wgOut->setPageTitle( wfMessage( 'spamprotectiontitle' ) ); - $wgOut->setRobotPolicy( 'noindex,nofollow' ); - $wgOut->setArticleRelated( false ); + $wgOut->prepareErrorPage( wfMessage( 'spamprotectiontitle' ) ); $wgOut->addHTML( '
' ); $wgOut->addWikiMsg( 'spamprotectiontext' ); @@ -2841,12 +2887,6 @@ HTML return strtr( $result, array( "�" => "&#x" ) ); } - function noCreatePermission() { - global $wgOut; - $wgOut->setPageTitle( wfMessage( 'nocreatetitle' ) ); - $wgOut->addWikiMsg( 'nocreatetext' ); - } - /** * Attempt submission * @return bool false if output is done, true if the rest of the form should be displayed @@ -2901,42 +2941,39 @@ HTML $wgOut->redirect( $this->mTitle->getFullURL( $extraQuery ) . $sectionanchor ); return false; + case self::AS_BLANK_ARTICLE: + $wgOut->redirect( $this->getContextTitle()->getFullURL() ); + return false; + case self::AS_SPAM_ERROR: $this->spamPageWithContent( $resultDetails['spam'] ); return false; case self::AS_BLOCKED_PAGE_FOR_USER: - $this->blockedPage(); - return false; + throw new UserBlockedError( $wgUser->mBlock ); case self::AS_IMAGE_REDIRECT_ANON: - $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' ); - return false; + 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' ); + case self::AS_READ_ONLY_PAGE: - $wgOut->readOnlyPage(); - return false; + throw new ReadOnlyError; case self::AS_RATE_LIMITED: - $wgOut->rateLimited(); - return false; + throw new ThrottledError(); case self::AS_NO_CREATE_PERMISSION: - $this->noCreatePermission(); - return false; + throw new MWException( 'nocreatetitle', 'nocreatetext' ); - case self::AS_BLANK_ARTICLE: - $wgOut->redirect( $this->getContextTitle()->getFullURL() ); - return false; - - case self::AS_IMAGE_REDIRECT_LOGGED: - $wgOut->permissionRequired( 'upload' ); - return false; } return false; } diff --git a/languages/messages/MessagesEn.php b/languages/messages/MessagesEn.php index 69cef9ddb0..03b1687712 100644 --- a/languages/messages/MessagesEn.php +++ b/languages/messages/MessagesEn.php @@ -1011,6 +1011,7 @@ Query: $2', Please try again in a few minutes.', 'protectedpagetext' => 'This page has been protected to prevent editing.', 'viewsourcetext' => 'You can view and copy the source of this page:', +'viewyourtext' => "You can view and copy the source of '''your edits''' to this page:", 'protectedinterface' => 'This page provides interface text for the software, and is protected to prevent abuse.', 'editinginterface' => "'''Warning:''' You are editing a page which is used to provide interface text for the software. Changes to this page will affect the appearance of the user interface for other users. @@ -1297,8 +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', -'blockedoriginalsource' => "The source of '''$1''' is shown below:", -'blockededitsource' => "The text of '''your edits''' to '''$1''' is shown below:", 'whitelistedittitle' => 'Login required to edit', 'whitelistedittext' => 'You have to $1 to edit pages.', 'confirmedittext' => 'You must confirm your e-mail address before editing pages. diff --git a/languages/messages/MessagesQqq.php b/languages/messages/MessagesQqq.php index ea7e7cc9a5..fe948520b5 100644 --- a/languages/messages/MessagesQqq.php +++ b/languages/messages/MessagesQqq.php @@ -660,6 +660,7 @@ $1 is a filename, I think.', * $1: the protection type, e.g. "protect" for fully protected pages', 'viewsourcetext' => 'The text shown when displaying the source of a page that the user has no permission to edit', +'viewyourtext' => 'Same as {{msg-mw|viewsourcetext}} but when showing the text submitted by the user, this happens e.g. when the user was blocked while he is editing the page', 'protectedinterface' => 'Message shown if a user without the "editinterface" right tries to edit a page in the MediaWiki namespace.', 'editinginterface' => "A message shown when editing pages in the namespace MediaWiki:. In the [http://translatewiki.net/wiki/Main_Page?setlang=en URL], '''change \"setlang=en\" to your own language code.'''", 'ns-specialprotected' => 'Error message displayed when trying to edit a page in the Special namespace', diff --git a/maintenance/language/messages.inc b/maintenance/language/messages.inc index c927defef2..a8e6e01c47 100644 --- a/maintenance/language/messages.inc +++ b/maintenance/language/messages.inc @@ -398,6 +398,7 @@ $wgMessageStructure = array( 'actionthrottledtext', 'protectedpagetext', 'viewsourcetext', + 'viewyourtext', 'protectedinterface', 'editinginterface', 'sqlhidden', @@ -587,8 +588,6 @@ $wgMessageStructure = array( 'blockedtext', 'autoblockedtext', 'blockednoreason', - 'blockedoriginalsource', - 'blockededitsource', 'whitelistedittitle', 'whitelistedittext', 'confirmedittext', -- 2.20.1