-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLabeledSectionTransclusion.class.php
566 lines (503 loc) · 15.3 KB
/
LabeledSectionTransclusion.class.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
<?php
class LabeledSectionTransclusion {
private static $loopCheck = array();
/**
* @param $parser Parser
* @return bool
*/
static function setup( $parser ) {
$parser->setHook( 'section', array( __CLASS__, 'noop' ) );
$parser->setFunctionHook( 'lst', array( __CLASS__, 'pfuncIncludeObj' ), Parser::SFH_OBJECT_ARGS );
$parser->setFunctionHook( 'lstx', array( __CLASS__, 'pfuncExcludeObj' ), Parser::SFH_OBJECT_ARGS );
$parser->setFunctionHook( 'lsth', array( __CLASS__, 'pfuncIncludeHeading' ) );
return true;
}
/**
* Add the magic words - possibly with more readable aliases
*
* @param $magicWords array
* @param $langCode string
* @return bool
*/
static function setupMagic( &$magicWords, $langCode ) {
global $wgParser, $wgLstLocal;
switch( $langCode ) {
case 'de':
$include = 'Abschnitt';
$exclude = 'Abschnitt-x';
$wgLstLocal = array( 'section' => 'Abschnitt', 'begin' => 'Anfang', 'end' => 'Ende' ) ;
break;
case 'he':
$include = 'קטע';
$exclude = 'בלי קטע';
$wgLstLocal = array( 'section' => 'קטע', 'begin' => 'התחלה', 'end' => 'סוף' ) ;
break;
case 'pt':
$include = 'trecho';
$exclude = 'trecho-x';
$wgLstLocal = array( 'section' => 'trecho', 'begin' => 'começo', 'end' => 'fim' );
break;
}
if ( isset( $include ) && isset( $exclude ) ) {
$magicWords['lst'] = array( 0, 'lst', 'section', $include );
$magicWords['lstx'] = array( 0, 'lstx', 'section-x', $exclude );
$wgParser->setHook( $include, array( __CLASS__, 'noop' ) );
} else {
$magicWords['lst'] = array( 0, 'lst', 'section' );
$magicWords['lstx'] = array( 0, 'lstx', 'section-x' );
}
$magicWords['lsth'] = array( 0, 'lsth', 'section-h' );
return true;
}
##############################################################
# To do transclusion from an extension, we need to interact with the parser
# at a low level. This is the general transclusion functionality
##############################################################
/**
* Register what we're working on in the parser, so we don't fall into a trap.
* @param $parser Parser
* @param $part1
* @return bool
*/
static function open_( $parser, $part1 ) {
// Infinite loop test
if ( isset( $parser->mTemplatePath[$part1] ) ) {
wfDebug( __METHOD__ . ": template loop broken at '$part1'\n" );
return false;
} else {
$parser->mTemplatePath[$part1] = 1;
return true;
}
}
/**
* Finish processing the function.
* @param $parser Parser
* @param $part1
* @return bool
*/
static function close_( $parser, $part1 ) {
// Infinite loop test
if ( isset( $parser->mTemplatePath[$part1] ) ) {
unset( $parser->mTemplatePath[$part1] );
} else {
wfDebug( __METHOD__ . ": close unopened template loop at '$part1'\n" );
}
}
/**
* Handle recursive substitution here, so we can break cycles, and set up
* return values so that edit sections will resolve correctly.
* @param Parser $parser
* @param Title $title of target page
* @param string $text
* @param string $part1 Key for cycle detection
* @param int $skiphead Number of source string headers to skip for numbering
* @return mixed string or magic array of bits
* @todo handle mixed-case </section>
* @private
*/
static function parse_( $parser, $title, $text, $part1, $skiphead = 0 ) {
// if someone tries something like<section begin=blah>lst only</section>
// text, may as well do the right thing.
$text = str_replace( '</section>', '', $text );
if ( self::open_( $parser, $part1 ) ) {
// Try to get edit sections correct by munging around the parser's guts.
return array( $text, 'title' => $title, 'replaceHeadings' => true,
'headingOffset' => $skiphead, 'noparse' => false, 'noargs' => false );
} else {
return "[[" . $title->getPrefixedText() . "]]" .
"<!-- WARNING: LST loop detected -->";
}
}
##############################################################
# And now, the labeled section transclusion
##############################################################
/**
* Parser tag hook for <section>.
* The section markers aren't paired, so we only need to remove them.
*
* @param string $in
* @param array $assocArgs
* @param Parser $parser
* @return string HTML output
*/
static function noop( $in, $assocArgs = array(), $parser = null ) {
return '';
}
/**
* Generate a regex to match the section(s) we're interested in.
* @param string $sec Name of target section
* @param string $to Optional name of section to end with, if transcluding
* multiple sections in sequence. If blank, will assume
* same section name as started with.
* @return string regex
* @private
*/
static function getPattern_( $sec, $to ) {
global $wgLstLocal;
$beginAttr = self::getAttrPattern_( $sec, 'begin' );
if ( $to == '' ) {
$endAttr = self::getAttrPattern_( $sec, 'end' );
} else {
$endAttr = self::getAttrPattern_( $to, 'end' );
}
if ( isset( $wgLstLocal ) ) {
$section_re = "(?i:section|$wgLstLocal[section])";
} else {
$section_re = "(?i:section)";
}
return "/<$section_re$beginAttr\/?>(.*?)\n?<$section_re$endAttr\/?>/s";
}
/**
* Generate a regex fragment matching the attribute portion of a section tag
* @param string $sec Name of the target section
* @param string $type Either "begin" or "end" depending on the type of section tag to be matched
* @return string
*/
static function getAttrPattern_( $sec, $type ) {
global $wgLstLocal;
$sec = preg_quote( $sec, '/' );
$ws = "(?:\s+[^>]*)?"; // was like $ws="\s*"
if ( isset( $wgLstLocal ) ) {
if ( $type == 'begin' ) {
$attrName = "(?i:begin|{$wgLstLocal['begin']})";
} else {
$attrName = "(?i:end|{$wgLstLocal['end']})";
}
} else {
if ( $type == 'begin' ) {
$attrName = "(?i:begin)";
} else {
$attrName = "(?i:end)";
}
}
return "$ws\s+$attrName=(?:$sec|\"$sec\"|'$sec')$ws";
}
/**
* Count headings in skipped text.
*
* Count skipped headings, so parser (as of r18218) can skip them, to
* prevent wrong heading links (see bug 6563).
*
* @param string $text
* @param int $limit Cutoff point in the text to stop searching
* @return int Number of matches
* @private
*/
static function countHeadings_( $text, $limit ) {
$pat = '^(={1,6}).+\1\s*$()';
$count = 0;
$offset = 0;
$m = array();
while ( preg_match( "/$pat/im", $text, $m, PREG_OFFSET_CAPTURE, $offset ) ) {
if ( $m[2][1] > $limit ) {
break;
}
$count++;
$offset = $m[2][1];
}
return $count;
}
/**
* Fetches content of target page if valid and found, otherwise
* produces wikitext of a link to the target page.
*
* @param Parser $parser
* @param string $page title text of target page
* @param (out) Title $title normalized title object
* @param (out) string $text wikitext output
* @return string bool true if returning text, false if target not found
* @private
*/
static function getTemplateText_( $parser, $page, &$title, &$text ) {
$title = Title::newFromText( $page );
if ( is_null( $title ) ) {
$text = '';
return true;
} else {
if ( method_exists( $parser, 'fetchTemplateAndTitle' ) ) {
list( $text, $title ) = $parser->fetchTemplateAndTitle( $title );
} else {
$text = $parser->fetchTemplate( $title );
}
}
// if article doesn't exist, return a red link.
if ( $text == false ) {
$text = "[[" . $title->getPrefixedText() . "]]";
return false;
} else {
return true;
}
}
/**
* Set up some variables for MW-1.12 parser functions
* @param $parser Parser
* @param $frame PPFrame
* @param $args array
* @param $func string
* @return array|string
*/
static function setupPfunc12( $parser, $frame, $args, $func = 'lst' ) {
if ( !count( $args ) ) {
return '';
}
$title = Title::newFromText( trim( $frame->expand( array_shift( $args ) ) ) );
if ( !$title ) {
return '';
}
if ( !$frame->loopCheck( $title ) ) {
return '<span class="error">'
. wfMessage( 'parser-template-loop-warning', $title->getPrefixedText() )->inContentLanguage()->text()
. '</span>';
}
list( $root, $finalTitle ) = $parser->getTemplateDom( $title );
// if article doesn't exist, return a red link.
if ( $root === false ) {
return "[[" . $title->getPrefixedText() . "]]";
}
$newFrame = $frame->newChild( false, $finalTitle );
if ( !count( $args ) ) {
return $newFrame->expand( $root );
}
$begin = trim( $frame->expand( array_shift( $args ) ) );
if ( $func == 'lstx' ) {
if ( !count( $args ) ) {
$repl = '';
} else {
$repl = trim( $frame->expand( array_shift( $args ) ) );
}
}
if ( !count( $args ) ) {
$end = $begin;
} else {
$end = trim( $frame->expand( array_shift( $args ) ) );
}
$beginAttr = self::getAttrPattern_( $begin, 'begin' );
$beginRegex = "/^$beginAttr$/s";
$endAttr = self::getAttrPattern_( $end, 'end' );
$endRegex = "/^$endAttr$/s";
return compact( 'dom', 'root', 'newFrame', 'repl', 'beginRegex', 'begin', 'endRegex' );
}
/**
* Returns true if the given extension name is "section"
*/
static function isSection( $name ) {
global $wgLstLocal;
$name = strtolower( $name );
return $name == 'section'
|| ( isset( $wgLstLocal['section'] ) && strtolower( $wgLstLocal['section'] ) == $name );
}
/**
* Returns the text for the inside of a split <section> node
* @param $parser Parser
* @param $frame PPFrame
* @param $parts array
* @return string
*/
static function expandSectionNode( $parser, $frame, $parts ) {
if ( isset( $parts['inner'] ) ) {
return $parser->replaceVariables( $parts['inner'], $frame );
} else {
return '';
}
}
/**
* @param $parser Parser
* @param $frame PPFrame
* @param $args array
* @return array|string
*/
static function pfuncIncludeObj( $parser, $frame, $args ) {
$setup = self::setupPfunc12( $parser, $frame, $args, 'lst' );
if ( !is_array( $setup ) ) {
return $setup;
}
/**
* @var $root PPNode
*/
$root = $setup['root'];
/**
* @var $newFrame PPFrame
*/
$newFrame = $setup['newFrame'];
$beginRegex = $setup['beginRegex'];
$endRegex = $setup['endRegex'];
$begin = $setup['begin'];
$text = '';
$node = $root->getFirstChild();
while ( $node ) {
// If the name of the begin node was specified, find it.
// Otherwise transclude everything from the beginning of the page.
if ( $begin != '' ) {
// Find the begin node
$found = false;
for ( ; $node; $node = $node->getNextSibling() ) {
if ( $node->getName() != 'ext' ) {
continue;
}
$parts = $node->splitExt();
$parts = array_map( array( $newFrame, 'expand' ), $parts );
if ( self::isSection( $parts['name'] ) ) {
if ( preg_match( $beginRegex, $parts['attr'] ) ) {
$found = true;
break;
}
}
}
if ( !$found || !$node ) {
break;
}
}
// Write the text out while looking for the end node
$found = false;
for ( ; $node; $node = $node->getNextSibling() ) {
if ( $node->getName() === 'ext' ) {
$parts = $node->splitExt();
$parts = array_map( array( $newFrame, 'expand' ), $parts );
if ( self::isSection( $parts['name'] ) ) {
if ( preg_match( $endRegex, $parts['attr'] ) ) {
$found = true;
break;
}
$text .= self::expandSectionNode( $parser, $newFrame, $parts );
} else {
$text .= $newFrame->expand( $node );
}
} else {
$text .= $newFrame->expand( $node );
}
}
if ( !$found ) {
break;
} elseif ( $begin == '' ) {
// When the end node was found and text is transcluded from
// the beginning of the page, finish the transclusion
break;
}
$node = $node->getNextSibling();
}
return $text;
}
/**
* @param $parser Parser
* @param $frame PPFrame
* @param $args array
* @return array|string
*/
static function pfuncExcludeObj( $parser, $frame, $args ) {
$setup = self::setupPfunc12( $parser, $frame, $args, 'lstx' );
if ( !is_array( $setup ) ) {
return $setup;
}
/**
* @var $root PPNode
*/
$root = $setup['root'];
/**
* @var $newFrame PPFrame
*/
$newFrame = $setup['newFrame'];
$beginRegex = $setup['beginRegex'];
$endRegex = $setup['endRegex'];
$repl = $setup['repl'];
$text = '';
for ( $node = $root->getFirstChild(); $node; $node = $node ? $node->getNextSibling() : false ) {
// Search for the start tag
$found = false;
for ( ; $node; $node = $node->getNextSibling() ) {
if ( $node->getName() == 'ext' ) {
$parts = $node->splitExt();
$parts = array_map( array( $newFrame, 'expand' ), $parts );
if ( self::isSection( $parts['name'] ) ) {
if ( preg_match( $beginRegex, $parts['attr'] ) ) {
$found = true;
break;
}
$text .= self::expandSectionNode( $parser, $newFrame, $parts );
} else {
$text .= $newFrame->expand( $node );
}
} else {
$text .= $newFrame->expand( $node );
}
}
if ( !$found ) {
break;
}
// Append replacement text
$text .= $repl;
// Search for the end tag
for ( ; $node; $node = $node->getNextSibling() ) {
if ( $node->getName() == 'ext' ) {
$parts = $node->splitExt( $node );
$parts = array_map( array( $newFrame, 'expand' ), $parts );
if ( self::isSection( $parts['name'] ) ) {
if ( preg_match( $endRegex, $parts['attr'] ) ) {
$text .= self::expandSectionNode( $parser, $newFrame, $parts );
break;
}
}
}
}
}
return $text;
}
/**
* section inclusion - include all matching sections
*
* A parser extension that further extends labeled section transclusion,
* adding a function, #lsth for transcluding marked sections of text,
*
* @todo: MW 1.12 version, as per #lst/#lstx
*
* @param $parser Parser
* @param $page string
* @param $sec string
* @param $to string
* @return mixed|string
*/
static function pfuncIncludeHeading( $parser, $page = '', $sec = '', $to = '' ) {
if ( self::getTemplateText_( $parser, $page, $title, $text ) == false ) {
return $text;
}
// Generate a regex to match the === classical heading section(s) === we're
// interested in.
if ( $sec == '' ) {
$begin_off = 0;
$head_len = 6;
} else {
$pat = '^(={1,6})\s*' . preg_quote( $sec, '/' ) . '\s*\1\s*($)' ;
if ( preg_match( "/$pat/im", $text, $m, PREG_OFFSET_CAPTURE ) ) {
$begin_off = $m[2][1];
$head_len = strlen( $m[1][0] );
} else {
return '';
}
}
if ( $to != '' ) {
// if $to is supplied, try and match it. If we don't match, just
// ignore it.
$pat = '^(={1,6})\s*' . preg_quote( $to, '/' ) . '\s*\1\s*$';
if ( preg_match( "/$pat/im", $text, $m, PREG_OFFSET_CAPTURE, $begin_off ) ) {
$end_off = $m[0][1] -1;
}
}
if ( !isset( $end_off ) ) {
$pat = '^(={1,' . $head_len . '})(?!=).*?\1\s*$';
if ( preg_match( "/$pat/im", $text, $m, PREG_OFFSET_CAPTURE, $begin_off ) ) {
$end_off = $m[0][1] -1;
}
}
$nhead = self::countHeadings_( $text, $begin_off );
if ( isset( $end_off ) ) {
$result = substr( $text, $begin_off, $end_off - $begin_off );
} else {
$result = substr( $text, $begin_off );
}
if ( method_exists( $parser, 'getPreprocessor' ) ) {
$frame = $parser->getPreprocessor()->newFrame();
$dom = $parser->preprocessToDom( $result );
$result = $frame->expand( $dom );
}
return self::parse_( $parser, $title, $result, "#lsth:${page}|${sec}", $nhead );
}
}