Repository navigation
Expand file tree
/
Copy pathLoopsHooks.php
More file actions
355 lines (298 loc) · 10.1 KB
/
Copy pathLoopsHooks.php
File metadata and controls
355 lines (298 loc) · 10.1 KB
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
/**
* 'Loops' is a MediaWiki extension expanding the parser with loops functions
*
* Documentation: http://www.mediawiki.org/wiki/Extension:Loops
* Support: http://www.mediawiki.org/wiki/Extension_talk:Loops
* Source code: http://svn.wikimedia.org/viewvc/mediawiki/trunk/extensions/Loops
*
* @license: GNU GPL v2 or higher
* @author: David M. Sledge
* @author: Daniel Werner < danweetz@web.de >
*
* @file Loops.php
* @ingroup Loops
*/
/**
* Class representing extension 'Loops', containing all parser functions and other
* extension logic stuff.
*/
class LoopsHooks {
const VERSION = '1.0.0';
/**
* Returns the extensions base installation directory.
*
* @since 0.4
*
* @return boolean
*/
public static function getDir() {
static $dir = null;
if ( $dir === null ) {
$dir = dirname( __FILE__ );
}
return $dir;
}
/**
* Sets up parser functions
*
* @since 0.4
*/
public static function onParserFirstCallInit( Parser &$parser ) {
if ( ! class_exists( 'ExtVariables' ) ) {
/*
* If Variables extension not defined, we can't use certain functions.
* Make sure they are disabled:
*/
global $wgLoopsEnabledFunctions;
$disabledFunctions = array( 'loop', 'forargs', 'fornumargs' );
$wgLoopsEnabledFunctions = array_diff( $wgLoopsEnabledFunctions, $disabledFunctions );
}
/*
* store for loops count per parser object. This will solve several bugs related to
* 'ParserClearState' hook resetting the count early in combination with certain
* other extensions or special page inclusion. (since v0.4)
*/
$parser->mExtLoopsCounter = 0;
self::initFunction( $parser, 'while' );
self::initFunction( $parser, 'dowhile' );
self::initFunction( $parser, 'loop' );
self::initFunction( $parser, 'forargs' );
self::initFunction( $parser, 'fornumargs' );
return true;
}
private static function initFunction( Parser &$parser, $name ) {
global $wgLoopsEnabledFunctions;
// don't register parser function if disabled by configuration:
if ( ! in_array( $name, $wgLoopsEnabledFunctions ) ) {
return;
}
$functionCallback = array( __CLASS__, 'pfObj_' . $name );
$parser->setFunctionHook( $name, $functionCallback, Parser::SFH_OBJECT_ARGS );
}
####################
# Parser Functions #
####################
public static function pfObj_while( Parser &$parser, $frame, $args ) {
return self::perform_while( $parser, $frame, $args, false );
}
public static function pfObj_dowhile( Parser &$parser, $frame, $args ) {
return self::perform_while( $parser, $frame, $args, true );
}
/**
* Generic function handling '#while' and '#dowhile' as one
*/
protected static function perform_while( Parser &$parser, $frame, $args, $dowhile = false ) {
// #(do)while: | condition | code
$rawCond = isset( $args[1] ) ? $args[1] : ''; // unexpanded condition
$rawCode = isset( $args[2] ) ? $args[2] : ''; // unexpanded loop code
if (
$dowhile === false
&& trim( $frame->expand( $rawCond ) ) === ''
) {
// while, but condition not fullfilled from the start
return '';
}
$output = '';
do {
// limit check:
if ( ! self::incrCounter( $parser ) ) {
return self::msgLoopsLimit( $output );
}
$output .= trim( $frame->expand( $rawCode ) );
} while( trim( $frame->expand( $rawCond ) ) );
return $output;
}
public static function pfObj_loop( Parser &$parser, PPFrame $frame, $args ) {
// #loop: var | start | count | code
$varName = isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
$startVal = isset( $args[1] ) ? (int)trim( $frame->expand( $args[1] ) ) : 0;
$loops = isset( $args[2] ) ? (int)trim( $frame->expand( $args[2] ) ) : 0;
$rawCode = isset( $args[3] ) ? $args[3] : ''; // unexpanded loop code
if ( $loops === 0 ) {
// no loops to perform
return '';
}
$output = '';
$endVal = $startVal + $loops;
$i = $startVal;
while( $i !== $endVal ) {
// limit check:
if ( ! self::incrCounter( $parser ) ) {
return self::msgLoopsLimit( $output );
}
// set current position as variable:
self::setVariable( $parser, $varName, (string)$i );
$output .= trim( $frame->expand( $rawCode ) );
// in-/decrease loop count (count can be negative):
( $i < $endVal ) ? $i++ : $i--;
}
return $output;
}
/**
* #forargs: filter | keyVarName | valVarName | code
*/
public static function pfObj_forargs( Parser &$parser, $frame, $args ) {
// The first arg is already expanded, but this is a good habit to have...
$filter = array_shift( $args );
$filter = $filter !== null ? trim( $frame->expand( $filter ) ) : '';
// if prefix contains numbers only or isn't set, get all arguments, otherwise just non-numeric
$tArgs = ( preg_match( '/^([1-9][0-9]*)?$/', $filter ) > 0 )
? $frame->getArguments()
: $frame->getNamedArguments();
return self::perform_forargs( $parser, $frame, $args, $tArgs, $filter );
}
/**
* #fornumargs: keyVarName | valVarName | code
* or (since 0.4 for more consistency)
* #fornumargs: | keyVarName | valVarName | code
*/
public static function pfObj_fornumargs( Parser &$parser, $frame, $args ) {
/*
* get numeric arguments, don't use PPFrame::getNumberedArguments because it would
* return explicitely numbered arguments only.
*/
$tNumArgs = $frame->getArguments();
foreach( $tNumArgs as $argKey => $argVal ) {
// allow all numeric, including negative values!
if ( is_string( $argKey ) ) {
unset( $tNumArgs[ $argKey ] );
}
}
ksort( $tNumArgs ); // sort from lowest to highest
if ( count( $args ) > 3 ) {
// compatbility to pre 0.4 but consistency with other Loop functions.
// this way the first argument can be ommitted like '#fornumargs: |varKey |varVal |code'
array_shift( $args );
}
return self::perform_forargs( $parser, $frame, $args, $tNumArgs, '' );
}
/**
* Generic function handling '#forargs' and '#fornumargs' as one
*/
protected static function perform_forargs( Parser &$parser, PPFrame $frame, array $funcArgs, array $templateArgs, $prefix = '' ) {
// if not called within template instance:
if ( !( $frame->isTemplate() ) ) {
return '';
}
// name of the variable to store the argument name:
$keyVar = array_shift( $funcArgs );
$keyVar = $keyVar !== null ? trim( $frame->expand( $keyVar ) ) : '';
// name of the variable to store the argument value:
$valVar = array_shift( $funcArgs );
$valVar = $valVar !== null ? trim( $frame->expand( $valVar ) ) : '';
// unexpanded code:
$rawCode = array_shift( $funcArgs );
$rawCode = $rawCode !== null ? $rawCode : '';
$output = '';
// if prefix contains numbers only or isn't set, get all arguments, otherwise just non-numeric
$tArgs = preg_match( '/^([1-9][0-9]*)?$/', $prefix ) > 0
? $frame->getArguments() : $frame->getNamedArguments();
foreach( $templateArgs as $argName => $argVal ) {
// if no filter or prefix in argument name:
if ( $prefix !== '' && strpos( $argName, $prefix ) !== 0 ) {
continue;
}
if ( $keyVar !== $valVar ) {
// variable with the argument name without prefix as value:
self::setVariable( $parser, $keyVar, substr( $argName, strlen( $prefix ) ) );
}
// variable with the arguments value:
self::setVariable( $parser, $valVar, $argVal );
// expand current run:
$output .= trim( $frame->expand( $rawCode ) );
}
return $output;
}
/**
* Connects to 'Variables' extension and sets a variable. Handles different versions of
* 'Variables' extension since there have changed some things along the way.
*
* @param Parser $parser
* @param string $varName
* @param string $varValue
*/
private static function setVariable( Parser &$parser, $varName, $varValue ) {
global $wgExtVariables;
static $newVersion = null;
if ( $newVersion === null ) {
// find out whether local wiki is using variables extension 2.0 or higher
$newVersion = ( defined( 'ExtVariables::VERSION' ) && version_compare( ExtVariables::VERSION, '1.9999', '>' ) );
}
if ( $newVersion ) {
// clean way since Variables 2.0:
ExtVariables::get( $parser )->setVarValue( $varName, $varValue );
}
else {
// make sure to trim values and convert them to string since old versions of Variables extension won't do this.
$wgExtVariables->vardefine( $parser, trim( $varName ), trim( $varValue ) );
}
}
###############
# Loops Count #
###############
/**
* Returns how many loops have been performed for a given Parser instance.
*
* @since 0.4
*
* @param Parser $parser
* @return int
*/
public static function getLoopsCount( Parser &$parser ) {
return $parser->mExtLoopsCounter;
}
/**
* Returns whether the maximum number of loops for the given Parser instance have
* been performed already.
*
* @since 0.4
*
* @param Parser $parser
* @return bool
*/
public static function maxLoopsPerformed( Parser &$parser ) {
global $wgLoopsMaxLoops;
return $wgLoopsMaxLoops > -1 && $parser->mExtLoopsCounter >= $wgLoopsMaxLoops;
}
/**
* If limit has not been exceeded already, this will increase the counter. If
* exceeded false will be returned, otherwise the new counter value
*
* @return false|int
*/
protected static function incrCounter( Parser &$parser ) {
if ( self::maxLoopsPerformed( $parser ) ) {
return false;
}
return ++$parser->mExtLoopsCounter;
}
/**
* div wrapped error message stating maximum number of loops have been performed.
*/
protected static function msgLoopsLimit( $output = '' ) {
if ( trim( $output ) !== '' ) {
$output .= "\n";
}
return $output .= '<div class="error">' . wfMessage( 'loops_max' ) . '</div>';
}
##################
# Hooks handling #
##################
public static function onParserClearState( Parser &$parser ) {
// reset loops counter since the parser process finished one page
$parser->mExtLoopsCounter = 0;
return true;
}
public static function onParserLimitReport( $parser, &$report ) {
global $wgLoopsMaxLoops;
// add performed loops to limit report:
$report .= 'ExtLoops count: ' . self::getLoopsCount( $parser );
if ( $wgLoopsMaxLoops > -1 ) {
// if limit is set, communicate the limit as well:
$report .= '/' . $wgLoopsMaxLoops;
}
$report .= "\n";
return true;
}
}