Difference between revisions of "Extension:ExtraMagic"

From Organic Design wiki
(add CURRENTUSERREALNAME update to version 2.0.3)
m (#ifuses)
 
(12 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<?php
+
{{svn|extensions|/MediaWiki/ExtraMagic/ExtraMagic.php}}<br />
/**
+
This extensions adds a number of useful [[MW:Help:Magic words|magic words]] which we've needed for our own projects over the years. Most of them are available already in various different extensions, but we preferred to have them all packaged together in one place.
* ExtraMagic extension - Adds useful variables and parser functions
 
*
 
* See http://www.organicdesign.co.nz/Extension:ExtraMagic.php
 
*
 
* @package MediaWiki
 
* @subpackage Extensions
 
* @author [http://www.organicdesign.co.nz/User:Nad User:Nad]
 
* @copyright © 2007 [http://www.organicdesign.co.nz/User:Nad User:Nad]
 
* @licence GNU General Public Licence 2.0 or later
 
*/
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
 
define('EXTRAMAGIC_VERSION', '2.0.3, 2009-07-20');
 
 
$wgExtensionCredits['parserhook'][] = array(
 
'name'        => 'ExtraMagic',
 
'author'      => '[http://www.organicdesign.co.nz/User:Nad User:Nad]',
 
'description' => 'Adds useful variables and parser functions',
 
'url'        => 'http://www.organicdesign.co.nz/Extension:ExtraMagic.php',
 
'version'    => EXTRAMAGIC_VERSION
 
);
 
 
 
$wgCustomVariables = array( 'CURRENTUSER', 'CURRENTUSERREALNAME', 'CURRENTLANG', 'CURRENTSKIN', 'IPADDRESS', 'DOMAIN', 'NUMBERINGOFF', 'GUID' );
 
 
$wgExtensionFunctions[]                    = 'efSetupExtraMagic';
 
$wgHooks['MagicWordMagicWords'][]          = 'efAddCustomVariable';
 
$wgHooks['MagicWordwgVariableIDs'][]      = 'efAddCustomVariableID';
 
$wgHooks['LanguageGetMagic'][]            = 'efAddCustomVariableLang';
 
$wgHooks['ParserGetVariableValueSwitch'][] = 'efGetCustomVariable';
 
 
/**
 
* Called from $wgExtensionFunctions array when initialising extensions
 
*/
 
function efSetupExtraMagic() {
 
global $wgParser;
 
$wgParser->setFunctionHook( 'REQUEST', 'efExtraMagicExpandRequest', SFH_NO_HASH );
 
}
 
 
/**
 
* Register magic words
 
*/
 
function efAddCustomVariable( &$magicWords ) {
 
global $wgCustomVariables;
 
foreach( $wgCustomVariables as $var ) $magicWords[] = "MAG_$var";
 
return true;
 
}
 
 
function efAddCustomVariableID( &$variables ) {
 
global $wgCustomVariables;
 
foreach( $wgCustomVariables as $var ) $variables[] = constant( "MAG_$var" );
 
return true;
 
}
 
 
function efAddCustomVariableLang( &$langMagic, $langCode = 0 ) {
 
global $wgCustomVariables;
 
 
# Magic words
 
foreach( $wgCustomVariables as $var ) {
 
$magic = "MAG_$var";
 
$langMagic[defined( $magic ) ? constant( $magic ) : $magic] = array( $langCode, $var );
 
}
 
 
# Parser functions
 
$langMagic['REQUEST'] = array( $langCode, 'REQUEST' );
 
 
return true;
 
}
 
 
/**
 
* Expand parser functions
 
*/
 
function efExtraMagicExpandRequest( &$parser, $param ) {
 
global $wgRequest;
 
$parser->disableCache();
 
return $wgRequest->getText( $param );
 
}
 
 
/**
 
* Process variable values
 
*/
 
function efGetCustomVariable( &$parser, &$cache, &$index, &$ret ) {
 
switch ( $index ) {
 
 
case MAG_CURRENTUSER:
 
global $wgUser;
 
$parser->disableCache();
 
$ret = $wgUser->mName;
 
break;
 
 
case MAG_CURRENTUSERREALNAME:
 
global $wgUser;
 
$parser->disableCache();
 
$ret = $wgUser->getRealName();
 
break;
 
  
case MAG_CURRENTLANG:
+
== CURRENTUSER ==
global $wgUser;
+
The username of the current user.
$parser->disableCache();
+
 
$ret = $wgUser->getOption( 'language' );
+
It is used as follows without any parameters (all the following ones without any example have the same basic syntax).
break;
+
<source>
+
{{CURRENTUSER}}
case MAG_CURRENTSKIN:
+
</source>
global $wgUser;
+
 
$parser->disableCache();
+
This gives the result: {{CURRENTUSER}}
$ret = $wgUser->getOption( 'skin' );
+
 
break;
+
== CURRENTPERSON ==
+
The real name of the current user if they entered one on sign up or in their preferences.
case MAG_IPADDRESS:
+
 
$parser->disableCache();
+
== USERID ==
$ret = $_SERVER['REMOTE_ADDR'];
+
The ID of the current user.
break;
+
 
+
== CURRENTLANG ==
case MAG_DOMAIN:
+
The two-letter language code preference of the current user.
$parser->disableCache();
+
 
$ret = $_SERVER['SERVER_NAME'];
+
== CURRENTSKIN ==
break;
+
The name of the skin selected by the current user.
+
 
case MAG_NUMBERINGOFF:
+
== ARTICLEID ==
global $wgUser;
+
The ID of the article being viewed.
$wgUser->setOption( 'numberheadings', false );
+
 
$ret = '';
+
== IPADDRESS ==
break;
+
The IP address of the current user.
+
 
case MAG_GUID:
+
== DOMAIN ==
$parser->disableCache();
+
The domain name used to make the current request.
$ret = strftime( '%Y%m%d', time() ) . '-' . substr( strtoupper( uniqid('', true) ), -5 );
+
 
break;
+
== GUID ==
+
A randomly generated globally unique identifier.
}
+
 
return true;
+
== USERPAGESELFEDITS ==
}
+
Returns a list of usernames for users who have made edits to their own user pages.
 +
 
 +
== REQUEST ==
 +
This is a parser-function which has as its first parameter the name of a query-string or posted value, it will return the content of that value. The second parameter is a default to return if the value didn't exist. For example:
 +
<source>
 +
{{REQUEST:mydata | No data set }}
 +
</source>
 +
 
 +
== COOKIE ==
 +
This is similar to the ''REQUEST'' function but works for cookie values instead.
 +
 
 +
== #ifgroup ==
 +
A conditional function for testing group membership, the first parameter is the group (or comma-separated list of groups), the second is what to return if the current user belongs to at least one of the groups, and the third (optional) parameter is what to return if the user is not in any of the groups. For example:
 +
<source>
 +
{{#ifgroup:sysop | You are a sysop }}
 +
</source>
 +
 
 +
<source>
 +
{{#ifgroup:user | You are logged in as {{CURRENTUSER}} | You are not logged in }}
 +
</source>
 +
 
 +
== #ifuses ==
 +
A conditional function for testing whether or not the current article uses a given template, the first parameter is the name of the template to test for, the second and third are the content to return if the condition is true or not true the same as for ''#ifgroup''.
 +
 
 +
== #ifcat ==
 +
This is another conditional test, in this case the first parameter is the name of a category to test whether the current article is a member of or not.
 +
 
 +
== #next, #prev ==
 +
Return previous or next items in a list given item text and a list. Used by the [[Bliki 2.0]] system.
 +
 
 +
== OWNER ==
 +
Returns the owner (creator) of an article given its title.
 +
[[Category:Extensions|ExtraMagic]]

Latest revision as of 14:14, 19 October 2018

Info.svg This code is in our Git repository here.

Note: If there is no information in this page about this code and it's a MediaWiki extension, there may be something at mediawiki.org.


This extensions adds a number of useful magic words which we've needed for our own projects over the years. Most of them are available already in various different extensions, but we preferred to have them all packaged together in one place.

CURRENTUSER

The username of the current user.

It is used as follows without any parameters (all the following ones without any example have the same basic syntax).

{{CURRENTUSER}}

This gives the result: 54.198.45.0

CURRENTPERSON

The real name of the current user if they entered one on sign up or in their preferences.

USERID

The ID of the current user.

CURRENTLANG

The two-letter language code preference of the current user.

CURRENTSKIN

The name of the skin selected by the current user.

ARTICLEID

The ID of the article being viewed.

IPADDRESS

The IP address of the current user.

DOMAIN

The domain name used to make the current request.

GUID

A randomly generated globally unique identifier.

USERPAGESELFEDITS

Returns a list of usernames for users who have made edits to their own user pages.

REQUEST

This is a parser-function which has as its first parameter the name of a query-string or posted value, it will return the content of that value. The second parameter is a default to return if the value didn't exist. For example:

{{REQUEST:mydata | No data set }}

COOKIE

This is similar to the REQUEST function but works for cookie values instead.

#ifgroup

A conditional function for testing group membership, the first parameter is the group (or comma-separated list of groups), the second is what to return if the current user belongs to at least one of the groups, and the third (optional) parameter is what to return if the user is not in any of the groups. For example:

{{#ifgroup:sysop | You are a sysop }}
{{#ifgroup:user | You are logged in as {{CURRENTUSER}} | You are not logged in }}

#ifuses

A conditional function for testing whether or not the current article uses a given template, the first parameter is the name of the template to test for, the second and third are the content to return if the condition is true or not true the same as for #ifgroup.

#ifcat

This is another conditional test, in this case the first parameter is the name of a category to test whether the current article is a member of or not.

#next, #prev

Return previous or next items in a list given item text and a list. Used by the Bliki 2.0 system.

OWNER

Returns the owner (creator) of an article given its title.