From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manualManual:Extensions
Crystal Clear action run.png

Release status:Extension status beta

ImplementationTemplate:Extension#type Hook
DescriptionTemplate:Extension#description Automatically add wikitext to an article from templates depending on the categories the page has
Author(s)Template:Extension#username Otso Helenius (piksitalk)
MediaWikiTemplate:Extension#mediawiki 1.15+
PHPTemplate:Extension#php 5.1+
LicenseTemplate:Extension#license BSD 3-clause "Modified" License
Download See below
Hooks usedTemplate:Extension#hook

Translate the AutoCategoryInclude extension if it is available at translatewiki.net

Check usage and version matrix.

What can this extension do?[edit]

The extension looks for a category link on an article page, and includes the wikitext of a page matching the category name in Template namespace to the article wikitext. This is done for all the category links that have matching and existing pages in the Template namespace.

I coded this extension to be used with Semantic Mediawiki data. I use it to include info/factboxes with semantic data automatically to pages, and prevent the users from messing up the box template call or removing it while editing the article text. Thus, the template doesn't bring a lot of added functionality, and is meant to hide confusing and often messy looking wikisyntax from users and enable them to concentrate on the article contents instead.

The code was hacked together in one afternoon and as I don't have a very deep knowledge of Mediawiki nor the proper usage of hooks, the code might be unnecessarily complicated or use the API incorrectly. I'm glad if anyone is willing to develop and enhance it further.


Just create pages matching category names in the template namespace, they will automatically show on the article pages when the page is viewed.


Due to a code in the bug, the wikicode is only added to a page after its cache is purged (action=purge). The getEnableLimitReport method is not reliable enough to determine when the parser is handling the page contents $text and not one of the other strings that get parsed when a page is loaded. I have not found any documentation regarding getEnableLimitReport, and am using it only because of a recommendation from #mediawiki. The other choice of fetching the page contents string and comparing it to the current $text variable also fails for reasons explained here.

Download instructions[edit]

Please cut and paste the code found below and place it in $IP/extensions/ExtensionName/ExtensionName.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.


To install this extension, add the following to LocalSettings.php:



 * @file AutoCategoryInclude.php
 * @author Otso Helenius

if ( !defined( 'MEDIAWIKI' ) ) die();

$wgExtensionCredits['parserhook'][]= array(
        'path' => __FILE__,
        'name' => 'AutoCategoryInclude',
        'version' => 1,
        'author' => 'Otso Helenius',
        'description' => 'Automatically add wikitext to an article from templates depending on the categories the page has',
        'descriptionmsg'  => 'autocategoryinclude-desc',

$wgHooks['ParserBeforeStrip'][] = 'fnAutoCategoryIncludeHook';

function fnAutoCategoryIncludeHook(&$parser, &$text)
        global $wgContLang, $wgRequest;

        $action = $wgRequest->getVal('action');

        // the templates will be included solely when the page is viewed, and only to the page wikitext, not during the other parserhook runs.
        // the method of determining which run is handling the article wikitext is shaky but seems to work
        if (($action == ('view' || 'print' || 'purge' || nil)) && ($parser->getOptions()->getEnableLimitReport())) {

                // fetch namespace strings for regexping
                $namespaceStrings = $wgContLang->getNamespaces();
                $categoryNamespaceString = $namespaceStrings[NS_CATEGORY];
                $templateNamespaceString = $namespaceStrings[NS_TEMPLATE];

                // fetch the names of all the categories a page has
                $CategoryList = $parser->getTitle()->getParentCategories();
                $CategoryList = array_keys($CategoryList);

                // replace Category: with Template: in the strings
                foreach ($CategoryList as &$CategoryListName) {
                        $CategoryListName = preg_replace("/^".$categoryNamespaceString . "/", $templateNamespaceString, $CategoryListName);
                        // concatenate template page wikitext with the article wikitext for each of the matching template pages
                        $tempTitle = Title::newFromText($CategoryListName);
                        if ($tempTitle->exists()) {
                                $tempText = new Article($tempTitle);
                                $text = $tempText->getContent() . $text;
        return true;