Extension:GetUserName

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
GetUserName

Release status: beta

Implementation Variable
Description Allows current user's name to be put in a document
Last version 1.0
License GPL
Download No link
Hooks used
ParserFirstCallInit

LanguageGetMagic

Check usage (experimental)

Contents

[edit] What can this extension do?

This extension can obtain the current user's name from wgUser and insert it on a page.

Note: This extension also serves as an example of how to add a variable easily to the system.

[edit] Usage

When {{#USERNAME:}} is found in the page, it will replace it with the current user name. I wrote this because I wanted to make a link where the users could create draft documents easily, with unique names, in their user namespace. I added this to my page:

Create a document "[[User:{{#USERNAME:}}/Draft-{{CURRENTTIMESTAMP}}]]"

Note that this extension isn't suitable for large wikis or long pages, because the cache is invalidated for pages using this extension (see Extension_talk:GetUserName page).

[edit] Download instructions

Please cut and paste the code found below and place it in $IP/extensions/GetUserName/GetUserName.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

[edit] Installation

To install this extension, add the following to LocalSettings.php:

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

[edit] Configuration parameters

None needed

[edit] User rights

None needed

[edit] Code

<?php
 
$wgHooks[ 'ParserFirstCallInit' ][] = "ExtGetUserName::setup";
$wgHooks[ 'LanguageGetMagic' ][]  = 'ExtGetUserName::languageGetMagic';
class ExtGetUserName {
    private static $parserFunctions = array(
        'USERNAME' => 'getUserName',
    );
 
    public static function setup( &$parser ) {
        // register each hook
        foreach( self::$parserFunctions as $hook => $function )
            $parser->setFunctionHook( $hook,
                array( __CLASS__, $function ), SFH_OBJECT_ARGS );
 
        return true;
    }
 
    public static function languageGetMagic( &$magicWords, $langCode ) {
        $magicWords[ 'USERNAME' ] = array( 0, 'USERNAME' ); 
        return true;
    }
 
    public static function getUserName( &$parser, $frame, $args ) {
        $parser->disableCache();
        global $wgUser;
        return trim( $wgUser->getName() );
    }
}
Personal tools
Namespaces

Variants
Actions
Navigation
Support
Download
Development
Communication
Print/export
Toolbox