Extension:UserMagic

MediaWiki extensions manual
UserMagic
Release status: unmaintained
Implementation Extended syntax , MyWiki
Description Defines several username-producing magic words
Author(s) Ross McClure (Algorithmtalk)
Latest version 2.1.0 (2017-08-11)
MediaWiki 1.19+
PHP 5.3+
Database changes No
License GNU General Public License 2.0 or later
Download see below

The UserMagic extension 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

Caveats edit

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.

Installation edit

Code edit

UserMagic.php
<?php
/**
 * UserMagic
 * Defines several username-producing magic words.
 *
 * @link https://www.mediawiki.org/wiki/Extension:UserMagic Documentation
 * @version 2.1.0 (2017-08-11)
 *
 * @ingroup Extensions
 * @package MediaWiki
 *
 * @author Ross McClure (Algorithm)
 * @author Sébastien Beyou (Seb35)
 * @author Karsten Hoffmeyer (Kghbln)
 * @copyright (C) 2006 Ross McClure (Algorithm)
 * @license https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License 2.0 or later
 */

// Ensure that the script cannot be executed outside of MediaWiki.
if ( !defined( 'MEDIAWIKI' ) ) {
	die( 'This is an extension to MediaWiki and cannot be run standalone.' );
}

// Display extension properties on MediaWiki.
$wgExtensionCredits['parserhook'][] = array(
	'path' => __FILE__,
	'name' => 'UserMagic',
	'author' =>  array(
		'Ross McClure',
		'...'
	),
	'description' => 'Defines several username-producing magic words',
	'url' => 'https://www.mediawiki.org/wiki/Extension:UserMagic',
	'version' => '2.1.0',
	'license-name' => 'GPL-2.0-or-later'
);

// Register extension hooks.
$wgHooks['OutputPageBeforeHTML'][] = 'wfUserMagic';

// Do the extension's action.
function wfUserMagic( $parserOutput, &$text ) {

	global $wgUser, $wgRequest;
	$text = str_replace(
			array(
				'__USERNAME__',
				'__USERPAGE__',
				'__USERIP__'
			),
			array(
				$wgUser->getName(),
				Linker::link( $wgUser->getUserPage(), $wgUser->getName() ),
				$wgRequest->getIP()
			),
			$text

		);

	return true;
}

See also edit