Extension:WikiUserInfo

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

Release status: beta

Implementation Parser function
Description Allows to get info and options of user specified by name.
Author(s) MichaelDubnerTalk
Last version 0.5 (2010-12-17)
MediaWiki 1.6.x and above
License No license specified
Download See below
Parameters

$wgWikiUserInfoSafeOptions

Added rights

showuseroption,showuseremail

Hooks used
LanguageGetMagic

Check usage (experimental)

Contents

[edit] What does this extension do?

This 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.

[edit] Download instructions

Please cut and paste the code found below and place it in $IP/extensions/WikiUserInfo/WikiUserInfo.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/WikiUserInfo/WikiUserInfo.php");
#To add new option to silently allowed (without 'showuseroption' right), use this:
$wgWikiUserInfoSafeOptions[]='math';
#To clear all allowed options, uncomment this:
#$wgWikiUserInfoSafeOptions=array();
#Setup user rights:
#$wgGroupPermissions['*']['showuseremail'] = true;
#$wgGroupPermissions['*']['showuseroption'] = true;

[edit] Code

<?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 extension",
"author" => "Michael P. Dubner",
"version" => 0.4,
"url" => "http://www.mediawiki.org/wiki/Extension:WikiUserInfo",
"description" => "WikiUserInfo extension"
);
 
$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"));
        }
 
        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();
        }
}

[edit] Documentation

[edit] Rationale

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

[edit] Declared functions

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.

[edit] Dangerous functions

{{#email:username}}
Displays 'E-mail:' 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'.


[edit] See also

Personal tools
Namespaces

Variants
Actions
Navigation
Support
Download
Development
Communication
Print/export
Toolbox