Difference between revisions of "Javascript extension"

From FollowTheScore
Jump to: navigation, search
(link fix)
 
Line 5: Line 5:
  
 
# Create a directory "javascript" under the home directory of your wiki (i.e. on the same level as extensions, includes, ..).
 
# Create a directory "javascript" under the home directory of your wiki (i.e. on the same level as extensions, includes, ..).
# put the script from kryogenix there : [http://kryogenix.org/code/browser/sorttable/sorttable.js script from kryogenix] there
+
# put the [http://kryogenix.org/code/browser/sorttable/sorttable.js script from kryogenix] there
 
# install the attached small piece of code as an extension (call it JavaScript) and invoke it from your LocalSettings.php
 
# install the attached small piece of code as an extension (call it JavaScript) and invoke it from your LocalSettings.php
 
# make suitable entries in your [[Mediawiki:Common.css]] article; define sortablewikitable or something similar there
 
# make suitable entries in your [[Mediawiki:Common.css]] article; define sortablewikitable or something similar there

Latest revision as of 22:59, 13 August 2007

There is a useful piece of java script code which can be used (e.g.) to create sortable tables in older wikis. The latest version of mediawiki come with built-in support for 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
  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"/></code> -- or create a suitable template, e.g. Template:Sortable Tables and include it from the page where you have your DPL definition using {{Sortable Tables}}
  6. use the proper DPL syntax in combination with mode=userformat; use class=sortablewikitable and do not forget to define an "id" for each table

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>'; 
}	
        
?>