Extension:UserMagic

From Linux Web Expert

MediaWiki extensions manual
UserMagic
Release status: unmaintained
Implementation Extended syntax , MyWiki
Description Defines several username-producing magic words
Author(s)
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
Quarterly downloads Lua error in Module:Extension at line 172: bad argument #1 to 'inNamespace' (unrecognized namespace name 'skin').
Public wikis using Lua error in Module:Extension at line 172: bad argument #1 to 'inNamespace' (unrecognized namespace name 'skin').

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

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

  • <translate> <tvar name=1>Copy the code into a file called "UserMagic.php"</tvar> and place the file(s) in a directory called <tvar name=name>UserMagic</tvar> in your <tvar name=ext>extensions/</tvar> folder.</translate>
  • <translate> Add the following code at the bottom of your <tvar name=1>LocalSettings.php </tvar> file:</translate>
    require_once "$IP/extensions/UserMagic/UserMagic.php";
    
  • File:OOjs UI icon check-constructive.svg <translate> Done</translate> – <translate> Navigate to <tvar name=special>Special:Version</tvar> on your wiki to verify that the extension is successfully installed.</translate>

Code

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