-
Notifications
You must be signed in to change notification settings - Fork 5
/
Tilesheets.body.php
355 lines (318 loc) · 10.9 KB
/
Tilesheets.body.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
use Wikimedia\Rdbms\ILoadBalancer;
use MediaWiki\MediaWikiServices;
/**
* Tilesheets main body file
*
* @file
* @ingroup Extensions
* @version 1.1.1
* @author Jinbobo <[email protected]>
* @license
*/
class Tilesheets {
static private $mQueriedItems;
static private $mQueriedSizes;
static public $tileLinks;
private $mOptions;
/**
* Prepare the tile for outputting, retrieve stuff from database if not already retrieved
*
* @param $options
*/
public function __construct($options, Parser $parser) {
$this->mOptions = $options;
// Set default values
$item = $options['item'];
$size = 32;
$mod = "undefined";
// Retrieve parser function parameters
if (!isset($options['item'])) return;
if (isset($options['size'])) $size = $options['size'];
if (isset($options['mod'])) $mod = $options['mod'];
TilesheetsError::log(wfMessage('tilesheets-log-prepare')->params($size, $item, $mod)->text());
$dbr = MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection(DB_REPLICA);
if (!isset(self::$mQueriedItems[$item])) {
$results = $dbr->newSelectQueryBuilder()
->select('*')
->from('ext_tilesheet_items')
->where(array('item_name' => $item))
->fetchResultSet();
if ($results === false || $results->numRows() == 0) {
self::$mQueriedItems[$item] = null;
} else {
// Build table
foreach ($results as $result) {
self::$mQueriedItems[$item][$result->mod_name] = $result;
}
}
}
}
/**
* Output tile
*
* @return array|string
*/
public function output(Parser $parser) {
// Set default values
$item = $this->mOptions['item'];
$size = 32;
$mod = "undefined";
// Retrieve parser function parameters
if (!isset($this->mOptions['item'])) return "";
if (isset($this->mOptions['size'])) $size = $this->mOptions['size'];
if (isset($this->mOptions['mod'])) $mod = $this->mOptions['mod'];
TilesheetsError::log(wfMessage('tilesheets-log-output')->params($size, $item, $mod)->text());
if (self::$mQueriedItems[$item] == null) {
$parser->addTrackingCategory('tilesheet-missing-item-category');
if ($mod == "undefined") {
$parser->addTrackingCategory('tilesheet-no-mod-provided-category');
}
TilesheetsError::error(wfMessage('tilesheets-error-missingitem')->params($item)->text());
return $this->errorTile($size);
}
if ($mod != "undefined") {
if (!isset(self::$mQueriedItems[$item][$mod])) {
TilesheetsError::error(wfMessage('tilesheets-error-missingitemmod')->params($item, $mod)->text());
return $this->errorTile($size);
} else {
$x = self::$mQueriedItems[$item][$mod]->x;
$y = self::$mQueriedItems[$item][$mod]->y;
$z = self::$mQueriedItems[$item][$mod]->z;
return $this->generateTile($parser, $mod, $size, $x, $y, $z, self::$mQueriedItems[$item][$mod]->entry_id);
}
} else {
if (count(self::$mQueriedItems[$item]) == 1) {
$x = current(self::$mQueriedItems[$item])->x;
$y = current(self::$mQueriedItems[$item])->y;
$z = current(self::$mQueriedItems[$item])->z;
$mod = current(self::$mQueriedItems[$item])->mod_name;
$parser->addTrackingCategory('tilesheet-no-mod-provided-easy-category');
TilesheetsError::warn(wfMessage('tilesheets-warning-nomodparam')->params($item, $mod)->text());
return $this->generateTile($parser, $mod, $size, $x, $y, $z, current(self::$mQueriedItems[$item])->entry_id);
} else {
$parser->addTrackingCategory('tilesheet-no-mod-provided-category');
TilesheetsError::error(wfMessage('tilesheets-error-multiple')->params($item)->text());
return $this->errorTile($size);
}
}
}
/**
* Generate a tile from the parameters give, will also check if the provided size is valid.
*
* @param $mod
* @param $size
* @param $x
* @param $y
* @param $z
* @param $entryID
* @return array
*/
private function generateTile(Parser $parser, $mod, $size, $x, $y, $z, $entryID) {
// Validate tilesheet size
Tilesheets::getModTileSizes($mod);
if (self::$mQueriedSizes[$mod] == null) {
$parser->addTrackingCategory('tilesheet-invalid-sheet-category');
TilesheetsError::error(wfMessage('tilesheets-error-undefmod')->params($mod)->text());
return $this->errorTile($size);
} else {
if (!in_array($size, self::$mQueriedSizes[$mod])) {
$parser->addTrackingCategory('tilesheet-invalid-size-category');
TilesheetsError::warn(wfMessage('tilesheets-warning-nosize')->params($size, $mod)->text());
$size = min(self::$mQueriedSizes[$mod]);
}
}
// New pages do not have an article ID, so we have to store it in the title and then get the ID when updating the db
$pageRef = $parser->getPage();
self::$tileLinks[$pageRef->getNamespace()][$pageRef->getText()][] = $entryID;
$file = MediaWikiServices::getInstance()->getRepoGroup()->findFile("Tilesheet $mod $size $z.png");
if ($file === false) {
$parser->addTrackingCategory('tilesheet-missing-image-category');
TilesheetsError::warn(wfMessage('tilesheets-warning-noimage')->params($mod, $size, $z)->text());
return $this->errorTile($size);
}
$url = $file->getFullUrl();
$x *= $size;
$y *= $size;
return array("<span class=\"tilesheet\" style=\"background:url($url) -{$x}px -{$y}px;width:{$size}px;height:{$size}px\"><br></span>", 'noparse' => true, 'isHTML' => true);
}
/**
* Get registered tilesheet sizes for the provided mod
*
* @param string $mod Mod to search
* @return mixed
*/
public static function getModTileSizes($mod) {
$dbr = MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection(DB_REPLICA);
if (!isset(self::$mQueriedSizes[$mod])) {
$result = $dbr->newSelectQueryBuilder()
->select('sizes')
->from('ext_tilesheet_images')
->where(array("`mod`" => $mod))
->fetchResultSet();
if ($result == false) {
self::$mQueriedSizes[$mod] = null;
} else {
self::$mQueriedSizes[$mod] = explode(",", $result->current()->sizes);
}
}
return self::$mQueriedSizes[$mod];
}
/**
* Generate a red semi-tranparent tile denoting an error.
*
* @param int $size
* @return array
*/
private function errorTile($size = 32) {
return array("<span class=\"tilesheet\" style=\"width:{$size}px;height:{$size}px\"><br></span>", 'noparse' => true, 'isHTML' => true);
}
public static function buildDiffString($diff) {
$diffString = "";
foreach ($diff as $field => $change) {
$diffString .= "$field [$change[0] -> $change[1]] ";
}
return $diffString;
}
/**
* Updates the tilesheet row.
* @param string $curMod The current mod abbreviation.
* @param string $toMod The new mod abbreviation.
* @param string $toSizes The new sizes, separated by commas.
* @param User $user The user performing the change.
* @param ILoadBalancer $dbLoadBalancer The DBLoadBalancer service.
* @param string $comment The edit summary.
* @return bool Whether or not the edit was successful.
*/
public static function updateSheetRow($curMod, $toMod, $toSizes, $user, ILoadBalancer $dbLoadBalancer, $comment = '') {
$dbw = $dbLoadBalancer->getConnection(DB_PRIMARY);
$stuff = $dbw->newSelectQueryBuilder()
->select('*')
->from('ext_tilesheet_images')
->where(array('`mod`' => $curMod))
->fetchResultSet();
try {
$dbw->newUpdateQueryBuilder()
->update('ext_tilesheet_images')
->set(array('sizes' => $toSizes, '`mod`' => $toMod))
->where(array('`mod`' => $curMod))
->execute();
} catch (Exception $e) {
return false;
}
if ($stuff->numRows() == 0) {
return false;
}
$diff = array();
if ($stuff->current()->sizes != $toSizes) {
$diff['sizes'][] = $stuff->current()->sizes;
$diff['sizes'][] = $toSizes;
}
$diffString = Tilesheets::buildDiffString($diff);
if ($diffString == "" || count($diff) == 0) {
return false;
}
// Start log
$logEntry = new ManualLogEntry('tilesheet', 'editsheet');
$logEntry->setPerformer($user);
$logEntry->setTarget(Title::newFromText("Sheet/$toMod", NS_SPECIAL));
$logEntry->setComment($comment);
$logEntry->setParameters(array("4::diff" => $diffString, "5::diff_json" => json_encode($diff), "6::mod" => $curMod, "7::sizes" => $stuff->current()->sizes, "8::to_sizes" => $toSizes));
$logId = $logEntry->insert();
$logEntry->publish($logId);
// End log
return true;
}
}
class TilesheetsError{
static private $mDebug;
private $mDebugMode;
/**
* @param $debugMode
*/
public function __construct($debugMode) {
$this->mDebugMode = $debugMode;
}
/**
* Generate errors on edit page preview
*
* @return string
*/
public function output() {
if (!isset(self::$mDebug)) return "";
$colors = array(
"log" => "#CEFFFD",
"warning" => "#FFFFB5",
"deprecated" => "#CCF",
"query" => "#D1FFB3",
"error" => "#FFCECE",
"notice" => "blue"
);
$textColors = array(
"log" => "black",
"warning" => "black",
"deprecated" => "black",
"query" => "black",
"error" => "black",
"notice" => "white"
);
$html = "<table class=\"wikitable\" style=\"width:100%;\">";
$html .= "<caption>" . wfMessage('tilesheets-warnings-header')->text() . "</caption>";
$html .= "<tr><th style=\"width:10%;\">" . wfMessage('tilesheets-warnings-header-type')->text() . "</th><th>" . wfMessage('tilesheets-warnings-header-msg')->text() . "</th><tr>";
$flag = true;
foreach (self::$mDebug as $message) {
if (!$this->mDebugMode && $message[0] != "warning" && $message[0] != "error" && $message[0] != "notice") {
continue;
}
$html .= "<tr><td style=\"text-align:center; background-color:{$colors[$message[0]]}; color:{$textColors[$message[0]]}; font-weight:bold;\">" . wfMessage("tilesheets-warnings-type-{$message[0]}")->text() . "</td><td>{$message[1]}</td></tr>";
if ($message[0] == "warnings" || $message[0] == "error") $flag = false;
}
if ($flag) {
$html .= "<tr><td style=\"text-align:center; background-color:blue; color:white; font-weight:bold;\">" . wfMessage('tilesheets-warnings-type-notice') . "</td><td>" . wfMessage('tilesheets-warnings-none')->text() . "</td></tr>";
}
$html .= "</table>";
return $html;
}
/**
* @param $message
* @param string $type
*/
public static function debug($message, $type = "log") {
self::$mDebug[] = array($type, $message);
}
/**
* @param $message
*/
public static function deprecated($message) {
MWDebug::deprecated("(Tilesheets) ".$message);
self::debug($message, "deprecated");
}
/**
* @param $message
*/
public static function log($message) {
MWDebug::log("(Tilesheets) ".$message);
self::debug($message);
}
/**
* @param $message
*/
public static function warn($message) {
MWDebug::warning("(Tilesheets) ".$message);
self::debug($message, "warning");
}
/**
* @param $message
*/
public static function error($message) {
MWDebug::warning("(Tilesheets) "."Error: ".$message);
self::debug($message, "error");
}
/**
* @param $message
*/
public static function notice($message) {
MWDebug::warning("(Tilesheets) "."Notice: ".$message);
self::debug($message, "notice");
}
}