Index: trunk/phase3/extensions/StringFunctionsEscaped/StringFunctionsEscaped.php |
— | — | @@ -0,0 +1,163 @@ |
| 2 | +<?php |
| 3 | +if ( !defined( 'MEDIAWIKI' ) ) |
| 4 | + die( 'StringFunctionsEscaped::This file is a MediaWiki extension, it is not a valid entry point' ); |
| 5 | +if ( !class_exists('ExtStringFunctions',false) && |
| 6 | + !(class_exists('ParserFunctions_HookStub',false) && isset($wgPFEnableStringFunctions) && $wgPFEnableStringFunctions)) |
| 7 | + die( 'StringFunctionsEscaped::You must have extension StringFunctions or extension ParserFunctions with string functions enabled' ); |
| 8 | +/* |
| 9 | + |
| 10 | + Defines a superset of string parser functions that allow character escaping in the 'search for' and 'replace with' arguments. |
| 11 | + |
| 12 | + {{#pos_e:value|key|offset}} |
| 13 | + |
| 14 | + Returns the first position of key inside the given value, or an empty string. |
| 15 | + If offset is defined, this method will not search the first offset characters. |
| 16 | + See: http://php.net/manual/function.strpos.php |
| 17 | + |
| 18 | + {{#rpos_e:value|key}} |
| 19 | + |
| 20 | + Returns the last position of key inside the given value, or -1 if the key is |
| 21 | + not found. When using this to search for the last delimiter, add +1 to the |
| 22 | + result to retreive position after the last delimiter. This also works when |
| 23 | + the delimiter is not found, because "-1 + 1" is zero, which is the beginning |
| 24 | + of the given value. |
| 25 | + See: http://php.net/manual/function.strrpos.php |
| 26 | + |
| 27 | + {{#pad_e:value|length|with|direction}} |
| 28 | + |
| 29 | + Returns the value padded to the certain length with the given with string. |
| 30 | + If the with string is not given, spaces are used for padding. The direction |
| 31 | + may be specified as: 'left', 'center' or 'right'. |
| 32 | + See: http://php.net/manual/function.str-pad.php |
| 33 | + |
| 34 | + |
| 35 | + {{#replace_e:value|from|to}} |
| 36 | + |
| 37 | + Returns the given value with all occurences of 'from' replaced with 'to'. |
| 38 | + See: http://php.net/manual/function.str-replace.php |
| 39 | + |
| 40 | + |
| 41 | + {{#explode_e:value|delimiter|position}} |
| 42 | + |
| 43 | + Splits the given value into pieces by the given delimiter and returns the |
| 44 | + position-th piece. Empty string is returned if there are not enough pieces. |
| 45 | + Note: Pieces are counted from 0. |
| 46 | + Note: A negative value can be used to count pieces from the end, instead of |
| 47 | + counting from the beginning. The last piece is at position -1. |
| 48 | + See: http://php.net/manual/function.explode.php |
| 49 | + |
| 50 | + |
| 51 | + Copyright (c) 2009 Jack D. Pond |
| 52 | + Licensed under GNU version 2 |
| 53 | +*/ |
| 54 | + |
| 55 | +$wgExtensionCredits['parserhook'][] = array( |
| 56 | + 'path' => __FILE__, |
| 57 | + 'name' => 'StringFunctionsEscaped', |
| 58 | + 'version' => '1.0.0', // Sept 7, 2009 |
| 59 | + 'description' => 'Allows escaped characters in string functions using c-like syntax', |
| 60 | + 'descriptionmsg' => 'pfunc_desc', |
| 61 | + 'author' => array('Jack D. Pond'), |
| 62 | + 'license' => 'GNU Version 2', |
| 63 | + 'url' => 'http://www.mediawiki.org/wiki/Extension:StringFunctionsEscaped', |
| 64 | +); |
| 65 | + |
| 66 | +$dir = dirname( __FILE__ ) . '/'; |
| 67 | +# RFU |
| 68 | +# $wgExtensionMessagesFiles['StringFunctionsEscaped'] = $dir . 'StringFunctionsEscaped.i18n.php'; |
| 69 | + |
| 70 | +$wgExtensionFunctions[] = 'wfStringFunctionsEscaped'; |
| 71 | + |
| 72 | +$wgHooks['LanguageGetMagic'][] = 'wfStringFunctionsEscapedLanguageGetMagic'; |
| 73 | + |
| 74 | +function wfStringFunctionsEscaped ( ) { |
| 75 | + global $wgParser, $wgExtStringFunctionsEscaped; |
| 76 | + |
| 77 | + $wgExtStringFunctionsEscaped = new ExtStringFunctionsEscaped ( ); |
| 78 | + |
| 79 | + $wgParser->setFunctionHook('pos_e', array(&$wgExtStringFunctionsEscaped,'runPos_e' )); |
| 80 | + $wgParser->setFunctionHook('rpos_e', array(&$wgExtStringFunctionsEscaped,'runRPos_e' )); |
| 81 | + $wgParser->setFunctionHook('pad_e', array(&$wgExtStringFunctionsEscaped,'runPad_e' )); |
| 82 | + $wgParser->setFunctionHook('replace_e', array(&$wgExtStringFunctionsEscaped,'runReplace_e' )); |
| 83 | + $wgParser->setFunctionHook('explode_e', array(&$wgExtStringFunctionsEscaped,'runExplode_e' )); |
| 84 | +} |
| 85 | + |
| 86 | +function wfStringFunctionsEscapedLanguageGetMagic( &$magicWords, $langCode = "en" ) { |
| 87 | + switch ( $langCode ) { |
| 88 | + default: |
| 89 | + $magicWords['pos_e'] = array ( 0, 'pos_e' ); |
| 90 | + $magicWords['rpos_e'] = array ( 0, 'rpos_e' ); |
| 91 | + $magicWords['pad_e'] = array ( 0, 'pad_e' ); |
| 92 | + $magicWords['replace_e'] = array ( 0, 'replace_e' ); |
| 93 | + $magicWords['explode_e'] = array ( 0, 'explode_e' ); |
| 94 | + } |
| 95 | + return true; |
| 96 | +} |
| 97 | + |
| 98 | +class ExtStringFunctionsEscaped { |
| 99 | + |
| 100 | + /** |
| 101 | + * {{#pos_e:value|key|offset}} |
| 102 | + * Note: If the needle is an empty string, single space is used instead. |
| 103 | + * Note: If the needle is not found, empty string is returned. |
| 104 | + * Note: The needle is limited to specific length. |
| 105 | + */ |
| 106 | + function runPos_e ( &$parser, $inStr = '', $inNeedle = '', $inOffset = 0 ) { |
| 107 | + global $wgParser; |
| 108 | + list($callback,$flags) = $wgParser->mFunctionHooks['pos']; |
| 109 | + return @call_user_func_array( $callback, |
| 110 | + array_merge(array($parser),array($inStr,stripcslashes($inNeedle),$inOffset) )); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * {{#rpos_e:value|key}} |
| 115 | + * Note: If the needle is an empty string, single space is used instead. |
| 116 | + * Note: If the needle is not found, -1 is returned. |
| 117 | + * Note: The needle is limited to specific length. |
| 118 | + */ |
| 119 | + function runRPos_e( &$parser, $inStr = '', $inNeedle = '' ) { |
| 120 | + global $wgParser; |
| 121 | + list($callback,$flags) = $wgParser->mFunctionHooks['rpos']; |
| 122 | + return @call_user_func_array( $callback, |
| 123 | + array_merge(array($parser),array($inStr,stripcslashes($inNeedle)) )); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * {{#pad_e:value|length|with|direction}} |
| 128 | + * Note: Length of the resulting string is limited. |
| 129 | + */ |
| 130 | + function runPad_e( &$parser, $inStr = '', $inLen = 0, $inWith = '', $inDirection = '' ) { |
| 131 | + global $wgParser; |
| 132 | + list($callback,$flags) = $wgParser->mFunctionHooks['pad']; |
| 133 | + return @call_user_func_array( $callback, |
| 134 | + array_merge(array($parser),array($inStr, $inLen , stripcslashes($inWith), $inDirection) )); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * {{#replace:value|from|to}} |
| 139 | + * Note: If the needle is an empty string, single space is used instead. |
| 140 | + * Note: The needle is limited to specific length. |
| 141 | + * Note: The product is limited to specific length. |
| 142 | + */ |
| 143 | + function runReplace_e( $parser, $inStr = '', $inReplaceFrom = '', $inReplaceTo = '' ) { |
| 144 | + global $wgParser; |
| 145 | + list($callback,$flags) = $wgParser->mFunctionHooks['replace']; |
| 146 | + return @call_user_func_array( $callback, |
| 147 | + array_merge(array($parser),array($inStr, stripcslashes($inReplaceFrom), stripcslashes($inReplaceTo)) )); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * {{#explode_e:value|delimiter|position}} |
| 152 | + * Note: Negative position can be used to specify tokens from the end. |
| 153 | + * Note: If the divider is an empty string, single space is used instead. |
| 154 | + * Note: The divider is limited to specific length. |
| 155 | + * Note: Empty string is returned, if there is not enough exploded chunks. |
| 156 | + */ |
| 157 | + function runExplode_e ( &$parser, $inStr = '', $inDiv = '', $inPos = 0 ) { |
| 158 | + global $wgParser; |
| 159 | + list($callback,$flags) = $wgParser->mFunctionHooks['explode']; |
| 160 | + return @call_user_func_array( $callback, |
| 161 | + array_merge(array($parser),array($inStr, stripcslashes($inDiv), $inPos) )); |
| 162 | + } |
| 163 | + |
| 164 | +} |
Property changes on: trunk/phase3/extensions/StringFunctionsEscaped/StringFunctionsEscaped.php |
___________________________________________________________________ |
Name: svn:eol-style |
1 | 165 | + native |
Index: trunk/phase3/extensions/StringFunctionsEscaped/README |
— | — | @@ -0,0 +1,185 @@ |
| 2 | +{{Extension|templatemode=
|
| 3 | +|name = StringFunctionsEscaped
|
| 4 | +|status = beta
|
| 5 | +|type1 = parser function
|
| 6 | +|type2 =
|
| 7 | +|hook1 = LanguageGetMagic
|
| 8 | +|hook2 =
|
| 9 | +|username = [[user:jpond | Jack D. Pond ]]
|
| 10 | +|author =
|
| 11 | +|description = Defines a superset of string parser functions that allow character escaping in the 'search for' and 'replace with' arguments.
|
| 12 | +|image =
|
| 13 | +|imagesize =
|
| 14 | +|version = 1.0.0
|
| 15 | +|update = 2009-09-11
|
| 16 | +|mediawiki = Tested with 1.14,1.15,1.16A, Should work with all
|
| 17 | +|php =
|
| 18 | +|license = GNU Version 2
|
| 19 | +|download =
|
| 20 | +|readme =
|
| 21 | +|changelog =
|
| 22 | +|parameters = $wgPFEnableStringFunctions
|
| 23 | +|tags =
|
| 24 | +|rights =
|
| 25 | +|example =
|
| 26 | +|compatibility =
|
| 27 | +}}
|
| 28 | +
|
| 29 | +
|
| 30 | +
|
| 31 | +==What can this extension do?==
|
| 32 | +
|
| 33 | +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.).
|
| 34 | +
|
| 35 | +These can be used (among other things) to make infoblox-type templates much more WYSIWIG (see Examples) for novice/non-technical users.
|
| 36 | +
|
| 37 | +==Usage==
|
| 38 | +
|
| 39 | +These functions are all invoked exactly as their string parser functions would be (except with the '_e' appended to distinguish).
|
| 40 | +
|
| 41 | +=== pos_e: (string position)===
|
| 42 | +
|
| 43 | +<nowiki>{{#pos_e:value|key|offset}}</nowiki>
|
| 44 | +
|
| 45 | +Returns the first position of key inside the given value, or an empty string.
|
| 46 | +If offset is defined, this method will not search the first offset characters.
|
| 47 | +
|
| 48 | +See: http://php.net/manual/function.strpos.php
|
| 49 | +
|
| 50 | +=== rpos_e: (string position, reverse) ===
|
| 51 | +<nowiki>{{#rpos_e:value|key}}</nowiki>
|
| 52 | +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.
|
| 53 | +
|
| 54 | +See: http://php.net/manual/function.strrpos.php
|
| 55 | +
|
| 56 | +=== pad_e: (pad string) ===
|
| 57 | +<nowiki>{{#pad_e:value|length|with|direction}}</nowiki>
|
| 58 | +
|
| 59 | +Returns the value padded to the certain length with the given with string.
|
| 60 | +If the with string is not given, spaces are used for padding. The direction may be specified as: 'left', 'center' or 'right'.
|
| 61 | +
|
| 62 | +See: http://php.net/manual/function.str-pad.php
|
| 63 | +
|
| 64 | +=== replace_e: (string replace) ===
|
| 65 | +
|
| 66 | +<nowiki>{{#replace_e:value|from|to}}</nowiki>
|
| 67 | +
|
| 68 | +Returns the given value with all occurences of 'from' replaced with 'to'.
|
| 69 | +
|
| 70 | +See: http://php.net/manual/function.str-replace.php
|
| 71 | +
|
| 72 | +=== explode_e: (explode string) ===
|
| 73 | +<nowiki>{{#explode_e:value|delimiter|position}}</nowiki>
|
| 74 | +
|
| 75 | +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.
|
| 76 | +
|
| 77 | +Note: Pieces are counted from 0.<br>
|
| 78 | +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.
|
| 79 | +
|
| 80 | +See: http://php.net/manual/function.explode.php
|
| 81 | +
|
| 82 | +==Download instructions==
|
| 83 | +<!-- revise these instructions if code is available via a download site -->
|
| 84 | +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]]''.
|
| 85 | +
|
| 86 | +==Installation==
|
| 87 | +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]].
|
| 88 | +
|
| 89 | +Install and test [[Extension:ParserFunctions]] and (if necessary) [[Extension:StringFunctions]] prior to installing this extension.
|
| 90 | +
|
| 91 | +This extension must be included AFTER the invocation of the string parser functions.
|
| 92 | +To install this extension, add the following to [[Manual:LocalSettings.php|LocalSettings.php]]:
|
| 93 | +
|
| 94 | +=== For MediaWiki 1.15.1 and before ===
|
| 95 | +<source lang="php">
|
| 96 | +require_once("$IP/extensions/ParserFunctions/ParserFunctions.php");
|
| 97 | +require_once("$IP/extensions/StringFunctions/StringFunctions.php");
|
| 98 | +require_once("$IP/extensions/StringFunctionsEscaped/StringFunctionsEscaped.php");
|
| 99 | +</source>
|
| 100 | +
|
| 101 | +=== For MediaWiki 1.16a and after ===
|
| 102 | +<source lang="php">
|
| 103 | +require_once("$IP/extensions/ParserFunctions/ParserFunctions.php");
|
| 104 | +$wgPFEnableStringFunctions = true; // Note: this must be after ParserFunctions and before StringFunctionsEscaped
|
| 105 | +require_once("$IP/extensions/StringFunctionsEscaped/StringFunctionsEscaped.php");
|
| 106 | +</source>
|
| 107 | +==Examples==
|
| 108 | +
|
| 109 | +=== pos_e ===
|
| 110 | +
|
| 111 | +<pre>
|
| 112 | +{{#pos_e:Line 1
|
| 113 | +Line 2
|
| 114 | +Line 3|\n|7}}
|
| 115 | +
|
| 116 | +Returns:
|
| 117 | +
|
| 118 | +13
|
| 119 | +</pre>
|
| 120 | +
|
| 121 | +=== rpos_e ===
|
| 122 | +<pre>
|
| 123 | +{{#rpos_e:Line 1
|
| 124 | +Line 2
|
| 125 | +Line 3|\n}}
|
| 126 | +
|
| 127 | +Returns:
|
| 128 | +
|
| 129 | +13
|
| 130 | +</pre>
|
| 131 | +
|
| 132 | +=== pad_e ===
|
| 133 | +<pre>
|
| 134 | +~~{{#pad_e:xox|9|\n|center}}~~
|
| 135 | +
|
| 136 | +Returns:
|
| 137 | +
|
| 138 | +~~
|
| 139 | +
|
| 140 | +
|
| 141 | +xox
|
| 142 | +
|
| 143 | +
|
| 144 | +~~
|
| 145 | +</pre>
|
| 146 | +
|
| 147 | +=== replace_e ===
|
| 148 | +<pre>
|
| 149 | +{{#replace_e:Line 1
|
| 150 | +Line 2
|
| 151 | +Line 3|\n|<br>\n}}
|
| 152 | +
|
| 153 | +Returns:
|
| 154 | +
|
| 155 | +Line 1<br>
|
| 156 | +Line 2<br>
|
| 157 | +Line 3
|
| 158 | +
|
| 159 | +Which would display as:
|
| 160 | +
|
| 161 | +Line 1
|
| 162 | +Line 2
|
| 163 | +Line 3
|
| 164 | +
|
| 165 | +Rather than the unescaped:
|
| 166 | +
|
| 167 | +Line 1 Line 2 Line 3
|
| 168 | +
|
| 169 | +</pre>
|
| 170 | +
|
| 171 | +=== explode_e ===
|
| 172 | +<pre>
|
| 173 | +{{#explode_e:Line 1
|
| 174 | +Line 2
|
| 175 | +Line 3|\n|1}}
|
| 176 | +
|
| 177 | +Returns:
|
| 178 | +
|
| 179 | +Line 2
|
| 180 | +
|
| 181 | +</pre>
|
| 182 | +==See also==
|
| 183 | +
|
| 184 | +* [[Extension:ParserFunctions]]
|
| 185 | +* [[Extension:StringFunctions]]
|
| 186 | +* [[Extension:Lua]]
|