jenkins-bot has submitted this change and it was merged.
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, 54 insertions(+), 57 deletions(-)
Approvals:
Krinkle: Looks good to me, approved
jenkins-bot: Verified
diff --git a/Graph.body.php b/Graph.body.php
index 4cefa99..acdd07d 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' );
+
+ $output->addJsConfigVars( 'wgGraphSpecs', $specs );
+ $output->setProperty( 'graph_specs',
+ FormatJson::encode( $specs, false,
FormatJson::ALL_OK ) );
+ }
}
/**
@@ -70,47 +77,33 @@
* @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();
$status = FormatJson::parse( $jsonText, FormatJson::TRY_FIXING
| FormatJson::STRIP_COMMENTS );
if ( !$status->isGood() ) {
return $status->getWikiText();
}
- $json = FormatJson::encode( $status->getValue(), false,
FormatJson::ALL_OK );
+ // Make sure that multiple json blobs that only differ in
spacing hash the same
+ $data = $status->getValue();
+ $hash = sha1( FormatJson::encode( $data, 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 );
- if ( array_key_exists( $id, $hashIds ) ) {
- $hashIds[$id] += 1;
- $id = $id . '-' . $hashIds[$id];
- } else {
- $hashIds[$id] = 1;
- }
- $spanAttrs['id'] = $id;
-
+ // Render fallback image rendering html (noscript and
old-script)
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 );
+ $url = sprintf( $wgGraphImgServiceUrl, $title, $revid,
$hash );
// 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(' .
+ Html::inlineScript(
'if(!window.mw){document.write(' .
FormatJson::encode( $img, false, FormatJson::UTF8_OK ) .
');}' )
.
Html::rawElement( 'noscript', array(), $img );
@@ -118,7 +111,14 @@
$backendImgLinks = '';
}
- return Html::element( 'span', $spanAttrs ) . $backendImgLinks;
+ $specs = $parserOutput->getExtensionData( 'graph_specs' ) ?:
array();
+ $specs[$hash] = $data;
+ $parserOutput->setExtensionData( 'graph_specs', $specs );
+
+ return Html::element( 'div', array(
+ 'class' => 'mw-wiki-graph',
+ 'data-graph-id' => $hash,
+ ) ) . $backendImgLinks;
}
}
@@ -138,14 +138,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,14 +146,12 @@
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 );
- public function getCompactJson() {
- return FormatJson::encode( $this->getData(), false,
FormatJson::ALL_OK );
+ // Since we invoke parser manually, the ParserAfterParse never
gets called, do it manually
+ Singleton::finalizeParserOutput( $output );
}
}
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: merged
Gerrit-Change-Id: If06f953ef349438d89a33e86e0fc51b3cdd80a73
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/Graph
Gerrit-Branch: master
Gerrit-Owner: Yurik <[email protected]>
Gerrit-Reviewer: Krinkle <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits