User:Brion.finlay/PathFunctions

From MediaWiki.org

Jump to: navigation, search
Manual on MediaWiki Extensions
List of MediaWiki Extensions
PathFunctions

Release status: stable

Implementation Parser function
Description Provides custom variables that allow specifying the page to use when evaluating page specific MediaWiki variables for page names, namespaces, and latest revisions (for example, {{PAGENAME}}), {{NAMESPACE}}, and {{REVISIONID}}).
Author(s) Carl Fürstenberg and Brion Finlay
Version 1.0.0 (2007-11-03)
MediaWiki 1.7+
Download N/A

This extension provides custom variables that allow specifying the page to use when evaluating page specific MediaWiki variables for page names, namespaces, and latest revisions (for example, {{PAGENAME}}), {{NAMESPACE}}, and {{REVISIONID}}.

Normally, for the rendering on page A, these variables depend on page A, even if page A contains the variable due to inclusion of page B. This extension allows you to evaluate the variables for page B or any other page and display the results on page A.


Contents

[edit] Example

  • {{ #PAGENAME: Help:Foo }} gives Foo
  • {{ #NAMESPACE: Help:Foo }} gives Help
  • {{ #REVISIONID: Help:Foo }} gives the latest revision id for Help:Foo

[edit] Uses

  • When including one article into another, this allows you to display information about the included page.
  • When including article B into article A, this allows you to display on page A the variables for the included page B.
  • For the rendering on page A, these variables depend on page A, even if page A contains the variable due to inclusion of page B.
  • When building templates, this allows you to extract portions of an article name passed in as a parameter.

[edit] Variables

[edit] Statistics and technical details

[edit] Latest revision to a page

The following variables return data about the latest edit to the current page, even if viewing an older version of the page.

Variable Output Description
{{#REVISIONID:<article path>}} 187831 Unique ID
{{#REVISIONDAY:<article path>}} 18 Day edit was made (unpadded number)
{{#REVISIONDAY2:<article path>}} 18 Day edit was made (zero-padded number)
{{#REVISIONMONTH:<article path>}} 5 Month edit was made (unpadded number)
{{#REVISIONYEAR:<article path>}} 2008 Year edit was made
{{#REVISIONTIMESTAMP:<article path>}} 20080518224918 Timestamp as of time of edit

[edit] Page names

Variable Output Description
{{#FULLPAGENAME:<article path>}} User:Brion.finlay/PathFunctions Namespace and page title
{{#BASEPAGENAME:<article path>}} Brion.finlay The namespace and page title excluding the current subpage ("Title" on "Title/foo")
{{#PAGENAME:<article path>}} Brion.finlay/PathFunctions Page title
{{#SUBPAGENAME:<article path>}} PathFunctions The subpage title ("foo" on "Title/foo")
{{#SUBJECTPAGENAME:<article path>}} User:Brion.finlay/PathFunctions The title of the associated content page.
{{#TALKPAGENAME:<article path>}} User talk:Brion.finlay/PathFunctions The title of the associated talk page.

The following are URL-encoded equivalents:

  • {{#FULLPAGENAMEE:(<article path>}}
  • {{#PAGENAMEE:<article path>}}
  • {{#BASEPAGENAMEE:<article path>}}
  • {{#SUBPAGENAMEE:<article path>}}
  • {{#SUBJECTPAGENAMEE:<article path>}}
  • {{#TALKPAGENAMEE:<article path>}}

[edit] Namespaces

Variable Output Description
{{#NAMESPACE:<article path>}} User Namespace (name)
{{#SUBJECTSPACE:<article path>}} User Name of the associated content namespace
{{#TALKSPACE:<article path>}} User talk Name of the associated talk namespace

The following are URL-encoded equivalents:

  • {{#NAMESPACEE:<article path>}}
  • {{#SUBJECTSPACEE:<article path>}}
  • {{#TALKSPACEE:<article path>}}

[edit] Configuration

Setting the variable $wgPathFunctionsUseNoPrefixHash to true allows the variables to be used without the preceeding hash symbol, and so appear more like the built in variables. For example {{ PAGENAME: Help:Foo }}.

[edit] 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:

[edit] PathFunctions.php

<?php
 
if ( !defined( 'MEDIAWIKI' ) ) {
        die( 'This file is a MediaWiki extension, it is not a valid entry point' );
}
 
/**
 * This extension provides custom variables that allow specifying the page to use when evaluating page specific MediaWiki variables for page names, 
 * namespaces, and latest revisions (for example, {{PAGENAME}}), {{NAMESPACE}}, and {{REVISIONID}}.
 * Setting the variable $wgPathFunctionsUseNoPrefixHash to true allows the variables to be used without the preceeding hash symbol, and so appear 
 * more like the built in variables. For example {{ PAGENAME: Help:Foo }}.
 *
 * @package MediaWiki
 * @subpackage Extensions
 *
 * @link http://www.mediawiki.org/wiki/Extension:PathFunctions Documentation
 *
 * @author Carl Fürstenberg (AzaToth) <azatoth@gmail.com>
 * @author Brion Finlay
 * @copyright Copyright © 2006 Carl Fürstenberg
 * @copyright Copyright © 2007 Brion Finlay
 * @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' => '1.0.0',
        'description' => 'Provides custom variables that allow specifying the page to use when evaluating page specific MediaWiki variables for page names, namespaces, and latest revisions (for example, <nowiki>{{PAGENAME}})</nowiki>, <nowiki>{{NAMESPACE}}</nowiki>, and <nowiki>{{REVISIONID}}</nowiki>).',
        'name' => 'PathFunctions',
        'url' => 'http://www.mediawiki.org/wiki/Extension:PathFunctions',
        # With apologies to Mr. Fürstenberg, the "ü" seems to disrupt the Special:Version page from displaying in MW 1.9 and 1.10
        'author' => array('Carl Furstenberg (AzaToth)', 'Brion Finlay')
);
 
$wgHooks['LanguageGetMagic'][]       = 'wfPathFunctionsLanguageGetMagic';
 
class ExtPathFunctions {
 
        public static function pagename( &$parser , $path = false ) {
                $path = trim($path);
                $title = $path ? Title::newFromText( $path ) : $parser->mTitle;
                return $title->getText();
        }
 
        public static function pagenamee( &$parser , $path = false ) {
                $path = trim($path);
                $title = $path ? Title::newFromText( $path ) : $parser->mTitle;
                return $title->getPartialUrl();
        }
 
        public static function fullpagename( &$parser , $path = false ) {
                $path = trim($path);
                $title = $path ? Title::newFromText( $path ) : $parser->mTitle;
                return $title->getPrefixedText();
        }
 
        public static function fullpagenamee( &$parser , $path = false ) {
                $path = trim($path);
                $title = $path ? Title::newFromText( $path ) : $parser->mTitle;
                return $title->getPrefixedURL();
        }
 
        public static function subpagename( &$parser , $path = false ) {
                $path = trim($path);
                $title = $path ? Title::newFromText( $path ) : $parser->mTitle;
                return $title->getSubpageText();
        }
 
        public static function subpagenamee( &$parser , $path = false ) {
                $path = trim($path);
                $title = $path ? Title::newFromText( $path ) : $parser->mTitle;
                return $title->getSubpageUrlForm();
        }
 
        public static function basepagename( &$parser , $path = false ) {
                $path = trim($path);
                $title = $path ? Title::newFromText( $path ) : $parser->mTitle;
                return $title->getBaseText();
        }
 
        public static function basepagenamee( &$parser , $path = false ) {
                $path = trim($path);
                $title = $path ? Title::newFromText( $path ) : $parser->mTitle;
                return $title->getBaseText();
        }
 
        public static 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 static 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 static function subjectpagename( &$parser , $path = false ) {
                $path = trim($path);
                $title = $path ? Title::newFromText( $path ) : $parser->mTitle;
                return $title->getSubjectPage()->getPrefixedText();
        }
 
        public static function subjectpagenamee( &$parser , $path = false ) {
                $path = trim($path);
                $title = $path ? Title::newFromText( $path ) : $parser->mTitle;
                return $title->getSubjectPage()->getPrefixedUrl();
        }
 
        public static function namespace( &$parser , $path = false ) {
                $path = trim($path);
                $title = $path ? Title::newFromText( $path ) : $parser->mTitle;
                return strtr( $title->getNsText() , '_' , ' ' );
        }
 
        public static function namespacee( &$parser , $path = false ) {
                $path = trim($path);
                $title = $path ? Title::newFromText( $path ) : $parser->mTitle;
                return wfUrlencode( $title->getNsText() );
        }
 
        public static 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 static 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 static function subjectspace( &$parser , $path = false ) {
                $path = trim($path);
                $title = $path ? Title::newFromText( $path ) : $parser->mTitle;
                return strtr( $title->getSubjectNsText() , '_' , ' ' );
        }
 
        public static function subjectspacee( &$parser , $path = false ) {
                $path = trim($path);
                $title = $path ? Title::newFromText( $path ) : $parser->mTitle;
                return wfUrlencode( $title->getSubjectNsText() );
        }
 
        public static function revisionid( &$parser , $path = false ) {
                $path = trim($path);
                $title = $path ? Title::newFromText( $path ) : $parser->mTitle;
                return $title ? $title->getLatestRevID() : ' ';
        }
 
        public static function revisiontimestamp( &$parser , $path = false ) {
                $path = trim($path);
                $title = $path ? Title::newFromText( $path ) : $parser->mTitle;
                $revision = $title ? Revision::newFromTitle($title, 0) : null;
                return $revision ? $revision->getTimestamp() : ' ';
        }
 
        public static function revisionday( &$parser , $path = false ) {
                $path = trim($path);
                $title = $path ? Title::newFromText( $path ) : $parser->mTitle;
                $revision = $title ? Revision::newFromTitle($title, 0) : null;
                return $revision ? intval( substr( $revision->getTimestamp(), 6, 2 ) ) : ' ';
        }
 
        public static function revisionday2( &$parser , $path = false ) {
                $path = trim($path);
                $title = $path ? Title::newFromText( $path ) : $parser->mTitle;
                $revision = $title ? Revision::newFromTitle($title, 0) : null;
                return $revision ? substr( $revision->getTimestamp(), 6, 2 ) : ' ';
        }
 
        public static function revisionmonth( &$parser , $path = false ) {
                $path = trim($path);
                $title = $path ? Title::newFromText( $path ) : $parser->mTitle;
                $revision = $title ? Revision::newFromTitle($title, 0) : null;
                return $revision ? intval( substr( $revision->getTimestamp(), 4, 2 ) ) : ' ';
        }
 
        public static function revisionyear( &$parser , $path = false ) {
                $path = trim($path);
                $title = $path ? Title::newFromText( $path ) : $parser->mTitle;
                $revision = $title ? Revision::newFromTitle($title, 0) : null;
                return $revision ? substr( $revision->getTimestamp(), 0, 4 ) : ' ';
        }
 
}
 
function wfSetupPathFunctions() {
        global $wgParser, $wgMessageCache, $wgPathFunctionsMessages, $wgPathFunctionsUseNoPrefixHash;
        $flags = $wgPathFunctionsUseNoPrefixHash ? SFH_NO_HASH : 0;
 
        $wgParser->setFunctionHook( 'pagename', array( 'ExtPathFunctions', 'pagename' ), $flags );
        $wgParser->setFunctionHook( 'pagenamee', array( 'ExtPathFunctions', 'pagenamee' ), $flags );
        $wgParser->setFunctionHook( 'fullpagename', array( 'ExtPathFunctions', 'fullpagename' ), $flags );
        $wgParser->setFunctionHook( 'fullpagenamee', array( 'ExtPathFunctions', 'fullpagenamee' ), $flags );
        $wgParser->setFunctionHook( 'subpagename', array( 'ExtPathFunctions', 'subpagename' ), $flags );
        $wgParser->setFunctionHook( 'subpagenamee', array( 'ExtPathFunctions', 'subpagenamee' ), $flags );
        $wgParser->setFunctionHook( 'basepagename', array( 'ExtPathFunctions', 'basepagename' ), $flags );
        $wgParser->setFunctionHook( 'basepagenamee', array( 'ExtPathFunctions', 'basepagenamee' ), $flags );
        $wgParser->setFunctionHook( 'talkpagename', array( 'ExtPathFunctions', 'talkpagename' ), $flags );
        $wgParser->setFunctionHook( 'talkpagenamee', array( 'ExtPathFunctions', 'talkpagenamee' ), $flags );
        $wgParser->setFunctionHook( 'subjectpagename', array( 'ExtPathFunctions', 'subjectpagename' ), $flags );
        $wgParser->setFunctionHook( 'subjectpagenamee', array( 'ExtPathFunctions', 'subjectpagenamee' ), $flags );
        $wgParser->setFunctionHook( 'namespace', array( 'ExtPathFunctions', 'namespace' ), $flags );
        $wgParser->setFunctionHook( 'namespacee', array( 'ExtPathFunctions', 'namespacee' ), $flags );
        $wgParser->setFunctionHook( 'talkspace', array( 'ExtPathFunctions', 'talkspace' ), $flags );
        $wgParser->setFunctionHook( 'talkspacee', array( 'ExtPathFunctions', 'talkspacee' ), $flags );
        $wgParser->setFunctionHook( 'subjectspace', array( 'ExtPathFunctions', 'subjectspace' ), $flags );
        $wgParser->setFunctionHook( 'subjectspacee', array( 'ExtPathFunctions', 'subjectspacee' ), $flags );
        $wgParser->setFunctionHook( 'revisionid', array( 'ExtPathFunctions', 'revisionid' ), $flags );
        $wgParser->setFunctionHook( 'revisiontimestamp', array( 'ExtPathFunctions', 'revisiontimestamp' ), $flags );
        $wgParser->setFunctionHook( 'revisionday', array( 'ExtPathFunctions', 'revisionday' ), $flags );
        $wgParser->setFunctionHook( 'revisionday2', array( 'ExtPathFunctions', 'revisionday2' ), $flags );
        $wgParser->setFunctionHook( 'revisionmonth', array( 'ExtPathFunctions', 'revisionmonth' ), $flags );
        $wgParser->setFunctionHook( 'revisionyear', array( 'ExtPathFunctions', 'revisionyear' ), $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;
}
?>

[edit] 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' ),
        'revisionid' => array( 0, 'revisionid' ),
        'revisiontimestamp' => array( 0, 'revisiontimestamp' ),
        'revisionday' => array( 0, 'revisionday' ),
        'revisionday2' => array( 0, 'revisionday2' ),
        'revisionmonth' => array( 0, 'revisionmonth' ),
        'revisionyear' => array( 0, 'revisionyear' )
);
?>

[edit] See also

Personal tools