Jump to content

Extension:PathFunctions

From mediawiki.org
Revision as of 00:36, 1 May 2009 by Emufarmers (talk | contribs) (These functions were not parameterized until 1.15)
MediaWiki extensions manual
PathFunctions
Release status: beta
Implementation Parser function
Description A reimplementation of the builtin variables PAGENAME etc. with the ability to specify what page to evaluate on.
Author(s) Carl Fürstenberg
Latest version 0.99.0 (2007-01-04)
MediaWiki 1.7+
License No license specified
Download N/A
GPL 2.0

This extension enables a reimplementation of builtin functions like PAGENAME, but with the possibillity to define the base path.

Example

{{ #PAGENAME: Help:Foo }} gives Foo

Configuration

If setting the variable $wgPathFunctionsUseNoPrefixHash to true, then the functions are without the preceeding hash symbol, and more like the built in variables. That is for example {{ PAGENAME: Help:Foo }}.

Installation

Add this line to LocalSettings.php:

require_once( "$IP/extensions/PathFunctions/PathFunctions.php" );

And copy the following to extensions/PathFunctions/PathFunctions.php and extensions/PathFunctions/PathFunctions.i18n.php:

PathFunctions.php

<?php

if ( !defined( 'MEDIAWIKI' ) ) {
	die( 'This file is a MediaWiki extension, it is not a valid entry point' );
}

/**
 * A reimplementation of the builtin variables PAGENAME etc. with the ability to specify what page to evaluate on.
 * If set $wgPathFunctionsUseNoPrefixHash to true, then the functions are without the preceeding hash symbol, and more like the built in variables.
 *
 * @package MediaWiki
 * @subpackage Extensions
 *
 * @link http://www.mediawiki.org/wiki/Extension:PathFunctions Documentation
 *
 * @author Carl Fürstenberg (AzaToth) <azatoth@gmail.com>
 * @copyright Copyright © 2006 Carl Fürstenberg
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 */

require_once('PathFunctions.i18n.php');

$wgExtensionFunctions[] = 'wfSetupPathFunctions';
$wgExtensionCredits['parserhook'][] = array( 
	'version' => '0.99.0',
	'description' => 'A reimplementation of the builtin variables PAGENAME etc. with the ability to specify what page to evaluate on.',
	'name' => 'PathFunctions', 
	'url' => 'http://www.mediawiki.org/wiki/Extension:PathFunctions',
	'author' => 'Carl Fürstenberg (AzaToth)' 
);

$wgHooks['LanguageGetMagic'][]       = 'wfPathFunctionsLanguageGetMagic';

class ExtPathFunctions {

	public function pagename( &$parser , $path = false ) {
		$path = trim($path);
		$title = $path ? Title::newFromText( $path ) : $parser->mTitle;
		return $title->getText();
	}

	public function pagenamee( &$parser , $path = false ) {
		$path = trim($path);
		$title = $path ? Title::newFromText( $path ) : $parser->mTitle;
		return $title->getPartialUrl();
	}

	public function fullpagename( &$parser , $path = false ) {
		$path = trim($path);
		$title = $path ? Title::newFromText( $path ) : $parser->mTitle;
		return $title->getPrefixedText();
	}

	public function fullpagenamee( &$parser , $path = false ) {
		$path = trim($path);
		$title = $path ? Title::newFromText( $path ) : $parser->mTitle;
		return $title->getPrefixedURL();
	}

	public function subpagename( &$parser , $path = false ) {
		$path = trim($path);
		$title = $path ? Title::newFromText( $path ) : $parser->mTitle;
		return $title->getSubpageText();
	}

	public function subpagenamee( &$parser , $path = false ) {
		$path = trim($path);
		$title = $path ? Title::newFromText( $path ) : $parser->mTitle;
		return $title->getSubpageUrlForm();
	}

	public function basepagename( &$parser , $path = false ) {
		$path = trim($path);
		$title = $path ? Title::newFromText( $path ) : $parser->mTitle;
		return $title->getBaseText();
	}

	public function basepagenamee( &$parser , $path = false ) {
		$path = trim($path);
		$title = $path ? Title::newFromText( $path ) : $parser->mTitle;
		return $title->getBaseText();
	}

	public function talkpagename( &$parser , $path = false ) {
		$path = trim($path);
		$title = $path ? Title::newFromText( $path ) : $parser->mTitle;
		if( !$title->canTalk() ) {
			return wfMsg( 'pathfunc_no_talk' , $title->getNsText() );
		} else {
			return $title->getTalkPage()->getPrefixedText();
		}
	}

	public function talkpagenamee( &$parser , $path = false ) {
		$path = trim($path);
		$title = $path ? Title::newFromText( $path ) : $parser->mTitle;
		if( !$title->canTalk() ) {
			return wfMsg( 'pathfunc_no_talk' , $title->getNsText() );
		} else {
			return $title->getTalkPage()->getPrefixedUrl();
		}
	}

	public function subjectpagename( &$parser , $path = false ) {
		$path = trim($path);
		$title = $path ? Title::newFromText( $path ) : $parser->mTitle;
		return $title->getSubjectPage()->getPrefixedText();
	}

	public function subjectpagenamee( &$parser , $path = false ) {
		$path = trim($path);
		$title = $path ? Title::newFromText( $path ) : $parser->mTitle;
		return $title->getSubjectPage()->getPrefixedUrl();
	}

	public function namespace( &$parser , $path = false ) {
		$path = trim($path);
		$title = $path ? Title::newFromText( $path ) : $parser->mTitle;
		return strtr( $title->getNsText() , '_' , ' ' );
	}

	public function namespacee( &$parser , $path = false ) {
		$path = trim($path);
		$title = $path ? Title::newFromText( $path ) : $parser->mTitle;
		return wfUrlencode( $title->getNsText() );
	}

	public function talkspace( &$parser , $path = false ) {
		$path = trim($path);
		$title = $path ? Title::newFromText( $path ) : $parser->mTitle;
		if( !$title->canTalk() ) {
			return wfMsg( 'pathfunc_no_talk' , $title->getNsText() );
		} else {
			return strtr( $title->getTalkNsText() , '_' , ' ' );
		}
	}

	public function talkspacee( &$parser , $path = false ) {
		$path = trim($path);
		$title = $path ? Title::newFromText( $path ) : $parser->mTitle;
		if( !$title->canTalk() ) {
			return wfMsg( 'pathfunc_no_talk' , $title->getNsText() );
		} else {
			return wfUrlencode( $title->getTalkNsText() );
		}
	}

	public function subjectspace( &$parser , $path = false ) {
		$path = trim($path);
		$title = $path ? Title::newFromText( $path ) : $parser->mTitle;
		return strtr( $title->getSubjectNsText() , '_' , ' ' );
	}

	public function subjectspacee( &$parser , $path = false ) {
		$path = trim($path);
		$title = $path ? Title::newFromText( $path ) : $parser->mTitle;
		return wfUrlencode( $title->getSubjectNsText() );
	}
}

function wfSetupPathFunctions() {
	global $wgParser, $wgExtPathFunctions, $wgMessageCache, $wgPathFunctionsMessages, $wgPathFunctionsUseNoPrefixHash;
	$wgExtPathFunctions = new ExtPathFunctions();
	$flags = $wgPathFunctionsUseNoPrefixHash ? SFH_NO_HASH : 0;

	$wgParser->setFunctionHook( 'pagename'			, array( &$wgExtPathFunctions, 'pagename' )			, $flags );	
	$wgParser->setFunctionHook( 'pagenamee'			, array( &$wgExtPathFunctions, 'pagenamee' )		, $flags );	
	$wgParser->setFunctionHook( 'fullpagename'		, array( &$wgExtPathFunctions, 'fullpagename' )		, $flags );	
	$wgParser->setFunctionHook( 'fullpagenamee'		, array( &$wgExtPathFunctions, 'fullpagenamee' )	, $flags );	
	$wgParser->setFunctionHook( 'subpagename'		, array( &$wgExtPathFunctions, 'subpagename' )		, $flags );	
	$wgParser->setFunctionHook( 'subpagenamee'		, array( &$wgExtPathFunctions, 'subpagenamee' )		, $flags );	
	$wgParser->setFunctionHook( 'basepagename'		, array( &$wgExtPathFunctions, 'basepagename' )		, $flags );	
	$wgParser->setFunctionHook( 'basepagenamee'		, array( &$wgExtPathFunctions, 'basepagenamee' )	, $flags );	
	$wgParser->setFunctionHook( 'talkpagename'		, array( &$wgExtPathFunctions, 'talkpagename' )		, $flags );	
	$wgParser->setFunctionHook( 'talkpagenamee'		, array( &$wgExtPathFunctions, 'talkpagenamee' )	, $flags );	
	$wgParser->setFunctionHook( 'subjectpagename'	, array( &$wgExtPathFunctions, 'subjectpagename' )	, $flags );	
	$wgParser->setFunctionHook( 'subjectpagenamee'	, array( &$wgExtPathFunctions, 'subjectpagenamee' )	, $flags );	
	$wgParser->setFunctionHook( 'namespace'			, array( &$wgExtPathFunctions, 'namespace' )		, $flags );	
	$wgParser->setFunctionHook( 'namespacee'		, array( &$wgExtPathFunctions, 'namespacee' )		, $flags );	
	$wgParser->setFunctionHook( 'talkspace'			, array( &$wgExtPathFunctions, 'talkspace' )		, $flags );	
	$wgParser->setFunctionHook( 'talkspacee'		, array( &$wgExtPathFunctions, 'talkspacee' )		, $flags );	
	$wgParser->setFunctionHook( 'subjectspace'		, array( &$wgExtPathFunctions, 'subjectspace' )		, $flags );	
	$wgParser->setFunctionHook( 'subjectspacee'		, array( &$wgExtPathFunctions, 'subjectspacee' )	, $flags );	

	foreach( $wgPathFunctionsMessages as $key => $value ) {
		$wgMessageCache->addMessages( $value, $key );
	}
}

function wfPathFunctionsLanguageGetMagic( &$magicWords, $langCode ) {
	global $wgPathFunctionsMagic;
	
	if( !in_array( $langCode , $wgPathFunctionsMagic ) ) {
		$langCode = 'en';
	}

	$magicWords = array_merge( $magicWords , $wgPathFunctionsMagic[$langCode] );
	return true;
}
?>


PathFunctions.i18n.php

<?php

if ( !defined( 'MEDIAWIKI' ) ) {
	die( 'This file is a MediaWiki extension, it is not a valid entry point' );
}

$wgPathFunctionsMessages = array();
$wgPathFunctionsMagic = array();

$wgPathFunctionsMessages['en'] = array(
	'pathfunc_no_talk' => "No talk pages in namespace $1",
);

$wgPathFunctionsMagic['en'] = array(
	'pagename' => array( 0, 'pagename' ),
	'pagenamee' => array( 0, 'pagenamee' ),
	'fullpagename' => array( 0, 'fullpagename' ),
	'fullpagenamee' => array( 0, 'fullpagenamee' ),
	'subpagename' => array( 0, 'subpagename' ),
	'subpagenamee' => array( 0, 'subpagenamee' ),
	'basepagename' => array( 0, 'basepagename' ),
	'basepagenamee' => array( 0, 'basepagenamee' ),
	'talkpagename' => array( 0, 'talkpagename' ),
	'talkpagenamee' => array( 0, 'talkpagenamee' ),
	'subjectpagename' => array( 0, 'subjectpagename' ),
	'subjectpagenamee' => array( 0, 'subjectpagenamee' ),
	'namespace' => array( 0, 'namespace' ),
	'namespacee' => array( 0, 'namespacee' ),
	'talkspace' => array( 0, 'talkspace' ),
	'talkspacee' => array( 0, 'talkspacee' ),
	'subjectspace' => array( 0, 'subjectspace' ),
	'subjectspacee' => array( 0, 'subjectspacee' ),
);
?>

See also