Open main menu

Extension:GetUserName

This page is a translated version of the page Extension:GetUserName and the translation is 47% complete.

Other languages:
Deutsch • ‎English • ‎Nederlands • ‎dansk • ‎español • ‎magyar • ‎polski • ‎sicilianu • ‎中文 • ‎日本語
MediaWiki 拡張機能マニュアル
OOjs UI icon advanced.svg
GetUserName
リリースの状態: 安定
実装 Variable
説明 Allows the current user's name to be put in a page
作者 (Ejcaputotalk)
最新バージョン 1.0 (2010-07-10)
ライセンス GNU General Public License 2.0
ダウンロード コードの節を参照してください
使用するフック
ParserFirstCallInit
LanguageGetMagic
translatewiki.net で翻訳を利用できる場合は、GetUserName 拡張機能の翻訳にご協力ください
使用状況とバージョン マトリクスを確認してください。
This extension IS maintained, it works even with the latest versions of MediaWiki (tested with 1.31). I find it to be a useful function, albeit extremely simple and not useful for large wikis, but if I'm the only one using it, or there are other extensions which do the same thing but better (e.g. MyVariables), I suppose that it makes sense to delete it.

The GetUserName extension allows to obtain the current user's name from wgUser and insert it on a page.

Contents

使用法

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. 私は以下を自分のページに追加しました:

Create a document "[[User:{{#USERNAME:}}/Draft-{{CURRENTTIMESTAMP}}]]"
This extension is not suitable for large wikis or long pages, because the cache is invalidated for pages using this extension (see Extension_talk:GetUserName page).

インストール

  • 以下のコードを LocalSettings.php の末尾に追加します:
    require_once "$IP/extensions/GetUserName/GetUserName.php";
    
  •   完了 – ウィキの「Special:Version」に移動して、拡張機能が正しくインストールされたことを確認します。

コード

GetUserName.php
<?php
if ( !defined( 'MEDIAWIKI' ) ) {
    die( 'This file is a MediaWiki extension and not a valid entry point' );
}
 
$wgExtensionCredits['parserhook'][] = array(
    'path' => __FILE__,
    'name' => 'GetUserName',
    'version' => '1.0',
    'url' => 'https://www.mediawiki.org/wiki/Extension:GetUserName',
    'author' => 'Ejcaputo',
    'description' => "Allows the current user's name to be put in a page"
);  

$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() );
    }
}


関連項目