Extension:AutoCategoryInclude

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

Release status: beta

Implementation Hook
Description Automatically add wikitext to an article from templates depending on the categories the page has
Author(s) Otso Helenius (piksitalk)
MediaWiki 1.15.1
PHP 5.1.6
License BSD
Download See below
Hooks used
ParserBeforeStrip

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

Check usage and version matrix; code metrics

What can this extension do?[edit | edit source]

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.

Usage[edit | edit source]

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

Notes[edit | edit source]

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 | edit source]

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.

Installation[edit | edit source]

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

require_once("$IP/extensions/AutoCategoryInclude/AutoCategoryInclude.php");

Code[edit | edit source]

<?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;
}