Index: trunk/extensions/ParserFun/includes/PFun_Caller.php |
— | — | @@ -181,6 +181,20 @@ |
182 | 182 | } |
183 | 183 | |
184 | 184 | /** |
| 185 | + * Returns a string, exactly like '{{CALLER}}' as variable would return it. |
| 186 | + * |
| 187 | + * @param PPFrame $frame |
| 188 | + * |
| 189 | + * @return string |
| 190 | + */ |
| 191 | + static function getCallerVar( PPFrame $frame ) { |
| 192 | + $siteFrame = ParserFunCaller::getFrameStackItem( $frame, 1 ); |
| 193 | + return ( $siteFrame !== null ) |
| 194 | + ? self::createSiteList( array( $siteFrame ), false ) |
| 195 | + : ''; |
| 196 | + } |
| 197 | + |
| 198 | + /** |
185 | 199 | * Returns a certain parent caller from a given frame by index. 0 returns the given frame, 1 would return |
186 | 200 | * the frame of the site which was calling the given frame and so on. |
187 | 201 | * |
Index: trunk/extensions/ParserFun/includes/PFun_This.php |
— | — | @@ -0,0 +1,147 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Class for the 'THIS' variable/parser function. |
| 6 | + * |
| 7 | + * @since 0.2 (in 'ExtParserFun' class before) |
| 8 | + * |
| 9 | + * @file PFun_This.php |
| 10 | + * @ingroup ParserFun |
| 11 | + * |
| 12 | + * @author Daniel Werner |
| 13 | + */ |
| 14 | +class ParserFunThis { |
| 15 | + |
| 16 | + /** |
| 17 | + * Magic word 'THIS:' to return certain information about the page the word actually is defined on |
| 18 | + */ |
| 19 | + static function pfObj_this( Parser &$parser, PPFrame $frame = null, $args = null ) { |
| 20 | + // if MW version is too old or something is wrong: |
| 21 | + if( $frame === null || $frame->title === null ) { |
| 22 | + return ''; |
| 23 | + } |
| 24 | + |
| 25 | + // get part behind 'THIS:' if only 'THIS', use 'FULLPAGENAME' |
| 26 | + $index = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : ''; |
| 27 | + |
| 28 | + $newArgs = array(); |
| 29 | + |
| 30 | + if( $index !== '' ) { |
| 31 | + // clean up arguments as if first argument never were set: |
| 32 | + unset( $args[0] ); |
| 33 | + foreach( $args as $arg ) { |
| 34 | + $newArgs[] = $arg; |
| 35 | + } |
| 36 | + |
| 37 | + // get magic word ID of the variable name: |
| 38 | + $mwId = self::getVariablesMagicWordId( $parser, $index ); |
| 39 | + if( $mwId === null ) { |
| 40 | + // requested variable doesn't exist, make the thing a template call |
| 41 | + return array( null, 'found' => false ); |
| 42 | + } |
| 43 | + } |
| 44 | + else { |
| 45 | + // if only '{{THIS}}', set magic word id to 'FULLPAGENAME' |
| 46 | + $mwId = 'fullpagename'; |
| 47 | + } |
| 48 | + |
| 49 | + // get value: |
| 50 | + $out = self::getThisVariableValue( $mwId, $parser, $frame, $newArgs ); |
| 51 | + if( $out === null ) { |
| 52 | + // requested variable doesn't support 'THIS:', make the thing a template call |
| 53 | + return array( null, 'found' => false ); |
| 54 | + } |
| 55 | + |
| 56 | + return $out; |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Returns the magic word ID for a variable like the user would write it. Returns null in case there |
| 62 | + * is no word for the given variables name. |
| 63 | + * |
| 64 | + * @param Parser $parser |
| 65 | + * @param type $word |
| 66 | + * |
| 67 | + * @return string|null |
| 68 | + */ |
| 69 | + static function getVariablesMagicWordId( Parser $parser, $word ) { |
| 70 | + // get all local (including translated) magic words IDs (values) with their actual literals (keys) |
| 71 | + // for case insensitive [0] and sensitive [1] |
| 72 | + $magicWords = $parser->mVariables->getHash(); |
| 73 | + |
| 74 | + if( array_key_exists( strtolower( $word ), $magicWords[0] ) ) { |
| 75 | + // case insensitive word match |
| 76 | + $mwId = $magicWords[0][ strtolower( $word ) ]; |
| 77 | + } |
| 78 | + elseif( array_key_exists( $word, $magicWords[1] ) ) { |
| 79 | + // case sensitive word match |
| 80 | + $mwId = $magicWords[1][ $word ]; |
| 81 | + } |
| 82 | + else { |
| 83 | + // requested magic word doesn't exist for variables |
| 84 | + |
| 85 | + return null; |
| 86 | + } |
| 87 | + return $mwId; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Returns the value of a variable like '{{FULLPAGENAME}}' in the context of the given PPFrame objects |
| 92 | + * $frame->$title instead of the Parser objects subject. Returns null in case the requested variable |
| 93 | + * does not support '{{THIS:}}'. |
| 94 | + * |
| 95 | + * @param string $mwId magic word ID of the variable |
| 96 | + * @param Parser $parser |
| 97 | + * @param PPFrame $frame |
| 98 | + * @param array $args |
| 99 | + * |
| 100 | + * @return string|null |
| 101 | + */ |
| 102 | + static function getThisVariableValue( $mwId, Parser &$parser, $frame, $args = array() ) { |
| 103 | + $ret = null; |
| 104 | + $title = $frame->title; |
| 105 | + |
| 106 | + if( $title === null ) { |
| 107 | + return null; |
| 108 | + } |
| 109 | + |
| 110 | + // check whether info is available, e.g. 'THIS:FULLPAGENAME' requires 'FULLPAGENAME' |
| 111 | + switch( $mwId ) { |
| 112 | + case 'namespace': |
| 113 | + // 'namespace' function name was renamed as PHP 5.3 came along |
| 114 | + if( is_callable( 'CoreParserFunctions::mwnamespace' ) ) { |
| 115 | + $ret = CoreParserFunctions::mwnamespace( $parser, $title->getPrefixedText() ); |
| 116 | + break; |
| 117 | + } |
| 118 | + // else: no different from the other variables |
| 119 | + // no-break, default function call |
| 120 | + case 'fullpagename': |
| 121 | + case 'fullpagenamee': |
| 122 | + case 'pagename': |
| 123 | + case 'pagenamee': |
| 124 | + case 'basepagename': |
| 125 | + case 'basepagenamee': |
| 126 | + case 'subpagename': |
| 127 | + case 'subpagenamee': |
| 128 | + case 'subjectpagename': |
| 129 | + case 'subjectpagenamee': |
| 130 | + case 'talkpagename': |
| 131 | + case 'talkpagenamee': |
| 132 | + case 'namespacee': // special treat for 'namespace', on top |
| 133 | + case 'subjectspace': |
| 134 | + case 'subjectspacee': |
| 135 | + case 'talkspace': |
| 136 | + case 'talkspacee': |
| 137 | + // core parser function information requested |
| 138 | + $ret = CoreParserFunctions::$mwId( $parser, $title->getPrefixedText() ); |
| 139 | + break; |
| 140 | + |
| 141 | + default: |
| 142 | + // give other extensions a chance to hook up with this and return their own values: |
| 143 | + wfRunHooks( 'GetThisVariableValueSwitch', array( &$parser, $title, &$mwId, &$ret, $frame, $args ) ); |
| 144 | + } |
| 145 | + return $ret; |
| 146 | + } |
| 147 | + |
| 148 | +} |
Property changes on: trunk/extensions/ParserFun/includes/PFun_This.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 149 | + native |
Index: trunk/extensions/ParserFun/ParserFun.php |
— | — | @@ -59,7 +59,8 @@ |
60 | 60 | $wgHooks['ParserGetVariableValueSwitch'][] = 'ExtParserFun::onParserGetVariableValueSwitch'; |
61 | 61 | |
62 | 62 | |
63 | | -// 'parse' and 'CALLER' parser function initializations: |
| 63 | +// Parser function initializations: |
| 64 | +$wgAutoloadClasses['ParserFunThis' ] = ExtParserFun::getDir() . '/includes/PFun_This.php'; |
64 | 65 | $wgAutoloadClasses['ParserFunParse' ] = ExtParserFun::getDir() . '/includes/PFun_Parse.php'; |
65 | 66 | $wgAutoloadClasses['ParserFunCaller'] = ExtParserFun::getDir() . '/includes/PFun_Caller.php'; |
66 | 67 | |
— | — | @@ -87,164 +88,43 @@ |
88 | 89 | const MAG_THIS = 'this'; |
89 | 90 | const MAG_CALLER = 'caller'; |
90 | 91 | |
91 | | - public static function init( Parser &$parser ) { |
92 | | - global $egParserFunEnabledFunctions; |
93 | | - if( in_array( self::MAG_THIS, $egParserFunEnabledFunctions ) ) { |
| 92 | + static function init( Parser &$parser ) { |
| 93 | + if( self::isEnabledFunction( self::MAG_THIS ) ) { |
94 | 94 | // only register function if not disabled by configuration |
95 | | - $parser->setFunctionHook( self::MAG_THIS, array( __CLASS__, 'pfObj_this' ), SFH_NO_HASH | SFH_OBJECT_ARGS ); |
| 95 | + $parser->setFunctionHook( self::MAG_THIS, array( 'ParserFunThis', 'pfObj_this' ), SFH_NO_HASH | SFH_OBJECT_ARGS ); |
96 | 96 | } |
97 | 97 | return true; |
98 | 98 | } |
99 | 99 | |
100 | 100 | /** |
| 101 | + * returns whether a certain variable/parser function is active by the local wiki configuration. |
| 102 | + * |
| 103 | + * @since 0.2 |
| 104 | + * |
| 105 | + * @param type $word |
| 106 | + * @return bool |
| 107 | + */ |
| 108 | + static function isEnabledFunction( $word ) { |
| 109 | + global $egParserFunEnabledFunctions; |
| 110 | + return in_array( $word, $egParserFunEnabledFunctions ); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
101 | 114 | * Returns the extensions base installation directory. |
102 | 115 | * |
103 | 116 | * @since 0.1 |
104 | 117 | * |
105 | 118 | * @return boolean |
106 | 119 | */ |
107 | | - public static function getDir() { |
| 120 | + static function getDir() { |
108 | 121 | static $dir = null; |
109 | | - |
110 | 122 | if( $dir === null ) { |
111 | 123 | $dir = dirname( __FILE__ ); |
112 | 124 | } |
113 | 125 | return $dir; |
114 | 126 | } |
115 | 127 | |
116 | | - /** |
117 | | - * Magic word 'THIS:' to return certain information about the page the word actually is defined on |
118 | | - */ |
119 | | - static function pfObj_this( Parser &$parser, PPFrame $frame = null, $args = null ) { |
120 | | - // if MW version is too old or something is wrong: |
121 | | - if( $frame === null || $frame->title === null ) { |
122 | | - return ''; |
123 | | - } |
124 | | - |
125 | | - // get part behind 'THIS:' if only 'THIS', use 'FULLPAGENAME' |
126 | | - $index = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : ''; |
127 | | - |
128 | | - $newArgs = array(); |
129 | | - |
130 | | - if( $index !== '' ) { |
131 | | - // clean up arguments as if first argument never were set: |
132 | | - unset( $args[0] ); |
133 | | - foreach( $args as $arg ) { |
134 | | - $newArgs[] = $arg; |
135 | | - } |
136 | | - |
137 | | - // get magic word ID of the variable name: |
138 | | - $mwId = self::getVariablesMagicWordId( $parser, $index ); |
139 | | - if( $mwId === null ) { |
140 | | - // requested variable doesn't exist, make the thing a template call |
141 | | - return array( null, 'found' => false ); |
142 | | - } |
143 | | - } |
144 | | - else { |
145 | | - // if only '{{THIS}}', set magic word id to 'FULLPAGENAME' |
146 | | - $mwId = 'fullpagename'; |
147 | | - } |
148 | | - |
149 | | - // get value: |
150 | | - $out = self::getThisVariableValue( $mwId, $parser, $frame, $newArgs ); |
151 | | - if( $out === null ) { |
152 | | - // requested variable doesn't support 'THIS:', make the thing a template call |
153 | | - return array( null, 'found' => false ); |
154 | | - } |
155 | | - |
156 | | - return $out; |
157 | | - |
158 | | - } |
159 | 128 | |
160 | | - /** |
161 | | - * Returns the magic word ID for a variable like the user would write it. Returns null in case there |
162 | | - * is no word for the given variables name. |
163 | | - * |
164 | | - * @param Parser $parser |
165 | | - * @param type $word |
166 | | - * |
167 | | - * @return string|null |
168 | | - */ |
169 | | - static function getVariablesMagicWordId( Parser $parser, $word ) { |
170 | | - // get all local (including translated) magic words IDs (values) with their actual literals (keys) |
171 | | - // for case insensitive [0] and sensitive [1] |
172 | | - $magicWords = $parser->mVariables->getHash(); |
173 | | - |
174 | | - if( array_key_exists( strtolower( $word ), $magicWords[0] ) ) { |
175 | | - // case insensitive word match |
176 | | - $mwId = $magicWords[0][ strtolower( $word ) ]; |
177 | | - } |
178 | | - elseif( array_key_exists( $word, $magicWords[1] ) ) { |
179 | | - // case sensitive word match |
180 | | - $mwId = $magicWords[1][ $word ]; |
181 | | - } |
182 | | - else { |
183 | | - // requested magic word doesn't exist for variables |
184 | | - |
185 | | - return null; |
186 | | - } |
187 | | - return $mwId; |
188 | | - } |
189 | | - |
190 | | - /** |
191 | | - * Returns the value of a variable like '{{FULLPAGENAME}}' in the context of the given PPFrame objects |
192 | | - * $frame->$title instead of the Parser objects subject. Returns null in case the requested variable |
193 | | - * does not support '{{THIS:}}'. |
194 | | - * |
195 | | - * @param string $mwId magic word ID of the variable |
196 | | - * @param Parser $parser |
197 | | - * @param PPFrame $frame |
198 | | - * @param array $args |
199 | | - * |
200 | | - * @return string|null |
201 | | - */ |
202 | | - static function getThisVariableValue( $mwId, Parser &$parser, $frame, $args = array() ) { |
203 | | - $ret = null; |
204 | | - $title = $frame->title; |
205 | | - |
206 | | - if( $title === null ) { |
207 | | - return null; |
208 | | - } |
209 | | - |
210 | | - // check whether info is available, e.g. 'THIS:FULLPAGENAME' requires 'FULLPAGENAME' |
211 | | - switch( $mwId ) { |
212 | | - case 'namespace': |
213 | | - // 'namespace' function name was renamed as PHP 5.3 came along |
214 | | - if( is_callable( 'CoreParserFunctions::mwnamespace' ) ) { |
215 | | - $ret = CoreParserFunctions::mwnamespace( $parser, $title->getPrefixedText() ); |
216 | | - break; |
217 | | - } |
218 | | - // else: no different from the other variables |
219 | | - // no-break, default function call |
220 | | - case 'fullpagename': |
221 | | - case 'fullpagenamee': |
222 | | - case 'pagename': |
223 | | - case 'pagenamee': |
224 | | - case 'basepagename': |
225 | | - case 'basepagenamee': |
226 | | - case 'subpagename': |
227 | | - case 'subpagenamee': |
228 | | - case 'subjectpagename': |
229 | | - case 'subjectpagenamee': |
230 | | - case 'talkpagename': |
231 | | - case 'talkpagenamee': |
232 | | - case 'namespacee': // special treat for 'namespace', on top |
233 | | - case 'subjectspace': |
234 | | - case 'subjectspacee': |
235 | | - case 'talkspace': |
236 | | - case 'talkspacee': |
237 | | - // core parser function information requested |
238 | | - $ret = CoreParserFunctions::$mwId( $parser, $title->getPrefixedText() ); |
239 | | - break; |
240 | | - |
241 | | - default: |
242 | | - // give other extensions a chance to hook up with this and return their own values: |
243 | | - wfRunHooks( 'GetThisVariableValueSwitch', array( &$parser, $title, &$mwId, &$ret, $frame, $args ) ); |
244 | | - } |
245 | | - return $ret; |
246 | | - } |
247 | | - |
248 | | - |
249 | 129 | ################## |
250 | 130 | # Hooks Handling # |
251 | 131 | ################## |
— | — | @@ -257,26 +137,23 @@ |
258 | 138 | switch( $magicWordId ) { |
259 | 139 | /** THIS **/ |
260 | 140 | case self::MAG_THIS: |
261 | | - $ret = self::pfObj_this( $parser, $frame, null ); |
| 141 | + $ret = ParserFunThis::pfObj_this( $parser, $frame, null ); |
262 | 142 | break; |
263 | 143 | |
264 | 144 | /** CALLER **/ |
265 | 145 | case self::MAG_CALLER: |
266 | | - $siteFrame = ParserFunCaller::getFrameStackItem( $frame, 1 ); |
267 | | - $ret = ( $siteFrame !== null ) ? $siteFrame->title->getPrefixedText() : ''; |
| 146 | + $ret = ParserFunCaller::getCallerVar( $frame ); |
268 | 147 | break; |
269 | 148 | } |
270 | 149 | return true; |
271 | 150 | } |
272 | 151 | |
273 | | - static function onMagicWordwgVariableIDs( &$variableIds ) { |
274 | | - global $egParserFunEnabledFunctions; |
| 152 | + static function onMagicWordwgVariableIDs( &$variableIds ) { |
275 | 153 | // only register variables if not disabled by configuration |
276 | | - |
277 | | - if( in_array( self::MAG_THIS, $egParserFunEnabledFunctions ) ) { |
| 154 | + if( self::isEnabledFunction( self::MAG_THIS ) ) { |
278 | 155 | $variableIds[] = self::MAG_THIS; |
279 | 156 | } |
280 | | - if( in_array( self::MAG_CALLER, $egParserFunEnabledFunctions ) ) { |
| 157 | + if( self::isEnabledFunction( self::MAG_CALLER ) ) { |
281 | 158 | $variableIds[] = self::MAG_CALLER; |
282 | 159 | } |
283 | 160 | return true; |