r35224 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r35223‎ | r35224 | r35225 >
Date:10:34, 23 May 2008
Author:werdna
Status:old (Comments)
Tags:
Comment:
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.
Modified paths:
  • /trunk/phase3/docs/hooks.txt (modified) (history)
  • /trunk/phase3/includes/Block.php (modified) (history)
  • /trunk/phase3/includes/EditPage.php (modified) (history)
  • /trunk/phase3/includes/OutputPage.php (modified) (history)
  • /trunk/phase3/includes/SpecialBlockip.php (modified) (history)
  • /trunk/phase3/includes/SpecialIpblocklist.php (modified) (history)
  • /trunk/phase3/includes/SpecialUserlogin.php (modified) (history)
  • /trunk/phase3/includes/Title.php (modified) (history)
  • /trunk/phase3/includes/User.php (modified) (history)
  • /trunk/phase3/languages/messages/MessagesEn.php (modified) (history)

Diff [purge]

Index: trunk/phase3/docs/hooks.txt
@@ -238,6 +238,10 @@
239239 This is a list of known events and parameters; please add to it if
240240 you're going to add events to the MediaWiki code.
241241
 242+'AbortAutoblock': Return false to cancel an autoblock.
 243+$autoblockip: The IP going to be autoblocked.
 244+$block: The block from which the autoblock is coming.
 245+
242246 'AbortLogin': Return false to cancel account login.
243247 $user: the User object being authenticated against
244248 $password: the password being submitted, not yet checked for validity
Index: trunk/phase3/includes/User.php
@@ -1036,7 +1036,6 @@
10371037
10381038 # Proxy blocking
10391039 if ( !$this->isAllowed('proxyunbannable') && !in_array( $ip, $wgProxyWhitelist ) ) {
1040 -
10411040 # Local list
10421041 if ( wfIsLocallyBlockedProxy( $ip ) ) {
10431042 $this->mBlockedby = wfMsg( 'proxyblocker' );
Index: trunk/phase3/includes/SpecialUserlogin.php
@@ -665,14 +665,16 @@
666666 }
667667
668668 /** */
669 - function userNotPrivilegedMessage() {
 669+ function userNotPrivilegedMessage($errors) {
670670 global $wgOut;
671671
672 - $wgOut->setPageTitle( wfMsg( 'whitelistacctitle' ) );
 672+ $wgOut->setPageTitle( wfMsg( 'permissionserrors' ) );
673673 $wgOut->setRobotpolicy( 'noindex,nofollow' );
674674 $wgOut->setArticleRelated( false );
675675
676 - $wgOut->addWikiMsg( 'whitelistacctext' );
 676+ $wgOut->addWikitext( $wgOut->formatPermissionsErrorMessage( $errors, 'createaccount' ) );
 677+ // Stuff that might want to be added at the end. For example, instructions if blocked.
 678+ $wgOut->addWikiMsg( 'cantcreateaccount-nonblock-text' );
677679
678680 $wgOut->returnToMain( false );
679681 }
@@ -711,14 +713,16 @@
712714 global $wgUser, $wgOut, $wgAllowRealName, $wgEnableEmail;
713715 global $wgCookiePrefix, $wgAuth, $wgLoginLanguageSelector;
714716 global $wgAuth, $wgEmailConfirmToEdit;
 717+
 718+ $titleObj = SpecialPage::getTitleFor( 'Userlogin' );
715719
716720 if ( $this->mType == 'signup' ) {
717 - if ( !$wgUser->isAllowed( 'createaccount' ) ) {
718 - $this->userNotPrivilegedMessage();
719 - return;
720 - } elseif ( $wgUser->isBlockedFromCreateAccount() ) {
 721+ if ( $wgUser->isBlockedFromCreateAccount() ) {
721722 $this->userBlockedMessage();
722723 return;
 724+ } elseif ( count( $permErrors = $titleObj->getUserPermissionsErrors( 'createaccount', $wgUser, true ) )>0 ) {
 725+ $wgOut->showPermissionsErrorPage( $permErrors, 'createaccount' );
 726+ return;
723727 }
724728 }
725729
Index: trunk/phase3/includes/EditPage.php
@@ -389,6 +389,7 @@
390390 }
391391
392392 $permErrors = $this->mTitle->getUserPermissionsErrors('edit', $wgUser);
 393+
393394 if( !$this->mTitle->exists() ) {
394395 $permErrors = array_merge( $permErrors,
395396 wfArrayDiff2( $this->mTitle->getUserPermissionsErrors('create', $wgUser), $permErrors ) );
@@ -414,10 +415,10 @@
415416 }
416417 }
417418 $permErrors = wfArrayDiff2( $permErrors, $remove );
418 -
 419+
419420 if ( $permErrors ) {
420421 wfDebug( __METHOD__.": User can't edit\n" );
421 - $this->readOnlyPage( $this->getContent(), true, $permErrors );
 422+ $this->readOnlyPage( $this->getContent(), true, $permErrors, 'edit' );
422423 wfProfileOut( __METHOD__ );
423424 return;
424425 } else {
@@ -489,7 +490,7 @@
490491 * Parameters are the same as OutputPage:readOnlyPage()
491492 * Redirect to the article page if redlink=1
492493 */
493 - function readOnlyPage( $source = null, $protected = false, $reasons = array() ) {
 494+ function readOnlyPage( $source = null, $protected = false, $reasons = array(), $action = null ) {
494495 global $wgRequest, $wgOut;
495496 if ( $wgRequest->getBool( 'redlink' ) ) {
496497 // The edit page was reached via a red link.
@@ -497,7 +498,7 @@
498499 // they really want a permission error.
499500 $wgOut->redirect( $this->mTitle->getFullUrl() );
500501 } else {
501 - $wgOut->readOnlyPage( $source, $protected, $reasons );
 502+ $wgOut->readOnlyPage( $source, $protected, $reasons, $action );
502503 }
503504 }
504505
Index: trunk/phase3/includes/SpecialBlockip.php
@@ -342,18 +342,10 @@
343343 if (strlen($expirestr) == 0) {
344344 return array('ipb_expiry_invalid');
345345 }
346 -
347 - if ( $expirestr == 'infinite' || $expirestr == 'indefinite' ) {
348 - $expiry = Block::infinity();
349 - } else {
350 - # Convert GNU-style date, on error returns -1 for PHP <5.1 and false for PHP >=5.1
351 - $expiry = strtotime( $expirestr );
352 -
353 - if ( $expiry < 0 || $expiry === false ) {
354 - return array('ipb_expiry_invalid');
355 - }
356 -
357 - $expiry = wfTimestamp( TS_MW, $expiry );
 346+
 347+ if ( false === ($expiry = Block::parseExpiryInput( $expirestr )) ) {
 348+ // Bad expiry.
 349+ return array('ipb_expiry_invalid');
358350 }
359351
360352 # Create block
Index: trunk/phase3/includes/OutputPage.php
@@ -960,7 +960,7 @@
961961 *
962962 * @param array $errors Error message keys
963963 */
964 - public function showPermissionsErrorPage( $errors )
 964+ public function showPermissionsErrorPage( $errors, $action = null )
965965 {
966966 global $wgTitle;
967967
@@ -973,7 +973,7 @@
974974 $this->enableClientCache( false );
975975 $this->mRedirect = '';
976976 $this->mBodytext = '';
977 - $this->addWikiText( $this->formatPermissionsErrorMessage( $errors ) );
 977+ $this->addWikiText( $this->formatPermissionsErrorMessage( $errors, $action ) );
978978 }
979979
980980 /** @deprecated */
@@ -1096,8 +1096,14 @@
10971097 * @param array $errors An array of arrays returned by Title::getUserPermissionsErrors
10981098 * @return string The wikitext error-messages, formatted into a list.
10991099 */
1100 - public function formatPermissionsErrorMessage( $errors ) {
1101 - $text = wfMsgNoTrans( 'permissionserrorstext', count( $errors ) ) . "\n\n";
 1100+ public function formatPermissionsErrorMessage( $errors, $action = null ) {
 1101+ if ($action == null) {
 1102+ $text = wfMsgNoTrans( 'permissionserrorstext', count($errors)). "\n\n";
 1103+ } else {
 1104+ $action_desc = wfMsg( "right-$action" );
 1105+ $action_desc[0] = strtolower($action_desc[0]);
 1106+ $text = wfMsgNoTrans( 'permissionserrorstext-withaction', count($errors), $action_desc ) . "\n\n";
 1107+ }
11021108
11031109 if (count( $errors ) > 1) {
11041110 $text .= '<ul class="permissions-errors">' . "\n";
@@ -1135,7 +1141,7 @@
11361142 * @param bool $protected Is this a permissions error?
11371143 * @param array $reasons List of reasons for this error, as returned by Title::getUserPermissionsErrors().
11381144 */
1139 - public function readOnlyPage( $source = null, $protected = false, $reasons = array() ) {
 1145+ public function readOnlyPage( $source = null, $protected = false, $reasons = array(), $action = null ) {
11401146 global $wgUser, $wgTitle;
11411147 $skin = $wgUser->getSkin();
11421148
@@ -1156,7 +1162,7 @@
11571163 } else {
11581164 $this->setPageTitle( wfMsg( 'badaccess' ) );
11591165 }
1160 - $this->addWikiText( $this->formatPermissionsErrorMessage( $reasons ) );
 1166+ $this->addWikiText( $this->formatPermissionsErrorMessage( $reasons, $action ) );
11611167 } else {
11621168 // Wiki is read only
11631169 $this->setPageTitle( wfMsg( 'readonly' ) );
Index: trunk/phase3/includes/SpecialIpblocklist.php
@@ -327,12 +327,7 @@
328328 $formattedTime = $wgLang->timeanddate( $block->mTimestamp, true );
329329
330330 $properties = array();
331 - if ( $block->mExpiry === "" || $block->mExpiry === Block::infinity() ) {
332 - $properties[] = $msg['infiniteblock'];
333 - } else {
334 - $properties[] = wfMsgReplaceArgs( $msg['expiringblock'],
335 - array( $wgLang->timeanddate( $block->mExpiry, true ) ) );
336 - }
 331+ $properties[] = Block::formatExpiry( $block->mExpiry );
337332 if ( $block->mAnonOnly ) {
338333 $properties[] = $msg['anononlyblock'];
339334 }
Index: trunk/phase3/includes/Title.php
@@ -1158,8 +1158,9 @@
11591159 else if ($result === false )
11601160 $errors[] = array('badaccess-group0'); # a generic "We don't want them to do that"
11611161 }
1162 -
1163 - if( NS_SPECIAL == $this->mNamespace ) {
 1162+
 1163+ $specialOKActions = array( 'createaccount', 'execute' );
 1164+ if( NS_SPECIAL == $this->mNamespace && !in_array( $action, $specialOKActions) ) {
11641165 $errors[] = array('ns-specialprotected');
11651166 }
11661167
Index: trunk/phase3/includes/Block.php
@@ -486,6 +486,12 @@
487487 wfDebug( " No match\n" );
488488 }
489489 }
 490+
 491+ ## Allow hooks to cancel the autoblock.
 492+ if (!wfRunHooks( 'AbortAutoblock', array( $autoblockip, &$this ) )) {
 493+ wfDebug( "Autoblock aborted by hook." );
 494+ return false;
 495+ }
490496
491497 # It's okay to autoblock. Go ahead and create/insert the block.
492498
@@ -704,5 +710,48 @@
705711 return $infinity;
706712 */
707713 }
 714+
 715+ /**
 716+ * Convert a DB-encoded expiry into a real string that humans can read.
 717+ */
 718+ static function formatExpiry( $encoded_expiry ) {
 719+
 720+ static $msg = null;
 721+
 722+ if( is_null( $msg ) ) {
 723+ $msg = array();
 724+ $keys = array( 'infiniteblock', 'expiringblock' );
 725+ foreach( $keys as $key ) {
 726+ $msg[$key] = wfMsgHtml( $key );
 727+ }
 728+ }
 729+
 730+ $expiry = Block::decodeExpiry( $encoded_expiry );
 731+ if ($expiry == 'infinity') {
 732+ $expirystr = $msg['infiniteblock'];
 733+ } else {
 734+ global $wgLang;
 735+ $expiretimestr = $wgLang->timeanddate( wfTimestamp( TS_MW, $expiry ), true );
 736+ $expirystr = wfMsgReplaceArgs( $msg['expiringblock'], array($expiretimestr) );
 737+ }
708738
 739+ return $expirystr;
 740+ }
 741+
 742+ /**
 743+ * Convert a typed-in expiry time into something we can put into the database.
 744+ */
 745+ static function parseExpiryInput( $expiry_input ) {
 746+ if ( $expiry_input == 'infinite' || $expiry_input == 'indefinite' ) {
 747+ $expiry = 'infinity';
 748+ } else {
 749+ $expiry = strtotime( $expiry_input );
 750+ if ($expiry < 0 || $expiry === false) {
 751+ return false;
 752+ }
 753+ }
 754+
 755+ return $expiry;
 756+ }
 757+
709758 }
Index: trunk/phase3/languages/messages/MessagesEn.php
@@ -1130,6 +1130,7 @@
11311131 'nocreate-loggedin' => 'You do not have permission to create new pages on {{SITENAME}}.',
11321132 'permissionserrors' => 'Permissions Errors',
11331133 'permissionserrorstext' => 'You do not have permission to do that, for the following {{PLURAL:$1|reason|reasons}}:',
 1134+'permissionserrorstext-withaction' => 'You do not have permission to $2, for the following {{PLURAL:$1|reason|reasons}}:',
11341135 'recreate-deleted-warn' => "'''Warning: You are recreating a page that was previously deleted.'''
11351136
11361137 You should consider whether it is appropriate to continue editing this page.
@@ -1158,6 +1159,7 @@
11591160 'cantcreateaccount-text' => "Account creation from this IP address ('''$1''') has been blocked by [[User:$3|$3]].
11601161
11611162 The reason given by $3 is ''$2''",
 1163+'cantcreateaccount-nonblock-text' => '', # do not translate or duplicate this message to other languages
11621164
11631165 # History pages
11641166 'viewpagelogs' => 'View logs for this page',

Comments

#Comment by Reedy (talk | contribs)   17:46, 2 December 2010

Ping Werdna, any chance you could chip in on r75759?

It's a followup to this (was fixin a wrong function call), but we're struggling to see which way is best to "fix" it

Status & tagging log