Yurik has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/186331

Change subject: Switch to use page properties to store graph spec
......................................................................

Switch to use page properties to store graph spec

Thanks to Krinkle, who was looking over my shoulder

Change-Id: If06f953ef349438d89a33e86e0fc51b3cdd80a73
---
M Graph.body.php
M Graph.php
M js/graph.js
3 files changed, 52 insertions(+), 40 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Graph 
refs/changes/31/186331/1

diff --git a/Graph.body.php b/Graph.body.php
index 4cefa99..1362c30 100644
--- a/Graph.body.php
+++ b/Graph.body.php
@@ -21,10 +21,12 @@
 class Singleton {
 
        public static function onParserFirstCallInit( Parser $parser ) {
-               global $wgEnableGraphParserTag;
-               if ( $wgEnableGraphParserTag ) {
-                       $parser->setHook( 'graph', 
'Graph\Singleton::onGraphTag' );
-               }
+               $parser->setHook( 'graph', 'Graph\Singleton::onGraphTag' );
+               return true;
+       }
+
+       public static function onParserAfterParse( Parser $parser ) {
+               self::finalizeParserOutput( $parser->getOutput() );
                return true;
        }
 
@@ -37,18 +39,23 @@
         */
        public static function onGraphTag( $input, /** @noinspection 
PhpUnusedParameterInspection */
                                           array $args, Parser $parser, 
\PPFrame $frame ) {
-
                // expand template arguments and other wiki markup
                $input = $parser->recursivePreprocess( $input, $frame );
-               self::updateParserOutput( $parser->getOutput() );
-               return self::buildHtml( $input, $parser->getTitle(), 
$parser->getRevisionId() );
+               return self::buildHtml( $input, $parser->getTitle(), 
$parser->getRevisionId(),
+                       $parser->getOutput() );
        }
 
-       public static function updateParserOutput( ParserOutput $parserOutput ) 
{
-               global $wgGraphDataDomains;
-               $parserOutput->addJsConfigVars( 'wgGraphDataDomains', 
$wgGraphDataDomains );
-               $parserOutput->addModules( 'ext.graph' );
-               return $parserOutput;
+       public static function finalizeParserOutput( ParserOutput $output ) {
+               $specs = $output->getExtensionData( 'graph_specs' );
+               if ( $specs !== null ) {
+                       global $wgGraphDataDomains;
+                       $output->addJsConfigVars( 'wgGraphDataDomains', 
$wgGraphDataDomains );
+                       $output->addModules( 'ext.graph' );
+
+                       $json = FormatJson::encode( $specs, false, 
FormatJson::ALL_OK );
+                       $output->setProperty( 'graph_specs', $json );
+                       $output->addJsConfigVars( 'wgGraphSpecs', $specs );
+               }
        }
 
        /**
@@ -70,9 +77,10 @@
         * @param string $jsonText
         * @param Title $title
         * @param int $revid
+        * @param ParserOutput $parserOutput
         * @return string
         */
-       public static function buildHtml( $jsonText, $title, $revid ) {
+       public static function buildHtml( $jsonText, $title, $revid, 
$parserOutput ) {
 
                global $wgGraphImgServiceUrl;
                static $hashIds = array();
@@ -82,26 +90,25 @@
                        return $status->getWikiText();
                }
 
-               $json = FormatJson::encode( $status->getValue(), false, 
FormatJson::ALL_OK );
+//             $json = FormatJson::encode( $status->getValue(), false, 
FormatJson::ALL_OK );
 
-               $spanAttrs = array(
-                       'class' => 'mw-wiki-graph',
-                       'data-spec' => $json,
-               );
 
                // ensure that the same ID is not used multiple times,
                // e.g. identical graph is included multiple times
-               $id = 'mw-graph-' . sha1( $json );
+               $id = sha1( $jsonText );
                if ( array_key_exists( $id, $hashIds ) ) {
                        $hashIds[$id] += 1;
                        $id = $id . '-' . $hashIds[$id];
                } else {
                        $hashIds[$id] = 1;
                }
-               $spanAttrs['id'] = $id;
+               $spanAttrs = array(
+                       'class' => 'mw-wiki-graph',
+                       'data-graph-id' => $id,
+               );
 
                if ( $wgGraphImgServiceUrl ) {
-                       $title = !$title ? '' : rawurlencode( str_replace( 
$title->getText(), ' ', '_' ) );
+                       $title = !$title ? '' : rawurlencode( str_replace( ' ', 
'_', $title->getText() ) );
                        $revid = rawurlencode( (string)$revid ) ?: '0';
                        $url = sprintf( $wgGraphImgServiceUrl, $title, $revid, 
$id );
 
@@ -110,13 +117,17 @@
                        $img = Html::rawElement( 'img', array( 'src' => $url ) 
);
 
                        $backendImgLinks =
-                               Html::inlineScript( 
'if(!mw.window){document.write(' .
+                               Html::inlineScript( 
'if(!window.mw){document.write(' .
                                                                        
FormatJson::encode( $img, false, FormatJson::UTF8_OK ) .
                                                                        ');}' ) 
.
                                Html::rawElement( 'noscript', array(), $img );
                } else {
                        $backendImgLinks = '';
                }
+
+               $specs = $parserOutput->getExtensionData( 'graph_specs' ) ?: 
array();
+               $specs[$id] = $status->getValue();
+               $parserOutput->setExtensionData( 'graph_specs', $specs );
 
                return Html::element( 'span', $spanAttrs ) . $backendImgLinks;
        }
@@ -138,14 +149,7 @@
 class Content extends JCContent {
 
        public function getWikitextForTransclusion() {
-               //
-               // TODO: Somehow we need to avoid wgParser here
-               global $wgParser;
-               Singleton::updateParserOutput( $wgParser->getOutput() );
-               return Singleton::buildHtml(
-                       $this->getNativeData(),
-                       $wgParser->getTitle(),
-                       $wgParser->getRevisionId() );
+               return '<graph>' . $this->getNativeData() . '</graph>';
        }
 
        protected function fillParserOutput( Title $title, $revId, 
ParserOptions $options, $generateHtml,
@@ -153,11 +157,13 @@
                global $wgParser;
                $text = $this->getNativeData();
                $parser = $wgParser->getFreshParser();
-//             $output = $parser->parse( $text, $title, $options, true, true, 
$revId );
                $text = $parser->preprocess( $text, $title, $options, $revId );
 
-               Singleton::updateParserOutput( $output );
-               $output->setText( $generateHtml ? Singleton::buildHtml( $text, 
$title, $revId ) : '' );
+               $html = $generateHtml ? Singleton::buildHtml( $text, $title, 
$revId, $output ) : '';
+               $output->setText( $html );
+
+               // Since we invoke parser manually, the ParserAfterParse never 
gets called, do it manually
+               Singleton::finalizeParserOutput( $output );
        }
 
        public function getCompactJson() {
diff --git a/Graph.php b/Graph.php
index e2411a2..0ccc2ea 100644
--- a/Graph.php
+++ b/Graph.php
@@ -33,11 +33,6 @@
 $wgAutoloadClasses['Graph\Content'] = $graphBodyFile;
 unset( $graphBodyFile );
 
-/**
- * @var bool $wgEnableGraphParserTag Set to true to enable <graph> tag in wiki 
markup
- */
-$wgEnableGraphParserTag = false;
-
 /** @var false|string[] $wgGraphDataDomains a list of domains that the vega 
code is allowed to pull data from.
  * If false, there are no restrictions. An empty list disables any external 
data (inline only).
  * NOTE: Setting this value to anything other than 'false' will also enable 
safe mode formula/filter evaluation
@@ -54,6 +49,7 @@
 
 $wgHooks['ParserFirstCallInit'][] = 'Graph\Singleton::onParserFirstCallInit';
 $wgHooks['EditPage::showEditForm:initial'][] = 
'Graph\Singleton::editPageShowEditFormInitial';
+$wgHooks['ParserAfterParse'][] = 'Graph\Singleton::onParserAfterParse';
 
 $extGraphBoilerplate = array(
     'localBasePath' => __DIR__,
diff --git a/js/graph.js b/js/graph.js
index 4bf7f5c..38b4889 100644
--- a/js/graph.js
+++ b/js/graph.js
@@ -1,11 +1,21 @@
 ( function( $ ) {
        $( function() {
+               var specs = mw.config.get('wgGraphSpecs');
+               if (!specs) {
+                       return;
+               }
                vg.config.domainWhiteList = mw.config.get('wgGraphDataDomains');
                vg.config.safeMode = vg.config.domainWhiteList !== false;
                $('.mw-wiki-graph').each(function () {
-                       var definition = $(this).data('spec'),
+                       var graphId = $(this).data('graph-id'),
                                el = this;
-                       vg.parse.spec(definition, function(chart) { 
chart({el:el}).update(); });
+                       if (!specs[graphId]) {
+                               mw.log.warn(graphId);
+                       } else {
+                               vg.parse.spec(specs[graphId], function (chart) {
+                                       chart({el: el}).update();
+                               });
+                       }
                });
        });
 } ( jQuery ) );

-- 
To view, visit https://gerrit.wikimedia.org/r/186331
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If06f953ef349438d89a33e86e0fc51b3cdd80a73
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Graph
Gerrit-Branch: master
Gerrit-Owner: Yurik <[email protected]>

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

Reply via email to