From d941edc9be13fa513055c7ac7a915ba4a275cb02 Mon Sep 17 00:00:00 2001 From: "Jack D. Pond" Date: Fri, 11 Sep 2009 22:50:34 +0000 Subject: [PATCH] Added string functions (replace_e,pad_e,pos_e,rpos_e,and explode_e) that allow the use of c-type escaped characters ("\n","\t", etc.) in relevant existing string functions. --- extensions/StringFunctionsEscaped/README | 185 ++++++++++++++++++ .../StringFunctionsEscaped.php | 163 +++++++++++++++ 2 files changed, 348 insertions(+) create mode 100644 extensions/StringFunctionsEscaped/README create mode 100644 extensions/StringFunctionsEscaped/StringFunctionsEscaped.php diff --git a/extensions/StringFunctionsEscaped/README b/extensions/StringFunctionsEscaped/README new file mode 100644 index 0000000000..18c5c38e95 --- /dev/null +++ b/extensions/StringFunctionsEscaped/README @@ -0,0 +1,185 @@ +{{Extension|templatemode= +|name = StringFunctionsEscaped +|status = beta +|type1 = parser function +|type2 = +|hook1 = LanguageGetMagic +|hook2 = +|username = [[user:jpond | Jack D. Pond ]] +|author = +|description = Defines a superset of string parser functions that allow character escaping in the 'search for' and 'replace with' arguments. +|image = +|imagesize = +|version = 1.0.0 +|update = 2009-09-11 +|mediawiki = Tested with 1.14,1.15,1.16A, Should work with all +|php = +|license = GNU Version 2 +|download = +|readme = +|changelog = +|parameters = $wgPFEnableStringFunctions +|tags = +|rights = +|example = +|compatibility = +}} + + + +==What can this extension do?== + +Wikitext allows the imbedding of certain control characters (newline, tab, etc.). These parser functions allow them to be identified and used with standard c-type escape character sequence (/n,/t, etc.). + +These can be used (among other things) to make infoblox-type templates much more WYSIWIG (see Examples) for novice/non-technical users. + +==Usage== + +These functions are all invoked exactly as their string parser functions would be (except with the '_e' appended to distinguish). + +=== pos_e: (string position)=== + +{{#pos_e:value|key|offset}} + +Returns the first position of key inside the given value, or an empty string. +If offset is defined, this method will not search the first offset characters. + +See: http://php.net/manual/function.strpos.php + +=== rpos_e: (string position, reverse) === +{{#rpos_e:value|key}} +Returns the last position of key inside the given value, or -1 if the key is not found. When using this to search for the last delimiter, add +1 to the result to retreive position after the last delimiter. This also works when the delimiter is not found, because "-1 + 1" is zero, which is the beginning of the given value. + +See: http://php.net/manual/function.strrpos.php + +=== pad_e: (pad string) === +{{#pad_e:value|length|with|direction}} + +Returns the value padded to the certain length with the given with string. +If the with string is not given, spaces are used for padding. The direction may be specified as: 'left', 'center' or 'right'. + +See: http://php.net/manual/function.str-pad.php + +=== replace_e: (string replace) === + +{{#replace_e:value|from|to}} + +Returns the given value with all occurences of 'from' replaced with 'to'. + +See: http://php.net/manual/function.str-replace.php + +=== explode_e: (explode string) === +{{#explode_e:value|delimiter|position}} + +Splits the given value into pieces by the given delimiter and returns the position-th piece. Empty string is returned if there are not enough pieces. + +Note: Pieces are counted from 0.
+Note: A negative value can be used to count pieces from the end, instead of counting from the beginning. The last piece is at position -1. + +See: http://php.net/manual/function.explode.php + +==Download instructions== + +Please cut and paste the code found [[#Code|below]] and place it in $IP/extensions/ExtensionName/ExtensionName.php. ''Note: [[Manual:$IP|$IP]] stands for the root directory of your MediaWiki installation, the same directory that holds [[Manual:LocalSettings.php|LocalSettings.php]]''. + +==Installation== +String functions were integrated into [[Extension:ParserFunctions]] extension as of [[Special:Code/MediaWiki/50997|r50997]]. This revision of [[Extension:ParserFunctions]] is designed for MediaWiki 1.16, but updating to the trunk version may work on previous versions. If you are using a prior version of [[Extension:ParserFunctions]], you will also have to include [[Extension:StringFunctions]]. + +Install and test [[Extension:ParserFunctions]] and (if necessary) [[Extension:StringFunctions]] prior to installing this extension. + +This extension must be included AFTER the invocation of the string parser functions. +To install this extension, add the following to [[Manual:LocalSettings.php|LocalSettings.php]]: + +=== For MediaWiki 1.15.1 and before === + +require_once("$IP/extensions/ParserFunctions/ParserFunctions.php"); +require_once("$IP/extensions/StringFunctions/StringFunctions.php"); +require_once("$IP/extensions/StringFunctionsEscaped/StringFunctionsEscaped.php"); + + +=== For MediaWiki 1.16a and after === + +require_once("$IP/extensions/ParserFunctions/ParserFunctions.php"); +$wgPFEnableStringFunctions = true; // Note: this must be after ParserFunctions and before StringFunctionsEscaped +require_once("$IP/extensions/StringFunctionsEscaped/StringFunctionsEscaped.php"); + +==Examples== + +=== pos_e === + +
+{{#pos_e:Line 1
+Line 2
+Line 3|\n|7}}
+
+Returns:
+
+13
+
+ +=== rpos_e === +
+{{#rpos_e:Line 1
+Line 2
+Line 3|\n}}
+
+Returns:
+
+13
+
+ +=== pad_e === +
+~~{{#pad_e:xox|9|\n|center}}~~
+
+Returns:
+
+~~ 
+
+
+xox 
+
+
+~~ 
+
+ +=== replace_e === +
+{{#replace_e:Line 1
+Line 2
+Line 3|\n|
\n}} + +Returns: + +Line 1
+Line 2
+Line 3 + +Which would display as: + +Line 1 +Line 2 +Line 3 + +Rather than the unescaped: + +Line 1 Line 2 Line 3 + +
+ +=== explode_e === +
+{{#explode_e:Line 1
+Line 2
+Line 3|\n|1}}
+
+Returns:
+
+Line 2
+
+
+==See also== + +* [[Extension:ParserFunctions]] +* [[Extension:StringFunctions]] +* [[Extension:Lua]] diff --git a/extensions/StringFunctionsEscaped/StringFunctionsEscaped.php b/extensions/StringFunctionsEscaped/StringFunctionsEscaped.php new file mode 100644 index 0000000000..db38e9f40f --- /dev/null +++ b/extensions/StringFunctionsEscaped/StringFunctionsEscaped.php @@ -0,0 +1,163 @@ + __FILE__, + 'name' => 'StringFunctionsEscaped', + 'version' => '1.0.0', // Sept 7, 2009 + 'description' => 'Allows escaped characters in string functions using c-like syntax', + 'descriptionmsg' => 'pfunc_desc', + 'author' => array('Jack D. Pond'), + 'license' => 'GNU Version 2', + 'url' => 'http://www.mediawiki.org/wiki/Extension:StringFunctionsEscaped', +); + +$dir = dirname( __FILE__ ) . '/'; +# RFU +# $wgExtensionMessagesFiles['StringFunctionsEscaped'] = $dir . 'StringFunctionsEscaped.i18n.php'; + +$wgExtensionFunctions[] = 'wfStringFunctionsEscaped'; + +$wgHooks['LanguageGetMagic'][] = 'wfStringFunctionsEscapedLanguageGetMagic'; + +function wfStringFunctionsEscaped ( ) { + global $wgParser, $wgExtStringFunctionsEscaped; + + $wgExtStringFunctionsEscaped = new ExtStringFunctionsEscaped ( ); + + $wgParser->setFunctionHook('pos_e', array(&$wgExtStringFunctionsEscaped,'runPos_e' )); + $wgParser->setFunctionHook('rpos_e', array(&$wgExtStringFunctionsEscaped,'runRPos_e' )); + $wgParser->setFunctionHook('pad_e', array(&$wgExtStringFunctionsEscaped,'runPad_e' )); + $wgParser->setFunctionHook('replace_e', array(&$wgExtStringFunctionsEscaped,'runReplace_e' )); + $wgParser->setFunctionHook('explode_e', array(&$wgExtStringFunctionsEscaped,'runExplode_e' )); +} + +function wfStringFunctionsEscapedLanguageGetMagic( &$magicWords, $langCode = "en" ) { + switch ( $langCode ) { + default: + $magicWords['pos_e'] = array ( 0, 'pos_e' ); + $magicWords['rpos_e'] = array ( 0, 'rpos_e' ); + $magicWords['pad_e'] = array ( 0, 'pad_e' ); + $magicWords['replace_e'] = array ( 0, 'replace_e' ); + $magicWords['explode_e'] = array ( 0, 'explode_e' ); + } + return true; +} + +class ExtStringFunctionsEscaped { + + /** + * {{#pos_e:value|key|offset}} + * Note: If the needle is an empty string, single space is used instead. + * Note: If the needle is not found, empty string is returned. + * Note: The needle is limited to specific length. + */ + function runPos_e ( &$parser, $inStr = '', $inNeedle = '', $inOffset = 0 ) { + global $wgParser; + list($callback,$flags) = $wgParser->mFunctionHooks['pos']; + return @call_user_func_array( $callback, + array_merge(array($parser),array($inStr,stripcslashes($inNeedle),$inOffset) )); + } + + /** + * {{#rpos_e:value|key}} + * Note: If the needle is an empty string, single space is used instead. + * Note: If the needle is not found, -1 is returned. + * Note: The needle is limited to specific length. + */ + function runRPos_e( &$parser, $inStr = '', $inNeedle = '' ) { + global $wgParser; + list($callback,$flags) = $wgParser->mFunctionHooks['rpos']; + return @call_user_func_array( $callback, + array_merge(array($parser),array($inStr,stripcslashes($inNeedle)) )); + } + + /** + * {{#pad_e:value|length|with|direction}} + * Note: Length of the resulting string is limited. + */ + function runPad_e( &$parser, $inStr = '', $inLen = 0, $inWith = '', $inDirection = '' ) { + global $wgParser; + list($callback,$flags) = $wgParser->mFunctionHooks['pad']; + return @call_user_func_array( $callback, + array_merge(array($parser),array($inStr, $inLen , stripcslashes($inWith), $inDirection) )); + } + + /** + * {{#replace:value|from|to}} + * Note: If the needle is an empty string, single space is used instead. + * Note: The needle is limited to specific length. + * Note: The product is limited to specific length. + */ + function runReplace_e( $parser, $inStr = '', $inReplaceFrom = '', $inReplaceTo = '' ) { + global $wgParser; + list($callback,$flags) = $wgParser->mFunctionHooks['replace']; + return @call_user_func_array( $callback, + array_merge(array($parser),array($inStr, stripcslashes($inReplaceFrom), stripcslashes($inReplaceTo)) )); + } + + /** + * {{#explode_e:value|delimiter|position}} + * Note: Negative position can be used to specify tokens from the end. + * Note: If the divider is an empty string, single space is used instead. + * Note: The divider is limited to specific length. + * Note: Empty string is returned, if there is not enough exploded chunks. + */ + function runExplode_e ( &$parser, $inStr = '', $inDiv = '', $inPos = 0 ) { + global $wgParser; + list($callback,$flags) = $wgParser->mFunctionHooks['explode']; + return @call_user_func_array( $callback, + array_merge(array($parser),array($inStr, stripcslashes($inDiv), $inPos) )); + } + +} -- 2.20.1