Refactor badaccess-groupX and friends to use User::getGroupsWithPermission().
authorChad Horohoe <demon@users.mediawiki.org>
Tue, 5 Aug 2008 02:37:28 +0000 (02:37 +0000)
committerChad Horohoe <demon@users.mediawiki.org>
Tue, 5 Aug 2008 02:37:28 +0000 (02:37 +0000)
* Should be a bit cleaner, and remove those ugly ugly switch() statements.
* Two messages are all that's needed here. Badaccess-group0, a generic "Permission denied" when no group is allowed access; and badaccess-groups which takes two parameters of $1 (groups required for access) and $2 (number of group matches, used for {{PLURAL:}}ing the message).

includes/OutputPage.php
includes/Title.php
languages/messages/MessagesEn.php

index 22de81d..1a55c81 100644 (file)
@@ -1069,28 +1069,22 @@ class OutputPage {
                $this->mBodytext = '';
 
                $groups = array();
-               foreach( $wgGroupPermissions as $key => $value ) {
-                       if( isset( $value[$permission] ) && $value[$permission] == true ) {
-                               $groupName = User::getGroupName( $key );
-                               $groupPage = User::getGroupPage( $key );
-                               if( $groupPage ) {
-                                       $skin = $wgUser->getSkin();
-                                       $groups[] = $skin->makeLinkObj( $groupPage, $groupName );
-                               } else {
-                                       $groups[] = $groupName;
-                               }
+               foreach( User::getGroupsWithPermission( $permission ) as $key ) {
+                       $groupName = User::getGroupName( $key );
+                       $groupPage = User::getGroupPage( $key );
+                       if( $groupPage ) {
+                               $skin = $wgUser->getSkin();
+                               $groups[] = $skin->makeLinkObj( $groupPage, $groupName );
+                       } else {
+                               $groups[] = $groupName;
                        }
                }
                $n = count( $groups );
-               $groups = implode( ', ', $groups );
-               switch( $n ) {
-                       case 0:
-                       case 1:
-                       case 2:
-                               $message = wfMsgHtml( "badaccess-group$n", $groups );
-                               break;
-                       default:
-                               $message = wfMsgHtml( 'badaccess-groups', $groups );
+               if ( $count > 0 ) {
+                       $message = wfMsgHtml( 'badaccess-groups', array( $groups, $n ) );
+               }
+               else {
+                       $message = wfMsgHtml( "badaccess-group0", $groups );
                }
                $this->addHtml( $message );
                $this->returnToMain();
index 859d98f..f885740 100644 (file)
@@ -1308,28 +1308,22 @@ class Title {
                } elseif ( !$user->isAllowed( $action ) ) {
                        $return = null;
                        $groups = array();
-                       global $wgGroupPermissions;
-                       foreach( $wgGroupPermissions as $key => $value ) {
-                               if( isset( $value[$action] ) && $value[$action] == true ) {
-                                       $groupName = User::getGroupName( $key );
-                                       $groupPage = User::getGroupPage( $key );
-                                       if( $groupPage ) {
-                                               $groups[] = '[['.$groupPage->getPrefixedText().'|'.$groupName.']]';
-                                       } else {
-                                               $groups[] = $groupName;
-                                       }
+                       foreach( User::getGroupsWithPermission( $action ) as $key ) {
+                               $groupName = User::getGroupName( $key );
+                               $groupPage = User::getGroupPage( $key );
+                               if( $groupPage ) {
+                                       $groups[] = '[['.$groupPage->getPrefixedText().'|'.$groupName.']]';
+                               } else {
+                                       $groups[] = $groupName;
                                }
                        }
                        $n = count( $groups );
-                       $groups = implode( ', ', $groups );
-                       switch( $n ) {
-                               case 0:
-                               case 1:
-                               case 2:
-                                       $return = array( "badaccess-group$n", $groups );
-                                       break;
-                               default:
-                                       $return = array( 'badaccess-groups', $groups );
+                       if ( $n > 0 ) {
+                               $groups = implode( ', ', $groups );
+                               $return = array( 'badaccess-groups', array( $groups, $n ) );
+                       }
+                       else {
+                               $return = array( "badaccess-group0", $groups );
                        }
                        $errors[] = $return;
                }
index dfd91a6..6698a30 100644 (file)
@@ -719,9 +719,7 @@ XHTML id names.
 
 'badaccess'        => 'Permission error',
 'badaccess-group0' => 'You are not allowed to execute the action you have requested.',
-'badaccess-group1' => 'The action you have requested is limited to users in the group $1.',
-'badaccess-group2' => 'The action you have requested is limited to users in one of the groups $1.',
-'badaccess-groups' => 'The action you have requested is limited to users in one of the groups $1.',
+'badaccess-groups' => 'The action you have requested is limited to users in {{PLURAL:$2|the group|one of the groups}}: $1.',
 
 'versionrequired'     => 'Version $1 of MediaWiki required',
 'versionrequiredtext' => 'Version $1 of MediaWiki is required to use this page.