Regexp wgWhitelistRead

From Meta, a Wikimedia project coordination wiki
Jump to: navigation, search

Regular Expressions for wgWhitelistRead[edit]

The function userCanRead() in includes/Title.php has been changed (similar to the hack for wgWhitelistEdit) like this:

       function userCanRead() {
               global $wgUser;
               if( $wgUser->isAllowed('read') ) {
                       return true;
               } else {
                       global $wgWhitelistRead;
                       /** If anon users can create an account,
                           they need to reach the login page first! */
                       if( $wgUser->isAllowed( 'createaccount' )
                           && $this->getNamespace() == NS_SPECIAL
                           && $this->getText() == 'Userlogin' ) {
                               return true;
                       }
                       /** some pages are explicitly allowed */
                       $name = $this->getPrefixedText();
                       # The following 3 lines have been replaced ...
                       # if( in_array( $name, $wgWhitelistRead ) ) {
                       #       return true;
                       # }
                       # ... with the following code to have regular expressions in wgWhitelistRead
                       if ( is_array($wgWhitelistRead) ) {
                               for ($i=0; $i<count($wgWhitelistRead); $i++) {
                                       if (preg_match($wgWhitelistRead[$i],$name)) {
                                               return true;
                                       }
                               }
                       }
                       # Compatibility with old settings
                       if( $this->getNamespace() == NS_MAIN ) {
                               if( in_array( ':' . $name, $wgWhitelistRead ) ) {
                                       return true;
                               }
                       }
               }
               return false;
       }

Afterwards one can use regular expressions in the file LocalSettings.php for specifying private pages or namespaces, which only can be read by authentified users:

  # Specify who can edit: true means only logged in users may edit pages
  $wgWhitelistEdit = true;
  # Pages anonymous (not-logged-in) users may see
  $wgWhitelistRead = array ("/^[^:]*$/", "/^Special:(Search|Categories)$/", "/^(Help|Image|User|Category|MMVLWiki):/" );
  # Specify who may create new accounts: 0 means no, 1 means yes
  $wgWhitelistAccount = array ( 'user' => 0, 'sysop' => 1, 'developer' => 1 );
  $wgShowIPinHeader = false; # For non-logged in users

Jan Wedekind