KJ7RRV

MediaWiki

I wrote a #MediaWiki template that allows for the creation of templates that change based on user configuration.

Edit: This will invalidate MediaWiki cache on pages that use it, increasing server CPU usage and page load times. Use this template sparingly.

Dependencies/Prerequisites

The GetUserName and ParserFunctions extensions are required. Also, you must create namespaces called DefaultConfig and UserConfig.

define("NS_DEFAULTCONFIG", 3000);
define("NS_DEFAULTCONFIG_TALK", 3001);
define("NS_USERCONFIG", 3002);
define("NS_USERCONFIG_TALK", 3003);
$wgExtraNamespaces[NS_USERCONFIG] = "UserConfig";
$wgExtraNamespaces[NS_USERCONFIG_TALK] = "UserConfig_talk";
$wgExtraNamespaces[NS_DEFAULTCONFIG] = "DefaultConfig";
$wgExtraNamespaces[NS_DEFAULTCONFIG_TALK] = "DefaultConfig_talk";
$wgNamespaceProtection[NS_DEFAULTCONFIG] = array("editdefaultconfig");
$wgGroupPermissions['sysop']['editdefaultconfig'] = true;
$wgCapitalLinkOverrides[NS_DEFAULTCONFIG] = false;
$wgCapitalLinkOverrides[NS_DEFAULTCONFIG_TALK] = false;
$wgNamespacesWithSubpages[NS_USERCONFIG] = true;
wfLoadExtension('GetUserName');
wfLoadExtension('ParserFunctions');

It is recommended to only allow administrators or other trusted users to edit the DefaultConfig namespace. Unfortunately, I am not aware of any way to prevent users from editing other users' UserConfig pages; this means that users can change the configuration settings of others.

Template

Put this in Template:Config:

{{#ifexist:DefaultConfig:{{{1}}}|{{#ifexist:UserConfig:{{#USERNAME:}}/{{{1}}}|{{UserConfig:{{#USERNAME:}}/{{{1}}}}}|{{ DefaultConfig:{{{1}}}}}}}|'''ERROR''': No such configuration option exists. Please {{#ifexist:UserConfig:{{#USERNAME:}}/isadmin|create|ask an administrator to create}} [[DefaultConfig:{{{1}}}]].}}

If you wish, you can create a UserConfig:[username]/isadmin page for all admin users. This has no effect other than changing the error message, so it is not important. Unlike other configuration settings, the content of this page does not matter; it only has to exist.

Usage

Simply use {{config|[option name]}} to get a configuration option. You must have a DefaultConfig:[option name] page. By default, {{config}} will return its content. However, if a user has a UserConfig:[user name]/[option name] page, its content will be returned instead.

Example

On the PBARC Web site, we use this for several things, including a {{time}} template. This takes time values in the format {{time|18|45}} and allows users to choose whether to see times as “6:45 PM” (12-hour time) or “18:45” (24-hour/military time).

Here is the code for Template:Time:

{{#ifeq: {{config|miltime}}|true|{{{1}}}:{{{2}}}|{{#ifexpr: ({{{1}}} = 12) and ({{{2}}} = 00)|12:00 noon|{{#ifexpr: ({{{1}}} = 00) and ({{{2}}} = 00)|12:00 midnight| {{#ifeq: {{#expr: {{{1}}} mod 12}} | 0 | 12 | {{#expr: {{{1}}} mod 12}}}}:{{{2}}} {{#ifexpr: {{{1}}} < 12|AM|PM}}}}}}}}

It will check the miltime configuration value and, if it is true, use 24-hour time; otherwise, it will use 12-hour time. On the PBARC site, it is set to false by default, but it could also be set to true.

License

This post, including all code, is released into the public domain under Creative Commons Zero. Feel free to use it for any purpose without asking permission or giving attribution.

#PBARC #FOSS #Programming