Extension:ProtectText/ProtectText.php

Alternative PHP module
The original above ProtectText php module has several issue's As no-one updates this extension, i took the freedom to update this extension for MediaWiki 1.25.5 and
 * Not functional for MediaWiki 1.25.5
 * Only suitable when editing the full page, otherwise when editing an section you always get an error

removed the section edit error

NOTE: the file names and tag are changed, to assure that no installation error could happen

Source of new ProtectABC.php
This new file should be allocated in /extensions/ProtectABCText/ProtectABCText.php ', 'license-name' 		=> 'Licence', 'url' 				=> 'https://www.mediawiki.org/wiki/Extension_talk:ProtectText' );

// Keep i18n globals so mergeMessageFileList.php doesn't break $wgMessagesDirs['ProtectABC'] 	= __DIR__. '/i18n'; $wgmodifyProtect 				= false; $wgOriginal_ABC_Page 			= '';

// Two new permissions $wgAvailableRights[] = 'protectABC'; $wgAvailableRights[] = 'protectsolecontributor';

// Set the appropiate rights $wgGroupPermissions['*']['protectABC']        				= false; $wgGroupPermissions['*']['protectsolecontributor'] 		= false; $wgGroupPermissions['sysop']['protectABC']        			= true; $wgGroupPermissions['sysop']['protectsolecontributor'] 	= true; $wgGroupPermissions['bureaucrat']['protectABC']   			= true; $wgGroupPermissions['bureaucrat']['protectsolecontributor']	= true;

// Register hooks $wgHooks['ParserBeforeStrip'][] = 'wfStripProtectTags' ; $wgHooks['EditFilter'][] 		= 'wfAllocateOriginalText' ; $wgHooks['PageContentSave'][] 	= 'wfCheckProtectText' ;

function wfCheckProtectText ( &$wikiPage, &$user, &$content, &$summary, $isMinor, $isWatch, $section, &$flags, &$status) { global $wgUser, $wgTitle, $wgVersion, $wgOriginal_ABC_Page;

if ( !$wgUser->isAllowed( 'protectABC' ) ) { $Submitted_page_text = $content->getNativeData; // Retrieve all the area's with protected text // the original text preg_match_all( "/(.*?)<\/protectABC>|\n;?:*;?§(.*?)\n|\{§(.*?)§\}/si", $wgOriginal_ABC_Page, $list1, PREG_SET_ORDER ); // the text as the user want to save preg_match_all( "/(.*?)<\/protectABC>|\n;?:*;?§(.*?)\n|\{§(.*?)§\}/si", $Submitted_page_text, $list2, PREG_SET_ORDER ); if( count($list1) < count($list2)) { # indeed an addition of an tag, we will have to protect the text $wgmodifyProtect = true; // create an error status, than the page is not saved $message = ' '.wfMessage( 'protecttext_add' )->parse.' '; } if( count($list1) > count($list2)) { # indeed an removal of the tag, we will have to protect the text $wgmodifyProtect = true; // create an error status, than the page is not saved $message = ' '.wfMessage( 'protecttext_remove' )->parse.' ';	} else for ( $i=0 ; $i < count( $list1 ); $i++ ) { // compair the content of an enclosed protected text if( $list1[$i][0] != $list2[$i][0]) { // create an error status if the encosed data differs ,than the page is not saved $wgmodifyProtect = true; $message = ' '. wfMessage( 'protecttext_modify' )->parse. ' ';					break; }			}			//************** Check on protectsolecontributor ************ //* Whenever we have an protection breach, but the user is an allowed contributer //***********************************************************						if ( $wgmodifyProtect && $wgUser->isAllowed( 'protectsolecontributor' ) ) { $conds['rev_page'] = $wgTitle->getArticleID; if( version_compare( $wgVersion, '1.11alpha', '>=' ) ) $conds[] = 'rev_deleted & '. Revision::DELETED_USER. ' = 0';				$contributors = array; $dbr = wfGetDB( DB_SLAVE ); $res = $dbr->select(					'revision',					array( 'COUNT(*) AS count', 'rev_user', 'rev_user_text', ),				$conds,				__METHOD__,				array( 'GROUP BY' => 'rev_user_text', 'ORDER BY' => 'count DESC', )				);				if( $res && $dbr->numRows( $res ) > 0 ) while( $row = $dbr->fetchObject( $res ) ) $contributors[ $row->rev_user_text ] = array( $row->rev_user ); $total = 0; foreach( $contributors as $username => $info ) { list( $id ) = $info; if ($wgUser->getID != $id) { $total++; break; }				}				// if we have the same page owner and not an different contributer, and they are aowed to contributer // than cancel the protection of the previous part if (!$total){ $wgmodifyProtect = false; } }

// If we have an protection breach, terminate the save and show an fatal error if($wgmodifyProtect){ $status->fatal(new RawMessage("$message")); } }

return; }

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

$text = preg_replace(			array('/(.*?)<\/protectABC>/si', '/(\n;?:*;?)§(.*?)\n/si', '/(\n;?:*;?)§(.*?)$/si', '/\{§(.*?)§\}/si'),			array("$1", "$1$2\n", "$1$2", "$1"),			$text); return true; }

/** * @param $editor * @param $text * @param $section * @param &$error * @param $summary */

function wfAllocateOriginalText ( $editor, $text, $section, &$error, $summary ) { global $wgOriginal_ABC_Page;

$wgOriginal_ABC_Page = $editor->mArticle->getContent(true); return true; }

?>