Yurik has uploaded a new change for review.

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

Change subject: Allow server-side service rendering
......................................................................

Allow server-side service rendering

Generates html to allow server-side PNG generation

Change-Id: I86ac640b2ae02247c718c7a794b011130d15ee94
---
M Graph.body.php
M Graph.php
2 files changed, 80 insertions(+), 35 deletions(-)


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

diff --git a/Graph.body.php b/Graph.body.php
index 9b89931..49aef25 100644
--- a/Graph.body.php
+++ b/Graph.body.php
@@ -12,7 +12,6 @@
 use FormatJson;
 use Html;
 use JsonConfig\JCContent;
-use JsonConfig\JCContentView;
 use JsonConfig\JCSingleton;
 use Parser;
 use ParserOptions;
@@ -47,7 +46,7 @@
                if ( $content->isValid() ) {
                        self::updateParser( $parser->getOutput() );
                }
-               return $content->getHtml();
+               return self::buildHtml( $content, $parser->getTitle(), 
$parser->getRevisionId() );
        }
 
        public static function updateParser( ParserOutput $parserOutput ) {
@@ -71,6 +70,61 @@
                }
                return true;
        }
+
+       /**
+        * @param Content $content
+        * @param string $title
+        * @param int $revid
+        * @return string
+        */
+       public static function buildHtml( $content, $title, $revid ) {
+
+               global $wgGraphImgServiceUrl;
+               static $hashIds = array();
+
+               if ( !$content->isValid() ) {
+                       return $content->getStatus();
+               }
+
+               // Everything is ok because Html::element() will do its own 
attrib encoding
+               $data = $content->getCompactJson();
+
+               $spanAttrs = array(
+                       'class' => 'mw-wiki-graph',
+                       'data-spec' => $data,
+               );
+
+               // ensure that the same ID is not used multiple times,
+               // e.g. identical graph is included multiple times
+               $id = 'mw-graph-' . sha1( $data );
+               if ( array_key_exists( $id, $hashIds ) ) {
+                       $hashIds[$id] += 1;
+                       $id = $id . '-' . $hashIds[$id];
+               } else {
+                       $hashIds[$id] = 1;
+               }
+               $spanAttrs['id'] = $id;
+
+               if ( $wgGraphImgServiceUrl ) {
+                       $title = rawurlencode( $title );
+                       $revid = rawurlencode( (string)$revid ) ?: '0';
+                       $url = sprintf( $wgGraphImgServiceUrl, $title, $revid, 
$id );
+
+                       // TODO: Use "width" and "height" from the definition 
if available
+                       // In some cases image might still be larger - need to 
investigate
+                       $img = Html::rawElement( 'img', array( 'src' => $url ) 
);
+
+                       $backendImgLinks =
+                               Html::inlineScript( 
'if(!mw.window){document.write(' .
+                                                                       
FormatJson::encode( $img, false, FormatJson::UTF8_OK ) .
+                                                                       ');}' ) 
.
+                               Html::rawElement( 'noscript', array(), $img );
+               } else {
+                       $backendImgLinks = '';
+               }
+
+               return Html::element( 'span', $spanAttrs ) . $backendImgLinks;
+       }
 }
 
 /**
@@ -92,37 +146,21 @@
                return $this->getHtml();
        }
 
-       public function getParserOutput( Title $title, $revId = null, 
ParserOptions $options = null,
-                                        $generateHtml = true ) {
-               return Singleton::updateParser( parent::getParserOutput( 
$title, $revId, $options, $generateHtml ) );
+       protected function fillParserOutput( Title $title, $revId, 
ParserOptions $options, $generateHtml,
+                                            ParserOutput &$output ) {
+
+               Singleton::updateParser( $output );
+
+               if ( $generateHtml ) {
+                       $html = Singleton::buildHtml( $this, $title, $revId );
+               } else {
+                       $html = '';
+               }
+
+               $output->setText( $html );
        }
 
-       protected function createDefaultView() {
-               return new ContentView();
-       }
-}
-
-class ContentView extends JCContentView {
-
-       /**
-        * Render JCContent object as HTML
-        * @param JCContent $content
-        * @return string
-        */
-       public function valueToHtml( JCContent $content ) {
-               return Html::element( 'div', array(
-                       'class' => 'mw-wiki-graph',
-                       'data-spec' => FormatJson::encode( $content->getData(), 
false, FormatJson::UTF8_OK ),
-               ) );
-       }
-
-       /**
-        * Returns default content for this object.
-        * The returned valued does not have to be valid JSON
-        * @param string $modelId
-        * @return string
-        */
-       public function getDefault( $modelId ) {
-               return '{}';
+       public function getCompactJson() {
+               return FormatJson::encode( $this->getData(), false, 
FormatJson::ALL_OK );
        }
 }
diff --git a/Graph.php b/Graph.php
index 81aa27e..de1084e 100644
--- a/Graph.php
+++ b/Graph.php
@@ -35,17 +35,24 @@
 unset( $graphBodyFile );
 
 /**
- * @var bool Set to true to enable <graph> tag in wiki markup
+ * @var bool $wgEnableGraphParserTag Set to true to enable <graph> tag in wiki 
markup
  */
 $wgEnableGraphParserTag = false;
 
-/**
- * @var false|string[] a list of domains that the vega code is allowed to pull 
data from.
+/** @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
  */
 $wgGraphDataDomains = array();
 
+/** @var string|false $wgGraphImgServiceUrl A format string to form a backend 
service request for the img.
+ * For example:  '/api/v1/pages/%1$s/graph/%2$s/%3$s.png'
+ * Parameters will be supplied in this order: title, revid, graph-hash-id
+ * All parameters will be escaped with rawurlencode()
+ * If the value is false, no <noscript> urls will be generated
+ */
+$wgGraphImgServiceUrl = false;
+
 $wgHooks['ParserFirstCallInit'][] = 'Graph\Singleton::onParserFirstCallInit';
 $wgHooks['EditPage::showEditForm:initial'][] = 
'Graph\Singleton::editPageShowEditFormInitial';
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I86ac640b2ae02247c718c7a794b011130d15ee94
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