-
Notifications
You must be signed in to change notification settings - Fork 5
/
OreDict.hooks.php
executable file
·205 lines (183 loc) · 4.87 KB
/
OreDict.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php
use MediaWiki\Hook\ParserFirstCallInitHook;
use MediaWiki\Hook\EditPage__showEditForm_initialHook;
/**
* OreDict hooks file
* Defines entry points to the extension
*
* @file
* @ingroup Extensions
* @version 1.0.1
* @author Jinbobo <paullee05149745@gmail.com>
* @license
*/
class OreDictHooks implements ParserFirstCallInitHook, EditPage__showEditForm_initialHook {
/**
* Entry point for parser functions.
*
* @param Parser $parser
* @return bool
*/
public function onParserFirstCallInit($parser) {
$parser->setFunctionHook('dict', 'OreDictHooks::RenderParser');
$parser->setFunctionHook('grid_foreach', 'OreDictHooks::RenderMultiple');
return true;
}
/**
* Generate grids from a string. Called by the #grid_foreach parser function (see onParserFirstCallInit).
*
* @param Parser $parser
* @return array|string
*/
public static function RenderMultiple(Parser $parser) {
$opts = array();
for ($i = 1; $i < func_num_args(); $i++) {
$opts[] = func_get_arg($i);
}
// Check if input is in the correct format
foreach ($opts as $opt) {
if (strpos("{{", $opt) !== false || strpos("}}", $opt) !== false) {
OreDictError::error(wfMessage('oredict-grid_foreach-format-error')->text());
return "";
}
}
// Check if separated by commas
if (strpos($opts[0], ',') !== false) {
$opts = explode(',', $opts[0]);
}
// Check for global parameters
$gParams = array();
foreach ($opts as $option) {
$pair = explode('=>', $option);
if (count($pair) == 2) {
$gParams[trim($pair[0])] = trim($pair[1]);
}
}
// Prepare items
$items = array();
foreach ($opts as $option) {
if (strpos($option, '=>') === false) {
// Pre-load global params
$items[] = $gParams;
end($items);
$iKey = key($items);
// Parse string
$gridOptions = explode('!', $option);
foreach ($gridOptions as $key => $gridOption) {
$pair = explode('=', $gridOption);
if (count($pair) == 2) {
$gridOptions[trim($pair[0])] = trim($pair[1]);
$items[$iKey][trim($pair[0])] = trim($pair[1]);
} else {
$items[$iKey][$key + 1] = trim($gridOption);
}
}
}
}
// Create grids
$outs = array();
foreach ($items as $options) {
// Set mod
$mod = '';
if (isset($options['mod'])) {
$mod = $options['mod'];
}
// Call OreDict
$dict = new OreDict($options[1], $mod);
$dict->exec(isset($options['tag']), isset($options['no-fallback']));
$outs[] = $dict->runHooks(self::BuildParamString($options));
}
$ret = "";
foreach ($outs as $out) {
if (!isset($out[0])) {
continue;
}
$ret .= $out[0];
}
// Return output
return array($ret, 'noparse' => false, 'isHTML' => false);
}
/**
* Query OreDict and return output. Called by the #dict parser function (see onParserFirstCallInit).
*
* @param Parser $parser
* @return array
*/
public static function RenderParser(Parser $parser) {
$opts = array();
for ($i = 1; $i < func_num_args(); $i++) {
$opts[] = func_get_arg($i);
}
$options = OreDictHooks::ExtractOptions($opts);
// Set mod
$mod = '';
if (isset($options['mod'])) {
$mod = $options['mod'];
}
// Call OreDict
$dict = new OreDict($options[1], $mod);
$dict->exec(isset($options['tag']), isset($options['no-fallback']));
return $dict->runHooks(self::BuildParamString($options));
}
/**
* Helper function to extract options from raw parser function input.
*
* @param array $opts
* @return array|bool
*/
public static function ExtractOptions($opts) {
if (count($opts) == 0) return array();
foreach ($opts as $key => $option) {
$pair = explode('=', $option);
if (count($pair) == 2) {
$name = trim($pair[0]);
$value = trim($pair[1]);
$results[$name] = $value;
} else {
$results[$key + 1] = trim($option);
}
}
return isset($results) ? $results : false;
}
/**
* Helper function to split a parameter string into an array.
*
* @param string $params
* @return array
*/
public static function ParseParamString($params) {
if ($params === "") {
return [];
}
return OreDictHooks::ExtractOptions(explode('|', $params));
}
/**
* Helper function to rebuild a parameter string from an array.
*
* @param array $params
* @return string
*/
public static function BuildParamString($params) {
foreach ($params as $key => $value) {
$pairs[] = "$key=$value";
}
if (!isset($pairs)) {
return "";
}
return implode("|", $pairs);
}
/**
* Entry point for the EditPage::showEditForm:initial hook, allows the oredict extension to modify the edit form. Displays errors on preview.
*
* @param EditPage $editPage
* @param OutputPage $out
* @return bool
*/
public function onEditPage__showEditForm_initial($editPage, $out) {
global $wgOreDictDebug;
// Output errors
$errors = new OreDictError($wgOreDictDebug);
$editPage->editFormTextAfterWarn .= $errors->output();
return true;
}
}