r37064 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r37063‎ | r37064 | r37065 >
Date:19:11, 4 July 2008
Author:skizzerz
Status:old
Tags:
Comment:
* add new extension RegexFunctions
Modified paths:
  • /trunk/extensions/RegexFunctions (added) (history)
  • /trunk/extensions/RegexFunctions/RegexFunctions.php (added) (history)

Diff [purge]

Index: trunk/extensions/RegexFunctions/RegexFunctions.php
@@ -0,0 +1,109 @@
 2+<?php
 3+/*
 4+* RegexFunctions extension by Ryan Schmidt
 5+* Regular Expression parser functions
 6+*/
 7+
 8+if( !defined( 'MEDIAWIKI' ) ) {
 9+ echo "This file is an extension of the MediaWiki software and cannot be used standalone\n";
 10+ die( 1 );
 11+}
 12+
 13+//credits and hooks
 14+$wgExtensionFunctions[] = 'wfRegexFunctions';
 15+$wgExtensionCredits['parserhook'][] = array(
 16+ 'name' => 'RegexFunctions',
 17+ 'author' => 'Ryan Schmidt',
 18+ 'url' => 'http://www.mediawiki.org/wiki/Extension:RegexFunctions',
 19+ 'version' => '1.0',
 20+ 'description' => 'Regular Expression parser functions',
 21+);
 22+
 23+$wgHooks['LanguageGetMagic'][] = 'wfRegexFunctionsLanguageGetMagic';
 24+
 25+//default globals
 26+//how many functions are allowed in a single page? Keep this at least above 3 for usability
 27+$wgRegexFunctionsPerPage = 10;
 28+//should we allow modifiers in the functions, e.g. the /g and /i modifiers for global and case-insensitive?
 29+//This does NOT enable the 'e' modifier for preg_replace, see the next variable for that
 30+$wgRegexFunctionsAllowModifiers = true;
 31+//should we allow the 'e' modifier in preg_replace? Requires AllowModifiers to be true.
 32+//Don't enable this unless you trust every single editor on your wiki, as it opens up a potential XSS vector
 33+$wgRegexFunctionsAllowE = false;
 34+//limit for rsplit and rreplace functions. -1 is unlimited
 35+$wgRegexFunctionsLimit = -1;
 36+//array of functions to disable, aka these functions cannot be used :)
 37+$wgRegexFunctionsDisable = array();
 38+
 39+function wfRegexFunctions() {
 40+ global $wgParser, $wgExtRegexFunctions;
 41+
 42+ $wgExtRegexFunctions = new ExtRegexFunctions();
 43+ $wgParser->setFunctionHook( 'rmatch', array(&$wgExtRegexFunctions, 'rmatch') );
 44+ $wgParser->setFunctionHook( 'rsplit', array(&$wgExtRegexFunctions, 'rsplit') );
 45+ $wgParser->setFunctionHook( 'rreplace', array(&$wgExtRegexFunctions, 'rreplace') );
 46+}
 47+
 48+function wfRegexFunctionsLanguageGetMagic( &$magicWords, $langCode ) {
 49+ switch ( $langCode ) {
 50+ default:
 51+ $magicWords['rmatch'] = array( 0, 'rmatch' );
 52+ $magicWords['rsplit'] = array( 0, 'rsplit' );
 53+ $magicWords['rreplace'] = array( 0, 'rreplace' );
 54+ }
 55+ return true;
 56+}
 57+
 58+Class ExtRegexFunctions {
 59+ var $num = 0;
 60+ function rmatch ( &$parser, $string = '', &$pattern = '', &$return = '', $notfound = '', $offset = 0 ) {
 61+ global $wgRegexFunctionsPerPage, $wgRegexFunctionsAllowModifiers, $wgRegexFunctionsDisable;
 62+ if(in_array('rmatch', $wgRegexFunctionsDisable))
 63+ return;
 64+ $this->num++;
 65+ if($this->num > $wgRegexFunctionsPerPage)
 66+ return;
 67+ if(!$wgRegexFunctionsAllowModifiers)
 68+ $pattern = str_replace('/', '\/', $pattern);
 69+ $num = preg_match( $pattern, $string, $matches, PREG_OFFSET_CAPTURE, $offset );
 70+ if($num === false)
 71+ return;
 72+ if($num === 0)
 73+ return $notfound;
 74+ $mn = 0;
 75+ foreach($matches as $match) {
 76+ if($mn > 9)
 77+ break;
 78+ $return = str_replace('$'.$mn, $matches[$mn][0], $return);
 79+ $return = str_replace('\\\\'.$mn, $matches[$mn][1], $return);
 80+ $mn++;
 81+ }
 82+ return $return;
 83+ }
 84+ function rsplit ( &$parser, $string = '', &$pattern = '', $piece = 0 ) {
 85+ global $wgRegexFunctionsPerPage, $wgRegexFunctionsAllowModifiers, $wgRegexFunctionsLimit, $wgRegexFunctionsDisable;
 86+ if(in_array('rmatch', $wgRegexFunctionsDisable))
 87+ return;
 88+ $this->num++;
 89+ if($this->num > $wgRegexFunctionsPerPage)
 90+ return;
 91+ if(!$wgRegexFunctionsAllowModifiers)
 92+ $pattern = str_replace('/', '\/', $pattern);
 93+ $res = preg_split( $pattern, $string, $wgRegexFunctionsLimit );
 94+ return $res[$piece];
 95+ }
 96+ function rreplace ( &$parser, $string = '', &$pattern = '', &$replace = '' ) {
 97+ global $wgRegexFunctionsPerPage, $wgRegexFunctionsAllowModifiers, $wgRegexFunctionsAllowE, $wgRegexFunctionsLimit, $wgRegexFunctionsDisable;
 98+ if(in_array('rmatch', $wgRegexFunctionsDisable))
 99+ return;
 100+ $this->num++;
 101+ if($this->num > $wgRegexFunctionsPerPage)
 102+ return;
 103+ if(!$wgRegexFunctionsAllowModifiers)
 104+ $pattern = str_replace('/', '\/', $pattern);
 105+ elseif(!$wgRegexFunctionsAllowE)
 106+ $pattern = preg_replace('/(\/.*?)e(.*?)$/i', '$1$2', $pattern);
 107+ $res = preg_replace($pattern, $replace, $string, $wgRegexFunctionsLimit);
 108+ return $res;
 109+ }
 110+}
\ No newline at end of file
Property changes on: trunk/extensions/RegexFunctions/RegexFunctions.php
___________________________________________________________________
Added: svn:eol-style
1111 + native

Status & tagging log