Split parser related files to have one class in one file
[lhc/web/wiklou.git] / includes / parser / PPFrame_Hash.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_Hash implements PPFrame {
28
29 /**
30 * @var Parser
31 */
32 public $parser;
33
34 /**
35 * @var Preprocessor
36 */
37 public $preprocessor;
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 array|bool|PPNode_Hash_Array $args
84 * @param Title|bool $title
85 * @param int $indexOffset
86 * @throws MWException
87 * @return PPTemplateFrame_Hash
88 */
89 public function newChild( $args = false, $title = false, $indexOffset = 0 ) {
90 $namedArgs = [];
91 $numberedArgs = [];
92 if ( $title === false ) {
93 $title = $this->title;
94 }
95 if ( $args !== false ) {
96 if ( $args instanceof PPNode_Hash_Array ) {
97 $args = $args->value;
98 } elseif ( !is_array( $args ) ) {
99 throw new MWException( __METHOD__ . ': $args must be array or PPNode_Hash_Array' );
100 }
101 foreach ( $args as $arg ) {
102 $bits = $arg->splitArg();
103 if ( $bits['index'] !== '' ) {
104 // Numbered parameter
105 $index = $bits['index'] - $indexOffset;
106 if ( isset( $namedArgs[$index] ) || isset( $numberedArgs[$index] ) ) {
107 $this->parser->getOutput()->addWarning( wfMessage( 'duplicate-args-warning',
108 wfEscapeWikiText( $this->title ),
109 wfEscapeWikiText( $title ),
110 wfEscapeWikiText( $index ) )->text() );
111 $this->parser->addTrackingCategory( 'duplicate-args-category' );
112 }
113 $numberedArgs[$index] = $bits['value'];
114 unset( $namedArgs[$index] );
115 } else {
116 // Named parameter
117 $name = trim( $this->expand( $bits['name'], PPFrame::STRIP_COMMENTS ) );
118 if ( isset( $namedArgs[$name] ) || isset( $numberedArgs[$name] ) ) {
119 $this->parser->getOutput()->addWarning( wfMessage( 'duplicate-args-warning',
120 wfEscapeWikiText( $this->title ),
121 wfEscapeWikiText( $title ),
122 wfEscapeWikiText( $name ) )->text() );
123 $this->parser->addTrackingCategory( 'duplicate-args-category' );
124 }
125 $namedArgs[$name] = $bits['value'];
126 unset( $numberedArgs[$name] );
127 }
128 }
129 }
130 return new PPTemplateFrame_Hash( $this->preprocessor, $this, $numberedArgs, $namedArgs, $title );
131 }
132
133 /**
134 * @throws MWException
135 * @param string|int $key
136 * @param string|PPNode $root
137 * @param int $flags
138 * @return string
139 */
140 public function cachedExpand( $key, $root, $flags = 0 ) {
141 // we don't have a parent, so we don't have a cache
142 return $this->expand( $root, $flags );
143 }
144
145 /**
146 * @throws MWException
147 * @param string|PPNode $root
148 * @param int $flags
149 * @return string
150 */
151 public function expand( $root, $flags = 0 ) {
152 static $expansionDepth = 0;
153 if ( is_string( $root ) ) {
154 return $root;
155 }
156
157 if ( ++$this->parser->mPPNodeCount > $this->parser->mOptions->getMaxPPNodeCount() ) {
158 $this->parser->limitationWarn( 'node-count-exceeded',
159 $this->parser->mPPNodeCount,
160 $this->parser->mOptions->getMaxPPNodeCount()
161 );
162 return '<span class="error">Node-count limit exceeded</span>';
163 }
164 if ( $expansionDepth > $this->parser->mOptions->getMaxPPExpandDepth() ) {
165 $this->parser->limitationWarn( 'expansion-depth-exceeded',
166 $expansionDepth,
167 $this->parser->mOptions->getMaxPPExpandDepth()
168 );
169 return '<span class="error">Expansion depth limit exceeded</span>';
170 }
171 ++$expansionDepth;
172 if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
173 $this->parser->mHighestExpansionDepth = $expansionDepth;
174 }
175
176 $outStack = [ '', '' ];
177 $iteratorStack = [ false, $root ];
178 $indexStack = [ 0, 0 ];
179
180 while ( count( $iteratorStack ) > 1 ) {
181 $level = count( $outStack ) - 1;
182 $iteratorNode =& $iteratorStack[$level];
183 $out =& $outStack[$level];
184 $index =& $indexStack[$level];
185
186 if ( is_array( $iteratorNode ) ) {
187 if ( $index >= count( $iteratorNode ) ) {
188 // All done with this iterator
189 $iteratorStack[$level] = false;
190 $contextNode = false;
191 } else {
192 $contextNode = $iteratorNode[$index];
193 $index++;
194 }
195 } elseif ( $iteratorNode instanceof PPNode_Hash_Array ) {
196 if ( $index >= $iteratorNode->getLength() ) {
197 // All done with this iterator
198 $iteratorStack[$level] = false;
199 $contextNode = false;
200 } else {
201 $contextNode = $iteratorNode->item( $index );
202 $index++;
203 }
204 } else {
205 // Copy to $contextNode and then delete from iterator stack,
206 // because this is not an iterator but we do have to execute it once
207 $contextNode = $iteratorStack[$level];
208 $iteratorStack[$level] = false;
209 }
210
211 $newIterator = false;
212 $contextName = false;
213 $contextChildren = false;
214
215 if ( $contextNode === false ) {
216 // nothing to do
217 } elseif ( is_string( $contextNode ) ) {
218 $out .= $contextNode;
219 } elseif ( $contextNode instanceof PPNode_Hash_Array ) {
220 $newIterator = $contextNode;
221 } elseif ( $contextNode instanceof PPNode_Hash_Attr ) {
222 // No output
223 } elseif ( $contextNode instanceof PPNode_Hash_Text ) {
224 $out .= $contextNode->value;
225 } elseif ( $contextNode instanceof PPNode_Hash_Tree ) {
226 $contextName = $contextNode->name;
227 $contextChildren = $contextNode->getRawChildren();
228 } elseif ( is_array( $contextNode ) ) {
229 // Node descriptor array
230 if ( count( $contextNode ) !== 2 ) {
231 throw new MWException( __METHOD__ .
232 ': found an array where a node descriptor should be' );
233 }
234 list( $contextName, $contextChildren ) = $contextNode;
235 } else {
236 throw new MWException( __METHOD__ . ': Invalid parameter type' );
237 }
238
239 // Handle node descriptor array or tree object
240 if ( $contextName === false ) {
241 // Not a node, already handled above
242 } elseif ( $contextName[0] === '@' ) {
243 // Attribute: no output
244 } elseif ( $contextName === 'template' ) {
245 # Double-brace expansion
246 $bits = PPNode_Hash_Tree::splitRawTemplate( $contextChildren );
247 if ( $flags & PPFrame::NO_TEMPLATES ) {
248 $newIterator = $this->virtualBracketedImplode(
249 '{{', '|', '}}',
250 $bits['title'],
251 $bits['parts']
252 );
253 } else {
254 $ret = $this->parser->braceSubstitution( $bits, $this );
255 if ( isset( $ret['object'] ) ) {
256 $newIterator = $ret['object'];
257 } else {
258 $out .= $ret['text'];
259 }
260 }
261 } elseif ( $contextName === 'tplarg' ) {
262 # Triple-brace expansion
263 $bits = PPNode_Hash_Tree::splitRawTemplate( $contextChildren );
264 if ( $flags & PPFrame::NO_ARGS ) {
265 $newIterator = $this->virtualBracketedImplode(
266 '{{{', '|', '}}}',
267 $bits['title'],
268 $bits['parts']
269 );
270 } else {
271 $ret = $this->parser->argSubstitution( $bits, $this );
272 if ( isset( $ret['object'] ) ) {
273 $newIterator = $ret['object'];
274 } else {
275 $out .= $ret['text'];
276 }
277 }
278 } elseif ( $contextName === 'comment' ) {
279 # HTML-style comment
280 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
281 # Not in RECOVER_COMMENTS mode (msgnw) though.
282 if ( ( $this->parser->ot['html']
283 || ( $this->parser->ot['pre'] && $this->parser->mOptions->getRemoveComments() )
284 || ( $flags & PPFrame::STRIP_COMMENTS )
285 ) && !( $flags & PPFrame::RECOVER_COMMENTS )
286 ) {
287 $out .= '';
288 } elseif ( $this->parser->ot['wiki'] && !( $flags & PPFrame::RECOVER_COMMENTS ) ) {
289 # Add a strip marker in PST mode so that pstPass2() can
290 # run some old-fashioned regexes on the result.
291 # Not in RECOVER_COMMENTS mode (extractSections) though.
292 $out .= $this->parser->insertStripItem( $contextChildren[0] );
293 } else {
294 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
295 $out .= $contextChildren[0];
296 }
297 } elseif ( $contextName === 'ignore' ) {
298 # Output suppression used by <includeonly> etc.
299 # OT_WIKI will only respect <ignore> in substed templates.
300 # The other output types respect it unless NO_IGNORE is set.
301 # extractSections() sets NO_IGNORE and so never respects it.
302 if ( ( !isset( $this->parent ) && $this->parser->ot['wiki'] )
303 || ( $flags & PPFrame::NO_IGNORE )
304 ) {
305 $out .= $contextChildren[0];
306 } else {
307 // $out .= '';
308 }
309 } elseif ( $contextName === 'ext' ) {
310 # Extension tag
311 $bits = PPNode_Hash_Tree::splitRawExt( $contextChildren ) +
312 [ 'attr' => null, 'inner' => null, 'close' => null ];
313 if ( $flags & PPFrame::NO_TAGS ) {
314 $s = '<' . $bits['name']->getFirstChild()->value;
315 if ( $bits['attr'] ) {
316 $s .= $bits['attr']->getFirstChild()->value;
317 }
318 if ( $bits['inner'] ) {
319 $s .= '>' . $bits['inner']->getFirstChild()->value;
320 if ( $bits['close'] ) {
321 $s .= $bits['close']->getFirstChild()->value;
322 }
323 } else {
324 $s .= '/>';
325 }
326 $out .= $s;
327 } else {
328 $out .= $this->parser->extensionSubstitution( $bits, $this );
329 }
330 } elseif ( $contextName === 'h' ) {
331 # Heading
332 if ( $this->parser->ot['html'] ) {
333 # Expand immediately and insert heading index marker
334 $s = $this->expand( $contextChildren, $flags );
335 $bits = PPNode_Hash_Tree::splitRawHeading( $contextChildren );
336 $titleText = $this->title->getPrefixedDBkey();
337 $this->parser->mHeadings[] = [ $titleText, $bits['i'] ];
338 $serial = count( $this->parser->mHeadings ) - 1;
339 $marker = Parser::MARKER_PREFIX . "-h-$serial-" . Parser::MARKER_SUFFIX;
340 $s = substr( $s, 0, $bits['level'] ) . $marker . substr( $s, $bits['level'] );
341 $this->parser->mStripState->addGeneral( $marker, '' );
342 $out .= $s;
343 } else {
344 # Expand in virtual stack
345 $newIterator = $contextChildren;
346 }
347 } else {
348 # Generic recursive expansion
349 $newIterator = $contextChildren;
350 }
351
352 if ( $newIterator !== false ) {
353 $outStack[] = '';
354 $iteratorStack[] = $newIterator;
355 $indexStack[] = 0;
356 } elseif ( $iteratorStack[$level] === false ) {
357 // Return accumulated value to parent
358 // With tail recursion
359 while ( $iteratorStack[$level] === false && $level > 0 ) {
360 $outStack[$level - 1] .= $out;
361 array_pop( $outStack );
362 array_pop( $iteratorStack );
363 array_pop( $indexStack );
364 $level--;
365 }
366 }
367 }
368 --$expansionDepth;
369 return $outStack[0];
370 }
371
372 /**
373 * @param string $sep
374 * @param int $flags
375 * @param string|PPNode ...$args
376 * @return string
377 */
378 public function implodeWithFlags( $sep, $flags, ...$args ) {
379 $first = true;
380 $s = '';
381 foreach ( $args as $root ) {
382 if ( $root instanceof PPNode_Hash_Array ) {
383 $root = $root->value;
384 }
385 if ( !is_array( $root ) ) {
386 $root = [ $root ];
387 }
388 foreach ( $root as $node ) {
389 if ( $first ) {
390 $first = false;
391 } else {
392 $s .= $sep;
393 }
394 $s .= $this->expand( $node, $flags );
395 }
396 }
397 return $s;
398 }
399
400 /**
401 * Implode with no flags specified
402 * This previously called implodeWithFlags but has now been inlined to reduce stack depth
403 * @param string $sep
404 * @param string|PPNode ...$args
405 * @return string
406 */
407 public function implode( $sep, ...$args ) {
408 $first = true;
409 $s = '';
410 foreach ( $args as $root ) {
411 if ( $root instanceof PPNode_Hash_Array ) {
412 $root = $root->value;
413 }
414 if ( !is_array( $root ) ) {
415 $root = [ $root ];
416 }
417 foreach ( $root as $node ) {
418 if ( $first ) {
419 $first = false;
420 } else {
421 $s .= $sep;
422 }
423 $s .= $this->expand( $node );
424 }
425 }
426 return $s;
427 }
428
429 /**
430 * Makes an object that, when expand()ed, will be the same as one obtained
431 * with implode()
432 *
433 * @param string $sep
434 * @param string|PPNode ...$args
435 * @return PPNode_Hash_Array
436 */
437 public function virtualImplode( $sep, ...$args ) {
438 $out = [];
439 $first = true;
440
441 foreach ( $args as $root ) {
442 if ( $root instanceof PPNode_Hash_Array ) {
443 $root = $root->value;
444 }
445 if ( !is_array( $root ) ) {
446 $root = [ $root ];
447 }
448 foreach ( $root as $node ) {
449 if ( $first ) {
450 $first = false;
451 } else {
452 $out[] = $sep;
453 }
454 $out[] = $node;
455 }
456 }
457 return new PPNode_Hash_Array( $out );
458 }
459
460 /**
461 * Virtual implode with brackets
462 *
463 * @param string $start
464 * @param string $sep
465 * @param string $end
466 * @param string|PPNode ...$args
467 * @return PPNode_Hash_Array
468 */
469 public function virtualBracketedImplode( $start, $sep, $end, ...$args ) {
470 $out = [ $start ];
471 $first = true;
472
473 foreach ( $args as $root ) {
474 if ( $root instanceof PPNode_Hash_Array ) {
475 $root = $root->value;
476 }
477 if ( !is_array( $root ) ) {
478 $root = [ $root ];
479 }
480 foreach ( $root as $node ) {
481 if ( $first ) {
482 $first = false;
483 } else {
484 $out[] = $sep;
485 }
486 $out[] = $node;
487 }
488 }
489 $out[] = $end;
490 return new PPNode_Hash_Array( $out );
491 }
492
493 public function __toString() {
494 return 'frame{}';
495 }
496
497 /**
498 * @param bool $level
499 * @return array|bool|string
500 */
501 public function getPDBK( $level = false ) {
502 if ( $level === false ) {
503 return $this->title->getPrefixedDBkey();
504 } else {
505 return $this->titleCache[$level] ?? false;
506 }
507 }
508
509 /**
510 * @return array
511 */
512 public function getArguments() {
513 return [];
514 }
515
516 /**
517 * @return array
518 */
519 public function getNumberedArguments() {
520 return [];
521 }
522
523 /**
524 * @return array
525 */
526 public function getNamedArguments() {
527 return [];
528 }
529
530 /**
531 * Returns true if there are no arguments in this frame
532 *
533 * @return bool
534 */
535 public function isEmpty() {
536 return true;
537 }
538
539 /**
540 * @param int|string $name
541 * @return bool Always false in this implementation.
542 */
543 public function getArgument( $name ) {
544 return false;
545 }
546
547 /**
548 * Returns true if the infinite loop check is OK, false if a loop is detected
549 *
550 * @param Title $title
551 *
552 * @return bool
553 */
554 public function loopCheck( $title ) {
555 return !isset( $this->loopCheckHash[$title->getPrefixedDBkey()] );
556 }
557
558 /**
559 * Return true if the frame is a template frame
560 *
561 * @return bool
562 */
563 public function isTemplate() {
564 return false;
565 }
566
567 /**
568 * Get a title of frame
569 *
570 * @return Title
571 */
572 public function getTitle() {
573 return $this->title;
574 }
575
576 /**
577 * Set the volatile flag
578 *
579 * @param bool $flag
580 */
581 public function setVolatile( $flag = true ) {
582 $this->volatile = $flag;
583 }
584
585 /**
586 * Get the volatile flag
587 *
588 * @return bool
589 */
590 public function isVolatile() {
591 return $this->volatile;
592 }
593
594 /**
595 * Set the TTL
596 *
597 * @param int $ttl
598 */
599 public function setTTL( $ttl ) {
600 if ( $ttl !== null && ( $this->ttl === null || $ttl < $this->ttl ) ) {
601 $this->ttl = $ttl;
602 }
603 }
604
605 /**
606 * Get the TTL
607 *
608 * @return int|null
609 */
610 public function getTTL() {
611 return $this->ttl;
612 }
613 }