Jump to content

User:Robchurch/Custom action and tab

From mediawiki.org
<?php

/**
 * Extension adds a custom action and corresponding tab to
 * article views
 *
 * @package MediaWiki
 * @subpackage Extensions
 * @author Rob Church <robchur@gmail.com>
 */
 
if( defined( 'MEDIAWIKI' ) ) {

	$wgExtensionFunctions[] = 'efCustomActionSetup';
	
	/**
	 * Extension setup function
	 */
	function efCustomActionSetup() {
		global $wgHooks, $wgMessageCache;
		$wgMessageCache->addMessage( 'customaction', 'Custom' );
		$wgHooks['SkinTemplateContentActions'][] = 'efCustomActionTab';
		$wgHooks['UnknownAction'][] = 'efCustomActionHandler';
	}
	
	/**
	 * Adds the custom action tab
	 *
	 * @param array $tabs
	 */
	function efCustomActionTab( &$tabs ) {
		global $wgTitle, $wgRequest;
		$action = $wgRequest->getText( 'action' );
		if( $wgTitle->getNamespace() != NS_SPECIAL ) {
			$tabs['custom'] = array(
				'class' => $action == 'custom' ? 'selected' : false,
				'text' => wfMsg( 'customaction' ),
				'href' => $wgTitle->getLocalUrl( 'action=custom' ),
			);
		}
	}
	
	/**
	 * Handles the custom action
	 *
	 * @param string $action
	 * @param Article $article
	 */
	function efCustomActionHandler( $action, $article ) {
		global $wgOut;
		if( $action == 'custom' ) {
			$wgOut->addWikiText( "You are performing a custom action on '''" . $article->getTitle()->getText() . "'''." );
		}
	}

} else {
	echo( "This file is an extension to the MediaWiki software, and cannot be used standalone.\n" );
	exit( 1 );
}

?>