Extension:ProtectSection

Rationale and Features
Sometimes wikis want to allow editing on only part of a page. ProtectSection implements tags to block editing by users who are not sysops or bureaucrats. Attempts to edit text between protect tags, or to delete protect tags will result in an error message being displayed when the user tries to save the page.

Status
There are actually two versions. ProtectSection.php can be found on svn or a version modified by Jim Hu can be used: <?php
 * 1) This program is free software; you can redistribute it and/or modify
 * 2) it under the terms of the GNU General Public License as published by
 * 3) the Free Software Foundation; either version 2 of the License, or
 * 4) (at your option) any later version.
 * 5) This program is distributed in the hope that it will be useful,
 * 6) but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 7) MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * 8) GNU General Public License for more details.
 * 9) You should have received a copy of the GNU General Public License along
 * 10) with this program; if not, write to the Free Software Foundation, Inc.,
 * 11) 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * 12) http://www.gnu.org/copyleft/gpl.html
 * 1) 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * 2) http://www.gnu.org/copyleft/gpl.html

/** * This is an extension that let you protect some part of a text. You just * have to be a member of a group with the 'protectsection' user right. * * To protect a text, enclose use in a block. * * @addtogroup Extensions * * @author ThomasV  * @copyright Copyright 2006, ThomasV * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later * * Modified by Jim Hu to remove Section Edit links in protected text, Jan 28, 2007 * Bug fixes on Feb 15, 2007 */

if ( ! defined( 'MEDIAWIKI' ) ) die;

// Two new permissions $wgGroupPermissions['sysop']['protectsection']        = true; $wgGroupPermissions['bureaucrat']['protectsection']   = true; $wgAvailableRights[] = 'protectsection'; $wgExtensionFunctions[] = 'wfProtectSectionSetup';

// Register hooks $wgHooks['ParserAfterTidy'][] = 'wfStripProtectTags' ; $wgHooks['EditFilter'][] = 'wfCheckProtectSection' ;

/** * TODO: use some arrays in ./languages/ for proper l10n */ function wfProtectSectionSetup { global $wgMessageCache; $wgMessageCache->addMessages(	array( 'protectsection_add_remove' => 'You tried to add or remove a protected section', 'protectsection_modify' => 'You tried to modify protected text', 'protectsection_forbidden' => 'Forbidden', )	); }

/** * @param &$parser The parser object * @param &$text The text being parsed * @param &$x Something not used FIXME */ function wfStripProtectTags ( &$parser, &$text) {

global $wgUser; $tmp = explode("&lt;protect&gt;",$text); $sections = array; $sections[] = array_shift($tmp); foreach($tmp as $block){ $tmp = explode("&lt;/protect&gt;",$block); if ( $wgUser->isAllowed( 'protectsection' ) ) { $sections[] = " ".$tmp[0]." "; }else{ $sections[] = " ".preg_replace("//i", "", $tmp[0])." ";		}		array_shift($tmp);		$sections[] = implode('',$tmp);	}	$text = implode("",$sections);	return true; }

/** * @todo Document * @param $editpage * @param $textbox1 * @param $section */ function wfCheckProtectSection ( $editpage, $textbox1, $section ) {

# check for partial protection global $wgUser,$wgParser;

if ( !$wgUser->isAllowed( 'protectsection' ) ) { $modifyProtect = false; $text1 = $editpage->mArticle->getContent(true); if( $section != '' ) { if( $section == 'new' ) { $text1 = $editpage->getPreloadedText( $preload ); } else { $text1 = $wgParser->getSection( $text1, $section ); }		}		$text2 = $textbox1 ;

preg_match_all( "/ (.*?)<\/protect>| (.*?)$/si", $text1, $list1, PREG_SET_ORDER ); preg_match_all( "/ (.*?)<\/protect>| (.*?)$/si", $text2, $list2, PREG_SET_ORDER );

if( count($list1) != count($list2)) { $msg = wfMsg( 'protectsection_add_remove'); $modifyProtect = true; }		else for ( $i=0 ; $i < count( $list1 ); $i++ ) { if( $list1[$i][0] != $list2[$i][0]) { $msg = wfMsg( 'protectsection_modify' ); $modifyProtect = true; break; }		}

if( $modifyProtect ) { global $wgOut; $wgOut->setPageTitle( wfMsg( 'protectsection_forbidden' ) ); $wgOut->addWikiText($msg); return false; }	}	return true; } ?>

Installation
The usual: Copy to extensions folder. Add require_once( "$IP/extensions/ProtectSection.php"); to LocalSettings.php

Alternatives
Extension:PageSecurity for page level granularity.