r79936 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r79935‎ | r79936 | r79937 >
Date:14:55, 10 January 2011
Author:yaron
Status:ok (Comments)
Tags:
Comment:
Added support for MediaWiki 1.17+ and ResourceLoader; added fix for page names that contain a single-quote/apostrophe
Modified paths:
  • /trunk/extensions/SemanticResultFormats/jqPlot/SRF_jqPlotBar.php (modified) (history)
  • /trunk/extensions/SemanticResultFormats/jqPlot/SRF_jqPlotPie.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticResultFormats/jqPlot/SRF_jqPlotPie.php
@@ -3,6 +3,7 @@
44 * A query printer for pie charts using the jqPlot JavaScript library.
55 *
66 * @author Sanyam Goyal
 7+ * @author Yaron Koren
78 */
89
910 if ( !defined( 'MEDIAWIKI' ) ) {
@@ -32,12 +33,53 @@
3334 return wfMsg( 'srf_printername_jqplotpie' );
3435 }
3536
36 - protected function getResultText( $res, $outputmode ) {
37 - global $smwgIQRunningNumber, $wgOut, $srfgScriptPath, $smwgScriptPath;
38 - global $wgParser;
 37+ public static function registerResourceModules() {
 38+ global $wgResourceModules, $srfgIP;
 39+
 40+ $resourceTemplate = array(
 41+ 'localBasePath' => $srfgIP . '/jqPlot',
 42+ 'remoteExtPath' => 'SemanticResultFormats'
 43+ );
 44+ $wgResourceModules['ext.srf.jqplot'] = $resourceTemplate + array(
 45+ 'scripts' => array(
 46+ 'jquery.jqplot.min.js',
 47+ ),
 48+ 'styles' => array(
 49+ 'jquery.jqplot.css',
 50+ ),
 51+ 'dependencies' => array(
 52+ 'jquery',
 53+ ),
 54+ );
 55+ $wgResourceModules['ext.srf.jqplotpie'] = $resourceTemplate + array(
 56+ 'scripts' => array(
 57+ 'jqplot.pieRenderer.min.js',
 58+ ),
 59+ 'styles' => array(
 60+ ),
 61+ 'dependencies' => array(
 62+ 'ext.srf.jqplot',
 63+ ),
 64+ );
 65+ }
 66+
 67+ protected function loadJavascriptAndCSS() {
 68+ global $wgOut;
 69+ $wgOut->addModules( 'ext.srf.jqplotpie' );
 70+ }
 71+
 72+ protected function addJavascriptAndCSS() {
 73+ if ( self::$m_piechartnum > 1 ) {
 74+ return;
 75+ }
 76+
 77+ // MW 1.17 +
 78+ if ( class_exists( 'ResourceLoader' ) ) {
 79+ self::loadJavascriptAndCSS();
 80+ return;
 81+ }
 82+ global $wgOut, $srfgScriptPath;
3983 global $smwgJQueryIncluded, $srfgJQPlotIncluded;
40 -
41 - $wgParser->disableCache();
4284
4385 if ( !$smwgJQueryIncluded ) {
4486 if ( method_exists( 'OutputPage', 'includeJQuery' ) ) {
@@ -54,12 +96,19 @@
5597 $wgOut->addScriptFile( "$srfgScriptPath/jqPlot/jquery.jqplot.min.js" );
5698 }
5799
58 - if ( self::$m_piechartnum == 1 ) {
59 - $wgOut->addScriptFile( "$srfgScriptPath/jqPlot/jqplot.pieRenderer.min.js" );
60 - }
 100+ $wgOut->addScriptFile( "$srfgScriptPath/jqPlot/jqplot.pieRenderer.min.js" );
61101
62102 // CSS file
63103 $wgOut->addExtensionStyle( "$srfgScriptPath/jqPlot/jquery.jqplot.css" );
 104+ }
 105+
 106+ protected function getResultText( $res, $outputmode ) {
 107+ global $wgOut, $wgParser;
 108+
 109+ $wgParser->disableCache();
 110+
 111+ self::addJavascriptAndCSS();
 112+
64113 $this->isHTML = true;
65114
66115 $t = "";
@@ -67,6 +116,7 @@
68117 // print all result rows
69118 while ( $row = $res->getNext() ) {
70119 $name = $row[0]->getNextObject()->getShortWikiText();
 120+ $name = str_replace( "'", "\'", $name );
71121 foreach ( $row as $field ) {
72122 while ( ( $object = $field->getNextObject() ) !== false ) {
73123 if ( $object->isNumeric() ) { // use numeric sortkey
@@ -75,12 +125,12 @@
76126 } else {
77127 $nr = $object->getNumericValue();
78128 }
79 - $jqplot_data[] .= "['$name', $nr]";
 129+ $pie_data[] .= "['$name', $nr]";
80130 }
81131 }
82132 }
83133 }
84 - $jqplot_data_str = "[[" . implode( ', ', $jqplot_data ) . "]]";
 134+ $pie_data_str = "[[" . implode( ', ', $pie_data ) . "]]";
85135 $pieID = 'pie' . self::$m_piechartnum;
86136 self::$m_piechartnum++;
87137
@@ -89,7 +139,7 @@
90140 jQuery.noConflict();
91141 jQuery(document).ready(function(){
92142 jQuery.jqplot.config.enablePlugins = true;
93 - plot1 = jQuery.jqplot('$pieID', $jqplot_data_str, {
 143+ plot1 = jQuery.jqplot('$pieID', $pie_data_str, {
94144 title: '$this->m_charttitle',
95145 seriesDefaults: {
96146 renderer: jQuery.jqplot.PieRenderer,
@@ -101,9 +151,8 @@
102152 });
103153 });
104154 </script>
105 -
106155 END;
107 - $wgOut->addScript($js_pie);
 156+ $wgOut->addScript( $js_pie );
108157
109158 $text =<<<END
110159 <div id="$pieID" style="margin-top: 20px; margin-left: 20px; width: {$this->m_width}px; height: {$this->m_height}px;"></div>
Index: trunk/extensions/SemanticResultFormats/jqPlot/SRF_jqPlotBar.php
@@ -52,13 +52,59 @@
5353 return wfMsg( 'srf_printername_jqplotbar' );
5454 }
5555
56 - protected function getResultText( $res, $outputmode ) {
57 - global $smwgIQRunningNumber, $wgOut, $srfgScriptPath;
58 - global $srfgJQPlotIncluded, $smwgJQueryIncluded;
59 - global $wgParser;
60 - $wgParser->disableCache();
 56+ public static function registerResourceModules() {
 57+ global $wgResourceModules, $srfgIP;
6158
62 - //adding scripts - this code may be moved to some other location
 59+ $resourceTemplate = array(
 60+ 'localBasePath' => $srfgIP . '/jqPlot',
 61+ 'remoteExtPath' => 'SemanticResultFormats'
 62+ );
 63+ $wgResourceModules['ext.srf.jqplot'] = $resourceTemplate + array(
 64+ 'scripts' => array(
 65+ 'jquery.jqplot.min.js',
 66+ ),
 67+ 'styles' => array(
 68+ 'jquery.jqplot.css',
 69+ ),
 70+ 'dependencies' => array(
 71+ 'jquery',
 72+ ),
 73+ );
 74+ $wgResourceModules['ext.srf.jqplotbar'] = $resourceTemplate + array(
 75+ 'scripts' => array(
 76+ 'jqplot.categoryAxisRenderer.min.js',
 77+ 'jqplot.barRenderer.min.js',
 78+ 'jqplot.canvasAxisTickRenderer.min.js',
 79+ 'jqplot.canvasTextRenderer.min.js',
 80+ ),
 81+ 'styles' => array(
 82+ ),
 83+ 'dependencies' => array(
 84+ 'ext.srf.jqplot',
 85+ ),
 86+ );
 87+ }
 88+
 89+ protected function loadJavascriptAndCSS() {
 90+ global $wgOut;
 91+ $wgOut->addModules( 'ext.srf.jqplot' );
 92+ $wgOut->addModules( 'ext.srf.jqplotbar' );
 93+ }
 94+
 95+ static public function addJavascriptAndCSS() {
 96+ if ( self::$m_barchartnum > 1 ) {
 97+ return;
 98+ }
 99+
 100+ // MW 1.17 +
 101+ if ( class_exists( 'ResourceLoader' ) ) {
 102+ self::loadJavascriptAndCSS();
 103+ return;
 104+ }
 105+
 106+ global $wgOut, $smwgJQueryIncluded, $srfgJQPlotIncluded;
 107+ global $srfgScriptPath;
 108+
63109 $scripts = array();
64110 if ( !$smwgJQueryIncluded ) {
65111 if ( method_exists( 'OutputPage', 'includeJQuery' ) ) {
@@ -75,12 +121,10 @@
76122 $srfgJQPlotIncluded = true;
77123 }
78124
79 - if ( self::$m_barchartnum == 1 ) {
80 - $scripts[] = "$srfgScriptPath/jqPlot/jqplot.categoryAxisRenderer.min.js";
81 - $scripts[] = "$srfgScriptPath/jqPlot/jqplot.barRenderer.min.js";
82 - $scripts[] = "$srfgScriptPath/jqPlot/jqplot.canvasAxisTickRenderer.min.js";
83 - $scripts[] = "$srfgScriptPath/jqPlot/jqplot.canvasTextRenderer.min.js";
84 - }
 125+ $scripts[] = "$srfgScriptPath/jqPlot/jqplot.categoryAxisRenderer.min.js";
 126+ $scripts[] = "$srfgScriptPath/jqPlot/jqplot.barRenderer.min.js";
 127+ $scripts[] = "$srfgScriptPath/jqPlot/jqplot.canvasAxisTickRenderer.min.js";
 128+ $scripts[] = "$srfgScriptPath/jqPlot/jqplot.canvasTextRenderer.min.js";
85129
86130 foreach ( $scripts as $script ) {
87131 $wgOut->addScriptFile( $script );
@@ -88,6 +132,15 @@
89133
90134 // CSS file
91135 $wgOut->addExtensionStyle( "$srfgScriptPath/jqPlot/jquery.jqplot.css" );
 136+ }
 137+
 138+ protected function getResultText( $res, $outputmode ) {
 139+ global $wgOut, $wgParser;
 140+
 141+ $wgParser->disableCache();
 142+
 143+ self::addJavascriptAndCSS();
 144+
92145 $this->isHTML = true;
93146
94147 $numbers = array();
@@ -98,6 +151,7 @@
99152 $min_number = 0;
100153 while ( $row = $res->getNext() ) {
101154 $name = $row[0]->getNextObject()->getShortWikiText();
 155+ $name = str_replace( "'", "\'", $name );
102156 foreach ( $row as $field ) {
103157 while ( ( $object = $field->getNextObject() ) !== false ) {
104158 if ( $object->isNumeric() ) { // use numeric sortkey
@@ -213,9 +267,8 @@
214268 });
215269 });
216270 </script>
217 -
218271 END;
219 - $wgOut->addScript($js_bar);
 272+ $wgOut->addScript( $js_bar );
220273 $text =<<<END
221274 <div id="$barID" style="margin-top: 20px; margin-left: 20px; width: {$this->m_width}px; height: {$this->m_height}px;"></div>
222275

Comments

#Comment by Jeroen De Dauw (talk | contribs)   15:32, 10 January 2011
  1. Awesomeness

Status & tagging log