Jump to content

Extension:CategoryLink

From mediawiki.org
Revision as of 17:06, 12 April 2008 by 82.69.110.221 (talk) (→‎Code: Edited to include db prefix)
MediaWiki extensions manual
CategoryLinks
Release status: stable
Implementation Tag , Parser function
Description Generates an N-column list of all categories in the wiki.
Author(s) Kyle van der Meer
Latest version 1.5.0 (2007-11-19)
MediaWiki 1.9.3-1.10.1
License No license specified
Download see below

This extensions adds the ability to create a N-column table of categories to a wiki site and it creates a link to each category. I couldn't find something simple that would do this, so I decided to write one myself. Any improvements to make it more dynamic would be appreciated.

To change how many Columns you want on a page, change the column variable value. Other than that, it is all set to go!

This extension can be called with:

<categorylinks />

or just

<categorylinks>

Installation

Copy code below to ../extensions/CategoryLink.php and include in LocalSettings.php.

Code

        $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.
                $width=ceil(100/$columns);
                $args.="<table width=\"95%\" style=\"border-style:outset; border-width:2px; border-color:#006600;\" align=\"center\">\n";
                for($j=0;$j<$num_rows;$j++){
                        if(($j % $columns)==0)
                                $args.="\t<tr>\n";
                        $args.="\t\t<td width=\"$width%\" valign=\"top\"><p><a href=\"../index.php/Category:$data[$j]\">".preg_replace("/_/"," ",$data[$j])."</a></p>\n";
                        if(($j % $columns)==$columns-1)
                                $args.="\t</tr>\n";
                }
                $args.="</table>";
                return (''.$args.'');
        }

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