Open main menu

Extension:WikiUserInfo

MediaWiki extensions manual
OOjs UI icon advanced.svg
WikiUserInfo
Release status: unmaintained
Implementation Parser function
Description Allows to get info and options of user specified by name.
Author(s) Michael Dubner (MichaelDubnertalk)
Latest version 0.5.1 (2015-06-02)
MediaWiki 1.6.x and later
Database changes No
License No license specified
Download See below
  • $wgWikiUserInfoSafeOptions
  • showuseroption
  • showuseremail
Translate the WikiUserInfo extension if it is available at translatewiki.net
Check usage and version matrix.

The WikiUserInfo extension adds parser functions that allows to show info and options of wiki user specified by name.

This extension was created with following usages in mind:

  • Template for local wiki user that shows all necessary info about - user name, real name, email, link to entry in corporate address book.
  • Template for category of pages supported by some user.
  • Template that allows to link to user page, but adds user contribution (number of edits) in superscript.

InstallationEdit

  • Add the following code at the bottom of your LocalSettings.php:
    require_once "$IP/extensions/WikiUserInfo/WikiUserInfo.php";
    // To add new option to silently allowed (without 'showuseroption' right), use e.g. this:
    $wgWikiUserInfoSafeOptions[] = 'math';
    // To clear all allowed options, uncomment this:
    # $wgWikiUserInfoSafeOptions=array();
    // Setup user rights:
    # $wgGroupPermissions['*']['showuseremail'] = true;
    # $wgGroupPermissions['*']['showuseroption'] = true;
    
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

CodeEdit

WikiUserInfo.php
<?php
# Not a valid entry point, skip unless MEDIAWIKI is defined
if ( !defined( 'MEDIAWIKI' ) ) {
	echo <<<HEREDOC
To install this extension, put the following line in LocalSettings.php:
require_once( "\$IP/extensions/WikiUserInfo/WikiUserInfo.php" );

HEREDOC;
	exit( 1 );
}

$wgExtensionCredits['other'][] = array(
	"name" => "WikiUserInfo",
	"author" => "Michael P. Dubner",
	"version" => "0.5.1",
	"url" => "https://www.mediawiki.org/wiki/Extension:WikiUserInfo",
	"description" => "Allows to get info and options of user specified by name"
);

$wgExtensionFunctions[] = "wfWikiUserInfoExtension";
$wgHooks['LanguageGetMagic'][] = array( "wfWikiUserInfoMagicWords" );
$wgWikiUserInfoSafeOptions = array( 'date', 'gender', 'language', 'nickname', 'skin', 'timecorrection' );
$wgAvailableRights[] = 'showuseroption';
if ( !array_key_exists( 'showuseroption', $wgGroupPermissions['*'] ) ) {
	$wgGroupPermissions['*']['showuseroption'] = false;
	$wgGroupPermissions['sysop']['showuseroption'] = true;
	$wgGroupPermissions['bureaucrat']['showuseroption'] = true;
}
$wgAvailableRights[] = 'showuseremail';
if ( !array_key_exists( 'showuseremail', $wgGroupPermissions['*'] ) ) {
	$wgGroupPermissions['*']['showuseremail'] = false;
	$wgGroupPermissions['sysop']['showuseremail'] = true;
	$wgGroupPermissions['bureaucrat']['showuseremail'] = true;
}

function wfWikiUserInfoExtension() {
	global $wgParser;
	WikiUserInfo_MediaWiki::registerHooks( $wgParser );
}

function wfWikiUserInfoMagicWords( &$magicWords, $langCode ) {
	return WikiUserInfo_MediaWiki::addMagicWord( $magicWords, $langCode );
}

class WikiUserInfo_MediaWiki {

	static function registerHooks( $parser ) {
		$parser->setFunctionHook( 'realname', array( __CLASS__, "realname" ) );
		$parser->setFunctionHook( 'email', array( __CLASS__, "email" ) );
		$parser->setFunctionHook( 'nickname', array( __CLASS__, "nickname" ) );
		$parser->setFunctionHook( 'useroption', array( __CLASS__, "useroption" ) );
		$parser->setFunctionHook( 'userregistration', array( __CLASS__, "userregistration" ) );
		$parser->setFunctionHook( 'usergroups', array( __CLASS__, "usergroups" ) );
		$parser->setFunctionHook( 'useredits', array( __CLASS__, "useredits" ) );
	}

	static function addMagicWord( &$magicWords, $langCode ) {
		$magicWords['realname'] = array( 0, 'realname' );
		$magicWords['email'] = array( 0, 'email' );
		$magicWords['nickname'] = array( 0, 'nickname' );
		$magicWords['useroption'] = array( 0, 'useroption' );
		$magicWords['userregistration'] = array( 0, 'userregistration' );
		$magicWords['usergroups'] = array( 0, 'usergroups' );
		$magicWords['useredits'] = array( 0, 'useredits' );
		return true;
	}

	static function getUser( $parser, $user ) {
		$title = Title::newFromText( $user );
		if ( is_object( $title ) && $title->getNamespace() == NS_USER ) $user = $title->getText();
		$user = User::newFromName( $user );
		if ( !$user ) {
			global $wgUser;
			$user = $wgUser;
		}
		return $user;
	}

	static function realname( $parser, $user ) {
		$user = WikiUserInfo_MediaWiki::getUser( $parser, $user );
		if ( !$user->getRealName() ) return $user->getName();
		return $user->getRealName();
	}

	static function email( $parser, $user ) {
		global $wgUser, $wgOut;
		if ( !$wgUser->isAllowed( 'showuseremail' ) && !$wgUser->isAllowed( 'lookupuser' ) ) {
			$wgOut->permissionRequired( 'showuseremail' );
			return;
		}
		$user = WikiUserInfo_MediaWiki::getUser( $parser, $user );
		return $user->getEmail();
	}

	static function nickname( $parser, $user ) {
		return WikiUserInfo_MediaWiki::useroption( $parser, $user, 'nickname' );
	}

	static function useroption( $parser, $user, $option ) {
		global $wgUser, $wgOut;
		if ( !in_array( $option, $wgWikiUserInfoSafeOptions ) && !$wgUser->isAllowed( 'showuseroption' ) &&
			!$wgUser->isAllowed( 'lookupuser' ) ) {
			$wgOut->permissionRequired( 'showuseroption' );
			return;
		}
		$user = WikiUserInfo_MediaWiki::getUser( $parser, $user );
		return $user->getOption( $option );
	}

	static function userregistration( $parser, $user ) {
		global $wgUser, $wgOut;
		if ( !$wgUser->isAllowed( 'showuseroption' ) && !$wgUser->isAllowed( 'lookupuser' ) ) {
			$wgOut->permissionRequired( 'showuseroption' );
			return;
		}
		$user = WikiUserInfo_MediaWiki::getUser( $parser, $user );
		return $user->getRegistration();
	}

	static function usergroups( $parser, $user ) {
		$user = WikiUserInfo_MediaWiki::getUser( $parser, $user );
		return join( ',', $user->getGroups() );
	}

	static function useredits( $parser, $user ) {
		$user = WikiUserInfo_MediaWiki::getUser( $parser, $user );
		return $user->getEditCount();
	}
}

DocumentationEdit

RationaleEdit

There are standard function {{gender:..}}) that allow access to one of user options. This extension adds functions to access another options for completeness.

Declared functionsEdit

All of these functions get's user name as their first argument.

{{#realname:username}}
Displays 'Real name:' field from user properties.
{{#nickname:username}}
Displays 'New signature:' field from user properties.
{{#userregistration:username}}
Displays 'Registration time:' field from user properties.
{{#useredits:username}}
Displays 'Number of edits:' field from user properties.
{{#usergroups:username}}
Displays 'Member of:' field from user properties.

Dangerous functionsEdit

{{#email:username}}
Displays 'Email:' field from user properties.
This may be dangerous, so this function is protected by special &'showuseremail' right.
{{#useroption:username|option}}
Displays selected field from user options.
This may be dangerous, so this function is protected by special &'showuseroption' right, excluding options mentioned in $wgWikiUserInfoSafeOptions array, which defaults to 'date', 'gender', 'language', 'nickname', 'skin' and 'timecorrection'.

See alsoEdit

English  • 日本語