### Eclipse Workspace Patch 1.0 #P Spark Index: Spark.class.php =================================================================== --- Spark.class.php (revision 95984) +++ Spark.class.php (working copy) @@ -45,44 +45,83 @@ * * @return string */ - public function render( Parser $parser, PPFrame $frame ) { + public function render( Parser $parser, $frame ) { + global $wgVersion; + global $wgOut; + global $egSparkScriptPath; global $wgResourceModules; - - if ( array_key_exists( 'data-spark-query', $this->parameters ) ) { - $query = htmlspecialchars( $this->parameters['data-spark-query'] ); - + + // What is loaded already? + static $loadedJsses = array(); + + wfDebugLog( 'myextension', 'Parameters alright? ' . print_r($this->parameters, true) ); + if ( array_key_exists( egSparkQuery, $this->parameters ) ) { + $query = htmlspecialchars( $this->parameters[egSparkQuery] ); + // Before that, shall we allow internal parse, at least for the query? // We replace variables, templates etc. - $query = $parser->replaceVariables($query, $frame); - + if (!isset($frame)) { + $query = $parser->replaceVariables($query, null); + } else { + $query = $parser->replaceVariables($query, $frame); + } + // Replace special characters $query = str_replace( array( '<', '>' ), array( '<', '>' ), $query ); - - unset( $this->parameters['data-spark-query'] ); - + + unset( $this->parameters[egSparkQuery] ); + // Depending on the format, we possibly need to add modules - if ( array_key_exists( 'data-spark-format', $this->parameters ) ) { - $format = htmlspecialchars( $this->parameters['data-spark-format'] ); + if ( array_key_exists( egSparkFormat, $this->parameters ) ) { + $format = htmlspecialchars( $this->parameters[egSparkFormat] ); // Remove everything before "spark.XXX" $format = substr($format , strpos($format, "spark.")); // Remove .js at the end $format = str_replace( array( '.js' ), array( '' ), $format ); $module = 'ext.'.$format; - if ( array_key_exists($module, $wgResourceModules)) { - // TODO: Do we need to check, whether module has been added already? - $parser->getOutput()->addModules( $module ); + // for older versions of MW, different + if ( version_compare( $wgVersion, '1.17', '<' ) ) { + if (isset($wgResourceModules) && array_key_exists($module, $wgResourceModules)) { + // only if not already loaded + if (!isset($loadedJsses[$module])) { + // scripts + foreach ($wgResourceModules[$module]['scripts'] as $script) { + $wgOut->addScript(''); + wfDebugLog( 'spark', "AddScript:".' ' ); + } + + // css + foreach ($wgResourceModules[$module]['styles'] as $style) { + $wgOut->addScript(''); + wfDebugLog( 'spark', "AddLink:".' ' ); + } + $loadedJsses[$module] = true; + } + } + } else { + // $wgResourceModules might not exist + if (isset($wgResourceModules) && array_key_exists($module, $wgResourceModules)) { + // TODO: Do we need to check, whether module has been added already? + $parser->getOutput()->addModules( $module ); + } } } - + $html = '
parameters ) . ' >' . ( is_null( $this->contents ) ? '' : htmlspecialchars( $this->contents ) ) . '
'; // In MW 1.17 there seems to be the problem that ? after an empty space is replaced by a non-breaking space ( ) Therefore we remove all spaces before ? which should still make the SPARQL query work $html = preg_replace( '/[ \t]+(\?)/', '$1', $html ); - - return array( $parser->insertStripItem( $html, $parser->mStripState ), 'noparse' => true, 'isHTML' => true ); + + // for older versions of MW, different + if ( version_compare( $wgVersion, '1.17', '<' ) ) { + $parser->disableCache(); + return $html; + } else { + return array( $parser->insertStripItem( $html, $parser->mStripState ), 'noparse' => true, 'isHTML' => true ); + } } else { return Html::element( 'i', array(), wfMsg( 'spark-missing-query' ) ); @@ -101,9 +140,12 @@ protected function getSparkParameters( array $args ) { $parameters = array(); + // For lower versions of MW, special chars were not allowed in tags, therefore, we simply add them, then. foreach ( $args as $name => $value ) { if ( strpos( $name, 'data-spark-' ) === 0 ) { $parameters[$name] = $value; + } else { + $parameters['data-spark-'.$name] = $value; } } Index: Spark.php =================================================================== --- Spark.php (revision 95490) +++ Spark.php (working copy) @@ -2,7 +2,7 @@ /** * Initialization file for the Spark extension. - * + * * Documentation: http://www.mediawiki.org/wiki/Extension:Spark * Support http://www.mediawiki.org/wiki/Extension_talk:Spark * Source code: http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/Spark @@ -24,8 +24,9 @@ die( 'Not an entry point.' ); } -if ( version_compare( $wgVersion, '1.17', '<' ) ) { - die( 'Error: Spark requires MediaWiki 1.17 or above.' ); +// We also want to support versions below 1.17 +if ( version_compare( $wgVersion, '1.15', '<' ) ) { + die( 'Error: Spark requires MediaWiki 1.15 or above.' ); } define( 'Spark_VERSION', '0.2 alpha' ); @@ -36,27 +37,36 @@ 'version' => Spark_VERSION, 'author' => array( '[http://www.mediawiki.org/wiki/User:Jeroen_De_Dauw Jeroen De Dauw]', - ), +), 'url' => 'http://www.mediawiki.org/wiki/Extension:Spark', 'descriptionmsg' => 'spark-desc' ); -$egSparkScriptPath = ( $wgExtensionAssetsPath === false ? $wgScriptPath . '/extensions' : $wgExtensionAssetsPath ) . '/Spark'; +// $wgExtensionAssetsPath does possibly not exist. +$egSparkScriptPath = ( (!isset($wgExtensionAssetsPath) || $wgExtensionAssetsPath === false) ? $wgScriptPath . '/extensions' : $wgExtensionAssetsPath ) . '/Spark'; $wgExtensionMessagesFiles['Spark'] = dirname( __FILE__ ) . '/Spark.i18n.php'; $wgAutoloadClasses['SparkHooks'] = dirname( __FILE__ ) . '/Spark.hooks.php'; $wgAutoloadClasses['SparkTag'] = dirname( __FILE__ ) . '/Spark.class.php'; -$wgResourceModules['ext.spark'] = array( +if ( version_compare( $wgVersion, '1.17', '<' ) ) { + // We do not have resource loader + $egSparkScriptJquery = $egSparkScriptPath.'/rdf-spark/lib/jquery-1.4.4.js'; + $egSparkScriptJquerySpark = $egSparkScriptPath.'/rdf-spark/jquery.spark.js'; +} else { + // We have resource loader + $wgResourceModules['ext.spark'] = array( 'localBasePath' => dirname( __FILE__ ), 'remoteBasePath' => $egSparkScriptPath, 'styles' => array(), 'scripts' => array( 'rdf-spark/jquery.spark.js' ), 'dependencies' => array(), 'messages' => array() -); + ); +} + $wgHooks['ParserFirstCallInit'][] = 'SparkHooks::onParserFirstCallInit'; require_once 'Spark.settings.php'; \ No newline at end of file Index: Spark.settings.php =================================================================== --- Spark.settings.php (revision 95984) +++ Spark.settings.php (working copy) @@ -19,33 +19,37 @@ die( 'Not an entry point.' ); } +/// Parameters ///// +define("egSparkQuery", "data-spark-query"); +define("egSparkFormat", "data-spark-format"); + /* * Example configuration modules - -$wgResourceModules['ext.spark.oatpivot'] = array( - 'localBasePath' => "$IP/extensions/Spark/", - 'remoteBasePath' => $egSparkScriptPath, - 'styles' => array('rdf-spark/lib/oat/styles/pivot.css'), - 'scripts' => array( 'rdf-spark/lib/oat/loader.js', 'rdf-spark/lib/oat/bootstrap.js', 'rdf-spark/lib/oat/animation.js', 'rdf-spark/lib/oat/barchart.js', 'rdf-spark/lib/oat/ghostdrag.js', 'rdf-spark/lib/oat/instant.js', 'rdf-spark/lib/oat/pivot.js', 'rdf-spark/lib/oat/statistics.js' ), - 'dependencies' => array(), - 'messages' => array() -); -$wgResourceModules['ext.spark.datechart'] = array( - 'localBasePath' => "$IP/extensions/Spark/", - 'remoteBasePath' => $egSparkScriptPath, - 'styles' => array(), - 'scripts' => array( 'rdf-spark/lib/jquery.jqplot.js', 'rdf-spark/lib/jqplot.pieRenderer.js', 'rdf-spark/lib/jqplot.dateAxisRenderer.js', 'rdf-spark/lib/jqplot.categoryAxisRenderer.js' ), - 'dependencies' => array(), - 'messages' => array() -); + $wgResourceModules['ext.spark.oatpivot'] = array( + 'localBasePath' => "$IP/extensions/Spark/", + 'remoteBasePath' => $egSparkScriptPath, + 'styles' => array('rdf-spark/lib/oat/styles/pivot.css'), + 'scripts' => array( 'rdf-spark/lib/oat/loader.js', 'rdf-spark/lib/oat/bootstrap.js', 'rdf-spark/lib/oat/animation.js', 'rdf-spark/lib/oat/barchart.js', 'rdf-spark/lib/oat/ghostdrag.js', 'rdf-spark/lib/oat/instant.js', 'rdf-spark/lib/oat/pivot.js', 'rdf-spark/lib/oat/statistics.js' ), + 'dependencies' => array(), + 'messages' => array() + ); -$wgResourceModules['ext.spark.piechart'] = array( - 'localBasePath' => "$IP/extensions/Spark/", - 'remoteBasePath' => $egSparkScriptPath, - 'styles' => array(), - 'scripts' => array( 'rdf-spark/lib/jquery.jqplot.js', 'rdf-spark/lib/jqplot.pieRenderer.js', 'rdf-spark/lib/jqplot.dateAxisRenderer.js', 'rdf-spark/lib/jqplot.categoryAxisRenderer.js' ), - 'dependencies' => array(), - 'messages' => array() -); + $wgResourceModules['ext.spark.datechart'] = array( + 'localBasePath' => "$IP/extensions/Spark/", + 'remoteBasePath' => $egSparkScriptPath, + 'styles' => array(), + 'scripts' => array( 'rdf-spark/lib/jquery.jqplot.js', 'rdf-spark/lib/jqplot.dateAxisRenderer.js', 'rdf-spark/lib/jqplot.categoryAxisRenderer.js' ), + 'dependencies' => array(), + 'messages' => array() + ); + + $wgResourceModules['ext.spark.piechart'] = array( + 'localBasePath' => "$IP/extensions/Spark/", + 'remoteBasePath' => $egSparkScriptPath, + 'styles' => array(), + 'scripts' => array( 'rdf-spark/lib/jquery.jqplot.js', 'rdf-spark/lib/jqplot.pieRenderer.js', 'rdf-spark/lib/jqplot.dateAxisRenderer.js', 'rdf-spark/lib/jqplot.categoryAxisRenderer.js' ), + 'dependencies' => array(), + 'messages' => array() + ); */ \ No newline at end of file Index: Spark.hooks.php =================================================================== --- Spark.hooks.php (revision 95984) +++ Spark.hooks.php (working copy) @@ -2,49 +2,70 @@ /** * Static class for hooks handled by the Spark extension. - * + * * @since 0.1 - * + * * @file Spark.hooks.php * @ingroup Spark - * + * * @licence GNU GPL v3+ * @author Jeroen De Dauw < jeroendedauw@gmail.com > */ final class SparkHooks { - + /** * Register the spark tag extension when the parser initializes. - * + * * @since 0.1 - * + * * @param Parser $parser - * + * * @return true */ public static function onParserFirstCallInit( Parser &$parser ) { $parser->setHook( 'spark', __CLASS__ . '::onSparkRender' ); return true; } - + /** * @since 0.1 - * + * * @param mixed $input * @param array $args * @param Parser $parser * @param PPFrame $frame */ - public static function onSparkRender( $input, array $args, Parser $parser, PPFrame $frame ) { + public static function onSparkRender( $input, array $args, Parser $parser, $frame = null) { + global $wgVersion; + global $wgOut; + global $egSparkScriptJquery; + global $egSparkScriptJquerySpark; + static $loadedJs = false; - - if ( !$loadedJs ) { - $parser->getOutput()->addModules( 'ext.spark' ); - $loadedJs = true; + + if ( version_compare( $wgVersion, '1.17', '<' ) ) { + // We do not have resource loader + if ( !$loadedJs ) { + $wgOut->addScript(''); + wfDebugLog( 'spark', "AddScript:".' ' ); + $wgOut->addScript(''); + wfDebugLog( 'spark', "AddScript:".' ' ); + $loadedJs = true; + } + + } else { + // We have resource loader + // If we have resource loader + if ( !$loadedJs ) { + $parser->getOutput()->addModules( 'ext.spark' ); + $loadedJs = true; + } } - $tag = new SparkTag( $args, $input ); + + // PPFrame maybe not existing return $tag->render( $parser, $frame ); + } - + } \ No newline at end of file