Added string functions (replace_e,pad_e,pos_e,rpos_e,and explode_e) that allow the...
authorJack D. Pond <jdpond@users.mediawiki.org>
Fri, 11 Sep 2009 22:50:34 +0000 (22:50 +0000)
committerJack D. Pond <jdpond@users.mediawiki.org>
Fri, 11 Sep 2009 22:50:34 +0000 (22:50 +0000)
extensions/StringFunctionsEscaped/README [new file with mode: 0644]
extensions/StringFunctionsEscaped/StringFunctionsEscaped.php [new file with mode: 0644]

diff --git a/extensions/StringFunctionsEscaped/README b/extensions/StringFunctionsEscaped/README
new file mode 100644 (file)
index 0000000..18c5c38
--- /dev/null
@@ -0,0 +1,185 @@
+{{Extension|templatemode=\r
+|name          = StringFunctionsEscaped\r
+|status        = beta\r
+|type1         = parser function\r
+|type2         =\r
+|hook1         = LanguageGetMagic\r
+|hook2         =\r
+|username      = [[user:jpond | Jack D. Pond ]]\r
+|author        = \r
+|description   = Defines a superset of string parser functions that allow character escaping in the 'search for' and 'replace with' arguments.\r
+|image         =\r
+|imagesize     =\r
+|version       = 1.0.0\r
+|update        = 2009-09-11\r
+|mediawiki     = Tested with 1.14,1.15,1.16A, Should work with all\r
+|php           =\r
+|license       = GNU Version 2\r
+|download      = \r
+|readme        =\r
+|changelog     =\r
+|parameters    = $wgPFEnableStringFunctions\r
+|tags          = \r
+|rights        = \r
+|example       =\r
+|compatibility =\r
+}}\r
+\r
+\r
+\r
+==What can this extension do?==\r
+\r
+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.).\r
+\r
+These can be used (among other things) to make infoblox-type templates much more WYSIWIG (see Examples) for novice/non-technical users.\r
+\r
+==Usage==\r
+\r
+These functions are all invoked exactly as their string parser functions would be (except with the '_e' appended to distinguish).\r
+\r
+===  pos_e: (string position)===\r
+\r
+<nowiki>{{#pos_e:value|key|offset}}</nowiki>\r
+\r
+Returns the first position of key inside the given value, or an empty string.\r
+If offset is defined, this method will not search the first offset characters.\r
+\r
+See: http://php.net/manual/function.strpos.php\r
+\r
+=== rpos_e: (string position, reverse) ===\r
+<nowiki>{{#rpos_e:value|key}}</nowiki>\r
+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.\r
+\r
+See: http://php.net/manual/function.strrpos.php\r
+\r
+=== pad_e: (pad string) ===\r
+<nowiki>{{#pad_e:value|length|with|direction}}</nowiki>\r
+\r
+Returns the value padded to the certain length with the given with string.\r
+If the with string is not given, spaces are used for padding. The direction may be specified as: 'left', 'center' or 'right'.\r
+\r
+See: http://php.net/manual/function.str-pad.php\r
+\r
+=== replace_e: (string replace) ===\r
+\r
+<nowiki>{{#replace_e:value|from|to}}</nowiki> \r
+\r
+Returns the given value with all occurences of 'from' replaced with 'to'.\r
+\r
+See: http://php.net/manual/function.str-replace.php\r
+\r
+=== explode_e: (explode string) ===\r
+<nowiki>{{#explode_e:value|delimiter|position}}</nowiki>\r
+\r
+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.\r
+\r
+Note: Pieces are counted from 0.<br>\r
+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.\r
+\r
+See: http://php.net/manual/function.explode.php\r
+\r
+==Download instructions==\r
+<!-- revise these instructions if code is available via a download site -->\r
+Please cut and paste the code found [[#Code|below]] and place it in <code>$IP/extensions/ExtensionName/ExtensionName.php</code>.  ''Note: [[Manual:$IP|$IP]] stands for the root directory of your MediaWiki installation, the same directory that holds [[Manual:LocalSettings.php|LocalSettings.php]]''.\r
+\r
+==Installation==\r
+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]].\r
+\r
+Install and test [[Extension:ParserFunctions]] and (if necessary) [[Extension:StringFunctions]] prior to installing this extension.\r
+\r
+This extension must be included AFTER the invocation of the string parser functions.\r
+To install this extension, add the following to [[Manual:LocalSettings.php|LocalSettings.php]]:\r
+\r
+=== For MediaWiki 1.15.1 and before ===\r
+<source lang="php">\r
+require_once("$IP/extensions/ParserFunctions/ParserFunctions.php");\r
+require_once("$IP/extensions/StringFunctions/StringFunctions.php");\r
+require_once("$IP/extensions/StringFunctionsEscaped/StringFunctionsEscaped.php");\r
+</source>\r
+\r
+=== For MediaWiki 1.16a and after ===\r
+<source lang="php">\r
+require_once("$IP/extensions/ParserFunctions/ParserFunctions.php");\r
+$wgPFEnableStringFunctions = true;  // Note: this must be after ParserFunctions and before StringFunctionsEscaped\r
+require_once("$IP/extensions/StringFunctionsEscaped/StringFunctionsEscaped.php");\r
+</source>\r
+==Examples==\r
+\r
+=== pos_e ===\r
+\r
+<pre>\r
+{{#pos_e:Line 1\r
+Line 2\r
+Line 3|\n|7}}\r
+\r
+Returns:\r
+\r
+13\r
+</pre>\r
+\r
+=== rpos_e ===\r
+<pre>\r
+{{#rpos_e:Line 1\r
+Line 2\r
+Line 3|\n}}\r
+\r
+Returns:\r
+\r
+13\r
+</pre>\r
+\r
+=== pad_e ===\r
+<pre>\r
+~~{{#pad_e:xox|9|\n|center}}~~\r
+\r
+Returns:\r
+\r
+~~ \r
+\r
+\r
+xox \r
+\r
+\r
+~~ \r
+</pre>\r
+\r
+=== replace_e ===\r
+<pre>\r
+{{#replace_e:Line 1\r
+Line 2\r
+Line 3|\n|<br>\n}}\r
+\r
+Returns:\r
+\r
+Line 1<br>\r
+Line 2<br>\r
+Line 3\r
+\r
+Which would display as:\r
+\r
+Line 1\r
+Line 2\r
+Line 3\r
+\r
+Rather than the unescaped:\r
+\r
+Line 1 Line 2 Line 3\r
+\r
+</pre>\r
+\r
+=== explode_e ===\r
+<pre>\r
+{{#explode_e:Line 1\r
+Line 2\r
+Line 3|\n|1}}\r
+\r
+Returns:\r
+\r
+Line 2\r
+\r
+</pre>\r
+==See also==\r
+\r
+* [[Extension:ParserFunctions]]\r
+* [[Extension:StringFunctions]]\r
+* [[Extension:Lua]]\r
diff --git a/extensions/StringFunctionsEscaped/StringFunctionsEscaped.php b/extensions/StringFunctionsEscaped/StringFunctionsEscaped.php
new file mode 100644 (file)
index 0000000..db38e9f
--- /dev/null
@@ -0,0 +1,163 @@
+<?php
+if ( !defined( 'MEDIAWIKI' ) )
+       die( 'StringFunctionsEscaped::This file is a MediaWiki extension, it is not a valid entry point' );
+if ( !class_exists('ExtStringFunctions',false) && 
+        !(class_exists('ParserFunctions_HookStub',false) && isset($wgPFEnableStringFunctions) && $wgPFEnableStringFunctions))
+       die( 'StringFunctionsEscaped::You must have extension StringFunctions or extension ParserFunctions with string functions enabled' );
+/*
+
+ Defines a superset of string parser functions that allow character escaping in the 'search for' and 'replace with' arguments.
+
+ {{#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: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: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: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: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
+
+
+ Copyright (c) 2009 Jack D. Pond
+ Licensed under GNU version 2
+*/
+
+$wgExtensionCredits['parserhook'][] = array(
+       'path'            => __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) ));
+       }
+
+}