User:Seb35/modifyToolbox

From MediaWiki.org
Jump to: navigation, search

A small code snippet to allow administrators to modify the toolbox through MediaWiki:Sidebar. For example, move the "Recent changes" from the navigation to the toolbox:

* navigation
** mainpage|mainpage-description
  ** randompage-url|randompage
  ** helppage|help
  * SEARCH
  * TOOLBOX
  ** WHATLINKSHERE|whatlinkshere
  ** RECENTCHANGESLINKED|recentchangeslinked-toolbox
** recentchanges-url|recentchanges
  ** FEEDS|feeds
  ** CONTRIBUTIONS|contributions
  ** LOG|log
  ** BLOCKIP|blockip
  ** EMAILUSER|emailuser
  ** USERRIGHTS|userrights
  ** UPLOAD|upload
  ** SPECIALPAGES|specialpages
  ** PRINT|printableversion
  ** PERMALINK|permalink
  ** INFO|pageinfo-toolboxlink
* LANGUAGES

All possible keywords are written here, but this means they could be displayed on appropriate pages (e.g. BLOCKIP only on user pages) and they are always displayed. It is exactly the current behaviour.

A demo is (currently) available on http://wiki.seb35.fr/MediaWiki:Sidebar (you can walk on other pages to see how the items change).

Code[edit]

For now, this is only a code snippet. Perhaps it could evolve as an extension.

Note this code is not really beautiful, particularly the inner foreach, but I didn’t find another way to retrieve the exact content of "MediaWiki:Sidebar" (without reparsing it obviously), since only the URL is given in the $toolbox array. Possibly it would be possible to "invert" the URL function to get the title, but this is also a bit hacky. Another solution would be to modify the core (Skin::addToSidebarPlain()) to add this information in the array, but this a very bad solution to locally change MW core (without changing the official codebase itself).

// Licence WTFPL 2.0
$wgHooks['BaseTemplateToolbox'][] = 'modifyToolbox';

function modifyToolbox( BaseTemplate $baseTemplate, array &$toolbox ) {

	static $keywords = array( 'WHATLINKSHERE', 'RECENTCHANGESLINKED', 'FEEDS', 'CONTRIBUTIONS', 'LOG', 'BLOCKIP', 'EMAILUSER', 'USERRIGHTS', 'UPLOAD', 'SPECIALPAGES', 'PRINT', 'PERMALINK', 'INFO' );

	$modifiedToolbox = array();

	// Walk in the MediaWiki:Sidebar message, section toolbox
	foreach ( $baseTemplate->data['sidebar']['TOOLBOX'] as $value ) {
		$specialLink = false;

		// Search if the keyword exists
		foreach ( $keywords as $key ) {
			if ( $value['href'] == Title::newFromText($key)->fixSpecialName()->getLinkURL() ) {
				$specialLink = true;

				// This is a keyword, hence add this special link
				if ( array_key_exists( strtolower($key), $toolbox ) ) {
					$modifiedToolbox[strtolower($key)] = $toolbox[strtolower($key)];
					break;
				}
			}
		}

		// This is a normal link
		if ( !$specialLink ) {
			$modifiedToolbox[] = $value;
		}
	}

	$toolbox = $modifiedToolbox;

	return true;
}