Index: trunk/extensions/ParserFunctions/ParserFunctions.php |
— | — | @@ -216,6 +216,34 @@ |
217 | 217 | $this->mTimeCache[$format][$date] = $result; |
218 | 218 | return $result; |
219 | 219 | } |
| 220 | + |
| 221 | + /** |
| 222 | + * Obtain a specified number of slash-separated parts of a title, |
| 223 | + * e.g. {{#titleparts:Hello/World|1}} => "Hello" |
| 224 | + * |
| 225 | + * @param Parser $parser Parent parser |
| 226 | + * @param string $title Title to split |
| 227 | + * @param int $parts Number of parts to keep |
| 228 | + * @return string |
| 229 | + */ |
| 230 | + public function titleparts( $parser, $title = '', $parts = -1 ) { |
| 231 | + $parts = intval( $parts ); |
| 232 | + $ntitle = Title::newFromText( $title ); |
| 233 | + if( $ntitle instanceof Title ) { |
| 234 | + $bits = explode( '/', $ntitle->getPrefixedText() ); |
| 235 | + if( $parts <= 0 || $parts > count( $bits ) ) { |
| 236 | + return implode( '/', $bits ); |
| 237 | + } else { |
| 238 | + $keep = array(); |
| 239 | + for( $i = 0; $i < $parts; $i++ ) |
| 240 | + $keep[] = array_shift( $bits ); |
| 241 | + return implode( '/', $keep ); |
| 242 | + } |
| 243 | + } else { |
| 244 | + return $title; |
| 245 | + } |
| 246 | + } |
| 247 | + |
220 | 248 | } |
221 | 249 | |
222 | 250 | function wfSetupParserFunctions() { |
— | — | @@ -231,6 +259,7 @@ |
232 | 260 | $wgParser->setFunctionHook( 'ifexist', array( &$wgExtParserFunctions, 'ifexist' ) ); |
233 | 261 | $wgParser->setFunctionHook( 'time', array( &$wgExtParserFunctions, 'time' ) ); |
234 | 262 | $wgParser->setFunctionHook( 'rel2abs', array( &$wgExtParserFunctions, 'rel2abs' ) ); |
| 263 | + $wgParser->setFunctionHook( 'titleparts', array( &$wgExtParserFunctions, 'titleparts' ) ); |
235 | 264 | |
236 | 265 | $wgMessageCache->addMessage( 'pfunc_time_error', "Error: invalid time" ); |
237 | 266 | $wgMessageCache->addMessage( 'pfunc_time_too_long', "Error: too many #time calls" ); |
— | — | @@ -262,6 +291,7 @@ |
263 | 292 | $magicWords['ifexist'] = array( 0, 'ifexist' ); |
264 | 293 | $magicWords['time'] = array( 0, 'time' ); |
265 | 294 | $magicWords['rel2abs'] = array( 0, 'rel2abs' ); |
| 295 | + $magicWords['titleparts'] = array( 0, 'titleparts' ); |
266 | 296 | } |
267 | 297 | return true; |
268 | 298 | } |