Extension:Hierachy of categories

This extension gives an overview about the root, currently 4 levels of sub and the incorrectly (cycles creating) categorized items. In addition the number of associated pages are given.

Source
copy the following to extensions/Hierachy.php and extensions/SpecialHOC.php respectively

moreover, add the following to lines to you LocalSettings.php:

require_once("extensions/SpecialHOC.php"); include("extensions/Hierachy.php");

Hierachy.php
<?php /* * @author Christoph Sawade * @date 20.12.2006 */

// no error reporting error_reporting( 0 );

class Hierachy { var $db;

function __construct { $db = &wfGetDB(DB_SLAVE); }	function getNumberOfSubpages( $category ) { $query = 'SELECT count(*) FROM categorylinks WHERE cl_to = "' . $category . '"'; $result = mysql_query($query) or die('Query failed: ' . mysql_error . "\n"); while ($row = mysql_fetch_array($result)) $res[] = $row[0]; return $res[0]; }

function getCategories { $query = 'SELECT DISTINCT cl_to FROM categorylinks;'; $result = mysql_query($query) or die('Query failed: ' . mysql_error . "\n"); while ($row = mysql_fetch_array($result)) $res[] = $row[0]; return $res; }

function getSubCategories( $category ) { // namespace 14 = category $query = 'SELECT page_title FROM categorylinks JOIN page on cl_from = page_id WHERE page_namespace=14 AND cl_to = "' . $category . '"'; $result = mysql_query($query) or die('Query failed: ' . mysql_error . "\n"); while ($row = mysql_fetch_array($result)) { $res[] = $row[0]; }	 return $res; }	function getAllSubCategories { // namespace 14 = category $query = 'SELECT page_title FROM categorylinks JOIN page on cl_from = page_id WHERE page_namespace=14'; $result = mysql_query($query) or die('Query failed: ' . mysql_error . "\n"); while ($row = mysql_fetch_array($result)) { $res[] = $row[0]; }	 return $res; }

function getParentCategories( $category ) { // namespace 14 = category $query = 'SELECT cl_to FROM categorylinks JOIN page on cl_from = page_id WHERE page_namespace=14 AND page_title = "' . $category . '"'; $result = mysql_query($query) or die('Query failed: ' . mysql_error . "\n"); while ($row = mysql_fetch_array($result)) { $res[] = $row[0]; }	 return $res; }

function getAllParentCategories { // namespace 14 = category $query = 'SELECT cl_to FROM categorylinks JOIN page on cl_from = page_id WHERE page_namespace=14'; $result = mysql_query($query) or die('Query failed: ' . mysql_error . "\n"); while ($row = mysql_fetch_array($result)) { $res[] = $row[0]; }	 return $res; }

function getRootCategories { return array_diff($this->getCategories, $this->getAllSubCategories); } }

SpecialHOC.php
 'Hierachy of Categories',        'description' => 'shows dynamically the category structure',        'url' => 'http://meta.wikimedia.org/wiki/Hierachy_of_categories',        'author' => 'Christoph Sawade',        'version' => '1.0.1' );

function HOC { global $IP, $wgMessageCache; require_once( "$IP/includes/SpecialPage.php" ); require_once( "Hierachy.php" );

class HOC extends SpecialPage { function HOC { SpecialPage::SpecialPage( 'HOC' ); $this->includable( true ); }               function execute { global $wgOut, $wgRequest, $wgUser; $color = array("-1" => "#FF0000", "#7777FF", "#55AA55", "#77CC77","#BBFFBB", "#FFFFFF"); $coldef = array("-1" => "incorrectly categorized",					"Root Category", 					"Subcategory Level 1",					"Subcategory Level 2", 					"Subcategory Level 3", 					"Subcategory Level 4");

$this->setHeaders; $wgOut->setPagetitle('Hierachy of Categories'); $out = $question;

$Hrch = new Hierachy; $root = $Hrch->getRootCategories; // categories $out .= ' ';

// legend $out .= ' '; $wgOut->addHTML($out); }

function getBoxText($cat, $color, $art, $subcat) { $info = ''. str_replace('_', ' ', $cat). ''; if ($art != -1) $info .= ' Articles: '. $art; if ($subcat != -1) $info .= ' Subcategories: '. $subcat; return $info. ' ';		 }

function getList($cat, $level, $err = array) { $Hrch = new Hierachy; $sc = $Hrch->getSubCategories($cat); $list[] = array("name" => $cat, "level" => $level, "article" => $Hrch->getNumberOfSubpages($cat) - count($sc), "subcategories" => count($sc));

foreach ($sc as $item) { if (in_array($item, $err)) $list[] = array("name" => $item, "level" => "-1", "article" => -1, "subcategories" => -1); else $list = array_merge($list, $this->getList($item, $level + 1, array_merge($err, array($item)))); }			return $list; }       }        SpecialPage::addPage( new HOC ); $wgMessageCache->addMessage('hoc' ,'Hierachy of Categories'); }