MediaWiki  REL1_24
SpecialListgrouprights.php
Go to the documentation of this file.
00001 <?php
00031 class SpecialListGroupRights extends SpecialPage {
00032     function __construct() {
00033         parent::__construct( 'Listgrouprights' );
00034     }
00035 
00040     public function execute( $par ) {
00041         $this->setHeaders();
00042         $this->outputHeader();
00043 
00044         $out = $this->getOutput();
00045         $out->addModuleStyles( 'mediawiki.special' );
00046 
00047         $out->wrapWikiMsg( "<div class=\"mw-listgrouprights-key\">\n$1\n</div>", 'listgrouprights-key' );
00048 
00049         $out->addHTML(
00050             Xml::openElement( 'table', array( 'class' => 'wikitable mw-listgrouprights-table' ) ) .
00051                 '<tr>' .
00052                 Xml::element( 'th', null, $this->msg( 'listgrouprights-group' )->text() ) .
00053                 Xml::element( 'th', null, $this->msg( 'listgrouprights-rights' )->text() ) .
00054                 '</tr>'
00055         );
00056 
00057         $config = $this->getConfig();
00058         $groupPermissions = $config->get( 'GroupPermissions' );
00059         $revokePermissions = $config->get( 'RevokePermissions' );
00060         $addGroups = $config->get( 'AddGroups' );
00061         $removeGroups = $config->get( 'RemoveGroups' );
00062         $groupsAddToSelf = $config->get( 'GroupsAddToSelf' );
00063         $groupsRemoveFromSelf = $config->get( 'GroupsRemoveFromSelf' );
00064         $allGroups = array_unique( array_merge(
00065             array_keys( $groupPermissions ),
00066             array_keys( $revokePermissions ),
00067             array_keys( $addGroups ),
00068             array_keys( $removeGroups ),
00069             array_keys( $groupsAddToSelf ),
00070             array_keys( $groupsRemoveFromSelf )
00071         ) );
00072         asort( $allGroups );
00073 
00074         foreach ( $allGroups as $group ) {
00075             $permissions = isset( $groupPermissions[$group] )
00076                 ? $groupPermissions[$group]
00077                 : array();
00078             $groupname = ( $group == '*' ) // Replace * with a more descriptive groupname
00079                 ? 'all'
00080                 : $group;
00081 
00082             $msg = $this->msg( 'group-' . $groupname );
00083             $groupnameLocalized = !$msg->isBlank() ? $msg->text() : $groupname;
00084 
00085             $msg = $this->msg( 'grouppage-' . $groupname )->inContentLanguage();
00086             $grouppageLocalized = !$msg->isBlank() ?
00087                 $msg->text() :
00088                 MWNamespace::getCanonicalName( NS_PROJECT ) . ':' . $groupname;
00089 
00090             if ( $group == '*' ) {
00091                 // Do not make a link for the generic * group
00092                 $grouppage = htmlspecialchars( $groupnameLocalized );
00093             } else {
00094                 $grouppage = Linker::link(
00095                     Title::newFromText( $grouppageLocalized ),
00096                     htmlspecialchars( $groupnameLocalized )
00097                 );
00098             }
00099 
00100             if ( $group === 'user' ) {
00101                 // Link to Special:listusers for implicit group 'user'
00102                 $grouplink = '<br />' . Linker::linkKnown(
00103                     SpecialPage::getTitleFor( 'Listusers' ),
00104                     $this->msg( 'listgrouprights-members' )->escaped()
00105                 );
00106             } elseif ( !in_array( $group, $config->get( 'ImplicitGroups' ) ) ) {
00107                 $grouplink = '<br />' . Linker::linkKnown(
00108                     SpecialPage::getTitleFor( 'Listusers' ),
00109                     $this->msg( 'listgrouprights-members' )->escaped(),
00110                     array(),
00111                     array( 'group' => $group )
00112                 );
00113             } else {
00114                 // No link to Special:listusers for other implicit groups as they are unlistable
00115                 $grouplink = '';
00116             }
00117 
00118             $revoke = isset( $revokePermissions[$group] ) ? $revokePermissions[$group] : array();
00119             $addgroups = isset( $addGroups[$group] ) ? $addGroups[$group] : array();
00120             $removegroups = isset( $removeGroups[$group] ) ? $removeGroups[$group] : array();
00121             $addgroupsSelf = isset( $groupsAddToSelf[$group] ) ? $groupsAddToSelf[$group] : array();
00122             $removegroupsSelf = isset( $groupsRemoveFromSelf[$group] )
00123                 ? $groupsRemoveFromSelf[$group]
00124                 : array();
00125 
00126             $id = $group == '*' ? false : Sanitizer::escapeId( $group );
00127             $out->addHTML( Html::rawElement( 'tr', array( 'id' => $id ), "
00128                 <td>$grouppage$grouplink</td>
00129                     <td>" .
00130                     $this->formatPermissions( $permissions, $revoke, $addgroups, $removegroups,
00131                         $addgroupsSelf, $removegroupsSelf ) .
00132                     '</td>
00133                 '
00134             ) );
00135         }
00136         $out->addHTML( Xml::closeElement( 'table' ) );
00137         $this->outputNamespaceProtectionInfo();
00138     }
00139 
00140     private function outputNamespaceProtectionInfo() {
00141         global $wgParser, $wgContLang;
00142         $out = $this->getOutput();
00143         $namespaceProtection = $this->getConfig()->get( 'NamespaceProtection' );
00144 
00145         if ( count( $namespaceProtection ) == 0 ) {
00146             return;
00147         }
00148 
00149         $header = $this->msg( 'listgrouprights-namespaceprotection-header' )->parse();
00150         $out->addHTML(
00151             Html::rawElement( 'h2', array(), Html::element( 'span', array(
00152                 'class' => 'mw-headline',
00153                 'id' => $wgParser->guessSectionNameFromWikiText( $header )
00154             ), $header ) ) .
00155             Xml::openElement( 'table', array( 'class' => 'wikitable' ) ) .
00156             Html::element(
00157                 'th',
00158                 array(),
00159                 $this->msg( 'listgrouprights-namespaceprotection-namespace' )->text()
00160             ) .
00161             Html::element(
00162                 'th',
00163                 array(),
00164                 $this->msg( 'listgrouprights-namespaceprotection-restrictedto' )->text()
00165             )
00166         );
00167 
00168         ksort( $namespaceProtection );
00169         foreach ( $namespaceProtection as $namespace => $rights ) {
00170             if ( !in_array( $namespace, MWNamespace::getValidNamespaces() ) ) {
00171                 continue;
00172             }
00173 
00174             if ( $namespace == NS_MAIN ) {
00175                 $namespaceText = $this->msg( 'blanknamespace' )->text();
00176             } else {
00177                 $namespaceText = $wgContLang->convertNamespace( $namespace );
00178             }
00179 
00180             $out->addHTML(
00181                 Xml::openElement( 'tr' ) .
00182                 Html::rawElement(
00183                     'td',
00184                     array(),
00185                     Linker::link(
00186                         SpecialPage::getTitleFor( 'Allpages' ),
00187                         $namespaceText,
00188                         array(),
00189                         array( 'namespace' => $namespace )
00190                     )
00191                 ) .
00192                 Xml::openElement( 'td' ) . Xml::openElement( 'ul' )
00193             );
00194 
00195             if ( !is_array( $rights ) ) {
00196                 $rights = array( $rights );
00197             }
00198 
00199             foreach ( $rights as $right ) {
00200                 $out->addHTML(
00201                     Html::rawElement( 'li', array(), $this->msg(
00202                         'listgrouprights-right-display',
00203                         User::getRightDescription( $right ),
00204                         Html::element(
00205                             'span',
00206                             array( 'class' => 'mw-listgrouprights-right-name' ),
00207                             $right
00208                         )
00209                     )->parse() )
00210                 );
00211             }
00212 
00213             $out->addHTML(
00214                 Xml::closeElement( 'ul' ) .
00215                 Xml::closeElement( 'td' ) .
00216                 Xml::closeElement( 'tr' )
00217             );
00218         }
00219         $out->addHTML( Xml::closeElement( 'table' ) );
00220     }
00221 
00233     private function formatPermissions( $permissions, $revoke, $add, $remove, $addSelf, $removeSelf ) {
00234         $r = array();
00235         foreach ( $permissions as $permission => $granted ) {
00236             //show as granted only if it isn't revoked to prevent duplicate display of permissions
00237             if ( $granted && ( !isset( $revoke[$permission] ) || !$revoke[$permission] ) ) {
00238                 $description = $this->msg( 'listgrouprights-right-display',
00239                     User::getRightDescription( $permission ),
00240                     '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
00241                 )->parse();
00242                 $r[] = $description;
00243             }
00244         }
00245         foreach ( $revoke as $permission => $revoked ) {
00246             if ( $revoked ) {
00247                 $description = $this->msg( 'listgrouprights-right-revoked',
00248                     User::getRightDescription( $permission ),
00249                     '<span class="mw-listgrouprights-right-name">' . $permission . '</span>'
00250                 )->parse();
00251                 $r[] = $description;
00252             }
00253         }
00254 
00255         sort( $r );
00256 
00257         $lang = $this->getLanguage();
00258         $allGroups = User::getAllGroups();
00259 
00260         if ( $add === true ) {
00261             $r[] = $this->msg( 'listgrouprights-addgroup-all' )->escaped();
00262         } elseif ( is_array( $add ) ) {
00263             $add = array_intersect( array_values( array_unique( $add ) ), $allGroups );
00264             if ( count( $add ) ) {
00265                 $r[] = $this->msg( 'listgrouprights-addgroup',
00266                     $lang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $add ) ),
00267                     count( $add )
00268                 )->parse();
00269             }
00270         }
00271 
00272         if ( $remove === true ) {
00273             $r[] = $this->msg( 'listgrouprights-removegroup-all' )->escaped();
00274         } elseif ( is_array( $remove ) ) {
00275             $remove = array_intersect( array_values( array_unique( $remove ) ), $allGroups );
00276             if ( count( $remove ) ) {
00277                 $r[] = $this->msg( 'listgrouprights-removegroup',
00278                     $lang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $remove ) ),
00279                     count( $remove )
00280                 )->parse();
00281             }
00282         }
00283 
00284         if ( $addSelf === true ) {
00285             $r[] = $this->msg( 'listgrouprights-addgroup-self-all' )->escaped();
00286         } elseif ( is_array( $addSelf ) ) {
00287             $addSelf = array_intersect( array_values( array_unique( $addSelf ) ), $allGroups );
00288             if ( count( $addSelf ) ) {
00289                 $r[] = $this->msg( 'listgrouprights-addgroup-self',
00290                     $lang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $addSelf ) ),
00291                     count( $addSelf )
00292                 )->parse();
00293             }
00294         }
00295 
00296         if ( $removeSelf === true ) {
00297             $r[] = $this->msg( 'listgrouprights-removegroup-self-all' )->parse();
00298         } elseif ( is_array( $removeSelf ) ) {
00299             $removeSelf = array_intersect( array_values( array_unique( $removeSelf ) ), $allGroups );
00300             if ( count( $removeSelf ) ) {
00301                 $r[] = $this->msg( 'listgrouprights-removegroup-self',
00302                     $lang->listToText( array_map( array( 'User', 'makeGroupLinkWiki' ), $removeSelf ) ),
00303                     count( $removeSelf )
00304                 )->parse();
00305             }
00306         }
00307 
00308         if ( empty( $r ) ) {
00309             return '';
00310         } else {
00311             return '<ul><li>' . implode( "</li>\n<li>", $r ) . '</li></ul>';
00312         }
00313     }
00314 
00315     protected function getGroupName() {
00316         return 'users';
00317     }
00318 }