User:PyneJ/Hacks/PageWhiteList

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

Overview[edit]

This will change the Wiki to allow Anonymous users to View pages but to only Edit Talk pages. There have been many requests to allow anonymous users to edit only the talk pages so I took the time to find a workable solution.

Well, looks like this domain name has expired, but then again, the hack is for a very old version of mediawiki so it's probably not a problem. Upgrade to at least 1.5 and use the wgGroupPermissions settings given later on this page.

http://www.omegawarriors.com/wiki/index.php?title=Wiki_Hacks:Edit_Rights More Info

Ver 1.4.x[edit]

/includes/EditPage.php[edit]

Replace at line 55-59 and 200-204 (before adding code at 55-59)

I noticed it was lines 55-59 and 220-223 (after adding code at 55-59)

if ( !$wgUser->getID() && $wgWhitelistEdit ) {
  $this->userNotLoggedInPage();
  return;
}

With

if ( !$wgUser->getID() && $wgWhitelistEdit ) {
  $editlocked=true;
  if ( is_array($wgWhitelistEdit) ) {
    for ($i=0; $i<count($wgWhitelistEdit); $i++) {
      if (preg_match($wgWhitelistEdit[$i],$this->mTitle->getPrefixedText())) {
        $editlocked=false;
      }
    }
  }
  if ( $editlocked ) {
    $this->userNotLoggedInPage();
    return;
  }
}

Source

/LocalSettings.php[edit]

file and add/update the following variable. $wgWhitelistEdit = array ('/Talk:/i');

This will allow anonymous users to edit all talk/discussion pages.

Bugs[edit]

  • The wgWhitelistEdit variable will need the proper language names also. Eg:
$wgWhitelistEdit = array ('/Talk:/i', '/talk_in_french:/i');


Ver 1.5.x[edit]

/LocalSettings.php[edit]

Add, or update, the following lines:

$wgGroupPermissions['*'    ]['read']            = true;
$wgGroupPermissions['*'    ]['edit']            = false;
$wgGroupPermissions['*'    ]['talk']            = true;


/includes/User.php[edit]

Replace the isAllowed function that is used to see if a user has permissions to edit a page.


--- mediawiki-1.5.2/includes/User.php   2005-11-13 22:52:13.484375000 +0800
+++ mediawiki/includes/User.php 2005-11-13 22:57:09.234375000 +0800
@@ -1017,9 +1017,28 @@
         * @param string $action Action to be checked (see $wgAvailableRights in Defines.php for possible actions).
         * @return boolean True: action is allowed, False: action should not be allowed
         */
-       function isAllowed($action='') {
+       function isAllowed($action='', $title = NULL) {
+               if( $title == NULL ) {
+                       global $wgTitle;
+                       $title = $wgTitle;
+               }
+
                $this->loadFromDatabase();
+
+               if( in_array( $action , $this->mRights ) ) {
+                       return true;
+               }
+                       
+               // If user wants to edit a talk page and has the talk right, and the subject page exists, allow him to do so...
+               if( $action == 'edit' && $title->isTalkPage() && in_array('talk', $this->mRights)) {
+                       $subject_page = $title->getSubjectPage();
+                       if ($subject_page->exists())
+                       {
+                               return true;
+                       }
+               }
+       
+               return false;
        }
 
        /**

Contributed by: -- Jiangxin 05:48, 15 November 2005 (UTC)

Deprecated method for 1.5.x[edit]

/includes/EditPage.php[edit]

Edit includes/EditPage.php, find the following lines(twice, currently ~158 and ~355)

if ( !$wgUser->isAllowed('edit') ) {
  if ( $wgUser->isAnon() ) {
      $this->userNotLoggedInPage();
      return;
  } else {
      $wgOut->readOnlyPage( $this->mArticle->getContent( true ), true );
      return;
  }
}

replace the first line with

if ( !$wgUser->isAllowed('edit') && (!$this->mTitle->isTalkPage() || !$wgUser->isAllowed('talk'))   ) {

Bugs[edit]

Update from en:User:Nickj: Then to if want to make it clearer to anonymous users what is happening, edit "MediaWiki:WhitelistEditTitle", and replace the "Login required to edit" message with "Editing not permitted by anonymous users". Also edit "MediaWiki:WhitelistEditText", and replace the "You have to [[Special:Userlogin|login]] to edit pages." message with 'You can however edit the talk page, using the "discussion" link.'

Update from (Unregistered user) DaZjorz: The pages above are called "MediaWiki:Whitelistedittitle" and "MediaWiki:Whitelistedittext", as the page names seem to be case sensitive.

Note: The above still allows anons to create talk pages at any location, even if there is no associated article. To prevent this, use this code substitution instead:

$subject_page = $this->mTitle->getSubjectPage();
if ( !$wgUser->isAllowed('edit') 
     && ( (!$this->mTitle->isTalkPage() || !$wgUser->isAllowed('talk'))
         || !$subject_page->exists() 
         )  ) {

Note: Patch Bug 1924: Restricted read access for subset of wiki show us another way to have a separate 'talk' acl. Only includes/User.php needs to be patched.


Ver 1.6.x[edit]

You can easily restrict access to certain MediaWiki features without modifying the core files using the userCan hook available since version 1.6.0.