Split parser related files to have one class in one file
[lhc/web/wiklou.git] / includes / parser / PPFrame_DOM.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Parser
20 */
21
22 /**
23 * An expansion frame, used as a context to expand the result of preprocessToObj()
24 * @ingroup Parser
25 */
26 // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
27 class PPFrame_DOM implements PPFrame {
28
29 /**
30 * @var Preprocessor
31 */
32 public $preprocessor;
33
34 /**
35 * @var Parser
36 */
37 public $parser;
38
39 /**
40 * @var Title
41 */
42 public $title;
43 public $titleCache;
44
45 /**
46 * Hashtable listing templates which are disallowed for expansion in this frame,
47 * having been encountered previously in parent frames.
48 */
49 public $loopCheckHash;
50
51 /**
52 * Recursion depth of this frame, top = 0
53 * Note that this is NOT the same as expansion depth in expand()
54 */
55 public $depth;
56
57 private $volatile = false;
58 private $ttl = null;
59
60 /**
61 * @var array
62 */
63 protected $childExpansionCache;
64
65 /**
66 * Construct a new preprocessor frame.
67 * @param Preprocessor $preprocessor The parent preprocessor
68 */
69 public function __construct( $preprocessor ) {
70 $this->preprocessor = $preprocessor;
71 $this->parser = $preprocessor->parser;
72 $this->title = $this->parser->mTitle;
73 $this->titleCache = [ $this->title ? $this->title->getPrefixedDBkey() : false ];
74 $this->loopCheckHash = [];
75 $this->depth = 0;
76 $this->childExpansionCache = [];
77 }
78
79 /**
80 * Create a new child frame
81 * $args is optionally a multi-root PPNode or array containing the template arguments
82 *
83 * @param bool|array $args
84 * @param Title|bool $title
85 * @param int $indexOffset
86 * @return PPTemplateFrame_DOM
87 */
88 public function newChild( $args = false, $title = false, $indexOffset = 0 ) {
89 $namedArgs = [];
90 $numberedArgs = [];
91 if ( $title === false ) {
92 $title = $this->title;
93 }
94 if ( $args !== false ) {
95 $xpath = false;
96 if ( $args instanceof PPNode ) {
97 $args = $args->node;
98 }
99 foreach ( $args as $arg ) {
100 if ( $arg instanceof PPNode ) {
101 $arg = $arg->node;
102 }
103 if ( !$xpath || $xpath->document !== $arg->ownerDocument ) {
104 $xpath = new DOMXPath( $arg->ownerDocument );
105 }
106
107 $nameNodes = $xpath->query( 'name', $arg );
108 $value = $xpath->query( 'value', $arg );
109 if ( $nameNodes->item( 0 )->hasAttributes() ) {
110 // Numbered parameter
111 $index = $nameNodes->item( 0 )->attributes->getNamedItem( 'index' )->textContent;
112 $index = $index - $indexOffset;
113 if ( isset( $namedArgs[$index] ) || isset( $numberedArgs[$index] ) ) {
114 $this->parser->getOutput()->addWarning( wfMessage( 'duplicate-args-warning',
115 wfEscapeWikiText( $this->title ),
116 wfEscapeWikiText( $title ),
117 wfEscapeWikiText( $index ) )->text() );
118 $this->parser->addTrackingCategory( 'duplicate-args-category' );
119 }
120 $numberedArgs[$index] = $value->item( 0 );
121 unset( $namedArgs[$index] );
122 } else {
123 // Named parameter
124 $name = trim( $this->expand( $nameNodes->item( 0 ), PPFrame::STRIP_COMMENTS ) );
125 if ( isset( $namedArgs[$name] ) || isset( $numberedArgs[$name] ) ) {
126 $this->parser->getOutput()->addWarning( wfMessage( 'duplicate-args-warning',
127 wfEscapeWikiText( $this->title ),
128 wfEscapeWikiText( $title ),
129 wfEscapeWikiText( $name ) )->text() );
130 $this->parser->addTrackingCategory( 'duplicate-args-category' );
131 }
132 $namedArgs[$name] = $value->item( 0 );
133 unset( $numberedArgs[$name] );
134 }
135 }
136 }
137 return new PPTemplateFrame_DOM( $this->preprocessor, $this, $numberedArgs, $namedArgs, $title );
138 }
139
140 /**
141 * @throws MWException
142 * @param string|int $key
143 * @param string|PPNode_DOM|DOMDocument $root
144 * @param int $flags
145 * @return string
146 */
147 public function cachedExpand( $key, $root, $flags = 0 ) {
148 // we don't have a parent, so we don't have a cache
149 return $this->expand( $root, $flags );
150 }
151
152 /**
153 * @throws MWException
154 * @param string|PPNode_DOM|DOMDocument $root
155 * @param int $flags
156 * @return string
157 */
158 public function expand( $root, $flags = 0 ) {
159 static $expansionDepth = 0;
160 if ( is_string( $root ) ) {
161 return $root;
162 }
163
164 if ( ++$this->parser->mPPNodeCount > $this->parser->mOptions->getMaxPPNodeCount() ) {
165 $this->parser->limitationWarn( 'node-count-exceeded',
166 $this->parser->mPPNodeCount,
167 $this->parser->mOptions->getMaxPPNodeCount()
168 );
169 return '<span class="error">Node-count limit exceeded</span>';
170 }
171
172 if ( $expansionDepth > $this->parser->mOptions->getMaxPPExpandDepth() ) {
173 $this->parser->limitationWarn( 'expansion-depth-exceeded',
174 $expansionDepth,
175 $this->parser->mOptions->getMaxPPExpandDepth()
176 );
177 return '<span class="error">Expansion depth limit exceeded</span>';
178 }
179 ++$expansionDepth;
180 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
181 $this->parser->mHighestExpansionDepth = $expansionDepth;
182 }
183
184 if ( $root instanceof PPNode_DOM ) {
185 $root = $root->node;
186 }
187 if ( $root instanceof DOMDocument ) {
188 $root = $root->documentElement;
189 }
190
191 $outStack = [ '', '' ];
192 $iteratorStack = [ false, $root ];
193 $indexStack = [ 0, 0 ];
194
195 while ( count( $iteratorStack ) > 1 ) {
196 $level = count( $outStack ) - 1;
197 $iteratorNode =& $iteratorStack[$level];
198 $out =& $outStack[$level];
199 $index =& $indexStack[$level];
200
201 if ( $iteratorNode instanceof PPNode_DOM ) {
202 $iteratorNode = $iteratorNode->node;
203 }
204
205 if ( is_array( $iteratorNode ) ) {
206 if ( $index >= count( $iteratorNode ) ) {
207 // All done with this iterator
208 $iteratorStack[$level] = false;
209 $contextNode = false;
210 } else {
211 $contextNode = $iteratorNode[$index];
212 $index++;
213 }
214 } elseif ( $iteratorNode instanceof DOMNodeList ) {
215 if ( $index >= $iteratorNode->length ) {
216 // All done with this iterator
217 $iteratorStack[$level] = false;
218 $contextNode = false;
219 } else {
220 $contextNode = $iteratorNode->item( $index );
221 $index++;
222 }
223 } else {
224 // Copy to $contextNode and then delete from iterator stack,
225 // because this is not an iterator but we do have to execute it once
226 $contextNode = $iteratorStack[$level];
227 $iteratorStack[$level] = false;
228 }
229
230 if ( $contextNode instanceof PPNode_DOM ) {
231 $contextNode = $contextNode->node;
232 }
233
234 $newIterator = false;
235
236 if ( $contextNode === false ) {
237 // nothing to do
238 } elseif ( is_string( $contextNode ) ) {
239 $out .= $contextNode;
240 } elseif ( is_array( $contextNode ) || $contextNode instanceof DOMNodeList ) {
241 $newIterator = $contextNode;
242 } elseif ( $contextNode instanceof DOMNode ) {
243 if ( $contextNode->nodeType == XML_TEXT_NODE ) {
244 $out .= $contextNode->nodeValue;
245 } elseif ( $contextNode->nodeName == 'template' ) {
246 # Double-brace expansion
247 $xpath = new DOMXPath( $contextNode->ownerDocument );
248 $titles = $xpath->query( 'title', $contextNode );
249 $title = $titles->item( 0 );
250 $parts = $xpath->query( 'part', $contextNode );
251 if ( $flags & PPFrame::NO_TEMPLATES ) {
252 $newIterator = $this->virtualBracketedImplode( '{{', '|', '}}', $title, $parts );
253 } else {
254 $lineStart = $contextNode->getAttribute( 'lineStart' );
255 $params = [
256 'title' => new PPNode_DOM( $title ),
257 'parts' => new PPNode_DOM( $parts ),
258 'lineStart' => $lineStart ];
259 $ret = $this->parser->braceSubstitution( $params, $this );
260 if ( isset( $ret['object'] ) ) {
261 $newIterator = $ret['object'];
262 } else {
263 $out .= $ret['text'];
264 }
265 }
266 } elseif ( $contextNode->nodeName == 'tplarg' ) {
267 # Triple-brace expansion
268 $xpath = new DOMXPath( $contextNode->ownerDocument );
269 $titles = $xpath->query( 'title', $contextNode );
270 $title = $titles->item( 0 );
271 $parts = $xpath->query( 'part', $contextNode );
272 if ( $flags & PPFrame::NO_ARGS ) {
273 $newIterator = $this->virtualBracketedImplode( '{{{', '|', '}}}', $title, $parts );
274 } else {
275 $params = [
276 'title' => new PPNode_DOM( $title ),
277 'parts' => new PPNode_DOM( $parts ) ];
278 $ret = $this->parser->argSubstitution( $params, $this );
279 if ( isset( $ret['object'] ) ) {
280 $newIterator = $ret['object'];
281 } else {
282 $out .= $ret['text'];
283 }
284 }
285 } elseif ( $contextNode->nodeName == 'comment' ) {
286 # HTML-style comment
287 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
288 # Not in RECOVER_COMMENTS mode (msgnw) though.
289 if ( ( $this->parser->ot['html']
290 || ( $this->parser->ot['pre'] && $this->parser->mOptions->getRemoveComments() )
291 || ( $flags & PPFrame::STRIP_COMMENTS )
292 ) && !( $flags & PPFrame::RECOVER_COMMENTS )
293 ) {
294 $out .= '';
295 } elseif ( $this->parser->ot['wiki'] && !( $flags & PPFrame::RECOVER_COMMENTS ) ) {
296 # Add a strip marker in PST mode so that pstPass2() can
297 # run some old-fashioned regexes on the result.
298 # Not in RECOVER_COMMENTS mode (extractSections) though.
299 $out .= $this->parser->insertStripItem( $contextNode->textContent );
300 } else {
301 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
302 $out .= $contextNode->textContent;
303 }
304 } elseif ( $contextNode->nodeName == 'ignore' ) {
305 # Output suppression used by <includeonly> etc.
306 # OT_WIKI will only respect <ignore> in substed templates.
307 # The other output types respect it unless NO_IGNORE is set.
308 # extractSections() sets NO_IGNORE and so never respects it.
309 if ( ( !isset( $this->parent ) && $this->parser->ot['wiki'] )
310 || ( $flags & PPFrame::NO_IGNORE )
311 ) {
312 $out .= $contextNode->textContent;
313 } else {
314 $out .= '';
315 }
316 } elseif ( $contextNode->nodeName == 'ext' ) {
317 # Extension tag
318 $xpath = new DOMXPath( $contextNode->ownerDocument );
319 $names = $xpath->query( 'name', $contextNode );
320 $attrs = $xpath->query( 'attr', $contextNode );
321 $inners = $xpath->query( 'inner', $contextNode );
322 $closes = $xpath->query( 'close', $contextNode );
323 if ( $flags & PPFrame::NO_TAGS ) {
324 $s = '<' . $this->expand( $names->item( 0 ), $flags );
325 if ( $attrs->length > 0 ) {
326 $s .= $this->expand( $attrs->item( 0 ), $flags );
327 }
328 if ( $inners->length > 0 ) {
329 $s .= '>' . $this->expand( $inners->item( 0 ), $flags );
330 if ( $closes->length > 0 ) {
331 $s .= $this->expand( $closes->item( 0 ), $flags );
332 }
333 } else {
334 $s .= '/>';
335 }
336 $out .= $s;
337 } else {
338 $params = [
339 'name' => new PPNode_DOM( $names->item( 0 ) ),
340 'attr' => $attrs->length > 0 ? new PPNode_DOM( $attrs->item( 0 ) ) : null,
341 'inner' => $inners->length > 0 ? new PPNode_DOM( $inners->item( 0 ) ) : null,
342 'close' => $closes->length > 0 ? new PPNode_DOM( $closes->item( 0 ) ) : null,
343 ];
344 $out .= $this->parser->extensionSubstitution( $params, $this );
345 }
346 } elseif ( $contextNode->nodeName == 'h' ) {
347 # Heading
348 $s = $this->expand( $contextNode->childNodes, $flags );
349
350 # Insert a heading marker only for <h> children of <root>
351 # This is to stop extractSections from going over multiple tree levels
352 if ( $contextNode->parentNode->nodeName == 'root' && $this->parser->ot['html'] ) {
353 # Insert heading index marker
354 $headingIndex = $contextNode->getAttribute( 'i' );
355 $titleText = $this->title->getPrefixedDBkey();
356 $this->parser->mHeadings[] = [ $titleText, $headingIndex ];
357 $serial = count( $this->parser->mHeadings ) - 1;
358 $marker = Parser::MARKER_PREFIX . "-h-$serial-" . Parser::MARKER_SUFFIX;
359 $count = $contextNode->getAttribute( 'level' );
360 $s = substr( $s, 0, $count ) . $marker . substr( $s, $count );
361 $this->parser->mStripState->addGeneral( $marker, '' );
362 }
363 $out .= $s;
364 } else {
365 # Generic recursive expansion
366 $newIterator = $contextNode->childNodes;
367 }
368 } else {
369 throw new MWException( __METHOD__ . ': Invalid parameter type' );
370 }
371
372 if ( $newIterator !== false ) {
373 if ( $newIterator instanceof PPNode_DOM ) {
374 $newIterator = $newIterator->node;
375 }
376 $outStack[] = '';
377 $iteratorStack[] = $newIterator;
378 $indexStack[] = 0;
379 } elseif ( $iteratorStack[$level] === false ) {
380 // Return accumulated value to parent
381 // With tail recursion
382 while ( $iteratorStack[$level] === false && $level > 0 ) {
383 $outStack[$level - 1] .= $out;
384 array_pop( $outStack );
385 array_pop( $iteratorStack );
386 array_pop( $indexStack );
387 $level--;
388 }
389 }
390 }
391 --$expansionDepth;
392 return $outStack[0];
393 }
394
395 /**
396 * @param string $sep
397 * @param int $flags
398 * @param string|PPNode_DOM|DOMDocument ...$args
399 * @return string
400 */
401 public function implodeWithFlags( $sep, $flags, ...$args ) {
402 $first = true;
403 $s = '';
404 foreach ( $args as $root ) {
405 if ( $root instanceof PPNode_DOM ) {
406 $root = $root->node;
407 }
408 if ( !is_array( $root ) && !( $root instanceof DOMNodeList ) ) {
409 $root = [ $root ];
410 }
411 foreach ( $root as $node ) {
412 if ( $first ) {
413 $first = false;
414 } else {
415 $s .= $sep;
416 }
417 $s .= $this->expand( $node, $flags );
418 }
419 }
420 return $s;
421 }
422
423 /**
424 * Implode with no flags specified
425 * This previously called implodeWithFlags but has now been inlined to reduce stack depth
426 *
427 * @param string $sep
428 * @param string|PPNode_DOM|DOMDocument ...$args
429 * @return string
430 */
431 public function implode( $sep, ...$args ) {
432 $first = true;
433 $s = '';
434 foreach ( $args as $root ) {
435 if ( $root instanceof PPNode_DOM ) {
436 $root = $root->node;
437 }
438 if ( !is_array( $root ) && !( $root instanceof DOMNodeList ) ) {
439 $root = [ $root ];
440 }
441 foreach ( $root as $node ) {
442 if ( $first ) {
443 $first = false;
444 } else {
445 $s .= $sep;
446 }
447 $s .= $this->expand( $node );
448 }
449 }
450 return $s;
451 }
452
453 /**
454 * Makes an object that, when expand()ed, will be the same as one obtained
455 * with implode()
456 *
457 * @param string $sep
458 * @param string|PPNode_DOM|DOMDocument ...$args
459 * @return array
460 */
461 public function virtualImplode( $sep, ...$args ) {
462 $out = [];
463 $first = true;
464
465 foreach ( $args as $root ) {
466 if ( $root instanceof PPNode_DOM ) {
467 $root = $root->node;
468 }
469 if ( !is_array( $root ) && !( $root instanceof DOMNodeList ) ) {
470 $root = [ $root ];
471 }
472 foreach ( $root as $node ) {
473 if ( $first ) {
474 $first = false;
475 } else {
476 $out[] = $sep;
477 }
478 $out[] = $node;
479 }
480 }
481 return $out;
482 }
483
484 /**
485 * Virtual implode with brackets
486 * @param string $start
487 * @param string $sep
488 * @param string $end
489 * @param string|PPNode_DOM|DOMDocument ...$args
490 * @return array
491 */
492 public function virtualBracketedImplode( $start, $sep, $end, ...$args ) {
493 $out = [ $start ];
494 $first = true;
495
496 foreach ( $args as $root ) {
497 if ( $root instanceof PPNode_DOM ) {
498 $root = $root->node;
499 }
500 if ( !is_array( $root ) && !( $root instanceof DOMNodeList ) ) {
501 $root = [ $root ];
502 }
503 foreach ( $root as $node ) {
504 if ( $first ) {
505 $first = false;
506 } else {
507 $out[] = $sep;
508 }
509 $out[] = $node;
510 }
511 }
512 $out[] = $end;
513 return $out;
514 }
515
516 public function __toString() {
517 return 'frame{}';
518 }
519
520 public function getPDBK( $level = false ) {
521 if ( $level === false ) {
522 return $this->title->getPrefixedDBkey();
523 } else {
524 return $this->titleCache[$level] ?? false;
525 }
526 }
527
528 /**
529 * @return array
530 */
531 public function getArguments() {
532 return [];
533 }
534
535 /**
536 * @return array
537 */
538 public function getNumberedArguments() {
539 return [];
540 }
541
542 /**
543 * @return array
544 */
545 public function getNamedArguments() {
546 return [];
547 }
548
549 /**
550 * Returns true if there are no arguments in this frame
551 *
552 * @return bool
553 */
554 public function isEmpty() {
555 return true;
556 }
557
558 /**
559 * @param int|string $name
560 * @return bool Always false in this implementation.
561 */
562 public function getArgument( $name ) {
563 return false;
564 }
565
566 /**
567 * Returns true if the infinite loop check is OK, false if a loop is detected
568 *
569 * @param Title $title
570 * @return bool
571 */
572 public function loopCheck( $title ) {
573 return !isset( $this->loopCheckHash[$title->getPrefixedDBkey()] );
574 }
575
576 /**
577 * Return true if the frame is a template frame
578 *
579 * @return bool
580 */
581 public function isTemplate() {
582 return false;
583 }
584
585 /**
586 * Get a title of frame
587 *
588 * @return Title
589 */
590 public function getTitle() {
591 return $this->title;
592 }
593
594 /**
595 * Set the volatile flag
596 *
597 * @param bool $flag
598 */
599 public function setVolatile( $flag = true ) {
600 $this->volatile = $flag;
601 }
602
603 /**
604 * Get the volatile flag
605 *
606 * @return bool
607 */
608 public function isVolatile() {
609 return $this->volatile;
610 }
611
612 /**
613 * Set the TTL
614 *
615 * @param int $ttl
616 */
617 public function setTTL( $ttl ) {
618 if ( $ttl !== null && ( $this->ttl === null || $ttl < $this->ttl ) ) {
619 $this->ttl = $ttl;
620 }
621 }
622
623 /**
624 * Get the TTL
625 *
626 * @return int|null
627 */
628 public function getTTL() {
629 return $this->ttl;
630 }
631 }