forked from WikimapsAtlas/mediawiki-extensions-Graph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraph.body.php
More file actions
112 lines (97 loc) · 2.91 KB
/
Graph.body.php
File metadata and controls
112 lines (97 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/**
*
* @license MIT
* @file
*
* @author Dan Andreescu, Yuri Astrakhan
*/
namespace Graph;
use FormatJson;
use Html;
use JsonConfig\JCContent;
use JsonConfig\JCContentView;
use Parser;
use ParserOptions;
use ParserOutput;
use Title;
class Singleton {
public static function onParserFirstCallInit( Parser $parser ) {
global $wgEnableGraphParserTag;
if ( $wgEnableGraphParserTag ) {
$parser->setHook( 'graph', 'Graph\Singleton::onGraphTag' );
}
return true;
}
/**
* @param $input
* @param array $args
* @param Parser $parser
* @param \PPFrame $frame
* @return string
*/
public static function onGraphTag( $input, /** @noinspection PhpUnusedParameterInspection */
array $args, Parser $parser, \PPFrame $frame ) {
// expand template arguments and other wiki markup
// TODO: we might want to add some magic $args parameter to disable template expansion
$input = $parser->recursiveTagParse( $input, $frame );
$content = new Content( $input, 'graph-temp.json', true );
if ( $content->isValid() ) {
self::updateParser( $parser->getOutput() );
}
return $content->getHtml();
}
public static function updateParser( ParserOutput $parserOutput ) {
global $wgGraphDataDomains;
$parserOutput->addJsConfigVars( 'wgGraphDataDomains', $wgGraphDataDomains );
$parserOutput->addModules( 'ext.graph' );
return $parserOutput;
}
}
/**
* Class Content represents JSON content that Graph understands
* as the definition of a visualization.
*
* This is based on TextContent, and represents JSON as a string.
*
* TODO: determine if a different representation makes more sense and implement it with
* ContentHandler::serializeContent() and ContentHandler::unserializeContent()
*
* TODO: create a visual editor for Graph definitions that introspects what is allowed
* in each part of the definition and presents documentation to aid with discovery.
*
*/
class Content extends JCContent {
public function getWikitextForTransclusion() {
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 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 '{}';
}
}