r22941 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r22940‎ | r22941 | r22942 >
Date:00:26, 13 June 2007
Author:robchurch
Status:old
Tags:
Comment:
Accept a third parameter to {{#titleparts}}; offset, which users can use to chop up the title to their heart's content
Modified paths:
  • /trunk/extensions/ParserFunctions/ParserFunctions.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ParserFunctions/ParserFunctions.php
@@ -224,17 +224,23 @@
225225 * @param Parser $parser Parent parser
226226 * @param string $title Title to split
227227 * @param int $parts Number of parts to keep
 228+ * @param int $offset Offset starting at 1
228229 * @return string
229230 */
230 - public function titleparts( $parser, $title = '', $parts = -1 ) {
 231+ public function titleparts( $parser, $title = '', $parts = -1, $offset = 1 ) {
231232 $parts = intval( $parts );
 233+ $offset = intval( $offset ) - 1;
232234 $ntitle = Title::newFromText( $title );
233235 if( $ntitle instanceof Title ) {
234236 $bits = explode( '/', $ntitle->getPrefixedText() );
235237 if( $parts <= 0 || $parts > count( $bits ) ) {
236238 return $ntitle->getPrefixedText();
 239+ } elseif( $offset < 0 || $offset > count( $bits ) ) {
 240+ return $ntitle->getPrefixedText();
237241 } else {
238242 $keep = array();
 243+ for( $i = 0; $i < $offset; $i++ )
 244+ array_shift( $bits );
239245 for( $i = 0; $i < $parts; $i++ )
240246 $keep[] = array_shift( $bits );
241247 return implode( '/', $keep );

Status & tagging log