Benutzer:Rene/SGPageProtection: Unterschied zwischen den Versionen

aus Stargate Wiki, dem deutschsprachigen Stargate-Lexikon
Zur Navigation springen Zur Suche springen
Die Seite wurde neu angelegt: „<pre><nowiki> <?php if(! defined('MEDIAWIKI') ) { echo("This is an extension to the MediaWiki package and cannot be run standalone"); die(-1); } /** * Ext…“
(kein Unterschied)

Version vom 17. November 2009, 23:54 Uhr

<?php
if(! defined('MEDIAWIKI') ) {
    echo("This is an extension to the MediaWiki package and cannot be run standalone");
    die(-1);
}
/**
* Extension: SGPageProtection.php
* Created: 17 November 2009
* Author: René Raule
* Version: 0.2
* Copyright (c) René Raule
*
* Progam based on the Mediawiki extensions
* ValidUserEdit by Jörg-Sascha Heiland
* UserPageEditProtection by Lisa Ridley, Eric Gingell
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You can find a copy of the GNU General Public License at http://www.gnu.org/copyleft/gpl.html
* A paper copy can be obtained by writing to:  Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* SG Page Protection
*
* Allows to protect all pages in one or more namespaces.
* In namespace USER and USER_TALK the owner can not be blocked.
* With the tag <user>Name</user> in the article some users are allowed to edit.
* <user>*</user> allows any registered user, <user>**</user> also ips.
* 
* Installation:
* ------------
* Save this file in your extensions folder of your MediaWiki installation.
* Add the following to LocalSettings.php:
*
*    require_once("$IP/extensions/SGPageProtection.php");
*
* If you want to handel Sysops as normal users, means they can be blocked, add
*
*    $wgGroupPermissions['sysop']['SGPPEditFree'] = false; (default is true)
*
* If the owner of an article (user namespace) is allowed to block
*
*    $wgSGPPOwnerAlways = false; (default is true)
*
* Add the namespaces you want to protect in array $wgSGPPBlockNamespaces and
* $wgSGPPOpenNamespaces. The default definition is:
*
*    $wgSGPPBlockNamespaces = array(NS_USER);
*    $wgSGPPOpenNamespaces = array(NS_USER_TALK);
*
* All namespaces in $wgSGPPBlockNamespaces are protected for all users execpt the
* once that are defined in the article by <user>name</user>.
*
* All namespaces in $wgSGPPOpenNamespaces are free for all users if no user-tag
* is defined. If a tag is defined only the defined users are allowed.
*/

/* Use the userCan hook to check the page permissions */
$wgHooks['userCan'][] = 'fnSGPageProtection';
/* Parserhook setup for handel user-tag */
if( defined('MW_SUPPORTS_PARSERFIRSTCALLINIT') ) {
    $wgHooks['ParserFirstCallInit'][] = 'fnSGPPSetup';
} else {
    $wgExtensionFunctions[] = 'fnSGPPSetup';
}

/* register extension */
$wgExtensionCredits['other'][] = array(
    'path' => __FILE__,
    'name' => 'SGPageProtection',
    'description' => 'Page-Protect-System. Protect pages by default or by user.',
    'version' => '0.2',
    'url' => 'http://www.stargate-wiki.de/Benutzer:Rene/SGPageProtection',
    'author' => 'René Raule',
    );

$wgExtensionCredits['parserhook'][] = array(
    'path' => __FILE__,
    'name' => 'SGPageProtection',
    'description' => 'Page-Protect-System. Protect pages by default or by user.',
    'version' => '0.2',
    'url' => 'http://www.stargate-wiki.de/Benutzer:Rene/SGPageProtection',
    'author' => 'René Raule',
    );
    
/* Default values for parameters */
if(!is_array($wgSGPPBlockNamespaces)) { $wgSGPPBlockNamespaces = array(NS_USER); }
if(!is_array($wgSGPPOpenNamespaces)) { $wgSGPPOpenNamespaces = array(NS_USER_TALK); }
if(!is_bool($wgSGPPOwnerAlways)) { $wgSGPPOwnerAlways = true; }
if(!is_bool($wgGroupPermissions['sysop']['SGPPEditFree'])) { $wgGroupPermissions['sysop']['SGPPEditFree'] = true; }

/* Place to buffer results from fnSGPageProtection to speed up program */
$lastResult = array();

/* Define parserhook for user tag */
function fnSGPPSetup() {
    global $wgParser;
    
    $wgParser->setHook('user','fnSGPPUser');
    return true;
}

/* Handel user-tags, just drop them */
function fnSGPPUser($input,$argv,&$parser) {
    return ''; 
}

/* Main */
function fnSGPageProtection( $title, $user, $action, &$result ) {
    global $wgSGPPBlockNamespaces;
    global $wgSGPPOpenNamespaces;
    global $wgSGPPOwnerAlways;
    global $lastResult;

    /* if this article was already testet just give the result */
    if(isset($lastResult[$title->getText()][$action])) {
      return $lastResult[$title->getText()][$action];
    }
    /* Name of actual user */
    $username = preg_quote($user->getName());
    /* Do nothing if ... */
    /* ... usergroup is free */
    if( $user->isAllowed('SGPPEditFree') ) {
      $lastResult[$title->getText()][$action] = true;
      return true;
    }
    /* ... wrong namespace */
    if( !in_array($title->mNamespace,array_merge($wgSGPPBlockNamespaces,$wgSGPPOpenNamespaces)) ) {
      $lastResult[$title->getText()][$action] = true;
      return true;
    }
    /* ... action not edit or move */
    if( !($action == 'edit' || $action == 'move') ) {
      $lastResult[$title->getText()][$action] = true;
      return true;
    }
    /* ... user is owner of article and ownerflag is set (only in USER & USER_TALK namespaces) */
    if( ($title->mNamespace == NS_USER) || ($title->mNamespace == NS_USER_TALK) ) {
      /* get user name, title and get user part out of title */
      $fulltitle = $title->getText();
      list($usertitle,$subtitle) = explode('/',$fulltitle,2);
      if( $wgSGPPOwnerAlways && ($usertitle == $username) ) {
        $lastResult[$title->getText()][$action] = true;
        return true;
      }
    }
    /* simple checks are over now, look for user-tags in article content */
    /* get article (? can we find it already somewhere ?) */
    $article = new Article($title);
    $text = $article->fetchContent(0);
    /* drop <nowiki>xxx parts */
    $expr = '/(.*?)<\s*nowiki\s*>(?s).*?<\/\s*nowiki\s*>(.*?)/i';;
    $replace = '$1';
    $text=preg_replace($expr,$replace,$text);
    /* ip-user */
    if( $user->mId == 0 ) {
      /* look for <user>**</user> */
      if( preg_match("/<\s*user\s*>\s*\*\*\s*<\/\s*user\s*>/",$text) > 0 ) {
        $lastResult[$title->getText()][$action] = true;
        return true;
      }
    /* normal user */
    } else {
      /* look for <user>$username</user> */
      if( preg_match("/<\s*user\s*>\s*$username\s*<\/\s*user\s*>/",$text) > 0 ) {
        $lastResult[$title->getText()][$action] = true;
        return true;
      }
      /* look for <user>*</user> */
      if( preg_match("/<\s*user\s*>\s*\*\s*<\/\s*user\s*>/",$text) > 0 ) {
        $lastResult[$title->getText()][$action] = true;
        return true;
      }
    }
    /* if open namespaces and no user-tag is defined */
    if( in_array($title->mNamespace,$wgSGPPOpenNamespaces) && preg_match("/<\s*user\s*>.*<\/\s*user\s*>/",$text) == 0 )  {
      $lastResult[$title->getText()][$action] = true;
      return true;
    }
    /* Nothing matched so the result ist NOT ALLOWED */
    $lastResult[$title->getText()][$action] = false;
    $result = false;
    return false;	
}
</nowiki>