Extension:CategoryLink

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

Release status: stable

CategoryLink v2.0.png
Implementation Tag, Parser function
Description Generates an N-column list of all categories in the wiki.
Author(s) Kyle van der Meer
Last version 2.0 (2010-02-10)
MediaWiki 1.9.3-1.10.1
License No license specified
Download see below
Example Yahowto
Check usage (experimental)

This extension can be used to create an N column display of the categories on a page. It uses <div> tags and css for web 2.0 and standards compliance. Customization of the color scheme is handled by the css.

This extension can be called with:

<categorylinks />

Contents

[edit] Installation

  1. Copy the CSS code below to the wiki page at wiki/index.php/MediaWiki:Common.css
  2. Copy php code below to extensions/CategoryLinks/CategoryLinks.php
  3. Add require_once "$IP/extensions/CategoryLinks/CategoryLinks.php"; to the end of your LocalSettings.php


[edit] Code

[edit] CategoryLinks.php

<?php
        $wgExtensionFunctions[] = 'registercategorylinks';
        $wgExtensionCredits['parserhook'][] = array(
                'name' => 'Category Links',
                'author' => 'Kyle van der Meer',
                'url' => 'http://www.mediawiki.org/wiki/Extension:CategoryLink',
                'description' => 'Displays a N-column table of categories.',);
 
        function registercategorylinks() {
                global $wgParser;
                $wgParser->setHook('categorylinks', 'printcategorylinks');
        }
 
       function printcategorylinks() {
                global $wgDBuser,$wgDBpassword, $wgDBname,$wgDBserver,$IP, $wgDBprefix;
                $link = mysql_connect($wgDBserver, $wgDBuser, $wgDBpassword)
                or die('Could not connect: ' . mysql_error());
                mysql_select_db($wgDBname) or die('Could not select database');
                $query = 'SELECT cl_to FROM '.$wgDBprefix.'categorylinks group by cl_to';
                $result = mysql_query($query) or die('Query failed: ' . mysql_error());
                $num_rows = mysql_num_rows($result);
                while($num_row = mysql_fetch_array($result))
                        $data[] = $num_row['cl_to'];
 
                $columns=3;//change this number to the number of columns you want.
                                $boxwidth=$columns*150;/*Default width part 1  Create a box width that is related to the number of columns.*/
                                $current_column = -1;
                                $column = array();
                                $change = 0;
                                $max_column_length=0;
                                $cur_column_length=0;
                                for($j=0;$j<$num_rows;$j++){
                                        if($j%ceil($num_rows/$columns) == 0){
                                                $change++;
                                                $current_column = ($current_column+1)%$columns;
                                                $cur_column_length=0;
                                        }
                                        if($change > 0)$cur_column_length++;
                                        if($change == 1)$max_column_length++;
                                        $column[$current_column].="\n\t\t\t\t<a href=\"../index.php/Category:$data[$j]\">".preg_replace("/_/"," ",$data[$j])."</a>";
 
                                }
                                for($k=$cur_column_length; $k<$max_column_length; $k++){
                                        $column[$current_column].="<span style=\"padding:3px;line-height:1;display:block;\">&nbsp;</span>";
                                }
                                $args .= "\n\t\t<div v-align=\"top\" style=\"width:".$boxwidth."px;\">";
 
                                for($i=0; $i<$columns;$i++){
                                        if($i < $columns)$args.="\n\t\t\t<div width=\"". $boxwidth/$columns ."\" class=\"category_link\">".$column[$i]."\n\t\t\t</div>";
 
                                }
                                $args .= "\n\t\t</div>";
 
 
                return (''.$args.'');
        }
?>

[edit] CSS

/* CATEGORY LINK START
 * Created by http://mediawiki.org/User:Kyle_van_der_meer
 * Used for Version 2 for non-table layout.
 */
.category_link{
        v-align:top;
        display:inline-block; 
        width:144px;           /*Default width part 2*/
}
 
.category_link a{
        border:1px solid #333; /*Default dark color*/
        color:#fff;            /*Default Light color*/
        background-color:#333; /*Default dark color*/
        padding:2px;
        line-height:1;
        display:block;
}
 
.category_link a:hover{
        border:1px solid #333; /*Default dark color*/
        color:#333;            /*Default dark color*/
        background-color:#eee; /*Default light color*/
}
/*CATEGORY LINK END*/

[edit] Related Extensions

These can also produce category lists, but with different appearances and options.

Personal tools
Namespaces

Variants
Actions
Navigation
Support
Download
Development
Communication
Print/export
Toolbox