r95984 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r95983‎ | r95984 | r95985 >
Date:13:44, 1 September 2011
Author:bkaempgen
Status:deferred
Tags:
Comment:
==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 (modified) (history)
  • /trunk/extensions/Spark/Spark.hooks.php (modified) (history)
  • /trunk/extensions/Spark/Spark.settings.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Spark/Spark.settings.php
@@ -19,3 +19,33 @@
2020 die( 'Not an entry point.' );
2121 }
2222
 23+/*
 24+ * Example configuration modules
 25+
 26+$wgResourceModules['ext.spark.oatpivot'] = array(
 27+ 'localBasePath' => "$IP/extensions/Spark/",
 28+ 'remoteBasePath' => $egSparkScriptPath,
 29+ 'styles' => array('rdf-spark/lib/oat/styles/pivot.css'),
 30+ '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' ),
 31+ 'dependencies' => array(),
 32+ 'messages' => array()
 33+);
 34+
 35+$wgResourceModules['ext.spark.datechart'] = array(
 36+ 'localBasePath' => "$IP/extensions/Spark/",
 37+ 'remoteBasePath' => $egSparkScriptPath,
 38+ 'styles' => array(),
 39+ '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' ),
 40+ 'dependencies' => array(),
 41+ 'messages' => array()
 42+);
 43+
 44+$wgResourceModules['ext.spark.piechart'] = array(
 45+ 'localBasePath' => "$IP/extensions/Spark/",
 46+ 'remoteBasePath' => $egSparkScriptPath,
 47+ 'styles' => array(),
 48+ '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' ),
 49+ 'dependencies' => array(),
 50+ 'messages' => array()
 51+);
 52+ */
\ No newline at end of file
Index: trunk/extensions/Spark/Spark.hooks.php
@@ -44,7 +44,7 @@
4545 }
4646
4747 $tag = new SparkTag( $args, $input );
48 - return $tag->render( $parser );
 48+ return $tag->render( $parser, $frame );
4949 }
5050
5151 }
\ No newline at end of file
Index: trunk/extensions/Spark/Spark.class.php
@@ -2,87 +2,112 @@
33
44 /**
55 * Class to render spark tags.
6 - *
 6+ *
77 * @since 0.1
8 - *
 8+ *
99 * @file Spark.class.php
1010 * @ingroup Spark
11 - *
 11+ *
1212 * @licence GNU GPL v3+
1313 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
1414 */
1515 final class SparkTag {
16 -
 16+
1717 /**
1818 * List of spark parameters.
19 - *
 19+ *
2020 * @since 0.1
21 - *
 21+ *
2222 * @var array
2323 */
2424 protected $parameters;
25 -
 25+
2626 /**
2727 * Constructor.
28 - *
 28+ *
2929 * @since 0.1
30 - *
 30+ *
3131 * @var string or null
3232 */
3333 protected $contents;
34 -
 34+
3535 public function __construct( array $args, $contents ) {
3636 $this->parameters = $this->getSparkParameters( $args );
3737 $this->contents = $contents;
3838 }
39 -
 39+
4040 /**
4141 * Renrder the spark div.
42 - *
 42+ *
4343 * @since 0.1
44 - *
 44+ *
4545 * @param Parser $parser
46 - *
 46+ *
4747 * @return string
4848 */
49 - public function render( Parser $parser ) {
 49+ public function render( Parser $parser, PPFrame $frame ) {
 50+ global $wgResourceModules;
 51+
5052 if ( array_key_exists( 'data-spark-query', $this->parameters ) ) {
5153 $query = htmlspecialchars( $this->parameters['data-spark-query'] );
 54+
 55+ // Before that, shall we allow internal parse, at least for the query?
 56+ // We replace variables, templates etc.
 57+ $query = $parser->replaceVariables($query, $frame);
 58+
 59+ // Replace special characters
5260 $query = str_replace( array( '&lt;', '&gt;' ), array( '<', '>' ), $query );
 61+
5362 unset( $this->parameters['data-spark-query'] );
 63+
 64+ // Depending on the format, we possibly need to add modules
 65+ if ( array_key_exists( 'data-spark-format', $this->parameters ) ) {
 66+ $format = htmlspecialchars( $this->parameters['data-spark-format'] );
 67+ // Remove everything before "spark.XXX"
 68+ $format = substr($format , strpos($format, "spark."));
 69+ // Remove .js at the end
 70+ $format = str_replace( array( '.js' ), array( '' ), $format );
 71+ $module = 'ext.'.$format;
 72+ if ( array_key_exists($module, $wgResourceModules)) {
 73+ // TODO: Do we need to check, whether module has been added already?
 74+ $parser->getOutput()->addModules( $module );
5475
 76+ }
 77+ }
 78+
5579 $html = '<div class="spark" data-spark-query="' . $query . '" ' . Html::expandAttributes( $this->parameters ) . ' >' .
56 - ( is_null( $this->contents ) ? '' : htmlspecialchars( $this->contents ) ) .
 80+ ( is_null( $this->contents ) ? '' : htmlspecialchars( $this->contents ) ) .
5781 '</div>';
5882
 83+ // 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
5984 $html = preg_replace( '/[ \t]+(\?)/', '$1', $html );
60 -
 85+
6186 return array( $parser->insertStripItem( $html, $parser->mStripState ), 'noparse' => true, 'isHTML' => true );
6287 }
6388 else {
6489 return Html::element( 'i', array(), wfMsg( 'spark-missing-query' ) );
6590 }
6691 }
67 -
 92+
6893 /**
6994 * Get the spark parameters from a list of key value pairs.
70 - *
 95+ *
7196 * @since 0.1
72 - *
 97+ *
7398 * @param array $args
74 - *
 99+ *
75100 * @return array
76101 */
77102 protected function getSparkParameters( array $args ) {
78103 $parameters = array();
79 -
 104+
80105 foreach ( $args as $name => $value ) {
81106 if ( strpos( $name, 'data-spark-' ) === 0 ) {
82107 $parameters[$name] = $value;
83108 }
84109 }
85 -
 110+
86111 return $parameters;
87112 }
88 -
 113+
89114 }
\ No newline at end of file

Status & tagging log