Extension:AddScript

Details
Adds   tags (e.g. for JavaScript) to the page's head section.

Better Option
See .

Usage


The 'filename' must be located in 'home/jsscripts' directory and not be appended with the '.js' extension as it is automatically added.

Code
Copy this to $IP/extensions/AddScript/AddScript.php

tag to the head section of a page', 'url'    => 'https://www.mediawiki.org/wiki/Extension:AddScript', );

$wgExtensionFunctions[] = "wfAddScriptSetup"; $GLOBALS['asObj'] = new AddScriptClass; $wgHooks['LanguageGetMagic'][] = array( $asObj, 'hGetMagic' );

function wfAddScriptSetup {	global $asObj; global $wgHooks;

$wgHooks['ParserAfterTidy'][] = array( $asObj, 'feedScripts' );

global $wgParser; $wgParser->setHook(        'addscript', array( &$asObj, 'pSet' ) ); $wgParser->setFunctionHook( 'addscript', array( &$asObj, 'mg_pSet' ) ); }

class AddScriptClass {	static $base = 'jsscripts/'; static function getGlobalObjectName { return "asObj";          } static function &getGlobalObject   { return $GLOBALS['asObj']; }

var $slist; public function pSet( &$text, &$argv, &$parser) {		$text = $this->processURI( $argv['src'] ); return $text; }	public function mg_pSet( &$parser, $uri ) {		$e = explode( '=', $uri ); if ( count($e) > 1) { 			if ($e[0] != 'src') return; $uri = $e[1]; }		return $this->processURI( $uri ); }	private function processURI( $uri ) {		$uri = $this->cleanURI( $uri ); if ($this->checkURI( $uri )) {			$this->slist[] = $uri; return; }		return 'addscript: invalid uri  '.$uri.' '; }	private function cleanURI( $uri ) {		// v1.2: address XSS vulnerability $clean_uri = str_replace( array('/../', '../', '\\..\\', "..\\",'"','`','&','?',										 '<','>','.' ), "", $uri);		return $clean_uri;	}	private function checkURI( $uri )	{		// uri must resolved to a local file in the $base directory.		$spath = self::$base.$uri.'.js';		// we need the full path on windows system...		$cpath = dirname(__FILE__);		// assume the script path is one level up		// i.e. this file should be in '/extensions' but the js script		// directory should be in the base.		$bpath = dirname( $cpath );		return file_exists( $bpath."/{$spath}" );	} 	public function hGetMagic( &$magicWords, $langCode )	{		$magicWords['addscript'] = array( 0, 'addscript' );		return true;			}	public function feedScripts( &$parser, &$text )	{		global $wgScriptPath;		global $wgOut;		if (!empty($this->slist))			foreach($this->slist as $sc)				$wgOut->addScript(' ');		return true; // v1.2 fix } } // END CLASS DEFINITION