2 if ( !defined( 'MEDIAWIKI' ) )
3 die( 'StringFunctionsEscaped::This file is a MediaWiki extension, it is not a valid entry point' );
4 if ( !class_exists('ExtStringFunctions',false) &&
5 !(class_exists('ParserFunctions_HookStub',false) && isset($wgPFEnableStringFunctions) && $wgPFEnableStringFunctions))
6 die( 'StringFunctionsEscaped::You must have extension StringFunctions or extension ParserFunctions with string functions enabled' );
9 Defines a superset of string parser functions that allow character escaping in the 'search for' and 'replace with' arguments.
11 {{#pos_e:value|key|offset}}
13 Returns the first position of key inside the given value, or an empty string.
14 If offset is defined, this method will not search the first offset characters.
15 See: http://php.net/manual/function.strpos.php
19 Returns the last position of key inside the given value, or -1 if the key is
20 not found. When using this to search for the last delimiter, add +1 to the
21 result to retreive position after the last delimiter. This also works when
22 the delimiter is not found, because "-1 + 1" is zero, which is the beginning
24 See: http://php.net/manual/function.strrpos.php
26 {{#pad_e:value|length|with|direction}}
28 Returns the value padded to the certain length with the given with string.
29 If the with string is not given, spaces are used for padding. The direction
30 may be specified as: 'left', 'center' or 'right'.
31 See: http://php.net/manual/function.str-pad.php
34 {{#replace_e:value|from|to}}
36 Returns the given value with all occurences of 'from' replaced with 'to'.
37 See: http://php.net/manual/function.str-replace.php
40 {{#explode_e:value|delimiter|position}}
42 Splits the given value into pieces by the given delimiter and returns the
43 position-th piece. Empty string is returned if there are not enough pieces.
44 Note: Pieces are counted from 0.
45 Note: A negative value can be used to count pieces from the end, instead of
46 counting from the beginning. The last piece is at position -1.
47 See: http://php.net/manual/function.explode.php
50 Copyright (c) 2009 Jack D. Pond
51 Licensed under GNU version 2
54 $wgExtensionCredits['parserhook'][] = array(
56 'name' => 'StringFunctionsEscaped',
57 'version' => '1.0.0', // Sept 7, 2009
58 'description' => 'Allows escaped characters in string functions using c-like syntax',
59 'descriptionmsg' => 'pfunc_desc',
60 'author' => array('Jack D. Pond'),
61 'license' => 'GNU Version 2',
62 'url' => 'http://www.mediawiki.org/wiki/Extension:StringFunctionsEscaped',
65 $dir = dirname( __FILE__
) . '/';
67 # $wgExtensionMessagesFiles['StringFunctionsEscaped'] = $dir . 'StringFunctionsEscaped.i18n.php';
69 $wgExtensionFunctions[] = 'wfStringFunctionsEscaped';
71 $wgHooks['LanguageGetMagic'][] = 'wfStringFunctionsEscapedLanguageGetMagic';
73 function wfStringFunctionsEscaped ( ) {
74 global $wgParser, $wgExtStringFunctionsEscaped;
76 $wgExtStringFunctionsEscaped = new ExtStringFunctionsEscaped ( );
78 $wgParser->setFunctionHook('pos_e', array(&$wgExtStringFunctionsEscaped,'runPos_e' ));
79 $wgParser->setFunctionHook('rpos_e', array(&$wgExtStringFunctionsEscaped,'runRPos_e' ));
80 $wgParser->setFunctionHook('pad_e', array(&$wgExtStringFunctionsEscaped,'runPad_e' ));
81 $wgParser->setFunctionHook('replace_e', array(&$wgExtStringFunctionsEscaped,'runReplace_e' ));
82 $wgParser->setFunctionHook('explode_e', array(&$wgExtStringFunctionsEscaped,'runExplode_e' ));
85 function wfStringFunctionsEscapedLanguageGetMagic( &$magicWords, $langCode = "en" ) {
86 switch ( $langCode ) {
88 $magicWords['pos_e'] = array ( 0, 'pos_e' );
89 $magicWords['rpos_e'] = array ( 0, 'rpos_e' );
90 $magicWords['pad_e'] = array ( 0, 'pad_e' );
91 $magicWords['replace_e'] = array ( 0, 'replace_e' );
92 $magicWords['explode_e'] = array ( 0, 'explode_e' );
97 class ExtStringFunctionsEscaped
{
100 * {{#pos_e:value|key|offset}}
101 * Note: If the needle is an empty string, single space is used instead.
102 * Note: If the needle is not found, empty string is returned.
103 * Note: The needle is limited to specific length.
105 function runPos_e ( &$parser, $inStr = '', $inNeedle = '', $inOffset = 0 ) {
107 list($callback,$flags) = $wgParser->mFunctionHooks
['pos'];
108 return @call_user_func_array
( $callback,
109 array_merge(array($parser),array($inStr,stripcslashes($inNeedle),$inOffset) ));
113 * {{#rpos_e:value|key}}
114 * Note: If the needle is an empty string, single space is used instead.
115 * Note: If the needle is not found, -1 is returned.
116 * Note: The needle is limited to specific length.
118 function runRPos_e( &$parser, $inStr = '', $inNeedle = '' ) {
120 list($callback,$flags) = $wgParser->mFunctionHooks
['rpos'];
121 return @call_user_func_array
( $callback,
122 array_merge(array($parser),array($inStr,stripcslashes($inNeedle)) ));
126 * {{#pad_e:value|length|with|direction}}
127 * Note: Length of the resulting string is limited.
129 function runPad_e( &$parser, $inStr = '', $inLen = 0, $inWith = '', $inDirection = '' ) {
131 list($callback,$flags) = $wgParser->mFunctionHooks
['pad'];
132 return @call_user_func_array
( $callback,
133 array_merge(array($parser),array($inStr, $inLen , stripcslashes($inWith), $inDirection) ));
137 * {{#replace:value|from|to}}
138 * Note: If the needle is an empty string, single space is used instead.
139 * Note: The needle is limited to specific length.
140 * Note: The product is limited to specific length.
142 function runReplace_e( $parser, $inStr = '', $inReplaceFrom = '', $inReplaceTo = '' ) {
144 list($callback,$flags) = $wgParser->mFunctionHooks
['replace'];
145 return @call_user_func_array
( $callback,
146 array_merge(array($parser),array($inStr, stripcslashes($inReplaceFrom), stripcslashes($inReplaceTo)) ));
150 * {{#explode_e:value|delimiter|position}}
151 * Note: Negative position can be used to specify tokens from the end.
152 * Note: If the divider is an empty string, single space is used instead.
153 * Note: The divider is limited to specific length.
154 * Note: Empty string is returned, if there is not enough exploded chunks.
156 function runExplode_e ( &$parser, $inStr = '', $inDiv = '', $inPos = 0 ) {
158 list($callback,$flags) = $wgParser->mFunctionHooks
['explode'];
159 return @call_user_func_array
( $callback,
160 array_merge(array($parser),array($inStr, stripcslashes($inDiv), $inPos) ));