Extension:CategoryDropdown

From Linux Web Expert

The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
MediaWiki extensions manual
CategoryDropdown
Release status: unmaintained
Implementation Tag
Description Creates a dropdown menu within the page content with options that when selected navigate to those pages
Author(s) Kyle Wiering (KyleWieringtalk)
Latest version 0.0.1
MediaWiki 1.26+
PHP 5.4
Database changes No
License MIT License
Download See code section
Quarterly downloads Lua error in Module:Extension at line 172: bad argument #1 to 'inNamespace' (unrecognized namespace name 'skin').
Public wikis using Lua error in Module:Extension at line 172: bad argument #1 to 'inNamespace' (unrecognized namespace name 'skin').

The CategoryDropdown extension creates a dropdown within the content of a page with options that when selected navigate to those pages. It was triggered on the question posted on stack overflow requesting a category dropdown extension.

Usage

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

  • <translate> <tvar name=1>Copy the code into files</tvar> and place the file(s) in a directory called <tvar name=name>CategoryDropdown</tvar> in your <tvar name=ext>extensions/</tvar> folder.</translate>
  • <translate> Add the following code at the bottom of your <tvar name=1>LocalSettings.php </tvar> file:</translate>
    wfLoadExtension( 'CategoryDropdown' );
    
  • File:OOjs UI icon check-constructive.svg <translate> Done</translate> – <translate> Navigate to <tvar name=special>Special:Version</tvar> on your wiki to verify that the extension is successfully installed.</translate>

Code

CategoryDropdown_body.php
<?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_REPLICA );
		$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_REPLICA );
		$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
{
	"name": "CategoryDropdown",
	"version": "0.0.1",
	"author": [
		"Kyle Wiering"
	],
	"url": "https://www.mediawiki.org/wiki/Extension:CategoryDropdown",
	"descriptionmsg": "Creates a dropdown menu within the page content with options that when selected navigate to those pages",
	"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
}

See also