Javascript extension

From FollowTheScore
Revision as of 15:19, 15 February 2007 by Gero (talk | contribs)
Jump to: navigation, search

There is a very useful piece of java script code which can be used to create sortable tables.

The following steps are needed:

  1. Create a directory "javascript" under the home directory of your wiki (i.e. on the same level as extensions, includes, ..).
  2. put the script from kryogenix there : script from kryogenix there
  3. install the attached small piece of code as an extension (call it JavaScript) and invoke it from your LocalSettings.php
  4. make suitable entries in your Mediawiki:Common.css article; define sortablewikitable or something similar there
  5. call the script using <code><script src="sorttable.js"/></code> -- or create a suitable template )(e.g. {{Sortable Tables}} and include it from the page where you have your DPL definition
  6. use the proper DPL syntax in combination with mode=userformat; use class=sortbalewikitable and do not forget to define an "id"

And here is the code for the javascript extension:


<?php

if ( !defined( 'MEDIAWIKI' ) ) {
	die( 'This file is a MediaWiki extension, it is not a valid entry point' );
}

/*
  inserts a reference to a pre-defined javascript file 

  <javascript src="file"/>   file must be a plain name (path & extension are stripped)
                             which can be found under /javascript.
*/

/**
 * Register the extension with MediaWiki
 */
$wgExtensionFunctions[] = "wfJavaScript";
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'JavaScript',
        'author' => 'Gero Scholz',
        'description' => 'allows to insert a reference to a predefined javascript file',
        'url' => 'http://www.mediawiki.org/wiki/JavaScript'
        );

$wgJavaScriptMessages = array();
$wgJavaScriptMessages['en'] = array(
	'no_path_' . '1' => "ERROR: No path allowed"
);
               
function wfJavaScript() {
	global $wgParser, $wgMessageCache, $wgJavaScriptMessages;
	foreach( $wgJavaScriptMessages as $sLang => $aMsgs ) {
		$wgMessageCache->addMessages( $aMsgs, $sLang );
	}
	$wgParser->setHook( "javascript", "JavaScript" );
}

function JavaScript( $input, $params, &$parser ) {

	error_reporting(E_ALL);
	
	global  $wgUser, $wgContLang, $wgScriptPath;
	
	// INVALIDATE CACHE
	$parser->disableCache();

	$text='';
	$file='';
	foreach($params as $param => $value) {
		if ($param == 'src') {
			$file = ereg_replace("^[^/]*/+",'',$value);
			$file = ereg_replace("[.].*",'',$file);
		} 
	}
	if ($file == '') return '';
	return '<script type="text/javascript" src="'.$wgScriptPath.'/javascript/'.$file.'.js"></script>'; 
}	
        
?>