Extension talk:Javascript
The existing JavaScript.php gives a PHP warning on line 28 if there are no *.js files along with it and on line 39 if there are no *.css files along with it like:
PHP Warning: Invalid argument supplied for foreach() in /var/www/mediawiki/extensions/JavaScript/JavaScript.php on line 28, referer: http://www.....
This is because the glob PHP function cannot distinguish between no entries and an error condition when returning false.
It can be remedied with a check to eliminate the warnings by making the file as:
<?php
/**
* JavaScript extension - Includes all *.js files in the directory containing this script
*
* @package MediaWiki
* @subpackage Extensions
* @author [http://www.organicdesign.co.nz/nad User:Nad]
* @licence GNU General Public Licence 2.0 or later
* Fixed Invalid argument in glob - Ap.Muthu - 2011-03-20
*
*/
if ( !defined( 'MEDIAWIKI' ) ) die( 'Not an entry point.' );
define( 'JAVASCRIPT_VERSION', '2.1.3, 2011-01-14' );
$wgExtensionCredits['other'][] = array(
'name' => "JavaScript",
'author' => "[http://www.organicdesign.co.nz/nad User:Nad]",
'description' => "Includes all *.js files in the directory containing this script",
'url' => "http://www.organicdesign.co.nz/Extension:JavaScript",
'version' => JAVASCRIPT_VERSION
);
$wgHooks['BeforePageDisplay'][] = 'wfJavaScriptAddScripts';
function wfJavaScriptAddScripts( &$out, $skin = false ) {
global $wgJsMimeType, $wgScriptPath;
# Load JavaScript files
if ( glob( dirname( __FILE__ ) . "/*.js" ) ) {
foreach ( glob( dirname( __FILE__ ) . "/*.js" ) as $file ) {
if ( is_callable( array( $out, 'includeJQuery' ) ) && preg_match( "|/jquery-\d|", $file ) ) {
$out->includeJQuery();
$out->addScript( "<script type='$wgJsMimeType'>$=jQuery</script>" );
} else {
$file = preg_replace( "|^.*/extensions/|", "$wgScriptPath/extensions/", $file );
$out->addScript( "<script src='$file' type='$wgJsMimeType'></script>" );
}
}
}
# Load CSS files
if ( glob( dirname( __FILE__ ) . "/*.css" ) ) {
foreach ( glob( dirname( __FILE__ ) . "/*.css" ) as $file ) {
$file = preg_replace( "|^.*/extensions/|", "$wgScriptPath/extensions/", $file );
$out->addStyle( $file, 'screen', '', 'ltr' );
}
}
return true;
}
- Wheres the rest of this code? A PHP alway and with a ?> . But your code don't end with it.
- I think it will still work without the closing php tag. I have seen many PHP source code on mediawiki without the closing tag. But I always add it before copying it to my extensions directory. 60.241.61.96 13:45, 30 September 2011 (UTC)
Advantage over MediaWiki:Common.js is not clear
editCould someone update the description of this extension so that its advantage over MediaWiki:Common.js is more clear? Badon 05:26, 23 November 2011 (UTC)
'Undefined variable: organicdesign' error when using this extension
editI got error Undefined variable: organicdesign when using this script. Can someone please explain how to fix it?
- simply write
$organicdesign = "";
somewhere before the first use of the variable$organicdesign
(possibly after line 23) Kappa (talk) 17:31, 4 March 2014 (UTC)
- simply write
Revised version
editThe original version of this excellent little extension has a few issues. I have added a revised version below. In it I have removed the support for Mediawiki versions 1.7 and lower since I have no way of testing the script in an old version of Mediawiki and users should upgrade to a more recent version anyway. I have also removed the support for loading stylesheets (CSS files) as this is not what the extension is about. I have edited the meta-information displayed for this extension on Mediawiki's 'Version' page. The 'organicdesign' bug referred to elsewhere has been removed.
<?php /** * JavaScript extension - Includes all *.js files found in the directory containing this extension * * @package MediaWiki * @subpackage Extensions * @author [http://www.organicdesign.co.nz/nad User:Nad]. Revised by [http://www.irhb.org Henrik Thiil Nielsen] * @license GNU General Public Licence 2.0 or later * */ if ( !defined( 'MEDIAWIKI' ) ) die( 'Not an entry point.' ); define( 'JAVASCRIPT_VERSION', '4.0.0' ); $wgUseMWJquery = true; $wgExtensionCredits['other'][] = array( 'name' => "JavaScript", 'author' => "[http://www.organicdesign.co.nz/nad User:Nad]. Rev. [http://www.irhb.org Henrik Thiil Nielsen]", 'description' => "Includes all *.js files found in the directory containing this extension", 'url' => "http://www.mediawiki.org/wiki/Extension:Javascript", 'version' => JAVASCRIPT_VERSION ); $wgJavaScriptExterenalPath = wfJavaScriptExternalPath( dirname( __FILE__ ) ); $wgResourceModules['ext.JavaScript'] = array( 'scripts' => array(), 'styles' => array(), 'dependencies' => array( 'jquery' ), 'localBasePath' => dirname( __FILE__ ), 'remoteExtPath' => basename( dirname( __FILE__ ) ), 'position' => 'top' ); foreach( glob( dirname( __FILE__ ) . "/*.js" ) as $file ) { $wgResourceModules['ext.JavaScript']['scripts'][] = basename( $file ); } $wgHooks['BeforePageDisplay'][] = 'wfJavaScriptAddModules'; function wfJavaScriptAddModules( &$out, $skin = false ) { $out->addModules( 'ext.JavaScript' ); return true; } /** * Convert an internal resource path to an external one */ function wfJavaScriptExternalPath( $internalPath ) { global $wgScriptPath; return preg_replace( "|^.*/extensions|", "$wgScriptPath/extensions", $internalPath ); } ?>