-
Notifications
You must be signed in to change notification settings - Fork 5
/
Tilesheets.hooks.php
180 lines (160 loc) · 4.87 KB
/
Tilesheets.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
<?php
use MediaWiki\MediaWikiServices;
use OreDict\Hook\OreDictOutputHook;
use MediaWiki\Hook\BeforePageDisplayHook;
use MediaWiki\Hook\EditPage__showEditForm_initialHook;
use MediaWiki\Hook\ParserFirstCallInitHook;
/**
* Tilesheets hooks file
* Entrance points to the tilesheets extension
*
* @file
* @ingroup Extensions
* @version 1.1.1
* @author Jinbobo <[email protected]>
* @license
*/
class TilesheetsHooks implements ParserFirstCallInitHook, BeforePageDisplayHook, EditPage__showEditForm_initialHook, OreDictOutputHook {
static private $mOreDictMainErrorOutputted = false;
/**
* Entry point for parser functions.
*
* @param Parser $parser
* @return bool
*/
public function onParserFirstCallInit($parser) {
$parser->setFunctionHook('icon', 'TilesheetsHooks::RenderParser');
$parser->setFunctionHook('iconloc', 'TilesheetsHooks::IconLocalization');
return true;
}
/**
* Handler for BeforePageDisplay hook.
* @see http://www.mediawiki.org/wiki/Manual:Hooks/BeforePageDisplay
* @param $out OutputPage object
* @param $skin Skin being used.
* @return void
*/
public function onBeforePageDisplay($out, $skin): void {
// Load default styling module
$out->addModuleStyles('ext.tilesheets');
}
/**
* Generate parser function output. Called by #icon parser function, see #onParserFirstCallInit
*
* @param Parser $parser
* @return array Raw HTML ready for display, will not be parsed again by parser.
*/
public static function RenderParser(Parser $parser) {
// Extract options
$opts = array();
for ($i = 1; $i < func_num_args(); $i++) {
$opts[] = func_get_arg($i);
}
$options = self::ExtractOptions($opts);
// Run main class and output
$tile = new Tilesheets($options, $parser);
return $tile->output($parser);
}
/**
* Gets the localized name or description for the given item/mod. Called by iconloc parser function. See #onParserFirstCallInit
* @param Parser $parser
* @param string $item The item's name.
* @param string $mod The mod abbreviation.
* @param string $type Either 'name' or 'description'. The type of data to get.
* @param string $language The language code. Falls back to 'en'.
* @return string The localized content, or the provided item's name as fall back.
*/
public static function IconLocalization(Parser $parser, $item, $mod, $type = 'name', $language = 'en') {
$dbr = MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection(DB_REPLICA);
$resultItem = $dbr->newSelectQueryBuilder()
->select('entry_id')
->from('ext_tilesheet_items')
->where(array('item_name' => $item, 'mod_name' => $mod))
->fetchRow();
if (!$resultItem) {
return $type == 'name' ? $item : '';
}
$loc = $dbr->newSelectQueryBuilder()
->select('*')
->from('ext_tilesheet_languages')
->where(array(
'entry_id' => $resultItem->entry_id,
'lang' => $language
))
->fetchRow();
if (!$loc) {
return $type == 'name' ? $item : '';
}
if ($type == 'name') {
$name = $loc->display_name;
return empty($name) ? $item : $name;
} else if ($type == 'description') {
return $loc->description;
} else {
return $item;
}
}
/**
* Helper function to extract options from raw parser function input.
*
* @param $opts
* @return array
*/
public static function ExtractOptions($opts) {
foreach ($opts as $option) {
$pair = explode('=', $option);
if (count($pair) == 2) {
if (!empty($pair[1])) {
$name = trim($pair[0]);
$value = trim($pair[1]);
$results[$name] = $value;
}
}
}
if (!isset($results)) {
$results = array();
}
return $results;
}
/**
* Entry point for the EditPage::showEditForm:initial hook, allows the tilesheet 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 $wgTileSheetDebug;
// Output errors
$errors = new TilesheetsError($wgTileSheetDebug);
$editPage->editFormTextAfterWarn .= $errors->output();
return true;
}
/**
* Entry point for the OreDictOutput hook.
*
* @param string $out
* @param array $items
* @param string $params
* @return bool
*/
public function onOreDictOutput(&$out, $items, $params) {
if (!self::$mOreDictMainErrorOutputted) {
TilesheetsError::notice(wfMessage('tilesheets-notice-oredict')->text());
self::$mOreDictMainErrorOutputted = true;
}
foreach ($items as $item) {
if (is_object($item) && get_class($item) == 'OreDictItem') {
$item->joinParams($params, true);
$templateParams = $item->getParamString();
$out .= "{{G/Cell|$templateParams}}";
$itemNames[] = $item->getItemName();
}
}
if (isset($itemNames)) {
$itemNames = implode(",", $itemNames);
TilesheetsError::notice(wfMessage('tilesheets-notice-return')->params($itemNames)->text());
}
return true;
}
}