-
Notifications
You must be signed in to change notification settings - Fork 1
/
JsonData.hooks.php
157 lines (146 loc) · 4.82 KB
/
JsonData.hooks.php
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
class JsonDataHooks {
/**
* BeforePageDisplay hook
* Adds the modules to the page
*
* @param $out OutputPage output page
* @param $skin Skin current skin
* @return Boolean: always true
*/
public static function beforePageDisplay( $out, $skin ) {
global $wgJsonData;
if ( !is_null( $wgJsonData ) ) {
$out->addModules( 'ext.jsonwidget' );
}
return true;
}
/**
* Load the JsonData object if we're in one of the configured namespaces
* @param EditPage $editPage
*/
public static function onEditPageShowEditFormInitial( &$editPage ) {
global $wgJsonData;
$title = $editPage->getTitle();
$ns = $title->getNamespace();
if ( JsonData::isJsonDataNeeded( $ns ) ) {
$wgJsonData = new JsonData( $title );
try {
$jsonref = $wgJsonData->getJsonRef();
$jsonref->validate();
}
catch ( JsonSchemaException $e ) {
// if the JSON is null, don't sweat an error, since that will
// frequently be the case for new pages
if( $e->subtype != 'validate-fail-null' || !$editPage->firsttime ) {
//TODO: clean up server error mechanism
$wgJsonData->servererror .= "<b>" .
wfMessage( 'jsondata-server-error' ) . "</b>: " .
htmlspecialchars( $e->getMessage() ) . "<br/>";
}
}
catch ( Exception $e ) {
$wgJsonData->servererror .= "<b>" .
wfMessage('jsondata-server-error') . "</b>: " .
htmlspecialchars( $e->getMessage() ) . "<br/>";
}
$wgJsonData->outputEditor( &$editPage );
}
return true;
}
/**
* Remove the edit toolbar from the form
*/
public static function onEditPageBeforeEditToolbar( &$toolbar )
{
$toolbar = '';
return false;
}
/**
* Register the configured parser tags with default tag renderer.
*/
public static function onParserFirstCallInit( Parser &$parser ) {
global $wgJsonDataDefaultTagHandlers;
foreach ( $wgJsonDataDefaultTagHandlers as $tag ) {
$parser->setHook( $tag, __CLASS__ . '::jsonTagRender' );
}
return true;
}
/**
* Default parser tag renderer
*/
public static function jsonTagRender( $input, array $args, Parser $parser, PPFrame $frame ) {
global $wgJsonData;
$wgJsonData = new JsonData( $frame->title );
$json = $input;
$goodschema = true;
try {
$schematext = $wgJsonData->getSchemaText();
}
catch ( JsonDataException $e ) {
$schematext = $wgJsonData->readJsonFromPredefined( 'openschema' );
wfDebug( __METHOD__ . ": " . htmlspecialchars( $e->getMessage() ) . "\n" );
$goodschema = false;
}
$schematitletext = $wgJsonData->getSchemaTitleText();
if ( $goodschema && !is_null( $schematitletext ) ) {
// Register dependency in templatelinks, using technique (and a
// little code) from http://www.mediawiki.org/wiki/Manual:Tag_extensions
$schematitle = Title::newFromText( $schematitletext );
$schemarev = Revision::newFromTitle( $schematitle );
$schemaid = $schemarev ? $schemarev->getPage() : 0;
$parser->mOutput->addTemplate( $schematitle, $schemaid,
$schemarev ? $schemarev->getId() : 0 );
}
$data = json_decode( $json, true );
$schema = json_decode( $schematext, true );
$rootjson = new JsonTreeRef( $data );
$rootjson->attachSchema( $schema );
$markup = JsonDataMarkup::getMarkup( $rootjson, 0 );
return $parser->recursiveTagParse( $markup, $frame );
}
public static function onGetPreferences( $user, &$preferences ) {
$preferences['jsondata-schemaedit'] = array(
'type' => 'toggle',
'label-message' => 'jsondata-schemaedit-pref',
'section' => 'misc/jsondata',
);
return true;
}
public static function validateDataEditFilter( $editor, $text, $section, &$error, $summary ) {
// I can't remember if jsondataobj needs to be a singleton/global, but
// will chance calling a new instance here.
$title = $editor->getTitle();
$ns = $title->getNamespace();
if ( !JsonData::isJsonDataNeeded( $ns ) ) {
return true;
}
$jsondataobj = new JsonData( $title );
$json = JsonData::stripOuterTagsFromText( $text );
try {
$schematext = $jsondataobj->getSchemaText();
}
catch ( JsonDataException $e ) {
$schematext = $jsondataobj->readJsonFromPredefined( 'openschema' );
$error = "<b>" . wfMessage( 'jsondata-servervalidationerror' ) . "</b>: ";
$error .= wfMessage( 'jsondata-invalidjson' );
}
$data = json_decode( $json, true );
if( is_null( $data ) ) {
$error = "<b>" . wfMessage( 'jsondata-servervalidationerror' ) . "</b>: ";
$error .= wfMessage( 'jsondata-invalidjson' );
return true;
}
$schema = json_decode( $schematext, true );
$rootjson = new JsonTreeRef( $data );
$rootjson->attachSchema( $schema );
try {
$rootjson->validate();
}
catch ( JsonSchemaException $e ) {
$error = "<b>" . wfMessage( 'jsondata-servervalidationerror' ) . "</b>: ";
$error .= htmlspecialchars( $e->getMessage() );
}
return true;
}
}