Jump to content

Extension:CategoryDropdown

From mediawiki.org

Template:WfGetDB

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

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

[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]
  • Copy the code into files and place the file(s) in a directory called CategoryDropdown in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php file:
    wfLoadExtension( 'CategoryDropdown' );
    
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Code

[edit]
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

[edit]