Extension:CategoryTests

From MediaWiki.org

Jump to: navigation, search
Manual on MediaWiki Extensions
List of MediaWiki Extensions
Category Tests

Release status: stable

Implementation Parser function
Description Functions for category testing
Author(s) Ryan Schmidt (Skizzerz Talk)
Version 1.1
MediaWiki 1.11+
Download Download
Usage
Hooks used

LanguageGetMagic

Contents

[edit] Usage

This currently defines 3 parser functions for category testing.

  • {{#ifcategory:category|then|else|page}} -- tests if the given page is in the category given, then displays "then" or "else" (or current page if no page is given).
  • {{#ifnocategories:then|else|page}} -- tests if the given page does not belong in any categories, then displays "then" or "else" (or current page if no page is given).
  • {{#switchcategory:category1=result1|category2=result2|...|default}} -- Operates like the ParserFunction #switch, but doesn't take an initial parameter (it just starts cycling through right away) and tests if the page is in those categories instead of comparing it to a value. You can specify to test other pages using #page=pagename, where it would then test that page instead of the current page until the next #page definition, in which case it will test that page now instead. Making #page empty resets it to the current page.

[edit] Download instructions

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

[edit] Installation

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

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

[edit] Code

[edit] CategoryTests.php

<?php
/*
* CategoryTests extension by Ryan Schmidt
* Functions for category testing
* Check http://www.mediawiki.org/wiki/Extension:CategoryTests for more info on what everything does
*/
 
if( !defined( 'MEDIAWIKI' ) ) {
        echo "This file is an extension of the MediaWiki software and cannot be used standalone\n";
        die( 1 );
}
 
//credits and hooks
$wgExtensionFunctions[] = 'wfCategoryTests';
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'Category Tests',
        'version' => '1.1',
        'url' => 'http://www.mediawiki.org/wiki/Extension:CategoryTests',
        'author' => 'Ryan Schmidt',
        'description' => 'Functions for category testing'
);
 
$wgHooks['LanguageGetMagic'][] = 'wfCategoryTestsLanguageGetMagic';
 
function wfCategoryTests() {
        global $wgParser, $wgExtCategoryTests;
 
        $wgExtCategoryTests = new ExtCategoryTests();
        $wgParser->setFunctionHook( 'ifcategory', array(&$wgExtCategoryTests, 'ifcategory') );
        $wgParser->setFunctionHook( 'ifnocategories', array(&$wgExtCategoryTests, 'ifnocategories') );
        $wgParser->setFunctionHook( 'switchcategory', array(&$wgExtCategoryTests, 'switchcategory') );
}
 
function wfCategoryTestsLanguageGetMagic( &$magicWords, $langCode ) {
        switch ( $langCode ) {
        default:
                $magicWords['ifcategory'] = array( 0, 'ifcategory' );
                $magicWords['ifnocategories'] = array( 0, 'ifnocategories' );
                $magicWords['switchcategory'] = array( 0, 'switchcategory' );
        }
        return true;
} 
 
Class ExtCategoryTests {
 
        function ifcategory( &$parser, $category = '', $then = '', $else = '', $pagename = '' ) {
                if($category === '') {
                        return $then;
                }
                if($pagename === '') {
                        $title = $parser->getTitle();
                        $page = $title->getPartialURL();
                } else {
                        $title = Title::newFromText($pagename);
                        if(!$title->exists())
                                return $then;
                        $page = $title->getPartialURL();
                }
                $params = new FauxRequest(array ('action' => 'query', 'titles' => $page, 'prop' => 'categories') );
                $api = new ApiMain($params);
                $api->execute();
                $data =& $api->getResultData();
                if(!array_key_exists('categories', $data['query']['pages']['1'])) {
                        return $else;
                }
                foreach( $data['query']['pages']['1']['categories'] as $key => $value ) {
                        if("Category:{$category}" == $data['query']['pages']['1']['categories'][$key]['title']) {
                                return $then;
                        }
                }
                return $else;
        }
 
        function ifnocategories( &$parser, $then = '', $else = '', $pagename = '' ) {
                if($pagename === '') {
                        $title = $parser->getTitle();
                        $page = $title->getPartialURL();
                } else {
                        $title = Title::newFromText($pagename);
                        if(!$title->exists())
                                return $then;
                        $page = $title->getPartialURL();
                }
                $params = new FauxRequest(array ('action' => 'query', 'titles' => $page, 'prop' => 'categories') );
                $api = new ApiMain($params);
                $api->execute();
                $data =& $api->getResultData();
                if(!array_key_exists('categories', $data['query']['pages']['1'])) {
                        return $then;
                }
                return $else;
        }
 
        function switchcategory( &$parser ) {
                $args = func_get_args();
                array_shift( $args );
                $found = false;
                $parts = null;
                $default = null;
                $page = '';
                foreach( $args as $arg ) {
                        $parts = array_map( 'trim', explode( '=', $arg, 2 ) );
                        if ( count( $parts ) == 2 ) {
                                if($parts[0] == "#page") {
                                        $page = $parts[1];
                                        continue;
                                }
                                if ( $found || $this->ifcategory($parser, $parts[0], true, false, $page) ) {
                                        return $parts[1];
                                } else {
                                        $mwDefault =& MagicWord::get( 'default' );
                                        if ( $mwDefault->matchStartAndRemove( $parts[0] ) ) {
                                                $default = $parts[1];
                                        }
                                }
                        } elseif ( count( $parts ) == 1 ) {
                                if ( $this->ifcategory($parser, $parts[0], true, false, $page) ) {
                                        $found = true;
                                }
                        }
                }
 
                if ( count( $parts ) == 1) {
                        return $parts[0];
                } elseif ( !is_null( $default ) ) {
                        return $default;
                } else {
                        return '';
                }
        }
}

[edit] See also

Personal tools