Extension:CategoryLink

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manualManual:Extensions
Crystal Clear action run.png
CategoryLinks

Release status:Extension status stable

CategoryLink v2.0.png
ImplementationTemplate:Extension#type Tag, Parser function
DescriptionTemplate:Extension#description Generates an N-column list of all categories in the wiki.
Author(s)Template:Extension#username KyleWiering
Latest versionTemplate:Extension#version 2.1 (2015-11-27)
MediaWikiTemplate:Extension#mediawiki 1.9.3-1.22.4
LicenseTemplate:Extension#license No license specified
Download see below
ExampleTemplate:Extension#example GMOD

Translate the CategoryLink extension if it is available at translatewiki.net

Check usage and version matrix.


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 />

Installation[edit]

  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


Code[edit]

CategoryLinks.php[edit]

<?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 = mysqli_connect($wgDBserver, $wgDBuser, $wgDBpassword)
                or die('Could not connect: ' . mysqli_error());
                mysqli_select_db($wgDBname) or die('Could not select database');
                $query = 'SELECT cl_to FROM '.$wgDBprefix.'categorylinks group by cl_to';
                $result = mysqli_query($query) or die('Query failed: ' . mysqli_error());
                $num_rows = mysqli_num_rows($result);
                while($num_row = mysqli_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.'');
        }
?>

CSS[edit]

/* 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*/

Related Extensions[edit]

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