Extension:CategoryDropdown

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
OOjs UI icon advanced.svg
CategoryDropdown
Release status: beta
Implementation Tag
Description Based on the question posted on stack overflow requesting a category dropdown extension.
Author(s) (KyleWieringtalk)
Latest version 0.0.1
MediaWiki 1.26.0
PHP 5.4
License MIT License
Download
Currently using the newer extension.json - contact me if you need backwards compatibility...
Translate the CategoryDropdown extension if it is available at translatewiki.net
Check usage and version matrix.

The CategoryDropdown extension creates a dropdown within the content of a page with options that when selected navigate to those pages.

Usage[edit]

A couple of example usages, the 'all' doesn't really matter, there just has to be some value present.

<categorydropdown type="category" parent="all" />

<categorydropdown type="category" />

<categorydropdown type="page" parent="all" />

<categorydropdown type="page" />

Example of dropdown, nothing fancy.

Installation[edit]

  1. add wfLoadExtension( 'CategoryDropdown' ); to LocalSettings.php
  2. Create a directory CategoryDropdown inside of extensions
  3. Create two files and copy the code from this page into them.
    • CategoryDropdown_body.php
    • extension.json

Files[edit]

CategoryDropdown_body.php[edit]

<?php
/**
 * So maybe I'm doing this wrong, but it looks like MediaWiki goes all out to abuse classes in a static manner.
 */
class CategoryDropdown
{
	/**
	 * Do nothing.
	 */
	public static function onExtensionLoad() {
		// do nothing.  
	}
	
	/**
	 * Register any render callbacks with the parser
	 * @var Parser
	 */
	public static function onParserInit( Parser &$parser ) {
		$parser->setHook( 'categorydropdown', 'CategoryDropdown::renderTagCategoryDropdown' );
		
		return true;
	}
	
	/**
	 * Entry point method, determines the type of dropdown and the loosely termed 'parent'
	 *
	 * @param $input string
	 * @param $args array
	 * @param $parser Parser
	 * @param $frame PPFrame
	 */
	public static function renderTagCategoryDropdown($input, array $args, Parser $parser, PPFrame $frame) {
		
		// default to category, other wise use the arguments.
		$type = (isset($args) && isset($args['type'])) ? $args['type'] : 'page';
		$parent = (isset($args) && isset($args['parent'])) ?  null : $frame->title->mArticleID;
		
		switch($type)
		{
			case 'page': 
				$html = self::renderPageDropdown($parent);
			break;
			case 'category':
			default:
				$html = self::renderCategoryDropdown($parent);
			break;
		}
		
		return $html;
	}
	
	/**
	 * Render the dropdown for categories
	 * @param $parent string|null
	 */
	private static function renderCategoryDropdown($parent) {
		$whereCondition = ($parent !== null) ? 'cl_from = \''.$parent .'\' and cl_type != \'page\'': 'cl_to is not null and cl_type != \'page\'';
		
		// use the data access layer, it's safer.
		$dataAccessLayer = wfGetDB( DB_SLAVE );
		$resource = $dataAccessLayer->select('categorylinks', ['cl_to'], $whereCondition,__METHOD__, ['ORDER BY' => 'cl_sortkey ASC, cl_to ASC'] );
		$html = '<select onchange="location = \'index.php/Category:\'+this.options[this.selectedIndex].value;">';
		$html .= '<option value="">Click for categories</option>';
		foreach( $resource as $row ) {
			$html .= '<option value="'. $row->cl_to .'">'.$row->cl_to .'</option>';
		}
		$html .= '</select>';
				
		return $html;
	}
	
	/**
	 * Render the dropdown for pages
	 * @var string|null
	 */
	private static function renderPageDropdown($parent) {
		$whereCondition = ($parent !== null) ? 'cl_from = \''.$parent .'\' and cl_type = \'page\''  : 'cl_to is not null and cl_type = \'page\'';
		
		// use the data access layer, it's safer.
		$dataAccessLayer = wfGetDB( DB_SLAVE );
		$resource = $dataAccessLayer->select('categorylinks', ['cl_to'], $whereCondition,__METHOD__, ['ORDER BY' => 'cl_sortkey ASC, cl_to ASC'] );
		$html = '<select onchange="location = \'index.php/Category:\'+this.options[this.selectedIndex].value;">';
		$html .= '<option value="">Click for pages</option>';
		foreach( $resource as $row ) {
			$html .= '<option value="'. $row->cl_to .'">'.$row->cl_to .'</option>';
		}
		$html .= '</select>';
				
		return $html;
	}
}
?>

extension.json[edit]

{
	"name": "CategoryDropdown",
	"version": "0.0.1",
	"author": [
		"Kyle Wiering"
	],
	"url": "https://www.mediawiki.org/wiki/Extension:CategoryDropdown",
	"descriptionmsg": "Based on the question posted on stack overflow requesting a category dropdown extension http://stackoverflow.com/questions/25470877/navigate-mediawiki-category-pages-through-a-dropdownbox-on-wiki-page",
	"license-name": "MIT",
	"type": "other",
	"AutoloadClasses": {
		"CategoryDropdown": "CategoryDropdown_body.php"
	},
	"config": {
		"CategoryDropdownEnableFoo": true
	},
	"callback": "CategoryDropdown::onExtensionLoad",
	"Hooks": {
		"ParserFirstCallInit":[
			"CategoryDropdown::onParserInit"
		]
	},
	"ResourceFileModulePaths": {
		"localBasePath": "",
		"remoteExtPath": "CategoryDropdown"
	},
	"manifest_version": 1
}


Issues[edit]

Author's talk page, possibly the stack overflow question.

See also[edit]

CategoryLink