3 * Preprocessor using PHP arrays
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Differences from DOM schema:
26 * * attribute nodes are children
27 * * "<h>" nodes that aren't at the top are replaced with <possible-h>
30 class Preprocessor_Hash
implements Preprocessor
{
36 const CACHE_VERSION
= 1;
38 function __construct( $parser ) {
39 $this->parser
= $parser;
43 * @return PPFrame_Hash
46 return new PPFrame_Hash( $this );
51 * @return PPCustomFrame_Hash
53 function newCustomFrame( $args ) {
54 return new PPCustomFrame_Hash( $this, $args );
58 * @param $values array
59 * @return PPNode_Hash_Array
61 function newPartNodeArray( $values ) {
64 foreach ( $values as $k => $val ) {
65 $partNode = new PPNode_Hash_Tree( 'part' );
66 $nameNode = new PPNode_Hash_Tree( 'name' );
69 $nameNode->addChild( new PPNode_Hash_Attr( 'index', $k ) );
70 $partNode->addChild( $nameNode );
72 $nameNode->addChild( new PPNode_Hash_Text( $k ) );
73 $partNode->addChild( $nameNode );
74 $partNode->addChild( new PPNode_Hash_Text( '=' ) );
77 $valueNode = new PPNode_Hash_Tree( 'value' );
78 $valueNode->addChild( new PPNode_Hash_Text( $val ) );
79 $partNode->addChild( $valueNode );
84 $node = new PPNode_Hash_Array( $list );
89 * Preprocess some wikitext and return the document tree.
90 * This is the ghost of Parser::replace_variables().
92 * @param $text String: the text to parse
93 * @param $flags Integer: bitwise combination of:
94 * Parser::PTD_FOR_INCLUSION Handle "<noinclude>" and "<includeonly>" as if the text is being
95 * included. Default is to assume a direct page view.
97 * The generated DOM tree must depend only on the input text and the flags.
98 * The DOM tree must be the same in OT_HTML and OT_WIKI mode, to avoid a regression of bug 4899.
100 * Any flag added to the $flags parameter here, or any other parameter liable to cause a
101 * change in the DOM tree for a given text, must be passed through the section identifier
102 * in the section edit link and thus back to extractSections().
104 * The output of this function is currently only cached in process memory, but a persistent
105 * cache may be implemented at a later date which takes further advantage of these strict
106 * dependency requirements.
108 * @throws MWException
109 * @return PPNode_Hash_Tree
111 function preprocessToObj( $text, $flags = 0 ) {
112 wfProfileIn( __METHOD__
);
115 global $wgMemc, $wgPreprocessorCacheThreshold;
117 $cacheable = $wgPreprocessorCacheThreshold !== false && strlen( $text ) > $wgPreprocessorCacheThreshold;
119 wfProfileIn( __METHOD__
.'-cacheable' );
121 $cacheKey = wfMemcKey( 'preprocess-hash', md5($text), $flags );
122 $cacheValue = $wgMemc->get( $cacheKey );
124 $version = substr( $cacheValue, 0, 8 );
125 if ( intval( $version ) == self
::CACHE_VERSION
) {
126 $hash = unserialize( substr( $cacheValue, 8 ) );
128 wfDebugLog( "Preprocessor",
129 "Loaded preprocessor hash from memcached (key $cacheKey)" );
130 wfProfileOut( __METHOD__
.'-cacheable' );
131 wfProfileOut( __METHOD__
);
135 wfProfileIn( __METHOD__
.'-cache-miss' );
150 'names' => array( 2 => null ),
156 $forInclusion = $flags & Parser
::PTD_FOR_INCLUSION
;
158 $xmlishElements = $this->parser
->getStripList();
159 $enableOnlyinclude = false;
160 if ( $forInclusion ) {
161 $ignoredTags = array( 'includeonly', '/includeonly' );
162 $ignoredElements = array( 'noinclude' );
163 $xmlishElements[] = 'noinclude';
164 if ( strpos( $text, '<onlyinclude>' ) !== false && strpos( $text, '</onlyinclude>' ) !== false ) {
165 $enableOnlyinclude = true;
168 $ignoredTags = array( 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude' );
169 $ignoredElements = array( 'includeonly' );
170 $xmlishElements[] = 'includeonly';
172 $xmlishRegex = implode( '|', array_merge( $xmlishElements, $ignoredTags ) );
174 // Use "A" modifier (anchored) instead of "^", because ^ doesn't work with an offset
175 $elementsRegex = "~($xmlishRegex)(?:\s|\/>|>)|(!--)~iA";
177 $stack = new PPDStack_Hash
;
179 $searchBase = "[{<\n";
180 $revText = strrev( $text ); // For fast reverse searches
181 $lengthText = strlen( $text );
183 $i = 0; # Input pointer, starts out pointing to a pseudo-newline before the start
184 $accum =& $stack->getAccum(); # Current accumulator
185 $findEquals = false; # True to find equals signs in arguments
186 $findPipe = false; # True to take notice of pipe characters
188 $inHeading = false; # True if $i is inside a possible heading
189 $noMoreGT = false; # True if there are no more greater-than (>) signs right of $i
190 $findOnlyinclude = $enableOnlyinclude; # True to ignore all input up to the next <onlyinclude>
191 $fakeLineStart = true; # Do a line-start run without outputting an LF character
196 if ( $findOnlyinclude ) {
197 // Ignore all input up to the next <onlyinclude>
198 $startPos = strpos( $text, '<onlyinclude>', $i );
199 if ( $startPos === false ) {
200 // Ignored section runs to the end
201 $accum->addNodeWithText( 'ignore', substr( $text, $i ) );
204 $tagEndPos = $startPos +
strlen( '<onlyinclude>' ); // past-the-end
205 $accum->addNodeWithText( 'ignore', substr( $text, $i, $tagEndPos - $i ) );
207 $findOnlyinclude = false;
210 if ( $fakeLineStart ) {
211 $found = 'line-start';
214 # Find next opening brace, closing brace or pipe
215 $search = $searchBase;
216 if ( $stack->top
=== false ) {
217 $currentClosing = '';
219 $currentClosing = $stack->top
->close
;
220 $search .= $currentClosing;
226 // First equals will be for the template
230 # Output literal section, advance input counter
231 $literalLength = strcspn( $text, $search, $i );
232 if ( $literalLength > 0 ) {
233 $accum->addLiteral( substr( $text, $i, $literalLength ) );
234 $i +
= $literalLength;
236 if ( $i >= $lengthText ) {
237 if ( $currentClosing == "\n" ) {
238 // Do a past-the-end run to finish off the heading
246 $curChar = $text[$i];
247 if ( $curChar == '|' ) {
249 } elseif ( $curChar == '=' ) {
251 } elseif ( $curChar == '<' ) {
253 } elseif ( $curChar == "\n" ) {
257 $found = 'line-start';
259 } elseif ( $curChar == $currentClosing ) {
261 } elseif ( isset( $rules[$curChar] ) ) {
263 $rule = $rules[$curChar];
265 # Some versions of PHP have a strcspn which stops on null characters
266 # Ignore and continue
273 if ( $found == 'angle' ) {
275 // Handle </onlyinclude>
276 if ( $enableOnlyinclude && substr( $text, $i, strlen( '</onlyinclude>' ) ) == '</onlyinclude>' ) {
277 $findOnlyinclude = true;
281 // Determine element name
282 if ( !preg_match( $elementsRegex, $text, $matches, 0, $i +
1 ) ) {
283 // Element name missing or not listed
284 $accum->addLiteral( '<' );
289 if ( isset( $matches[2] ) && $matches[2] == '!--' ) {
290 // To avoid leaving blank lines, when a comment is both preceded
291 // and followed by a newline (ignoring spaces), trim leading and
292 // trailing spaces and one of the newlines.
295 $endPos = strpos( $text, '-->', $i +
4 );
296 if ( $endPos === false ) {
297 // Unclosed comment in input, runs to end
298 $inner = substr( $text, $i );
299 $accum->addNodeWithText( 'comment', $inner );
302 // Search backwards for leading whitespace
303 $wsStart = $i ?
( $i - strspn( $revText, ' ', $lengthText - $i ) ) : 0;
304 // Search forwards for trailing whitespace
305 // $wsEnd will be the position of the last space (or the '>' if there's none)
306 $wsEnd = $endPos +
2 +
strspn( $text, ' ', $endPos +
3 );
307 // Eat the line if possible
308 // TODO: This could theoretically be done if $wsStart == 0, i.e. for comments at
309 // the overall start. That's not how Sanitizer::removeHTMLcomments() did it, but
310 // it's a possible beneficial b/c break.
311 if ( $wsStart > 0 && substr( $text, $wsStart - 1, 1 ) == "\n"
312 && substr( $text, $wsEnd +
1, 1 ) == "\n" )
314 $startPos = $wsStart;
315 $endPos = $wsEnd +
1;
316 // Remove leading whitespace from the end of the accumulator
317 // Sanity check first though
318 $wsLength = $i - $wsStart;
320 && $accum->lastNode
instanceof PPNode_Hash_Text
321 && substr( $accum->lastNode
->value
, -$wsLength ) === str_repeat( ' ', $wsLength ) )
323 $accum->lastNode
->value
= substr( $accum->lastNode
->value
, 0, -$wsLength );
325 // Do a line-start run next time to look for headings after the comment
326 $fakeLineStart = true;
328 // No line to eat, just take the comment itself
334 $part = $stack->top
->getCurrentPart();
335 if ( ! (isset( $part->commentEnd
) && $part->commentEnd
== $wsStart - 1 )) {
336 $part->visualEnd
= $wsStart;
338 // Else comments abutting, no change in visual end
339 $part->commentEnd
= $endPos;
342 $inner = substr( $text, $startPos, $endPos - $startPos +
1 );
343 $accum->addNodeWithText( 'comment', $inner );
348 $lowerName = strtolower( $name );
349 $attrStart = $i +
strlen( $name ) +
1;
352 $tagEndPos = $noMoreGT ?
false : strpos( $text, '>', $attrStart );
353 if ( $tagEndPos === false ) {
354 // Infinite backtrack
355 // Disable tag search to prevent worst-case O(N^2) performance
357 $accum->addLiteral( '<' );
362 // Handle ignored tags
363 if ( in_array( $lowerName, $ignoredTags ) ) {
364 $accum->addNodeWithText( 'ignore', substr( $text, $i, $tagEndPos - $i +
1 ) );
370 if ( $text[$tagEndPos-1] == '/' ) {
372 $attrEnd = $tagEndPos - 1;
377 $attrEnd = $tagEndPos;
379 if ( preg_match( "/<\/" . preg_quote( $name, '/' ) . "\s*>/i",
380 $text, $matches, PREG_OFFSET_CAPTURE
, $tagEndPos +
1 ) )
382 $inner = substr( $text, $tagEndPos +
1, $matches[0][1] - $tagEndPos - 1 );
383 $i = $matches[0][1] +
strlen( $matches[0][0] );
384 $close = $matches[0][0];
386 // No end tag -- let it run out to the end of the text.
387 $inner = substr( $text, $tagEndPos +
1 );
392 // <includeonly> and <noinclude> just become <ignore> tags
393 if ( in_array( $lowerName, $ignoredElements ) ) {
394 $accum->addNodeWithText( 'ignore', substr( $text, $tagStartPos, $i - $tagStartPos ) );
398 if ( $attrEnd <= $attrStart ) {
401 // Note that the attr element contains the whitespace between name and attribute,
402 // this is necessary for precise reconstruction during pre-save transform.
403 $attr = substr( $text, $attrStart, $attrEnd - $attrStart );
406 $extNode = new PPNode_Hash_Tree( 'ext' );
407 $extNode->addChild( PPNode_Hash_Tree
::newWithText( 'name', $name ) );
408 $extNode->addChild( PPNode_Hash_Tree
::newWithText( 'attr', $attr ) );
409 if ( $inner !== null ) {
410 $extNode->addChild( PPNode_Hash_Tree
::newWithText( 'inner', $inner ) );
412 if ( $close !== null ) {
413 $extNode->addChild( PPNode_Hash_Tree
::newWithText( 'close', $close ) );
415 $accum->addNode( $extNode );
418 elseif ( $found == 'line-start' ) {
419 // Is this the start of a heading?
420 // Line break belongs before the heading element in any case
421 if ( $fakeLineStart ) {
422 $fakeLineStart = false;
424 $accum->addLiteral( $curChar );
428 $count = strspn( $text, '=', $i, 6 );
429 if ( $count == 1 && $findEquals ) {
430 // DWIM: This looks kind of like a name/value separator
431 // Let's let the equals handler have it and break the potential heading
432 // This is heuristic, but AFAICT the methods for completely correct disambiguation are very complex.
433 } elseif ( $count > 0 ) {
437 'parts' => array( new PPDPart_Hash( str_repeat( '=', $count ) ) ),
440 $stack->push( $piece );
441 $accum =& $stack->getAccum();
442 extract( $stack->getFlags() );
445 } elseif ( $found == 'line-end' ) {
446 $piece = $stack->top
;
447 // A heading must be open, otherwise \n wouldn't have been in the search list
448 assert( '$piece->open == "\n"' );
449 $part = $piece->getCurrentPart();
450 // Search back through the input to see if it has a proper close
451 // Do this using the reversed string since the other solutions (end anchor, etc.) are inefficient
452 $wsLength = strspn( $revText, " \t", $lengthText - $i );
453 $searchStart = $i - $wsLength;
454 if ( isset( $part->commentEnd
) && $searchStart - 1 == $part->commentEnd
) {
455 // Comment found at line end
456 // Search for equals signs before the comment
457 $searchStart = $part->visualEnd
;
458 $searchStart -= strspn( $revText, " \t", $lengthText - $searchStart );
460 $count = $piece->count
;
461 $equalsLength = strspn( $revText, '=', $lengthText - $searchStart );
462 if ( $equalsLength > 0 ) {
463 if ( $searchStart - $equalsLength == $piece->startPos
) {
464 // This is just a single string of equals signs on its own line
465 // Replicate the doHeadings behaviour /={count}(.+)={count}/
466 // First find out how many equals signs there really are (don't stop at 6)
467 $count = $equalsLength;
471 $count = min( 6, intval( ( $count - 1 ) / 2 ) );
474 $count = min( $equalsLength, $count );
477 // Normal match, output <h>
478 $element = new PPNode_Hash_Tree( 'possible-h' );
479 $element->addChild( new PPNode_Hash_Attr( 'level', $count ) );
480 $element->addChild( new PPNode_Hash_Attr( 'i', $headingIndex++
) );
481 $element->lastChild
->nextSibling
= $accum->firstNode
;
482 $element->lastChild
= $accum->lastNode
;
484 // Single equals sign on its own line, count=0
488 // No match, no <h>, just pass down the inner text
493 $accum =& $stack->getAccum();
494 extract( $stack->getFlags() );
496 // Append the result to the enclosing accumulator
497 if ( $element instanceof PPNode
) {
498 $accum->addNode( $element );
500 $accum->addAccum( $element );
502 // Note that we do NOT increment the input pointer.
503 // This is because the closing linebreak could be the opening linebreak of
504 // another heading. Infinite loops are avoided because the next iteration MUST
505 // hit the heading open case above, which unconditionally increments the
507 } elseif ( $found == 'open' ) {
508 # count opening brace characters
509 $count = strspn( $text, $curChar, $i );
511 # we need to add to stack only if opening brace count is enough for one of the rules
512 if ( $count >= $rule['min'] ) {
513 # Add it to the stack
516 'close' => $rule['end'],
518 'lineStart' => ($i > 0 && $text[$i-1] == "\n"),
521 $stack->push( $piece );
522 $accum =& $stack->getAccum();
523 extract( $stack->getFlags() );
525 # Add literal brace(s)
526 $accum->addLiteral( str_repeat( $curChar, $count ) );
529 } elseif ( $found == 'close' ) {
530 $piece = $stack->top
;
531 # lets check if there are enough characters for closing brace
532 $maxCount = $piece->count
;
533 $count = strspn( $text, $curChar, $i, $maxCount );
535 # check for maximum matching characters (if there are 5 closing
536 # characters, we will probably need only 3 - depending on the rules)
537 $rule = $rules[$piece->open
];
538 if ( $count > $rule['max'] ) {
539 # The specified maximum exists in the callback array, unless the caller
541 $matchingCount = $rule['max'];
543 # Count is less than the maximum
544 # Skip any gaps in the callback array to find the true largest match
545 # Need to use array_key_exists not isset because the callback can be null
546 $matchingCount = $count;
547 while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names'] ) ) {
552 if ($matchingCount <= 0) {
553 # No matching element found in callback array
554 # Output a literal closing brace and continue
555 $accum->addLiteral( str_repeat( $curChar, $count ) );
559 $name = $rule['names'][$matchingCount];
560 if ( $name === null ) {
561 // No element, just literal text
562 $element = $piece->breakSyntax( $matchingCount );
563 $element->addLiteral( str_repeat( $rule['end'], $matchingCount ) );
566 # Note: $parts is already XML, does not need to be encoded further
567 $parts = $piece->parts
;
568 $titleAccum = $parts[0]->out
;
571 $element = new PPNode_Hash_Tree( $name );
573 # The invocation is at the start of the line if lineStart is set in
574 # the stack, and all opening brackets are used up.
575 if ( $maxCount == $matchingCount && !empty( $piece->lineStart
) ) {
576 $element->addChild( new PPNode_Hash_Attr( 'lineStart', 1 ) );
578 $titleNode = new PPNode_Hash_Tree( 'title' );
579 $titleNode->firstChild
= $titleAccum->firstNode
;
580 $titleNode->lastChild
= $titleAccum->lastNode
;
581 $element->addChild( $titleNode );
583 foreach ( $parts as $part ) {
584 if ( isset( $part->eqpos
) ) {
587 for ( $node = $part->out
->firstNode
; $node; $node = $node->nextSibling
) {
588 if ( $node === $part->eqpos
) {
594 throw new MWException( __METHOD__
. ': eqpos not found' );
596 if ( $node->name
!== 'equals' ) {
597 throw new MWException( __METHOD__
.': eqpos is not equals' );
601 // Construct name node
602 $nameNode = new PPNode_Hash_Tree( 'name' );
603 if ( $lastNode !== false ) {
604 $lastNode->nextSibling
= false;
605 $nameNode->firstChild
= $part->out
->firstNode
;
606 $nameNode->lastChild
= $lastNode;
609 // Construct value node
610 $valueNode = new PPNode_Hash_Tree( 'value' );
611 if ( $equalsNode->nextSibling
!== false ) {
612 $valueNode->firstChild
= $equalsNode->nextSibling
;
613 $valueNode->lastChild
= $part->out
->lastNode
;
615 $partNode = new PPNode_Hash_Tree( 'part' );
616 $partNode->addChild( $nameNode );
617 $partNode->addChild( $equalsNode->firstChild
);
618 $partNode->addChild( $valueNode );
619 $element->addChild( $partNode );
621 $partNode = new PPNode_Hash_Tree( 'part' );
622 $nameNode = new PPNode_Hash_Tree( 'name' );
623 $nameNode->addChild( new PPNode_Hash_Attr( 'index', $argIndex++
) );
624 $valueNode = new PPNode_Hash_Tree( 'value' );
625 $valueNode->firstChild
= $part->out
->firstNode
;
626 $valueNode->lastChild
= $part->out
->lastNode
;
627 $partNode->addChild( $nameNode );
628 $partNode->addChild( $valueNode );
629 $element->addChild( $partNode );
634 # Advance input pointer
635 $i +
= $matchingCount;
639 $accum =& $stack->getAccum();
641 # Re-add the old stack element if it still has unmatched opening characters remaining
642 if ($matchingCount < $piece->count
) {
643 $piece->parts
= array( new PPDPart_Hash
);
644 $piece->count
-= $matchingCount;
645 # do we still qualify for any callback with remaining count?
646 $names = $rules[$piece->open
]['names'];
648 $enclosingAccum =& $accum;
649 while ( $piece->count
) {
650 if ( array_key_exists( $piece->count
, $names ) ) {
651 $stack->push( $piece );
652 $accum =& $stack->getAccum();
658 $enclosingAccum->addLiteral( str_repeat( $piece->open
, $skippedBraces ) );
661 extract( $stack->getFlags() );
663 # Add XML element to the enclosing accumulator
664 if ( $element instanceof PPNode
) {
665 $accum->addNode( $element );
667 $accum->addAccum( $element );
669 } elseif ( $found == 'pipe' ) {
670 $findEquals = true; // shortcut for getFlags()
672 $accum =& $stack->getAccum();
674 } elseif ( $found == 'equals' ) {
675 $findEquals = false; // shortcut for getFlags()
676 $accum->addNodeWithText( 'equals', '=' );
677 $stack->getCurrentPart()->eqpos
= $accum->lastNode
;
682 # Output any remaining unclosed brackets
683 foreach ( $stack->stack
as $piece ) {
684 $stack->rootAccum
->addAccum( $piece->breakSyntax() );
687 # Enable top-level headings
688 for ( $node = $stack->rootAccum
->firstNode
; $node; $node = $node->nextSibling
) {
689 if ( isset( $node->name
) && $node->name
=== 'possible-h' ) {
694 $rootNode = new PPNode_Hash_Tree( 'root' );
695 $rootNode->firstChild
= $stack->rootAccum
->firstNode
;
696 $rootNode->lastChild
= $stack->rootAccum
->lastNode
;
700 $cacheValue = sprintf( "%08d", self
::CACHE_VERSION
) . serialize( $rootNode );
701 $wgMemc->set( $cacheKey, $cacheValue, 86400 );
702 wfProfileOut( __METHOD__
.'-cache-miss' );
703 wfProfileOut( __METHOD__
.'-cacheable' );
704 wfDebugLog( "Preprocessor", "Saved preprocessor Hash to memcached (key $cacheKey)" );
707 wfProfileOut( __METHOD__
);
713 * Stack class to help Preprocessor::preprocessToObj()
716 class PPDStack_Hash
extends PPDStack
{
717 function __construct() {
718 $this->elementClass
= 'PPDStackElement_Hash';
719 parent
::__construct();
720 $this->rootAccum
= new PPDAccum_Hash
;
727 class PPDStackElement_Hash
extends PPDStackElement
{
728 function __construct( $data = array() ) {
729 $this->partClass
= 'PPDPart_Hash';
730 parent
::__construct( $data );
734 * Get the accumulator that would result if the close is not found.
736 * @return PPDAccum_Hash
738 function breakSyntax( $openingCount = false ) {
739 if ( $this->open
== "\n" ) {
740 $accum = $this->parts
[0]->out
;
742 if ( $openingCount === false ) {
743 $openingCount = $this->count
;
745 $accum = new PPDAccum_Hash
;
746 $accum->addLiteral( str_repeat( $this->open
, $openingCount ) );
748 foreach ( $this->parts
as $part ) {
752 $accum->addLiteral( '|' );
754 $accum->addAccum( $part->out
);
764 class PPDPart_Hash
extends PPDPart
{
765 function __construct( $out = '' ) {
766 $accum = new PPDAccum_Hash
;
768 $accum->addLiteral( $out );
770 parent
::__construct( $accum );
777 class PPDAccum_Hash
{
778 var $firstNode, $lastNode;
780 function __construct() {
781 $this->firstNode
= $this->lastNode
= false;
785 * Append a string literal
787 function addLiteral( $s ) {
788 if ( $this->lastNode
=== false ) {
789 $this->firstNode
= $this->lastNode
= new PPNode_Hash_Text( $s );
790 } elseif ( $this->lastNode
instanceof PPNode_Hash_Text
) {
791 $this->lastNode
->value
.= $s;
793 $this->lastNode
->nextSibling
= new PPNode_Hash_Text( $s );
794 $this->lastNode
= $this->lastNode
->nextSibling
;
801 function addNode( PPNode
$node ) {
802 if ( $this->lastNode
=== false ) {
803 $this->firstNode
= $this->lastNode
= $node;
805 $this->lastNode
->nextSibling
= $node;
806 $this->lastNode
= $node;
811 * Append a tree node with text contents
813 function addNodeWithText( $name, $value ) {
814 $node = PPNode_Hash_Tree
::newWithText( $name, $value );
815 $this->addNode( $node );
819 * Append a PPAccum_Hash
820 * Takes over ownership of the nodes in the source argument. These nodes may
821 * subsequently be modified, especially nextSibling.
823 function addAccum( $accum ) {
824 if ( $accum->lastNode
=== false ) {
826 } elseif ( $this->lastNode
=== false ) {
827 $this->firstNode
= $accum->firstNode
;
828 $this->lastNode
= $accum->lastNode
;
830 $this->lastNode
->nextSibling
= $accum->firstNode
;
831 $this->lastNode
= $accum->lastNode
;
837 * An expansion frame, used as a context to expand the result of preprocessToObj()
840 class PPFrame_Hash
implements PPFrame
{
859 * Hashtable listing templates which are disallowed for expansion in this frame,
860 * having been encountered previously in parent frames.
865 * Recursion depth of this frame, top = 0
866 * Note that this is NOT the same as expansion depth in expand()
872 * Construct a new preprocessor frame.
873 * @param $preprocessor Preprocessor: the parent preprocessor
875 function __construct( $preprocessor ) {
876 $this->preprocessor
= $preprocessor;
877 $this->parser
= $preprocessor->parser
;
878 $this->title
= $this->parser
->mTitle
;
879 $this->titleCache
= array( $this->title ?
$this->title
->getPrefixedDBkey() : false );
880 $this->loopCheckHash
= array();
885 * Create a new child frame
886 * $args is optionally a multi-root PPNode or array containing the template arguments
888 * @param array|bool|\PPNode_Hash_Array $args PPNode_Hash_Array|array
889 * @param $title Title|bool
891 * @param int $indexOffset
892 * @throws MWException
893 * @return PPTemplateFrame_Hash
895 function newChild( $args = false, $title = false, $indexOffset = 0 ) {
896 $namedArgs = array();
897 $numberedArgs = array();
898 if ( $title === false ) {
899 $title = $this->title
;
901 if ( $args !== false ) {
902 if ( $args instanceof PPNode_Hash_Array
) {
903 $args = $args->value
;
904 } elseif ( !is_array( $args ) ) {
905 throw new MWException( __METHOD__
. ': $args must be array or PPNode_Hash_Array' );
907 foreach ( $args as $arg ) {
908 $bits = $arg->splitArg();
909 if ( $bits['index'] !== '' ) {
910 // Numbered parameter
911 $index = $bits['index'] - $indexOffset;
912 $numberedArgs[$index] = $bits['value'];
913 unset( $namedArgs[$index] );
916 $name = trim( $this->expand( $bits['name'], PPFrame
::STRIP_COMMENTS
) );
917 $namedArgs[$name] = $bits['value'];
918 unset( $numberedArgs[$name] );
922 return new PPTemplateFrame_Hash( $this->preprocessor
, $this, $numberedArgs, $namedArgs, $title );
926 * @throws MWException
931 function expand( $root, $flags = 0 ) {
932 static $expansionDepth = 0;
933 if ( is_string( $root ) ) {
937 if ( ++
$this->parser
->mPPNodeCount
> $this->parser
->mOptions
->getMaxPPNodeCount() ) {
938 $this->parser
->limitationWarn( 'node-count-exceeded',
939 $this->parser
->mPPNodeCount
,
940 $this->parser
->mOptions
->getMaxPPNodeCount()
942 return '<span class="error">Node-count limit exceeded</span>';
944 if ( $expansionDepth > $this->parser
->mOptions
->getMaxPPExpandDepth() ) {
945 $this->parser
->limitationWarn( 'expansion-depth-exceeded',
947 $this->parser
->mOptions
->getMaxPPExpandDepth()
949 return '<span class="error">Expansion depth limit exceeded</span>';
952 if ( $expansionDepth > $this->parser
->mHighestExpansionDepth
) {
953 $this->parser
->mHighestExpansionDepth
= $expansionDepth;
956 $outStack = array( '', '' );
957 $iteratorStack = array( false, $root );
958 $indexStack = array( 0, 0 );
960 while ( count( $iteratorStack ) > 1 ) {
961 $level = count( $outStack ) - 1;
962 $iteratorNode =& $iteratorStack[ $level ];
963 $out =& $outStack[$level];
964 $index =& $indexStack[$level];
966 if ( is_array( $iteratorNode ) ) {
967 if ( $index >= count( $iteratorNode ) ) {
968 // All done with this iterator
969 $iteratorStack[$level] = false;
970 $contextNode = false;
972 $contextNode = $iteratorNode[$index];
975 } elseif ( $iteratorNode instanceof PPNode_Hash_Array
) {
976 if ( $index >= $iteratorNode->getLength() ) {
977 // All done with this iterator
978 $iteratorStack[$level] = false;
979 $contextNode = false;
981 $contextNode = $iteratorNode->item( $index );
985 // Copy to $contextNode and then delete from iterator stack,
986 // because this is not an iterator but we do have to execute it once
987 $contextNode = $iteratorStack[$level];
988 $iteratorStack[$level] = false;
991 $newIterator = false;
993 if ( $contextNode === false ) {
995 } elseif ( is_string( $contextNode ) ) {
996 $out .= $contextNode;
997 } elseif ( is_array( $contextNode ) ||
$contextNode instanceof PPNode_Hash_Array
) {
998 $newIterator = $contextNode;
999 } elseif ( $contextNode instanceof PPNode_Hash_Attr
) {
1001 } elseif ( $contextNode instanceof PPNode_Hash_Text
) {
1002 $out .= $contextNode->value
;
1003 } elseif ( $contextNode instanceof PPNode_Hash_Tree
) {
1004 if ( $contextNode->name
== 'template' ) {
1005 # Double-brace expansion
1006 $bits = $contextNode->splitTemplate();
1007 if ( $flags & PPFrame
::NO_TEMPLATES
) {
1008 $newIterator = $this->virtualBracketedImplode( '{{', '|', '}}', $bits['title'], $bits['parts'] );
1010 $ret = $this->parser
->braceSubstitution( $bits, $this );
1011 if ( isset( $ret['object'] ) ) {
1012 $newIterator = $ret['object'];
1014 $out .= $ret['text'];
1017 } elseif ( $contextNode->name
== 'tplarg' ) {
1018 # Triple-brace expansion
1019 $bits = $contextNode->splitTemplate();
1020 if ( $flags & PPFrame
::NO_ARGS
) {
1021 $newIterator = $this->virtualBracketedImplode( '{{{', '|', '}}}', $bits['title'], $bits['parts'] );
1023 $ret = $this->parser
->argSubstitution( $bits, $this );
1024 if ( isset( $ret['object'] ) ) {
1025 $newIterator = $ret['object'];
1027 $out .= $ret['text'];
1030 } elseif ( $contextNode->name
== 'comment' ) {
1031 # HTML-style comment
1032 # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
1033 if ( $this->parser
->ot
['html']
1034 ||
( $this->parser
->ot
['pre'] && $this->parser
->mOptions
->getRemoveComments() )
1035 ||
( $flags & PPFrame
::STRIP_COMMENTS
) )
1039 # Add a strip marker in PST mode so that pstPass2() can run some old-fashioned regexes on the result
1040 # Not in RECOVER_COMMENTS mode (extractSections) though
1041 elseif ( $this->parser
->ot
['wiki'] && ! ( $flags & PPFrame
::RECOVER_COMMENTS
) ) {
1042 $out .= $this->parser
->insertStripItem( $contextNode->firstChild
->value
);
1044 # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
1046 $out .= $contextNode->firstChild
->value
;
1048 } elseif ( $contextNode->name
== 'ignore' ) {
1049 # Output suppression used by <includeonly> etc.
1050 # OT_WIKI will only respect <ignore> in substed templates.
1051 # The other output types respect it unless NO_IGNORE is set.
1052 # extractSections() sets NO_IGNORE and so never respects it.
1053 if ( ( !isset( $this->parent
) && $this->parser
->ot
['wiki'] ) ||
( $flags & PPFrame
::NO_IGNORE
) ) {
1054 $out .= $contextNode->firstChild
->value
;
1058 } elseif ( $contextNode->name
== 'ext' ) {
1060 $bits = $contextNode->splitExt() +
array( 'attr' => null, 'inner' => null, 'close' => null );
1061 $out .= $this->parser
->extensionSubstitution( $bits, $this );
1062 } elseif ( $contextNode->name
== 'h' ) {
1064 if ( $this->parser
->ot
['html'] ) {
1065 # Expand immediately and insert heading index marker
1067 for ( $node = $contextNode->firstChild
; $node; $node = $node->nextSibling
) {
1068 $s .= $this->expand( $node, $flags );
1071 $bits = $contextNode->splitHeading();
1072 $titleText = $this->title
->getPrefixedDBkey();
1073 $this->parser
->mHeadings
[] = array( $titleText, $bits['i'] );
1074 $serial = count( $this->parser
->mHeadings
) - 1;
1075 $marker = "{$this->parser->mUniqPrefix}-h-$serial-" . Parser
::MARKER_SUFFIX
;
1076 $s = substr( $s, 0, $bits['level'] ) . $marker . substr( $s, $bits['level'] );
1077 $this->parser
->mStripState
->addGeneral( $marker, '' );
1080 # Expand in virtual stack
1081 $newIterator = $contextNode->getChildren();
1084 # Generic recursive expansion
1085 $newIterator = $contextNode->getChildren();
1088 throw new MWException( __METHOD__
.': Invalid parameter type' );
1091 if ( $newIterator !== false ) {
1093 $iteratorStack[] = $newIterator;
1095 } elseif ( $iteratorStack[$level] === false ) {
1096 // Return accumulated value to parent
1097 // With tail recursion
1098 while ( $iteratorStack[$level] === false && $level > 0 ) {
1099 $outStack[$level - 1] .= $out;
1100 array_pop( $outStack );
1101 array_pop( $iteratorStack );
1102 array_pop( $indexStack );
1108 return $outStack[0];
1116 function implodeWithFlags( $sep, $flags /*, ... */ ) {
1117 $args = array_slice( func_get_args(), 2 );
1121 foreach ( $args as $root ) {
1122 if ( $root instanceof PPNode_Hash_Array
) {
1123 $root = $root->value
;
1125 if ( !is_array( $root ) ) {
1126 $root = array( $root );
1128 foreach ( $root as $node ) {
1134 $s .= $this->expand( $node, $flags );
1141 * Implode with no flags specified
1142 * This previously called implodeWithFlags but has now been inlined to reduce stack depth
1145 function implode( $sep /*, ... */ ) {
1146 $args = array_slice( func_get_args(), 1 );
1150 foreach ( $args as $root ) {
1151 if ( $root instanceof PPNode_Hash_Array
) {
1152 $root = $root->value
;
1154 if ( !is_array( $root ) ) {
1155 $root = array( $root );
1157 foreach ( $root as $node ) {
1163 $s .= $this->expand( $node );
1170 * Makes an object that, when expand()ed, will be the same as one obtained
1173 * @return PPNode_Hash_Array
1175 function virtualImplode( $sep /*, ... */ ) {
1176 $args = array_slice( func_get_args(), 1 );
1180 foreach ( $args as $root ) {
1181 if ( $root instanceof PPNode_Hash_Array
) {
1182 $root = $root->value
;
1184 if ( !is_array( $root ) ) {
1185 $root = array( $root );
1187 foreach ( $root as $node ) {
1196 return new PPNode_Hash_Array( $out );
1200 * Virtual implode with brackets
1202 * @return PPNode_Hash_Array
1204 function virtualBracketedImplode( $start, $sep, $end /*, ... */ ) {
1205 $args = array_slice( func_get_args(), 3 );
1206 $out = array( $start );
1209 foreach ( $args as $root ) {
1210 if ( $root instanceof PPNode_Hash_Array
) {
1211 $root = $root->value
;
1213 if ( !is_array( $root ) ) {
1214 $root = array( $root );
1216 foreach ( $root as $node ) {
1226 return new PPNode_Hash_Array( $out );
1229 function __toString() {
1234 * @param $level bool
1235 * @return array|bool|String
1237 function getPDBK( $level = false ) {
1238 if ( $level === false ) {
1239 return $this->title
->getPrefixedDBkey();
1241 return isset( $this->titleCache
[$level] ) ?
$this->titleCache
[$level] : false;
1248 function getArguments() {
1255 function getNumberedArguments() {
1262 function getNamedArguments() {
1267 * Returns true if there are no arguments in this frame
1271 function isEmpty() {
1279 function getArgument( $name ) {
1284 * Returns true if the infinite loop check is OK, false if a loop is detected
1286 * @param $title Title
1290 function loopCheck( $title ) {
1291 return !isset( $this->loopCheckHash
[$title->getPrefixedDBkey()] );
1295 * Return true if the frame is a template frame
1299 function isTemplate() {
1304 * Get a title of frame
1308 function getTitle() {
1309 return $this->title
;
1314 * Expansion frame with template arguments
1317 class PPTemplateFrame_Hash
extends PPFrame_Hash
{
1318 var $numberedArgs, $namedArgs, $parent;
1319 var $numberedExpansionCache, $namedExpansionCache;
1322 * @param $preprocessor
1324 * @param $numberedArgs array
1325 * @param $namedArgs array
1326 * @param $title Title
1328 function __construct( $preprocessor, $parent = false, $numberedArgs = array(), $namedArgs = array(), $title = false ) {
1329 parent
::__construct( $preprocessor );
1331 $this->parent
= $parent;
1332 $this->numberedArgs
= $numberedArgs;
1333 $this->namedArgs
= $namedArgs;
1334 $this->title
= $title;
1335 $pdbk = $title ?
$title->getPrefixedDBkey() : false;
1336 $this->titleCache
= $parent->titleCache
;
1337 $this->titleCache
[] = $pdbk;
1338 $this->loopCheckHash
= /*clone*/ $parent->loopCheckHash
;
1339 if ( $pdbk !== false ) {
1340 $this->loopCheckHash
[$pdbk] = true;
1342 $this->depth
= $parent->depth +
1;
1343 $this->numberedExpansionCache
= $this->namedExpansionCache
= array();
1346 function __toString() {
1349 $args = $this->numberedArgs +
$this->namedArgs
;
1350 foreach ( $args as $name => $value ) {
1356 $s .= "\"$name\":\"" .
1357 str_replace( '"', '\\"', $value->__toString() ) . '"';
1363 * Returns true if there are no arguments in this frame
1367 function isEmpty() {
1368 return !count( $this->numberedArgs
) && !count( $this->namedArgs
);
1374 function getArguments() {
1375 $arguments = array();
1376 foreach ( array_merge(
1377 array_keys($this->numberedArgs
),
1378 array_keys($this->namedArgs
)) as $key ) {
1379 $arguments[$key] = $this->getArgument($key);
1387 function getNumberedArguments() {
1388 $arguments = array();
1389 foreach ( array_keys($this->numberedArgs
) as $key ) {
1390 $arguments[$key] = $this->getArgument($key);
1398 function getNamedArguments() {
1399 $arguments = array();
1400 foreach ( array_keys($this->namedArgs
) as $key ) {
1401 $arguments[$key] = $this->getArgument($key);
1408 * @return array|bool
1410 function getNumberedArgument( $index ) {
1411 if ( !isset( $this->numberedArgs
[$index] ) ) {
1414 if ( !isset( $this->numberedExpansionCache
[$index] ) ) {
1415 # No trimming for unnamed arguments
1416 $this->numberedExpansionCache
[$index] = $this->parent
->expand( $this->numberedArgs
[$index], PPFrame
::STRIP_COMMENTS
);
1418 return $this->numberedExpansionCache
[$index];
1425 function getNamedArgument( $name ) {
1426 if ( !isset( $this->namedArgs
[$name] ) ) {
1429 if ( !isset( $this->namedExpansionCache
[$name] ) ) {
1430 # Trim named arguments post-expand, for backwards compatibility
1431 $this->namedExpansionCache
[$name] = trim(
1432 $this->parent
->expand( $this->namedArgs
[$name], PPFrame
::STRIP_COMMENTS
) );
1434 return $this->namedExpansionCache
[$name];
1439 * @return array|bool
1441 function getArgument( $name ) {
1442 $text = $this->getNumberedArgument( $name );
1443 if ( $text === false ) {
1444 $text = $this->getNamedArgument( $name );
1450 * Return true if the frame is a template frame
1454 function isTemplate() {
1460 * Expansion frame with custom arguments
1463 class PPCustomFrame_Hash
extends PPFrame_Hash
{
1466 function __construct( $preprocessor, $args ) {
1467 parent
::__construct( $preprocessor );
1468 $this->args
= $args;
1471 function __toString() {
1474 foreach ( $this->args
as $name => $value ) {
1480 $s .= "\"$name\":\"" .
1481 str_replace( '"', '\\"', $value->__toString() ) . '"';
1490 function isEmpty() {
1491 return !count( $this->args
);
1498 function getArgument( $index ) {
1499 if ( !isset( $this->args
[$index] ) ) {
1502 return $this->args
[$index];
1505 function getArguments() {
1513 class PPNode_Hash_Tree
implements PPNode
{
1514 var $name, $firstChild, $lastChild, $nextSibling;
1516 function __construct( $name ) {
1517 $this->name
= $name;
1518 $this->firstChild
= $this->lastChild
= $this->nextSibling
= false;
1521 function __toString() {
1524 for ( $node = $this->firstChild
; $node; $node = $node->nextSibling
) {
1525 if ( $node instanceof PPNode_Hash_Attr
) {
1526 $attribs .= ' ' . $node->name
. '="' . htmlspecialchars( $node->value
) . '"';
1528 $inner .= $node->__toString();
1531 if ( $inner === '' ) {
1532 return "<{$this->name}$attribs/>";
1534 return "<{$this->name}$attribs>$inner</{$this->name}>";
1541 * @return PPNode_Hash_Tree
1543 static function newWithText( $name, $text ) {
1544 $obj = new self( $name );
1545 $obj->addChild( new PPNode_Hash_Text( $text ) );
1549 function addChild( $node ) {
1550 if ( $this->lastChild
=== false ) {
1551 $this->firstChild
= $this->lastChild
= $node;
1553 $this->lastChild
->nextSibling
= $node;
1554 $this->lastChild
= $node;
1559 * @return PPNode_Hash_Array
1561 function getChildren() {
1562 $children = array();
1563 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1564 $children[] = $child;
1566 return new PPNode_Hash_Array( $children );
1569 function getFirstChild() {
1570 return $this->firstChild
;
1573 function getNextSibling() {
1574 return $this->nextSibling
;
1577 function getChildrenOfType( $name ) {
1578 $children = array();
1579 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1580 if ( isset( $child->name
) && $child->name
=== $name ) {
1581 $children[] = $child;
1590 function getLength() {
1598 function item( $i ) {
1605 function getName() {
1610 * Split a "<part>" node into an associative array containing:
1611 * - name PPNode name
1612 * - index String index
1613 * - value PPNode value
1615 * @throws MWException
1618 function splitArg() {
1620 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1621 if ( !isset( $child->name
) ) {
1624 if ( $child->name
=== 'name' ) {
1625 $bits['name'] = $child;
1626 if ( $child->firstChild
instanceof PPNode_Hash_Attr
1627 && $child->firstChild
->name
=== 'index' )
1629 $bits['index'] = $child->firstChild
->value
;
1631 } elseif ( $child->name
=== 'value' ) {
1632 $bits['value'] = $child;
1636 if ( !isset( $bits['name'] ) ) {
1637 throw new MWException( 'Invalid brace node passed to ' . __METHOD__
);
1639 if ( !isset( $bits['index'] ) ) {
1640 $bits['index'] = '';
1646 * Split an "<ext>" node into an associative array containing name, attr, inner and close
1647 * All values in the resulting array are PPNodes. Inner and close are optional.
1649 * @throws MWException
1652 function splitExt() {
1654 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1655 if ( !isset( $child->name
) ) {
1658 if ( $child->name
== 'name' ) {
1659 $bits['name'] = $child;
1660 } elseif ( $child->name
== 'attr' ) {
1661 $bits['attr'] = $child;
1662 } elseif ( $child->name
== 'inner' ) {
1663 $bits['inner'] = $child;
1664 } elseif ( $child->name
== 'close' ) {
1665 $bits['close'] = $child;
1668 if ( !isset( $bits['name'] ) ) {
1669 throw new MWException( 'Invalid ext node passed to ' . __METHOD__
);
1675 * Split an "<h>" node
1677 * @throws MWException
1680 function splitHeading() {
1681 if ( $this->name
!== 'h' ) {
1682 throw new MWException( 'Invalid h node passed to ' . __METHOD__
);
1685 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1686 if ( !isset( $child->name
) ) {
1689 if ( $child->name
== 'i' ) {
1690 $bits['i'] = $child->value
;
1691 } elseif ( $child->name
== 'level' ) {
1692 $bits['level'] = $child->value
;
1695 if ( !isset( $bits['i'] ) ) {
1696 throw new MWException( 'Invalid h node passed to ' . __METHOD__
);
1702 * Split a "<template>" or "<tplarg>" node
1704 * @throws MWException
1707 function splitTemplate() {
1709 $bits = array( 'lineStart' => '' );
1710 for ( $child = $this->firstChild
; $child; $child = $child->nextSibling
) {
1711 if ( !isset( $child->name
) ) {
1714 if ( $child->name
== 'title' ) {
1715 $bits['title'] = $child;
1717 if ( $child->name
== 'part' ) {
1720 if ( $child->name
== 'lineStart' ) {
1721 $bits['lineStart'] = '1';
1724 if ( !isset( $bits['title'] ) ) {
1725 throw new MWException( 'Invalid node passed to ' . __METHOD__
);
1727 $bits['parts'] = new PPNode_Hash_Array( $parts );
1735 class PPNode_Hash_Text
implements PPNode
{
1736 var $value, $nextSibling;
1738 function __construct( $value ) {
1739 if ( is_object( $value ) ) {
1740 throw new MWException( __CLASS__
. ' given object instead of string' );
1742 $this->value
= $value;
1745 function __toString() {
1746 return htmlspecialchars( $this->value
);
1749 function getNextSibling() {
1750 return $this->nextSibling
;
1753 function getChildren() { return false; }
1754 function getFirstChild() { return false; }
1755 function getChildrenOfType( $name ) { return false; }
1756 function getLength() { return false; }
1757 function item( $i ) { return false; }
1758 function getName() { return '#text'; }
1759 function splitArg() { throw new MWException( __METHOD__
. ': not supported' ); }
1760 function splitExt() { throw new MWException( __METHOD__
. ': not supported' ); }
1761 function splitHeading() { throw new MWException( __METHOD__
. ': not supported' ); }
1767 class PPNode_Hash_Array
implements PPNode
{
1768 var $value, $nextSibling;
1770 function __construct( $value ) {
1771 $this->value
= $value;
1774 function __toString() {
1775 return var_export( $this, true );
1778 function getLength() {
1779 return count( $this->value
);
1782 function item( $i ) {
1783 return $this->value
[$i];
1786 function getName() { return '#nodelist'; }
1788 function getNextSibling() {
1789 return $this->nextSibling
;
1792 function getChildren() { return false; }
1793 function getFirstChild() { return false; }
1794 function getChildrenOfType( $name ) { return false; }
1795 function splitArg() { throw new MWException( __METHOD__
. ': not supported' ); }
1796 function splitExt() { throw new MWException( __METHOD__
. ': not supported' ); }
1797 function splitHeading() { throw new MWException( __METHOD__
. ': not supported' ); }
1803 class PPNode_Hash_Attr
implements PPNode
{
1804 var $name, $value, $nextSibling;
1806 function __construct( $name, $value ) {
1807 $this->name
= $name;
1808 $this->value
= $value;
1811 function __toString() {
1812 return "<@{$this->name}>" . htmlspecialchars( $this->value
) . "</@{$this->name}>";
1815 function getName() {
1819 function getNextSibling() {
1820 return $this->nextSibling
;
1823 function getChildren() { return false; }
1824 function getFirstChild() { return false; }
1825 function getChildrenOfType( $name ) { return false; }
1826 function getLength() { return false; }
1827 function item( $i ) { return false; }
1828 function splitArg() { throw new MWException( __METHOD__
. ': not supported' ); }
1829 function splitExt() { throw new MWException( __METHOD__
. ': not supported' ); }
1830 function splitHeading() { throw new MWException( __METHOD__
. ': not supported' ); }