Extension talk:Javascript

From MediaWiki.org
Jump to: navigation, search

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)

[edit] Advantage over MediaWiki:Common.js is not clear

Could 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)

Personal tools
Namespaces

Variants
Actions
Navigation
Support
Download
Development
Communication
Print/export
Toolbox