Extension:PromotedInterlanguages

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual
Crystal Clear action run.png
PromotedInterlanguages

Release status: experimental

PromotedInterlanguages.png
Implementation Parser function
Description Adds a {{#promoted:status|langcode}} function to customize the look of interlanguage links to foreign promoted articles
Author(s) Nicolas Dumazet (NicDumZtalk)
License LGPL
Download No link
Parameters

wgPilPromotedClasses

Hooks used
SkinTemplateOutputPageBeforeExec

LanguageGetMagic

Translate the PromotedInterlanguages extension if it is available at translatewiki.net

Check usage and version matrix; code metrics

What can this extension do?[edit | edit source]

This extension provides the ability to append predefined CSS classes to interlanguage links that got promoted status (featured articles, good articles, featured portals...) in other languages. This extension associates promoted statuses to CSS classes, and adds a {{#promoted:status|lang}} parser tag.

That extension is meant as an alternative to the Javascript hacks used on the Wikimedia projects to display golden stars next to the interlanguage links pointing to a featured article in a foreign language.

Usage[edit | edit source]

Assuming that a "FA" CSS class is defined and included by MediaWiki e.g.:

li.FA {
    /* use a custom list bullet, for example */
    list-style-image: url("http://upload.wikimedia.org/wikipedia/en/d/d4/Monobook-bullet-star.png");
}

and that the featured status is associated to the FA CSS class (see below), adding {{#promoted:featured|fr}} to an article will apply the FA CSS class to the fr: interlanguage links in that article.

Download instructions[edit | edit source]

Please cut and paste the code found below and place it in $IP/extensions/PromotedInterlanguages/. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

Installation[edit | edit source]

To install this extension, add the following to LocalSettings.php:

$wgPilPromotedClasses = array();
 
# Add a value to $wgPilPromotedClasses to add a promoted status.
# First key is the namespace where it applies
# Second key is the first {{#promoted argument expected
# Value is the CSS class to be appended to the promoted interlanguage link

# For instance, 
# $wgPilPromotedClasses['NS_MAIN']['featured'] = 'FArticle';
# $wgPilPromotedClasses['NS_MAIN']['good'] = 'GArticle';
# $wgPilPromotedClasses['NS_PROJECT']['featured'] = 'FPortal';
# creates three promoted statuses : Featured articles, good articles,
# and featured portals.
# In main namespace articles, {{#promoted:featured|xx}} will append the 
# FArticle CSS class to the xx: interlanguage links, {{#promoted:good|yy}}
# will append the GArticle CSS class to the yy interlanguage links

$wgPilPromotedClasses[NS_MAIN]['featured'] = 'FA';
 
require_once("$IP/extensions/PromotedInterlanguages/Promoted.php");

Code[edit | edit source]

PromotedInterlanguages/Promoted.php[edit | edit source]

<?php
 
if( !defined( 'MEDIAWIKI' ) ) {
	echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
	die( 1 );
}
 
include_once('Promoted.body.php');
 
$wgExtensionCredits['parserhook'][] = array(
	   'name' => 'PromotedInterlanguages',
	   'author' =>'Nicolas Dumazet', 
	   'url' => 'http://www.mediawiki.org/wiki/Extension:PromotedInterlanguages', 
	   'description' => '<nowiki>{{#promoted:status|langcode}}</nowiki> parser tag, enhances look of interlanguage links that have a featured or good article status'
	   );
 
$wgPilInstance = new PromotedInterlanguagesHandler;
 
$wgHooks['SkinTemplateOutputPageBeforeExec'][] = array( $wgPilInstance, 'modifyInterlanguages' );
 
$wgExtensionFunctions[] = 'PromotedInterlanguages_Setup';
 
$wgHooks['LanguageGetMagic'][] = 'PromotedInterlanguages_Magic';
 
 
function PromotedInterlanguages_Setup() {
		global $wgParser, $wgPilInstance;
		$wgParser->setFunctionHook( 'promoted', array( $wgPilInstance, 'promotedTagRender' ) );
}
 
 
function PromotedInterlanguages_Magic( &$magicWords, $langCode ) {
		$magicWords['promoted'] = array( 0, 'promoted' );
		return true;
}

PromotedInterlanguages/Promoted.body.php[edit | edit source]

<?php
 
if( !defined( 'MEDIAWIKI' ) ) {
	echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
	die( 1 );
}
 
class PromotedInterlanguagesHandler {
 
	# Stores the css classes to append.
	# Keys are language codes, values are css classes
	var $featuredBits = array();
 
	function promotedTagRender( &$parser, $type = '', $lang = '' ) {
		global $wgPilPromotedClasses, $wgTitle;
 
		$parser->disableCache();
 
   		if ( isset( $wgPilPromotedClasses[$wgTitle->getNamespace()][$type] ) ) {
   			$this->featuredBits[$lang][] = 
				$wgPilPromotedClasses[$wgTitle->getNamespace()][$type];
		}
		return "";
	}
 
 
	function modifyInterlanguages($skin, $tpl) {
 
		if ( empty( $this->featuredBits ) ) {
			return true;
		}
 
		global $wgOut, $wgContLang;
 
		$interlanguages =& $tpl->data['language_urls'];
 
		$i = 0;
 
		foreach( $wgOut->getLanguageLinks() as $l ) {
			$tmp = explode( ':', $l, 2 );
			$lang = $tmp[0];
			unset($tmp);
 
			if ( isset( $this->featuredBits[$lang] ) ) {
				foreach( $this->featuredBits[$lang] as $class ) {
					$interlanguages[$i]['class'] .= " " . $class;
				}
			}
			$i++;
		}
		return true;
	}
}

See also[edit | edit source]