Extension:UserMagic

From MediaWiki.org

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

Release status: unknown

Implementation Extended syntax, MyWiki
Description Defines several username-producing magic words.
Author(s) Algorithm
Version 1.0
MediaWiki 1.7+
Download see below
Example http://wikible.org/en/
Hooks used

OutputPageBeforeHTML

UserMagic allows editors to place the name of the current user inside the content of an article, without affecting the page cache. This is done by replacing magic words inside the fully-parsed HTML after the page has been rendered and cached.

Currently defined magic words:

  • __USERNAME__ -> Name of the current user
  • __USERPAGE__ -> Link to the page of the current user
  • __USERIP__ -> IP address of the current user

[edit] Caveats

Since the magic words are not replaced until after the HTML is rendered, they cannot be used as the target of links or as template arguments. However, they can still be used as the alternate text of links, e.g. [[Special:Mypage|__USERNAME__]]

Additionally, since <nowiki> tags will have already been parsed out, these magic words cannot be invalidated by <nowiki>. In order to explicitly print one of these words, &#95; must be used in place of an underscore.

Lastly, these magic words will not be replaced in Preview mode.

[edit] Code

This code has been tested in MediaWiki 1.7.

<?php
/**
 * UserMagic
 * Defines several username-producing magic words.
 * 
 * @author Algorithm [http://meta.wikimedia.org/wiki/User:Algorithm]
 * @version 1.0 (8/17/06)
 * @copyright © 2006 Algorithm
 */
 
$wgHooks['OutputPageBeforeHTML'][] = 'wfUserMagic';
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'UserMagic',
        'author' => 'Algorithm',
        'description' => 'Defines several username-producing magic words.',
        'url' => 'http://www.mediawiki.org/wiki/Extension:UserMagic',
        'version' => '1.0'
);
 
function wfUserMagic($parserOutput, $text) {
    global $wgUser;
    $text = str_replace(
         array("__USERNAME__","__USERPAGE__","__USERIP__"),
         array($wgUser->getName(),
               $wgUser->getSkin()->makeLinkObj(
                   $wgUser->getUserPage(), $wgUser->getName()),
               wfGetIP()),
         $text);
}
Personal tools