From mediawiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
OOjs UI icon advanced.svg
Release status: unmaintained
Description Change the labels of sections of categories
Author(s) Conrad Irwin (Conrad.Irwintalk)
Latest version 0.1
MediaWiki Only tested with r38247.
License No license specified
Download No link

Check usage and version matrix.

A hacked together extension to allow some customization over the text on individual Category pages, this is horribly hacky and will break if the CategoryPage output is changed even slightly. It is designed for situations when you know more about the items in the category than that they are "pages".


On a category page, or in a template that you transclude on a category page, use the new parser function {{#catlabel}} It can be used as follows:

{{#catlabel: nodivide}}
  • Removes the letters between sections of the category, useful for when there are only a few pages - though you'll have to play with your CSS if you want the items in the category evenly spaced.
{{#catlabel: pages | newtitle | newtext}}
  • Overwrites the """Pages in category "Example" """ and """The following 5 pages are in this category, out of 5 total.""" with newtitle and newtext.
{{#catlabel: categories | newtitle | newtext}}
  • Overwrites the """Subcategories""" and """This category has the following 5 subcategories, out of 5 total.""" with newtitle and newtext
{{#catlabel: media | newtitle | newtext}}
  • Overwrites the """Media in category "Examples" """ and """The following 5 files are in this category, out of 5 total. """ with newtitle and newtext.


Download the following and save it as extensions/CategoryLabel/CategoryLabel.php then add require_once ("$IP/extensions/CategoryLabel/CategoryLabel.php"); to your LocalSettings.php.

  $wgExtensionFunctions[] = 'wfCategoryLabelSetup';
  $wgHooks['LanguageGetMagic'][] = 'wfCategoryLabelMagic';
  $wgHooks['BeforePageDisplay'][] = 'wfCategoryLabelDisplay';
  $wgExtensionCredits['parserhook'][] = array(
      'name' => 'CategoryLabel',
      'author' => 'Conrad Irwin',
      'description' => 'Change the labels in category pages dynamically.',
  Based on idea by Jclerner
  Works by looking for any {{#label: }} in the category text and outputting a 
    ___CategoryLabel...=.............___ into the output,
  Then, on output, these snippets are found in the text and the necessary action is taken
  function wfCategoryLabelSetup() {
    global $wgParser;
    $wgParser->setFunctionHook ('categorylabel', 'wfCategoryLabelFunction');
    return true;
  function wfCategoryLabelMagic (&$magicWords, $langCode) {
    $magicWords['categorylabel'] = array( 0, 'catlabel' );
    return true;
  function wfCategoryLabelFunction (&$parser, $name = '', $title = '', $text='') {
    if ($name == 'categories' || $name == 'pages' || $name == 'media' || $name == 'nodivide' )
      return "___CategoryLabel$name=".base64_encode ($title)."|".base64_encode ($text)."___";
      return '<span style="color: #FF0000">First parameter must be "categories", "pages", "media" or "nodivide"</span>';
  function wfCategoryLabelDisplay (&$out, &$sk) {
    $text = $out->getHtml ();
    #Find any hints we've left ourselves
    if (preg_match_all ("/___CategoryLabel(categories|pages|media|nodivide)=([^_\|]*)\|([^_\|]*)___/", $text, $matches,  PREG_SET_ORDER)){
      foreach ($matches as $match) {
        if ($match[1] == 'media') {
          $text = preg_replace (
            '/<div id="mw-category-media">[^<]*<h2>[^<]*<\/h2>[^<]*<p>[^<]*<\/p>/e',
            '"<div id=\"mw-category-media\"><h2>'.base64_decode ($match[2]).'</h2>".("'.$match[3].'"?"<p>'.base64_decode ($match[3]).'<p>":"")', 
        }else if($match[1] == 'pages') {
          $text = preg_replace (
            '/<div id="mw-pages">[^<]*<h2>[^<]*<\/h2>[^<]*<p>[^<]*<\/p>/e',
            '"<div id=\"mw-pages\"><h2>'.base64_decode ($match[2]).'</h2>".("'.$match[3].'"?"<p>'.base64_decode ($match[3]).'<p>":"")', 
        }else if($match[1] == 'categories') {
          $text = preg_replace (
            '/<div id="mw-subcategories">[^<]*<h2>[^<]*<\/h2>[^<]*<p>[^<]*<\/p>/e',
            '"<div id=\"mw-subcategories\"><h2>'.base64_decode ($match[2]).'</h2>".("'.$match[3].'"?"<p>'.base64_decode ($match[3]).'<p>":"")', 
        }else if($match[1] == 'nodivide') {
          $text = preg_replace ('/<h3>.( cont\.)?<\/h3>/','',$text);
          print $match[1];
    $text = preg_replace ("/___CategoryLabel(categories|pages|media|nodivide)=([^_]*)___/", "", $text);
    $out->clearHtml ();
    $out->addHtml ($text);
    return true;