r22711 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r22710‎ | r22711 | r22712 >
Date:17:34, 4 June 2007
Author:robchurch
Status:old
Tags:
Comment:
(bug 10136, among others) Introduce TITLEPARTS parser function; returns a specified number of slash-separated segments of a title.
Modified paths:
  • /trunk/extensions/ParserFunctions/ParserFunctions.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ParserFunctions/ParserFunctions.php
@@ -216,6 +216,34 @@
217217 $this->mTimeCache[$format][$date] = $result;
218218 return $result;
219219 }
 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+
220248 }
221249
222250 function wfSetupParserFunctions() {
@@ -231,6 +259,7 @@
232260 $wgParser->setFunctionHook( 'ifexist', array( &$wgExtParserFunctions, 'ifexist' ) );
233261 $wgParser->setFunctionHook( 'time', array( &$wgExtParserFunctions, 'time' ) );
234262 $wgParser->setFunctionHook( 'rel2abs', array( &$wgExtParserFunctions, 'rel2abs' ) );
 263+ $wgParser->setFunctionHook( 'titleparts', array( &$wgExtParserFunctions, 'titleparts' ) );
235264
236265 $wgMessageCache->addMessage( 'pfunc_time_error', "Error: invalid time" );
237266 $wgMessageCache->addMessage( 'pfunc_time_too_long', "Error: too many #time calls" );
@@ -262,6 +291,7 @@
263292 $magicWords['ifexist'] = array( 0, 'ifexist' );
264293 $magicWords['time'] = array( 0, 'time' );
265294 $magicWords['rel2abs'] = array( 0, 'rel2abs' );
 295+ $magicWords['titleparts'] = array( 0, 'titleparts' );
266296 }
267297 return true;
268298 }

Status & tagging log