-
Notifications
You must be signed in to change notification settings - Fork 1
/
PgnJS.hooks.php
277 lines (253 loc) · 11.4 KB
/
PgnJS.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
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
<?php
/**
* Mediawiki PHP extension for displaying chess game in PGN format
*
* Copyright (C) 2007-2017 Michael Peeters <https://github.com/xeyownt>
*
* This file is part of the PgnJS MediaWiki extension
* <http://www.mediawiki.org/wiki/Extension:PgnJS>.
*
* @file
* @ingroup Extensions
*/
// namespace MediaWiki\Extension\PgnJS;
class PgnJSHooks {
public static function onParserFirstCallInit( Parser &$parser ) {
// Register parser handler for tag <pgn>
$parser->setHook( 'pgn', 'PgnJSHooks::parserHook' );
}
public static function parserHook( $input, array $args, Parser $parser, PPFrame $frame ) {
// Tell ResourceLoader that we need our css module
$parser->getOutput()->addModules( 'ext.PgnJS' );
return PgnJS::renderPgnjs($parser, $input, $args);
}
public static function onGetPreferences( $user, &$preferences ) {
global $wgUser;
$theme = $wgUser->getOption('pgnjs-theme');
$theme = $theme ? $theme : 'normal';
$preferences['pgnjs-theme'] = array(
'type' => 'select',
'label-message' => 'prefs-pgnjs-theme', // a system message
'section' => 'rendering/PgnJS',
// Array of options. Key = text to display. Value = HTML <option> value.
'options' => array(
'Beyer' => 'beyer',
'Blue' => 'blue',
'Chess.com' => 'chesscom',
'Falken' => 'falken',
'Green' => 'green',
'Informator' => 'informator',
'Normal' => 'normal',
'Sportverlag' => 'sportverlag',
'Zeit' => 'zeit'
),
'default' => $theme,
'help-message' => 'prefs-pgnjs-theme-help', // a system message (optional)
);
$pieceStyle = $wgUser->getOption('pgnjs-pieceStyle');
$pieceStyle = $pieceStyle ? $pieceStyle : 'merida';
$preferences['pgnjs-pieceStyle'] = array(
'type' => 'select',
'label-message' => 'prefs-pgnjs-pieceStyle', // a system message
'section' => 'rendering/PgnJS',
// Array of options. Key = text to display. Value = HTML <option> value.
'options' => array(
'Alpha' => 'alpha',
'Beyer' => 'beyer',
'Case' => 'case',
'Chess.com' => 'chesscom',
'Condal' => 'condal',
'Leipzig' => 'leipzig',
'Maya' => 'maya',
'Merida' => 'merida',
'USCF' => 'uscf' ,
'Wikipedia' => 'wikipedia'
),
'default' => $pieceStyle,
'help-message' => 'prefs-pgnjs-pieceStyle-help', // a system message (optional)
);
$timerTime = $wgUser->getOption('pgnjs-timerTime');
$timerTime = $timerTime && ($timerTime >= 100) && ($timerTime <= 5000) ? $timerTime : 700;
$preferences['pgnjs-timerTime'] = array(
'type' => 'int',
'label-message' => 'prefs-pgnjs-timerTime', // a system message
'section' => 'rendering/PgnJS',
'default' => $timerTime,
'help-message' => 'prefs-pgnjs-timerTime-help', // a system message (optional)
);
// Required return value of a hook function.
return true;
}
// Deprecated in MW 1.37, removed in MW 1.38
public static function onUserSaveOptions( User $user, array &$options ) {
if( ($options['pgnjs-timerTime'] < 100) || ($options['pgnjs-timerTime'] > 5000) ) {
$options['pgnjs-timerTime'] = 700;
}
}
// From MW 1.37 onwards
public static function onSaveUserOptions( UserIdentity $user, array &$modifiedOptions, array $originalOptions ) {
if( ($originalOptions['pgnjs-timerTime'] < 100) || ($originalOptions['pgnjs-timerTime'] > 5000) ) {
$modifiedOptions['pgnjs-timerTime'] = 700;
}
}
}
class PgnJS {
const A_STYLE = "style";
const A_CLASS = "class";
const A_MODE = "mode";
const MODE_DEFAULTS = "defaults";
const A_POSITION = "position";
const A_SHOW_NOTATION = "shownotation"; // Must be lower case or test fail!
const A_ORIENTATION = "orientation";
const A_THEME = "theme";
const A_PIECE_STYLE = "piecestyle"; // Must be lower case or test fail!
const A_TIMER_TIME = "timertime"; // Must be lower case or test fail!
const A_LOCALE = "locale";
const A_BOARD_SIZE = "boardsize"; // Must be lower case or test fail!
const A_SHOW_FEN = "showfen"; // Must be lower case or test fail!
const A_SIZE = "size"; // Must be lower case or test fail!
const A_MOVES_WIDTH = "moveswidth"; // Must be lower case or test fail!
const A_MOVES_HEIGHT = "movesheight"; // Must be lower case or test fail!
const A_SCROLLABLE = "scrollable";
const A_HEADERS = "headers";
const A_LAYOUT = "layout";
const A_GOTO = "goto";
const DEFAULTS = array( 'style' => 'width: 240px', 'pieceStyle' => 'merida' );
private static $board_id = 0;
private static $board_defaults = array();
private static $board_class_defaults = array();
// Render <pgn>
static public function renderPgnjs( $parser, $input, array $args ) {
global $wgUser;
global $wgLang;
$a_style = isset($args[self::A_STYLE]) ? $args[self::A_STYLE] : null;
$a_class = isset($args[self::A_CLASS]) ? $args[self::A_CLASS] : null;
$a_mode = isset($args[self::A_MODE]) ? $args[self::A_MODE] : "view";
$a_position = isset($args[self::A_POSITION]) ? $args[self::A_POSITION] : null;
$a_showNotation = isset($args[self::A_SHOW_NOTATION]) ? $args[self::A_SHOW_NOTATION] : null;
$a_orientation = isset($args[self::A_ORIENTATION]) ? $args[self::A_ORIENTATION] : null;
$a_theme = isset($args[self::A_THEME]) ? $args[self::A_THEME] : null;
$a_pieceStyle = isset($args[self::A_PIECE_STYLE]) ? $args[self::A_PIECE_STYLE] : null;
$a_timerTime = isset($args[self::A_TIMER_TIME]) ? $args[self::A_TIMER_TIME] : null;
$a_locale = isset($args[self::A_LOCALE]) ? $args[self::A_LOCALE] : null;
$a_boardSize = isset($args[self::A_BOARD_SIZE]) ? $args[self::A_BOARD_SIZE] : null;
$a_showFen = isset($args[self::A_SHOW_FEN]) ? $args[self::A_SHOW_FEN] : null;
$a_size = isset($args[self::A_SIZE]) ? $args[self::A_SIZE] : null;
$a_layout = isset($args[self::A_LAYOUT]) ? $args[self::A_LAYOUT] : null;
$a_movesWidth = isset($args[self::A_MOVES_WIDTH]) ? $args[self::A_MOVES_WIDTH] : null;
$a_movesHeight = isset($args[self::A_MOVES_HEIGHT]) ? $args[self::A_MOVES_HEIGHT] : null;
$a_scrollable = isset($args[self::A_SCROLLABLE]) ? $args[self::A_SCROLLABLE] : null;
$a_headers = isset($args[self::A_HEADERS]) ? $args[self::A_HEADERS] : null;
$a_goto = isset($args[self::A_GOTO]) ? $args[self::A_GOTO] : null;
$board = array();
$board_userprefs = array(
'theme' => $wgUser->getOption('pgnjs-theme'),
'pieceStyle' => $wgUser->getOption('pgnjs-pieceStyle'),
'locale' => $wgLang->getCode(),
'timerTime' => $wgUser->getOption('pgnjs-timerTime'),
);
if( $a_mode !== self::MODE_DEFAULTS) {
foreach( explode(' ', $a_class) as $c ) {
if( $c and isset(self::$board_class_defaults[$c]) ) {
$board += self::$board_class_defaults[$c]; // First listed class as precedence
}
}
$board += self::$board_defaults; // Anonymous defaults have less precedence
$board += $board_userprefs; // Then user preferences even less precedence
$board += self::DEFAULTS; // Finally hard-coded defaults have least precedence
}
$id = "pgnjs".(++self::$board_id);
if( $input ) {
$board['pgn'] = $parser->recursiveTagParse( $input );
} else {
if( ($a_mode !== 'edit') and ($a_mode !== self::MODE_DEFAULTS) ) {
$a_mode = 'board'; // Fall-back to 'board' mode, unless in edit or defaults mode
}
}
if( $a_style ) {
$board['style'] = $a_style;
}
if( $a_position ) {
$board['position'] = $a_position;
}
if( $a_showNotation ) {
$board['showNotation'] = ($a_showNotation === 'true');
}
if( $a_orientation ) {
$board['orientation'] = $a_orientation;
}
if( $a_theme ) {
$board['theme'] = $a_theme;
}
if( $a_pieceStyle ) {
$board['pieceStyle'] = $a_pieceStyle;
}
if( $a_timerTime ) {
$board['timerTime'] = $a_timerTime;
}
if( $a_locale ) {
$board['locale'] = $a_locale;
}
if( $a_boardSize ) {
$board['boardSize'] = $a_boardSize;
}
if( $a_showFen ) {
$board['showFen'] = ($a_showFen === 'true');
}
if( $a_size ) {
$board['size'] = $a_size;
}
if( $a_layout ) {
$board['layout'] = $a_layout;
}
if( $a_movesWidth ) {
$board['movesWidth'] = $a_movesWidth;
}
if( $a_movesHeight ) {
$board['movesHeight'] = $a_movesHeight;
}
if( $a_scrollable ) {
$board['scrollable'] = ($a_scrollable === 'true');
}
if( $a_headers ) {
$board['headers'] = ($a_headers === 'true');
}
if( $a_goto && ($a_goto === 'last' or $a_goto === 'first') ) {
$board['goto'] = $a_goto;
}
if( $a_mode === self::MODE_DEFAULTS ) {
// If mode is defaults, we save the config as defaults
$has_class = false;
foreach( explode(' ', $a_class) as $c ) {
if( $c ) {
self::$board_class_defaults[$c] = $board;
$has_class = true;
}
}
// non-class defaults assigned only if no class given
if( !$has_class ) {
self::$board_defaults = $board;
}
return "";
} else {
// mode is not setting defaults, generate HTML and JS to display the board
$board['mode'] = $a_mode; // must set the mode for our parser
// Remove attributes unknown to PgnViewerJS
$a_style = $board['style'];
unset($board['style']);
$jsBoard = json_encode($board);
$script = "<script>window.PgnJSBoards = window.PgnJSBoards || {}; window.PgnJSBoards.$id = $jsBoard;</script>";
$class_attr = $a_class ? " class=\"$a_class\"" : "";
return "<div id=\"$id\"$class_attr style=\"$a_style\"></div>$script";
}
}
}
$mediaWikiVersion = defined( 'MW_VERSION' ) ? MW_VERSION : null;
if (version_compare($mediaWikiVersion, '1.37', '<')) {
// Use the old hook name for versions up to 1.36
$GLOBALS['wgHooks']['UserSaveOptions'][] = 'PgnJSHooks::onUserSaveOptions';
} else {
// Use the new hook name for versions 1.37 and onwards
$GLOBALS['wgHooks']['SaveUserOptions'][] = 'PgnJSHooks::onSaveUserOptions';
}
?>