Added string functions (replace_e,pad_e,pos_e,rpos_e,and explode_e) that allow the...
[lhc/web/wiklou.git] / extensions / StringFunctionsEscaped / StringFunctionsEscaped.php
1 <?php
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' );
7 /*
8
9 Defines a superset of string parser functions that allow character escaping in the 'search for' and 'replace with' arguments.
10
11 {{#pos_e:value|key|offset}}
12
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
16
17 {{#rpos_e:value|key}}
18
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
23 of the given value.
24 See: http://php.net/manual/function.strrpos.php
25
26 {{#pad_e:value|length|with|direction}}
27
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
32
33
34 {{#replace_e:value|from|to}}
35
36 Returns the given value with all occurences of 'from' replaced with 'to'.
37 See: http://php.net/manual/function.str-replace.php
38
39
40 {{#explode_e:value|delimiter|position}}
41
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
48
49
50 Copyright (c) 2009 Jack D. Pond
51 Licensed under GNU version 2
52 */
53
54 $wgExtensionCredits['parserhook'][] = array(
55 'path' => __FILE__,
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',
63 );
64
65 $dir = dirname( __FILE__ ) . '/';
66 # RFU
67 # $wgExtensionMessagesFiles['StringFunctionsEscaped'] = $dir . 'StringFunctionsEscaped.i18n.php';
68
69 $wgExtensionFunctions[] = 'wfStringFunctionsEscaped';
70
71 $wgHooks['LanguageGetMagic'][] = 'wfStringFunctionsEscapedLanguageGetMagic';
72
73 function wfStringFunctionsEscaped ( ) {
74 global $wgParser, $wgExtStringFunctionsEscaped;
75
76 $wgExtStringFunctionsEscaped = new ExtStringFunctionsEscaped ( );
77
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' ));
83 }
84
85 function wfStringFunctionsEscapedLanguageGetMagic( &$magicWords, $langCode = "en" ) {
86 switch ( $langCode ) {
87 default:
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' );
93 }
94 return true;
95 }
96
97 class ExtStringFunctionsEscaped {
98
99 /**
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.
104 */
105 function runPos_e ( &$parser, $inStr = '', $inNeedle = '', $inOffset = 0 ) {
106 global $wgParser;
107 list($callback,$flags) = $wgParser->mFunctionHooks['pos'];
108 return @call_user_func_array( $callback,
109 array_merge(array($parser),array($inStr,stripcslashes($inNeedle),$inOffset) ));
110 }
111
112 /**
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.
117 */
118 function runRPos_e( &$parser, $inStr = '', $inNeedle = '' ) {
119 global $wgParser;
120 list($callback,$flags) = $wgParser->mFunctionHooks['rpos'];
121 return @call_user_func_array( $callback,
122 array_merge(array($parser),array($inStr,stripcslashes($inNeedle)) ));
123 }
124
125 /**
126 * {{#pad_e:value|length|with|direction}}
127 * Note: Length of the resulting string is limited.
128 */
129 function runPad_e( &$parser, $inStr = '', $inLen = 0, $inWith = '', $inDirection = '' ) {
130 global $wgParser;
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) ));
134 }
135
136 /**
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.
141 */
142 function runReplace_e( $parser, $inStr = '', $inReplaceFrom = '', $inReplaceTo = '' ) {
143 global $wgParser;
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)) ));
147 }
148
149 /**
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.
155 */
156 function runExplode_e ( &$parser, $inStr = '', $inDiv = '', $inPos = 0 ) {
157 global $wgParser;
158 list($callback,$flags) = $wgParser->mFunctionHooks['explode'];
159 return @call_user_func_array( $callback,
160 array_merge(array($parser),array($inStr, stripcslashes($inDiv), $inPos) ));
161 }
162
163 }