Extension:CategoryIndex

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
Crystal Clear action run.svg
CategoryIndex
Release status: beta
Implementation Parser function
Description This extensions allows you to create categories of pages where the page is added with key value, which can be displayed on the lists of parties or categories used to select subset of pages from category.
Author(s) Robert Paciorek (berciktalk)
Latest version 1.0 (2010-08-14)
MediaWiki 1.15
License GNU General Public License 3.0 or later
Download below
Hooks used
OutputPageParserOutput
LanguageGetMagic
Translate the CategoryIndex extension if it is available at translatewiki.net
Check usage and version matrix.

What can this extension do?[edit]

This extensions allows you to create categories of pages where the page is added with key value, which can be displayed on the lists of parties or categories used to select subset of pages from category

Usage[edit]

  1. on the pages that you want to add to index "list_of_ip" with value 192.168.0.1 add tag:
    {{#category_index:list_of_ip|192.168.0.1}}
  2. index can be shown on page as:
    1. space separated distinct list of page titles (as link to page) by tag:
      {{#category_index_get:pages|list_of_ip}}
    2. space separated list of index values (as link to page) by tag:
      {{#category_index_get:values|list_of_ip}}
    3. space separated list of index values by tag:
      {{#category_index_get:raw_values|list_of_ip}}
    4. distinct list of page titles (as link to page) by tag:
      {{#category_index_get:list_pages|list_of_ip}}
    5. list of index values (as link to page) by tag:
      {{#category_index_get:list_values|list_of_ip}}
    6. list of index values and page titles (as link to page) by tag:
      {{#category_index_get:list_values_pages|list_of_ip}}
    7. table of index values and page titles (as link to page) by tag:
      {{#category_index_get:table|list_of_ip}}
  3. in any tag from point 2 you can add the third argument that be passed to LIKE sql clause and limited showned index values, for example:
    {{#category_index_get:pages|list_of_ip|192.168.0.1}}
    shown only pages witch 192.168.0.1

Download instructions[edit]

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

Installation[edit]

To install this extension, add CategoryIndex table to wiki database:

CREATE TABLE CategoryIndex (pageid INT, name VARCHAR(255), value VARCHAR(255));

and add the following to LocalSettings.php:

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

Code[edit]

<?php

/**
 * This extensions allows you to create categories of pages where the page is added with key value,
 * which can be displayed on the lists of parties or categories used to select subset of pages from category
 * 
 * INSTALATION:
 *	1. put this file (CategoryIndex.php) to PATH_TO_YOUR_WIKI//extensions/CategoryIndex/
 *	2. add CategoryIndex table to wiki database:
 *		CREATE TABLE CategoryIndex (pageid INT, name VARCHAR(255), value VARCHAR(255));
 *	3. add to LocalSettings.php
 *		require_once("$IP/extensions/CategoryIndex/CategoryIndex.php");
 *
 * USAGE:
 *	1. on the pages that you want to add to index "list_of_ip" with value 192.168.0.1 add tag:
 *		{{#category_index:list_of_ip|192.168.0.1}}
 *	2. index can be shown on page as:
 *		a) space separated distinct list of page titles (as link to page) by tag:
 *			{{#category_index_get:pages|list_of_ip}}
 *		b) space separated list of index values (as link to page) by tag:
 *			{{#category_index_get:values|list_of_ip}}
 *		c) space separated list of index values by tag:
 *			{{#category_index_get:raw_values|list_of_ip}}
 *
 *		d) distinct list of page titles (as link to page) by tag:
 *			{{#category_index_get:list_pages|list_of_ip}}
 *		e) list of index values (as link to page) by tag:
 *			{{#category_index_get:list_values|list_of_ip}}
 *		f) list of index values and page titles (as link to page) by tag:
 *			{{#category_index_get:list_values_pages|list_of_ip}}
 *
 *		g) table of index values and page titles (as link to page) by tag:
 *			{{#category_index_get:list_values_pages|list_of_ip}}
 *	3. in any tag from point 2 you can add the third argument that
 *     be passed to LIKE sql clause and limited showned index values, for example:
 *		{{#category_index_get:pages|list_of_ip|192.168.0.1}} shown only pages witch 192.168.0.1
 *
 *
 * COPYRIGHT (c) 2010, Robert Paciorek (http://www.opcode.eu.org/), GNU GPL v 3.0
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * some code fragmets inspirated by IndexFunction extensions by Alex Zaddach
 *  (http://www.mediawiki.org/wiki/Extension:IndexFunction)
 *
**/


$wgExtensionFunctions[] = 'CategoryIndex::register';
$wgExtensionCredits['parserhook'][] = array(
    'name' => 'CategoryIndex',
    'author' => 'Robert Paciorek',
    'url' => 'http://opcode.eu.org/',
);
$wgHooks['LanguageGetMagic'][]       = "CategoryIndex::setMagicWords";
$wgHooks['OutputPageParserOutput'][] = 'CategoryIndex::updateIndexes';

class CategoryIndex {
	public static function register() {
		global $wgParser;
		$wgParser->setFunctionHook( 'category_index', array( 'CategoryIndex', 'parseTag' ) );
		$wgParser->setFunctionHook( 'category_index_get', array( 'CategoryIndex', 'parseGetTag' ) );
		return true;
	}

	public static function setMagicWords( &$magicWords, $langCode ) {
		$magicWords['category_index'] = array( 0, 'category_index' );
		$magicWords['category_index_get'] = array( 0, 'category_index_get' );
		return true;
	}

	public static function updateIndexes( &$out, $parseroutput ) {
		global $wgTitle;
		if ( !isset($parseroutput->mCategoryIndexes) ) {
			$parseroutput->mCategoryIndexes = array();
		}
		if ( $parseroutput->getProperty( 'preview' ) ) {
			return true;
		}
		$pageid = $wgTitle->getArticleID();
		$dbw = wfGetDB( DB_MASTER );

		$current = array();
		$res = $dbw->select( 'CategoryIndex', array('name', 'value'), array('pageid' => $pageid), __METHOD__ );
		foreach( $res as $row ) {
			$current[]=array($row->name, $row->value);
		}

		$toAdd = wfArrayDiff2( $parseroutput->mCategoryIndexes, $current );
		$toRem = wfArrayDiff2( $current, $parseroutput->mCategoryIndexes );

		if ( $toAdd || $toRem ) {
			$dbw->begin( __METHOD__ );
			if ( $toRem ) {
				$delCond = "pageid = $pageid AND (";
				$parts = array(); 
				foreach ( $toRem as $entry ) {
					$parts[] = "(name = " . $dbw->addQuotes($entry[0]) . " AND value = " . $dbw->addQuotes($entry[1]) . ")";
				}
				$delCond .= implode( ' OR ', $parts ) . ")";
				$dbw->delete( 'CategoryIndex', array($delCond), __METHOD__ );
			}
			if ( $toAdd ) {
				$ins = array();
				foreach ( $toAdd as $entry ) {
					$ins[] = array( 'pageid' => $pageid, 'name' => $entry[0], 'value' => $entry[1] );
				}
				$dbw->insert( 'CategoryIndex', $ins, __METHOD__ );
			}
			$dbw->commit( __METHOD__ );
		}

		return true;
	}

	public static function parseTag(&$parser, $a1, $a2) {
		if ( !isset($parser->mOutput->mCategoryIndexes) ) {
			$parser->mOutput->mCategoryIndexes = array();
		}
		$parser->mOutput->mCategoryIndexes[$a1 . $a2] = array($a1, $a2);
		
		return '';
	}
	public static function parseGetTag(&$parser, $a1, $a2="", $a3="") {
		$parser->disableCache();
		$dbw = wfGetDB( DB_MASTER );
		$output = '';

		if ($a3=="") {
			$where_sql=array('name'=>$a2);
		} else {
			$where_sql=array('name'=>$a2, "value LIKE " . $dbw->addQuotes($a3));
		}
		
		if ($a1=="pages" || $a1=="list_pages") {
			$field_sql=array('page_title');
			$options_sql=array('ORDER BY' => 'page_title', 'DISTINCT');
		} else {
			$field_sql=array('page_title', 'value');
			$options_sql=array('ORDER BY' => 'value,page_title');
		}
		
		$res = $dbw->select( array('page', 'CategoryIndex'),
			$field_sql,
			$where_sql,
			__METHOD__,
			$options_sql,
			array( 'CategoryIndex' => array('JOIN', 'page_id=pageid') )
		);

		switch($a1) {
			case "pages":
				foreach( $res as $row ) {
					$output .= "[[" . $row->page_title . "]] ";
				}
				return trim($output);
			case "values":
				foreach( $res as $row ) {
					$output .= "[[" . $row->page_title . "|" . $row->value . "]] ";
				}
				return trim($output);
			case "raw_values":
				foreach( $res as $row ) {
					$output .= $row->value . " ";
				}
				return trim($output);
			case "list_pages":
				foreach( $res as $row ) {
					$output .= "* [[" . $row->page_title . "]]\n";
				}
				return trim($output);
			case "list_values":
				foreach( $res as $row ) {
					$output .= "* [[" . $row->page_title . "|" . $row->value . "]]\n";
				}
				return trim($output);
			case "list_values_pages":
				foreach( $res as $row ) {
					$output .= "* " . $row->value . " - [[" . $row->page_title . "]]\n";
				}
				return trim($output);
			case "table":
				$output = "{|";
				foreach( $res as $row ) {
					$output .= "|-\n| [[" . $row->page_title . "]]\n| " . $row->value . "\n";
				}
				$output .= "|}";
				return $output;
		}
	}
}