Jump to content

User:Svippong/AdditionalTabs

From mediawiki.org
Warning: The following is for an old version of MediaWiki (probably 1.15) and will no longer work. Look at bug 13228 and change I14351aa5 for current status.

This is the presentation of a slight modification to SkinTemplate.php's function buildContentActionUrls().

Purpose

[edit]

Some wikis, e.g. Wikinews, has an additional tab. This tab is another namespace attached to these two other tabs; the regular ones "main" and "talk".

This code will allow for the ability to add these tabs, and add lots of them -- if you want to.

Usage

[edit]

You can easily apply this code to your LocalSettings.php

// First, let's define some namespaces
define(NS_ARTICLE,          100);
define(NS_ARTICLE_TALK,     101);
define(NS_ARTICLE_COMMENTS, 102);

// Indeed, we want an article namespace for articles like news, we want a place to discuss
// how to the produce the article and one for people to write comments and opinions about
// the content of the article.

$wgExtraNamespaces[NS_ARTICLE]          = "Article";
$wgExtraNamespaces[NS_ARTICLE_TALK]     = "Article_talk";
$wgExtraNamespaces[NS_ARTICLE_COMMENTS] = "Article_comments";

// So far so good, so far we have only used standard options in MediaWiki, unfortunately,
// if we go to "Article comments:Something", MediaWiki will think it is its own namespace
// without a talk page attached.  So that will cause some issues, and in indeed, that is
// not our intend.

// We thus use the cleverly named $wgAdditionalNamespaceDefinitions.  I seriously couldn't
// think of a better name at the time.

$wgAdditionalNamespaceDefinitions = array(
        NS_ARTICLE          => array(NS_ARTICLE, 'talk' => NS_ARTICLE_TALK, 'comments' => NS_ARTICLE_COMMENTS),
        NS_ARTICLE_TALK     => array(NS_ARTICLE, 'talk' => NS_ARTICLE_TALK, 'comments' => NS_ARTICLE_COMMENTS),
        NS_ARTICLE_COMMENTS => array(NS_ARTICLE, 'talk' => NS_ARTICLE_TALK, 'comments' => NS_ARTICLE_COMMENTS)
);

// To search it easily, each namespace in this "array" of namespaces has to have its own index in this array.
// Each value of these must be the same, however.
// The array in which they are given have two standard options, the first (0) is the main namespace,
// the second ("talk") is the talk page namespace, and the new one (in this case, "comments") is the
// additional namespace.  If you want more, just add more, in the order you wish.
// Of course, this can get a lot of code running, so perhaps defining it once in an array, and then applying
// it might do the trick:

$wgArticleNamespace = array(NS_ARTICLE, 'talk' => NS_ARTICLE_TALK, 'comments' => NS_ARTICLE_COMMENTS);

$wgAdditionalNamespaceDefinitions = array(
        NS_ARTICLE          => $wgArticleNamespace,
        NS_ARTICLE_TALK     => $wgArticleNamespace,
        NS_ARTICLE_COMMENTS => $wgArticleNamespace
);

// Of course, it doesn't have to be just *one* namespace that allows  this, you
// can easily have several, just as long as they don't include each other in their array.

Afterwards, your page should have these namespaces together. Though, your "comments" (for this example) will appear blank, as no MediaWiki: message is defined for it. Its name will be its parent's namespace id, a hyphen and the name of the additional namespace in the definition array. In other words, for this example, the "comments" namespace's MediaWiki: message would be "MediaWiki:100-comments".

Modified buildContentActionUrls()

[edit]

Find buildContentActionUrls() in SkinTemplate.php and replace these lines up until "wfProfileIn( __METHOD__."-edit" );".

	function buildContentActionUrls() {
		global $wgContLang, $wgLang, $wgOut;

		wfProfileIn( __METHOD__ );

		global $wgUser, $wgRequest, $wgAdditionalNamespaceDefinitions;
		$action = $wgRequest->getText( 'action' );
		$section = $wgRequest->getText( 'section' );
		$content_actions = array();

		$prevent_active_tabs = false ;
		wfRunHooks( 'SkinTemplatePreventOtherActiveTabs', array( &$this , &$prevent_active_tabs ) )	;

		if( $this->iscontent ) {
			$subjpage = $this->mTitle->getSubjectPage();

			$nskey = $this->mTitle->getNamespaceKey();
			$ns = $this->mTitle->getNamespace();
			$talkpage = $this->mTitle->getTalkPage();
			
 			$extra_selected = true; // default: true, if they have extra, we will set it
			if( isset($wgAdditionalNamespaceDefinitions[$ns]) ) {
			    $subjpage = Title::makeTitle($wgAdditionalNamespaceDefinitions[$ns][0], $this->mTitle->getText());
			    $extra_selected = ($ns==$wgAdditionalNamespaceDefinitions[$ns][0]);
			}
			
			$content_actions[$nskey] = $this->tabAction(
				$subjpage,
				$nskey,
				!$this->mTitle->isTalkPage() && $extra_selected && !$prevent_active_tabs,
				'', true);

			$extra_selected = true; // same use as for the main namespace.
			if( isset($wgAdditionalNamespaceDefinitions[$ns]) ) {
				$talkpage = Title::makeTitle($wgAdditionalNamespaceDefinitions[$ns]['talk'], $this->mTitle->getText());
				$extra_selected = ($ns==$wgAdditionalNamespaceDefinitions[$ns]['talk']);
			}
			$content_actions['talk'] = $this->tabAction(
				$talkpage,
				'talk',
				$this->mTitle->isTalkPage() && $extra_selected && !$prevent_active_tabs,
				'',
				true);
			
			//This is the loop for all additional tabs.
			if( isset($wgAdditionalNamespaceDefinitions[$ns]) ) {
			    $parent = $wgAdditionalNamespaceDefinitions[$ns][0];
			    foreach($wgAdditionalNamespaceDefinitions[$ns] as $type => $subns) {
			        if($type!==0 && $type!='talk') { //This are here already
			            $content_actions[$type] = $this->tabAction(
			                    Title::makeTitle($subns, $this->mTitle->getText()),
			                    $parent.'-'.$type,
			                    ($ns==$subns) && !$prevent_active_tabs,
			                    '',
			                    true);
			        }
			    }
			}
// snip
	}