http://www.mediawiki.org/wiki/Special:Code/MediaWiki/95984

Revision: 95984
Author:   bkaempgen
Date:     2011-09-01 13:44:26 +0000 (Thu, 01 Sep 2011)
Log Message:
-----------
==What Spark now does==
* It allows to have variables/templates etc in the data-spark-query. This is
useful, if the query shall depend on variables in the wiki page.
* It takes the data-spark-format and adds a module (simply the name of the
visualisation, prefixed with "ext.spark") to the page if defined in
Spark.settings.php or LocalSettings.php (e.g., ext.spark.datechart). This is
useful, if visualisations need additional java script files but those files
should only be loaded if the visualisation is used on a page.
* It adds some example modules to Spark.settings.php

Modified Paths:
--------------
    trunk/extensions/Spark/Spark.class.php
    trunk/extensions/Spark/Spark.hooks.php
    trunk/extensions/Spark/Spark.settings.php

Modified: trunk/extensions/Spark/Spark.class.php
===================================================================
--- trunk/extensions/Spark/Spark.class.php      2011-09-01 13:40:42 UTC (rev 
95983)
+++ trunk/extensions/Spark/Spark.class.php      2011-09-01 13:44:26 UTC (rev 
95984)
@@ -2,87 +2,112 @@
 
 /**
  * Class to render spark tags.
- * 
+ *
  * @since 0.1
- * 
+ *
  * @file Spark.class.php
  * @ingroup Spark
- * 
+ *
  * @licence GNU GPL v3+
  * @author Jeroen De Dauw < [email protected] >
  */
 final class SparkTag {
-       
+
        /**
         * List of spark parameters.
-        * 
+        *
         * @since 0.1
-        * 
+        *
         * @var array
         */
        protected $parameters;
-       
+
        /**
         * Constructor.
-        * 
+        *
         * @since 0.1
-        * 
+        *
         * @var string or null
         */
        protected $contents;
-       
+
        public function __construct( array $args, $contents ) {
                $this->parameters = $this->getSparkParameters( $args );
                $this->contents = $contents;
        }
-       
+
        /**
         * Renrder the spark div.
-        * 
+        *
         * @since 0.1
-        * 
+        *
         * @param Parser $parser
-        * 
+        *
         * @return string
         */
-       public function render( Parser $parser ) {
+       public function render( Parser $parser, PPFrame $frame  ) {
+               global $wgResourceModules;
+               
                if ( array_key_exists( 'data-spark-query', $this->parameters ) 
) {
                        $query = htmlspecialchars( 
$this->parameters['data-spark-query'] );
+                               
+                       // Before that, shall we allow internal parse, at least 
for the query?
+                       // We replace variables, templates etc.
+                       $query = $parser->replaceVariables($query, $frame);
+                               
+                       // Replace special characters
                        $query = str_replace( array( '&lt;', '&gt;' ), array( 
'<', '>' ), $query );
+                               
                        unset( $this->parameters['data-spark-query'] );
+                               
+                       // 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'] );
+                               // 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 );
 
+                               }
+                       }
+                       
                        $html = '<div class="spark" data-spark-query="' . 
$query . '" ' . Html::expandAttributes( $this->parameters ) . ' >' .
-                                               ( is_null( $this->contents ) ? 
'' : htmlspecialchars( $this->contents ) ) .
+                       ( is_null( $this->contents ) ? '' : htmlspecialchars( 
$this->contents ) ) .
                                        '</div>';
 
+                       // In MW 1.17 there seems to be the problem that ? 
after an empty space is replaced by a non-breaking space (&#160;) 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 );
                }
                else {
                        return Html::element( 'i', array(), wfMsg( 
'spark-missing-query' ) );
                }
        }
-       
+
        /**
         * Get the spark parameters from a list of key value pairs.
-        * 
+        *
         * @since 0.1
-        * 
+        *
         * @param array $args
-        * 
+        *
         * @return array
         */
        protected function getSparkParameters( array $args ) {
                $parameters = array();
-               
+
                foreach ( $args as $name => $value ) {
                        if ( strpos( $name, 'data-spark-' ) === 0 ) {
                                $parameters[$name] = $value;
                        }
                }
-               
+
                return $parameters;
        }
-       
+
 }
\ No newline at end of file

Modified: trunk/extensions/Spark/Spark.hooks.php
===================================================================
--- trunk/extensions/Spark/Spark.hooks.php      2011-09-01 13:40:42 UTC (rev 
95983)
+++ trunk/extensions/Spark/Spark.hooks.php      2011-09-01 13:44:26 UTC (rev 
95984)
@@ -44,7 +44,7 @@
                }
                
                $tag = new SparkTag( $args, $input );
-               return $tag->render( $parser );
+               return $tag->render( $parser, $frame );
        }
        
 }
\ No newline at end of file

Modified: trunk/extensions/Spark/Spark.settings.php
===================================================================
--- trunk/extensions/Spark/Spark.settings.php   2011-09-01 13:40:42 UTC (rev 
95983)
+++ trunk/extensions/Spark/Spark.settings.php   2011-09-01 13:44:26 UTC (rev 
95984)
@@ -19,3 +19,33 @@
        die( 'Not an entry point.' );
 }
 
+/*
+ * 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.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


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to