From: Andrew Garrett Date: Fri, 23 May 2008 10:34:11 +0000 (+0000) Subject: Core changes for GlobalBlocking and TorBlock extensions, plus some core refactoring... X-Git-Tag: 1.31.0-rc.0~47443 X-Git-Url: https://git.cyclocoop.org/%242?a=commitdiff_plain;h=bc23aede55e1fd3dcbcf26599a37617953b58628;p=lhc%2Fweb%2Fwiklou.git Core changes for GlobalBlocking and TorBlock extensions, plus some core refactoring work: * Instead of saying 'do that' in a permissions error, actually list what the action is (drawn from the right-$1 messages). This isn't perfect - it says you don't have permission to edit pages when you can't edit a single page, but it's better than 'do that'. * Refactor out some code from various block files into Block::formatExpiry and Block::parseExpiryInput. * Don't display 'you cannot edit special pages' when you're trying to execute, or create an account, or something like that. * New AbortAutoblock hook (for use in TorBlock extension), which allows extensions to cancel autoblocks. --- diff --git a/docs/hooks.txt b/docs/hooks.txt index cd953943ff..bc12558ca4 100644 --- a/docs/hooks.txt +++ b/docs/hooks.txt @@ -238,6 +238,10 @@ protocol came about after MediaWiki 1.4rc1. This is a list of known events and parameters; please add to it if you're going to add events to the MediaWiki code. +'AbortAutoblock': Return false to cancel an autoblock. +$autoblockip: The IP going to be autoblocked. +$block: The block from which the autoblock is coming. + 'AbortLogin': Return false to cancel account login. $user: the User object being authenticated against $password: the password being submitted, not yet checked for validity diff --git a/includes/Block.php b/includes/Block.php index 1c0bfb0c7e..3e91cf5720 100644 --- a/includes/Block.php +++ b/includes/Block.php @@ -486,6 +486,12 @@ class Block wfDebug( " No match\n" ); } } + + ## Allow hooks to cancel the autoblock. + if (!wfRunHooks( 'AbortAutoblock', array( $autoblockip, &$this ) )) { + wfDebug( "Autoblock aborted by hook." ); + return false; + } # It's okay to autoblock. Go ahead and create/insert the block. @@ -704,5 +710,48 @@ class Block return $infinity; */ } + + /** + * Convert a DB-encoded expiry into a real string that humans can read. + */ + static function formatExpiry( $encoded_expiry ) { + + static $msg = null; + + if( is_null( $msg ) ) { + $msg = array(); + $keys = array( 'infiniteblock', 'expiringblock' ); + foreach( $keys as $key ) { + $msg[$key] = wfMsgHtml( $key ); + } + } + + $expiry = Block::decodeExpiry( $encoded_expiry ); + if ($expiry == 'infinity') { + $expirystr = $msg['infiniteblock']; + } else { + global $wgLang; + $expiretimestr = $wgLang->timeanddate( wfTimestamp( TS_MW, $expiry ), true ); + $expirystr = wfMsgReplaceArgs( $msg['expiringblock'], array($expiretimestr) ); + } + + return $expirystr; + } + + /** + * Convert a typed-in expiry time into something we can put into the database. + */ + static function parseExpiryInput( $expiry_input ) { + if ( $expiry_input == 'infinite' || $expiry_input == 'indefinite' ) { + $expiry = 'infinity'; + } else { + $expiry = strtotime( $expiry_input ); + if ($expiry < 0 || $expiry === false) { + return false; + } + } + + return $expiry; + } } diff --git a/includes/EditPage.php b/includes/EditPage.php index cb25581010..a9855ad31c 100644 --- a/includes/EditPage.php +++ b/includes/EditPage.php @@ -389,6 +389,7 @@ class EditPage { } $permErrors = $this->mTitle->getUserPermissionsErrors('edit', $wgUser); + if( !$this->mTitle->exists() ) { $permErrors = array_merge( $permErrors, wfArrayDiff2( $this->mTitle->getUserPermissionsErrors('create', $wgUser), $permErrors ) ); @@ -414,10 +415,10 @@ class EditPage { } } $permErrors = wfArrayDiff2( $permErrors, $remove ); - + if ( $permErrors ) { wfDebug( __METHOD__.": User can't edit\n" ); - $this->readOnlyPage( $this->getContent(), true, $permErrors ); + $this->readOnlyPage( $this->getContent(), true, $permErrors, 'edit' ); wfProfileOut( __METHOD__ ); return; } else { @@ -489,7 +490,7 @@ class EditPage { * Parameters are the same as OutputPage:readOnlyPage() * Redirect to the article page if redlink=1 */ - function readOnlyPage( $source = null, $protected = false, $reasons = array() ) { + function readOnlyPage( $source = null, $protected = false, $reasons = array(), $action = null ) { global $wgRequest, $wgOut; if ( $wgRequest->getBool( 'redlink' ) ) { // The edit page was reached via a red link. @@ -497,7 +498,7 @@ class EditPage { // they really want a permission error. $wgOut->redirect( $this->mTitle->getFullUrl() ); } else { - $wgOut->readOnlyPage( $source, $protected, $reasons ); + $wgOut->readOnlyPage( $source, $protected, $reasons, $action ); } } diff --git a/includes/OutputPage.php b/includes/OutputPage.php index 0922e14717..0b2b6c5db3 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -960,7 +960,7 @@ class OutputPage { * * @param array $errors Error message keys */ - public function showPermissionsErrorPage( $errors ) + public function showPermissionsErrorPage( $errors, $action = null ) { global $wgTitle; @@ -973,7 +973,7 @@ class OutputPage { $this->enableClientCache( false ); $this->mRedirect = ''; $this->mBodytext = ''; - $this->addWikiText( $this->formatPermissionsErrorMessage( $errors ) ); + $this->addWikiText( $this->formatPermissionsErrorMessage( $errors, $action ) ); } /** @deprecated */ @@ -1096,8 +1096,14 @@ class OutputPage { * @param array $errors An array of arrays returned by Title::getUserPermissionsErrors * @return string The wikitext error-messages, formatted into a list. */ - public function formatPermissionsErrorMessage( $errors ) { - $text = wfMsgNoTrans( 'permissionserrorstext', count( $errors ) ) . "\n\n"; + public function formatPermissionsErrorMessage( $errors, $action = null ) { + if ($action == null) { + $text = wfMsgNoTrans( 'permissionserrorstext', count($errors)). "\n\n"; + } else { + $action_desc = wfMsg( "right-$action" ); + $action_desc[0] = strtolower($action_desc[0]); + $text = wfMsgNoTrans( 'permissionserrorstext-withaction', count($errors), $action_desc ) . "\n\n"; + } if (count( $errors ) > 1) { $text .= '