Open main menu

Extension:UserMagic

MediaWiki extensions manual
Crystal Clear action run.svg
UserMagic
Release status: beta
Implementation Extended syntax, MyWiki
Description Defines several username-producing magic words
Author(s) Ross McClure (Algorithm~mediawikiwikitalk)
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
Hooks used
OutputPageBeforeHTML
Translate the UserMagic extension if it is available at translatewiki.net
Check usage and version matrix.

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

Contents

CaveatsEdit

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.

InstallationEdit

  • Add the following code at the bottom of your LocalSettings.php:
    require_once "$IP/extensions/UserMagic/UserMagic.php";
    
  •   Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

CodeEdit

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+'
);

// 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 alsoEdit