]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blobdiff - includes/parser/Preprocessor_DOM.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / includes / parser / Preprocessor_DOM.php
index 2b635f7c0d251e1d060d6ea1de5a75b5528f1096..258896260416b8d314e157a59339c922e9078130 100644 (file)
@@ -2,19 +2,42 @@
 /**
  * Preprocessor using PHP's dom extension
  *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
  * @file
  * @ingroup Parser
  */
+
 /**
  * @ingroup Parser
  */
-class Preprocessor_DOM implements Preprocessor {
-       var $parser, $memoryLimit;
+// @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
+class Preprocessor_DOM extends Preprocessor {
+       // @codingStandardsIgnoreEnd
+
+       /**
+        * @var Parser
+        */
+       public $parser;
+
+       public $memoryLimit;
 
-       const CACHE_VERSION = 1;
+       const CACHE_PREFIX = 'preprocess-xml';
 
-       function __construct( $parser ) {
+       public function __construct( $parser ) {
                $this->parser = $parser;
                $mem = ini_get( 'memory_limit' );
                $this->memoryLimit = false;
@@ -27,41 +50,70 @@ class Preprocessor_DOM implements Preprocessor {
                }
        }
 
-       function newFrame() {
+       /**
+        * @return PPFrame_DOM
+        */
+       public function newFrame() {
                return new PPFrame_DOM( $this );
        }
 
-       function newCustomFrame( $args ) {
+       /**
+        * @param array $args
+        * @return PPCustomFrame_DOM
+        */
+       public function newCustomFrame( $args ) {
                return new PPCustomFrame_DOM( $this, $args );
        }
 
-       function newPartNodeArray( $values ) {
-               //NOTE: DOM manipulation is slower than building & parsing XML! (or so Tim sais)
-               $xml = "";
-               $xml .= "<list>";
+       /**
+        * @param array $values
+        * @return PPNode_DOM
+        * @throws MWException
+        */
+       public function newPartNodeArray( $values ) {
+               // NOTE: DOM manipulation is slower than building & parsing XML! (or so Tim sais)
+               $xml = "<list>";
 
                foreach ( $values as $k => $val ) {
-                        
                        if ( is_int( $k ) ) {
-                               $xml .= "<part><name index=\"$k\"/><value>" . htmlspecialchars( $val ) ."</value></part>";
+                               $xml .= "<part><name index=\"$k\"/><value>"
+                                       . htmlspecialchars( $val ) . "</value></part>";
                        } else {
-                               $xml .= "<part><name>" . htmlspecialchars( $k ) . "</name>=<value>" . htmlspecialchars( $val ) . "</value></part>";
+                               $xml .= "<part><name>" . htmlspecialchars( $k )
+                                       . "</name>=<value>" . htmlspecialchars( $val ) . "</value></part>";
                        }
                }
 
                $xml .= "</list>";
 
                $dom = new DOMDocument();
-               $dom->loadXML( $xml );
-               $root = $dom->documentElement;
+               MediaWiki\suppressWarnings();
+               $result = $dom->loadXML( $xml );
+               MediaWiki\restoreWarnings();
+               if ( !$result ) {
+                       // Try running the XML through UtfNormal to get rid of invalid characters
+                       $xml = UtfNormal\Validator::cleanUp( $xml );
+                       // 1 << 19 == XML_PARSE_HUGE, needed so newer versions of libxml2
+                       // don't barf when the XML is >256 levels deep
+                       $result = $dom->loadXML( $xml, 1 << 19 );
+               }
 
+               if ( !$result ) {
+                       throw new MWException( 'Parameters passed to ' . __METHOD__ . ' result in invalid XML' );
+               }
+
+               $root = $dom->documentElement;
                $node = new PPNode_DOM( $root->childNodes );
                return $node;
        }
 
-       function memCheck() {
+       /**
+        * @throws MWException
+        * @return bool
+        */
+       public function memCheck() {
                if ( $this->memoryLimit === false ) {
-                       return;
+                       return true;
                }
                $usage = memory_get_usage();
                if ( $usage > $this->memoryLimit * 0.9 ) {
@@ -75,13 +127,14 @@ class Preprocessor_DOM implements Preprocessor {
         * Preprocess some wikitext and return the document tree.
         * This is the ghost of Parser::replace_variables().
         *
-        * @param $text String: the text to parse
-        * @param $flags Integer: bitwise combination of:
-        *          Parser::PTD_FOR_INCLUSION    Handle <noinclude>/<includeonly> as if the text is being
-        *                                     included. Default is to assume a direct page view.
+        * @param string $text The text to parse
+        * @param int $flags Bitwise combination of:
+        *     Parser::PTD_FOR_INCLUSION  Handle "<noinclude>" and "<includeonly>"
+        *                                as if the text is being included. Default
+        *                                is to assume a direct page view.
         *
         * The generated DOM tree must depend only on the input text and the flags.
-        * The DOM tree must be the same in OT_HTML and OT_WIKI mode, to avoid a regression of bug 4899.
+        * The DOM tree must be the same in OT_HTML and OT_WIKI mode, to avoid a regression of T6899.
         *
         * Any flag added to the $flags parameter here, or any other parameter liable to cause a
         * change in the DOM tree for a given text, must be passed through the section identifier
@@ -91,97 +144,73 @@ class Preprocessor_DOM implements Preprocessor {
         * cache may be implemented at a later date which takes further advantage of these strict
         * dependency requirements.
         *
-        * @private
-        */
-       function preprocessToObj( $text, $flags = 0 ) {
-               wfProfileIn( __METHOD__ );
-               global $wgMemc, $wgPreprocessorCacheThreshold;
-               
-               $xml = false;
-               $cacheable = strlen( $text ) > $wgPreprocessorCacheThreshold;
-               if ( $cacheable ) {
-                       wfProfileIn( __METHOD__.'-cacheable' );
-
-                       $cacheKey = wfMemcKey( 'preprocess-xml', md5($text), $flags );
-                       $cacheValue = $wgMemc->get( $cacheKey );
-                       if ( $cacheValue ) {
-                               $version = substr( $cacheValue, 0, 8 );
-                               if ( intval( $version ) == self::CACHE_VERSION ) {
-                                       $xml = substr( $cacheValue, 8 );
-                                       // From the cache
-                                       wfDebugLog( "Preprocessor", "Loaded preprocessor XML from memcached (key $cacheKey)" );
-                               }
-                       }
-               }
+        * @throws MWException
+        * @return PPNode_DOM
+        */
+       public function preprocessToObj( $text, $flags = 0 ) {
+               $xml = $this->cacheGetTree( $text, $flags );
                if ( $xml === false ) {
-                       if ( $cacheable ) {
-                               wfProfileIn( __METHOD__.'-cache-miss' );
-                               $xml = $this->preprocessToXml( $text, $flags );
-                               $cacheValue = sprintf( "%08d", self::CACHE_VERSION ) . $xml;
-                               $wgMemc->set( $cacheKey, $cacheValue, 86400 );
-                               wfProfileOut( __METHOD__.'-cache-miss' );
-                               wfDebugLog( "Preprocessor", "Saved preprocessor XML to memcached (key $cacheKey)" );
-                       } else {
-                               $xml = $this->preprocessToXml( $text, $flags );
-                       }
+                       $xml = $this->preprocessToXml( $text, $flags );
+                       $this->cacheSetTree( $text, $flags, $xml );
+               }
 
+               // Fail if the number of elements exceeds acceptable limits
+               // Do not attempt to generate the DOM
+               $this->parser->mGeneratedPPNodeCount += substr_count( $xml, '<' );
+               $max = $this->parser->mOptions->getMaxGeneratedPPNodeCount();
+               if ( $this->parser->mGeneratedPPNodeCount > $max ) {
+                       // if ( $cacheable ) { ... }
+                       throw new MWException( __METHOD__ . ': generated node count limit exceeded' );
                }
-               wfProfileIn( __METHOD__.'-loadXML' );
+
                $dom = new DOMDocument;
-               wfSuppressWarnings();
+               MediaWiki\suppressWarnings();
                $result = $dom->loadXML( $xml );
-               wfRestoreWarnings();
+               MediaWiki\restoreWarnings();
                if ( !$result ) {
                        // Try running the XML through UtfNormal to get rid of invalid characters
-                       $xml = UtfNormal::cleanUp( $xml );
-                       $result = $dom->loadXML( $xml );
-                       if ( !$result ) {
-                               throw new MWException( __METHOD__.' generated invalid XML' );
-                       }
+                       $xml = UtfNormal\Validator::cleanUp( $xml );
+                       // 1 << 19 == XML_PARSE_HUGE, needed so newer versions of libxml2
+                       // don't barf when the XML is >256 levels deep.
+                       $result = $dom->loadXML( $xml, 1 << 19 );
                }
-               $obj = new PPNode_DOM( $dom->documentElement );
-               wfProfileOut( __METHOD__.'-loadXML' );
-               if ( $cacheable ) {
-                       wfProfileOut( __METHOD__.'-cacheable' );
+               if ( $result ) {
+                       $obj = new PPNode_DOM( $dom->documentElement );
+               }
+
+               // if ( $cacheable ) { ... }
+
+               if ( !$result ) {
+                       throw new MWException( __METHOD__ . ' generated invalid XML' );
                }
-               wfProfileOut( __METHOD__ );
                return $obj;
        }
-       
-       function preprocessToXml( $text, $flags = 0 ) {
-               wfProfileIn( __METHOD__ );
-               $rules = array(
-                       '{' => array(
-                               'end' => '}',
-                               'names' => array(
-                                       2 => 'template',
-                                       3 => 'tplarg',
-                               ),
-                               'min' => 2,
-                               'max' => 3,
-                       ),
-                       '[' => array(
-                               'end' => ']',
-                               'names' => array( 2 => null ),
-                               'min' => 2,
-                               'max' => 2,
-                       )
-               );
+
+       /**
+        * @param string $text
+        * @param int $flags
+        * @return string
+        */
+       public function preprocessToXml( $text, $flags = 0 ) {
+               global $wgDisableLangConversion;
 
                $forInclusion = $flags & Parser::PTD_FOR_INCLUSION;
 
                $xmlishElements = $this->parser->getStripList();
+               $xmlishAllowMissingEndTag = [ 'includeonly', 'noinclude', 'onlyinclude' ];
                $enableOnlyinclude = false;
                if ( $forInclusion ) {
-                       $ignoredTags = array( 'includeonly', '/includeonly' );
-                       $ignoredElements = array( 'noinclude' );
+                       $ignoredTags = [ 'includeonly', '/includeonly' ];
+                       $ignoredElements = [ 'noinclude' ];
                        $xmlishElements[] = 'noinclude';
-                       if ( strpos( $text, '<onlyinclude>' ) !== false && strpos( $text, '</onlyinclude>' ) !== false ) {
+                       if ( strpos( $text, '<onlyinclude>' ) !== false
+                               && strpos( $text, '</onlyinclude>' ) !== false
+                       ) {
                                $enableOnlyinclude = true;
                        }
                } else {
-                       $ignoredTags = array( 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude' );
-                       $ignoredElements = array( 'includeonly' );
+                       $ignoredTags = [ 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude' ];
+                       $ignoredElements = [ 'includeonly' ];
                        $xmlishElements[] = 'includeonly';
                }
                $xmlishRegex = implode( '|', array_merge( $xmlishElements, $ignoredTags ) );
@@ -191,22 +220,38 @@ class Preprocessor_DOM implements Preprocessor {
 
                $stack = new PPDStack;
 
-               $searchBase = "[{<\n"; #}
-               $revText = strrev( $text ); // For fast reverse searches
+               $searchBase = "[{<\n"; # }
+               if ( !$wgDisableLangConversion ) {
+                       $searchBase .= '-';
+               }
 
-               $i = 0;                     # Input pointer, starts out pointing to a pseudo-newline before the start
-               $accum =& $stack->getAccum();   # Current accumulator
+               // For fast reverse searches
+               $revText = strrev( $text );
+               $lengthText = strlen( $text );
+
+               // Input pointer, starts out pointing to a pseudo-newline before the start
+               $i = 0;
+               // Current accumulator
+               $accum =& $stack->getAccum();
                $accum = '<root>';
-               $findEquals = false;            # True to find equals signs in arguments
-               $findPipe = false;              # True to take notice of pipe characters
+               // True to find equals signs in arguments
+               $findEquals = false;
+               // True to take notice of pipe characters
+               $findPipe = false;
                $headingIndex = 1;
-               $inHeading = false;        # True if $i is inside a possible heading
-               $noMoreGT = false;         # True if there are no more greater-than (>) signs right of $i
-               $findOnlyinclude = $enableOnlyinclude; # True to ignore all input up to the next <onlyinclude>
-               $fakeLineStart = true;     # Do a line-start run without outputting an LF character
+               // True if $i is inside a possible heading
+               $inHeading = false;
+               // True if there are no more greater-than (>) signs right of $i
+               $noMoreGT = false;
+               // Map of tag name => true if there are no more closing tags of given type right of $i
+               $noMoreClosingTag = [];
+               // True to ignore all input up to the next <onlyinclude>
+               $findOnlyinclude = $enableOnlyinclude;
+               // Do a line-start run without outputting an LF character
+               $fakeLineStart = true;
 
                while ( true ) {
-                       //$this->memCheck();
+                       // $this->memCheck();
 
                        if ( $findOnlyinclude ) {
                                // Ignore all input up to the next <onlyinclude>
@@ -230,6 +275,13 @@ class Preprocessor_DOM implements Preprocessor {
                                $search = $searchBase;
                                if ( $stack->top === false ) {
                                        $currentClosing = '';
+                               } elseif (
+                                       $stack->top->close === '}-' &&
+                                       $stack->top->count > 2
+                               ) {
+                                       # adjust closing for -{{{...{{
+                                       $currentClosing = '}';
+                                       $search .= $currentClosing;
                                } else {
                                        $currentClosing = $stack->top->close;
                                        $search .= $currentClosing;
@@ -248,7 +300,7 @@ class Preprocessor_DOM implements Preprocessor {
                                        $accum .= htmlspecialchars( substr( $text, $i, $literalLength ) );
                                        $i += $literalLength;
                                }
-                               if ( $i >= strlen( $text ) ) {
+                               if ( $i >= $lengthText ) {
                                        if ( $currentClosing == "\n" ) {
                                                // Do a past-the-end run to finish off the heading
                                                $curChar = '';
@@ -258,7 +310,10 @@ class Preprocessor_DOM implements Preprocessor {
                                                break;
                                        }
                                } else {
-                                       $curChar = $text[$i];
+                                       $curChar = $curTwoChar = $text[$i];
+                                       if ( ( $i + 1 ) < $lengthText ) {
+                                               $curTwoChar .= $text[$i + 1];
+                                       }
                                        if ( $curChar == '|' ) {
                                                $found = 'pipe';
                                        } elseif ( $curChar == '=' ) {
@@ -271,14 +326,27 @@ class Preprocessor_DOM implements Preprocessor {
                                                } else {
                                                        $found = 'line-start';
                                                }
+                                       } elseif ( $curTwoChar == $currentClosing ) {
+                                               $found = 'close';
+                                               $curChar = $curTwoChar;
                                        } elseif ( $curChar == $currentClosing ) {
                                                $found = 'close';
-                                       } elseif ( isset( $rules[$curChar] ) ) {
+                                       } elseif ( isset( $this->rules[$curTwoChar] ) ) {
+                                               $curChar = $curTwoChar;
+                                               $found = 'open';
+                                               $rule = $this->rules[$curChar];
+                                       } elseif ( isset( $this->rules[$curChar] ) ) {
                                                $found = 'open';
-                                               $rule = $rules[$curChar];
+                                               $rule = $this->rules[$curChar];
                                        } else {
-                                               # Some versions of PHP have a strcspn which stops on null characters
-                                               # Ignore and continue
+                                               # Some versions of PHP have a strcspn which stops on
+                                               # null characters; ignore these and continue.
+                                               # We also may get '-' and '}' characters here which
+                                               # don't match -{ or $currentClosing.  Add these to
+                                               # output and continue.
+                                               if ( $curChar == '-' || $curChar == '}' ) {
+                                                       $accum .= $curChar;
+                                               }
                                                ++$i;
                                                continue;
                                        }
@@ -288,7 +356,9 @@ class Preprocessor_DOM implements Preprocessor {
                        if ( $found == 'angle' ) {
                                $matches = false;
                                // Handle </onlyinclude>
-                               if ( $enableOnlyinclude && substr( $text, $i, strlen( '</onlyinclude>' ) ) == '</onlyinclude>' ) {
+                               if ( $enableOnlyinclude
+                                       && substr( $text, $i, strlen( '</onlyinclude>' ) ) == '</onlyinclude>'
+                               ) {
                                        $findOnlyinclude = true;
                                        continue;
                                }
@@ -302,9 +372,10 @@ class Preprocessor_DOM implements Preprocessor {
                                }
                                // Handle comments
                                if ( isset( $matches[2] ) && $matches[2] == '!--' ) {
-                                       // To avoid leaving blank lines, when a comment is both preceded
-                                       // and followed by a newline (ignoring spaces), trim leading and
-                                       // trailing spaces and one of the newlines.
+                                       // To avoid leaving blank lines, when a sequence of
+                                       // space-separated comments is both preceded and followed by
+                                       // a newline (ignoring spaces), then
+                                       // trim leading and trailing spaces and the trailing newline.
 
                                        // Find the end
                                        $endPos = strpos( $text, '-->', $i + 4 );
@@ -312,28 +383,55 @@ class Preprocessor_DOM implements Preprocessor {
                                                // Unclosed comment in input, runs to end
                                                $inner = substr( $text, $i );
                                                $accum .= '<comment>' . htmlspecialchars( $inner ) . '</comment>';
-                                               $i = strlen( $text );
+                                               $i = $lengthText;
                                        } else {
                                                // Search backwards for leading whitespace
-                                               $wsStart = $i ? ( $i - strspn( $revText, ' ', strlen( $text ) - $i ) ) : 0;
+                                               $wsStart = $i ? ( $i - strspn( $revText, " \t", $lengthText - $i ) ) : 0;
+
                                                // Search forwards for trailing whitespace
-                                               // $wsEnd will be the position of the last space
-                                               $wsEnd = $endPos + 2 + strspn( $text, ' ', $endPos + 3 );
+                                               // $wsEnd will be the position of the last space (or the '>' if there's none)
+                                               $wsEnd = $endPos + 2 + strspn( $text, " \t", $endPos + 3 );
+
+                                               // Keep looking forward as long as we're finding more
+                                               // comments.
+                                               $comments = [ [ $wsStart, $wsEnd ] ];
+                                               while ( substr( $text, $wsEnd + 1, 4 ) == '<!--' ) {
+                                                       $c = strpos( $text, '-->', $wsEnd + 4 );
+                                                       if ( $c === false ) {
+                                                               break;
+                                                       }
+                                                       $c = $c + 2 + strspn( $text, " \t", $c + 3 );
+                                                       $comments[] = [ $wsEnd + 1, $c ];
+                                                       $wsEnd = $c;
+                                               }
+
                                                // Eat the line if possible
                                                // TODO: This could theoretically be done if $wsStart == 0, i.e. for comments at
                                                // the overall start. That's not how Sanitizer::removeHTMLcomments() did it, but
                                                // it's a possible beneficial b/c break.
                                                if ( $wsStart > 0 && substr( $text, $wsStart - 1, 1 ) == "\n"
-                                                       && substr( $text, $wsEnd + 1, 1 ) == "\n" )
-                                               {
-                                                       $startPos = $wsStart;
-                                                       $endPos = $wsEnd + 1;
+                                                       && substr( $text, $wsEnd + 1, 1 ) == "\n"
+                                               ) {
                                                        // Remove leading whitespace from the end of the accumulator
                                                        // Sanity check first though
                                                        $wsLength = $i - $wsStart;
-                                                       if ( $wsLength > 0 && substr( $accum, -$wsLength ) === str_repeat( ' ', $wsLength ) ) {
+                                                       if ( $wsLength > 0
+                                                               && strspn( $accum, " \t", -$wsLength ) === $wsLength
+                                                       ) {
                                                                $accum = substr( $accum, 0, -$wsLength );
                                                        }
+
+                                                       // Dump all but the last comment to the accumulator
+                                                       foreach ( $comments as $j => $com ) {
+                                                               $startPos = $com[0];
+                                                               $endPos = $com[1] + 1;
+                                                               if ( $j == ( count( $comments ) - 1 ) ) {
+                                                                       break;
+                                                               }
+                                                               $inner = substr( $text, $startPos, $endPos - $startPos );
+                                                               $accum .= '<comment>' . htmlspecialchars( $inner ) . '</comment>';
+                                                       }
+
                                                        // Do a line-start run next time to look for headings after the comment
                                                        $fakeLineStart = true;
                                                } else {
@@ -344,13 +442,11 @@ class Preprocessor_DOM implements Preprocessor {
 
                                                if ( $stack->top ) {
                                                        $part = $stack->top->getCurrentPart();
-                                                       if ( isset( $part->commentEnd ) && $part->commentEnd == $wsStart - 1 ) {
-                                                               // Comments abutting, no change in visual end
-                                                               $part->commentEnd = $wsEnd;
-                                                       } else {
+                                                       if ( !( isset( $part->commentEnd ) && $part->commentEnd == $wsStart - 1 ) ) {
                                                                $part->visualEnd = $wsStart;
-                                                               $part->commentEnd = $endPos;
                                                        }
+                                                       // Else comments abutting, no change in visual end
+                                                       $part->commentEnd = $endPos;
                                                }
                                                $i = $endPos + 1;
                                                $inner = substr( $text, $startPos, $endPos - $startPos + 1 );
@@ -375,13 +471,15 @@ class Preprocessor_DOM implements Preprocessor {
 
                                // Handle ignored tags
                                if ( in_array( $lowerName, $ignoredTags ) ) {
-                                       $accum .= '<ignore>' . htmlspecialchars( substr( $text, $i, $tagEndPos - $i + 1 ) ) . '</ignore>';
+                                       $accum .= '<ignore>'
+                                               . htmlspecialchars( substr( $text, $i, $tagEndPos - $i + 1 ) )
+                                               . '</ignore>';
                                        $i = $tagEndPos + 1;
                                        continue;
                                }
 
                                $tagStartPos = $i;
-                               if ( $text[$tagEndPos-1] == '/' ) {
+                               if ( $text[$tagEndPos - 1] == '/' ) {
                                        $attrEnd = $tagEndPos - 1;
                                        $inner = null;
                                        $i = $tagEndPos + 1;
@@ -389,17 +487,29 @@ class Preprocessor_DOM implements Preprocessor {
                                } else {
                                        $attrEnd = $tagEndPos;
                                        // Find closing tag
-                                       if ( preg_match( "/<\/" . preg_quote( $name, '/' ) . "\s*>/i", 
-                                                       $text, $matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 ) ) 
-                                       {
+                                       if (
+                                               !isset( $noMoreClosingTag[$name] ) &&
+                                               preg_match( "/<\/" . preg_quote( $name, '/' ) . "\s*>/i",
+                                                       $text, $matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 )
+                                       ) {
                                                $inner = substr( $text, $tagEndPos + 1, $matches[0][1] - $tagEndPos - 1 );
                                                $i = $matches[0][1] + strlen( $matches[0][0] );
                                                $close = '<close>' . htmlspecialchars( $matches[0][0] ) . '</close>';
                                        } else {
-                                               // No end tag -- let it run out to the end of the text.
-                                               $inner = substr( $text, $tagEndPos + 1 );
-                                               $i = strlen( $text );
-                                               $close = '';
+                                               // No end tag
+                                               if ( in_array( $name, $xmlishAllowMissingEndTag ) ) {
+                                                       // Let it run out to the end of the text.
+                                                       $inner = substr( $text, $tagEndPos + 1 );
+                                                       $i = $lengthText;
+                                                       $close = '';
+                                               } else {
+                                                       // Don't match the tag, treat opening tag as literal and resume parsing.
+                                                       $i = $tagEndPos + 1;
+                                                       $accum .= htmlspecialchars( substr( $text, $tagStartPos, $tagEndPos + 1 - $tagStartPos ) );
+                                                       // Cache results, otherwise we have O(N^2) performance for input like <foo><foo><foo>...
+                                                       $noMoreClosingTag[$name] = true;
+                                                       continue;
+                                               }
                                        }
                                }
                                // <includeonly> and <noinclude> just become <ignore> tags
@@ -423,9 +533,7 @@ class Preprocessor_DOM implements Preprocessor {
                                        $accum .= '<inner>' . htmlspecialchars( $inner ) . '</inner>';
                                }
                                $accum .= $close . '</ext>';
-                       }
-
-                       elseif ( $found == 'line-start' ) {
+                       } elseif ( $found == 'line-start' ) {
                                // Is this the start of a heading?
                                // Line break belongs before the heading element in any case
                                if ( $fakeLineStart ) {
@@ -437,45 +545,46 @@ class Preprocessor_DOM implements Preprocessor {
 
                                $count = strspn( $text, '=', $i, 6 );
                                if ( $count == 1 && $findEquals ) {
-                                       // DWIM: This looks kind of like a name/value separator
-                                       // Let's let the equals handler have it and break the potential heading
-                                       // This is heuristic, but AFAICT the methods for completely correct disambiguation are very complex.
+                                       // DWIM: This looks kind of like a name/value separator.
+                                       // Let's let the equals handler have it and break the
+                                       // potential heading. This is heuristic, but AFAICT the
+                                       // methods for completely correct disambiguation are very
+                                       // complex.
                                } elseif ( $count > 0 ) {
-                                       $piece = array(
+                                       $piece = [
                                                'open' => "\n",
                                                'close' => "\n",
-                                               'parts' => array( new PPDPart( str_repeat( '=', $count ) ) ),
+                                               'parts' => [ new PPDPart( str_repeat( '=', $count ) ) ],
                                                'startPos' => $i,
-                                               'count' => $count );
+                                               'count' => $count ];
                                        $stack->push( $piece );
                                        $accum =& $stack->getAccum();
                                        $flags = $stack->getFlags();
                                        extract( $flags );
                                        $i += $count;
                                }
-                       }
-
-                       elseif ( $found == 'line-end' ) {
+                       } elseif ( $found == 'line-end' ) {
                                $piece = $stack->top;
                                // A heading must be open, otherwise \n wouldn't have been in the search list
-                               assert( $piece->open == "\n" );
+                               assert( $piece->open === "\n" );
                                $part = $piece->getCurrentPart();
-                               // Search back through the input to see if it has a proper close
-                               // Do this using the reversed string since the other solutions (end anchor, etc.) are inefficient
-                               $wsLength = strspn( $revText, " \t", strlen( $text ) - $i );
+                               // Search back through the input to see if it has a proper close.
+                               // Do this using the reversed string since the other solutions
+                               // (end anchor, etc.) are inefficient.
+                               $wsLength = strspn( $revText, " \t", $lengthText - $i );
                                $searchStart = $i - $wsLength;
                                if ( isset( $part->commentEnd ) && $searchStart - 1 == $part->commentEnd ) {
                                        // Comment found at line end
                                        // Search for equals signs before the comment
                                        $searchStart = $part->visualEnd;
-                                       $searchStart -= strspn( $revText, " \t", strlen( $text ) - $searchStart );
+                                       $searchStart -= strspn( $revText, " \t", $lengthText - $searchStart );
                                }
                                $count = $piece->count;
-                               $equalsLength = strspn( $revText, '=', strlen( $text ) - $searchStart );
+                               $equalsLength = strspn( $revText, '=', $lengthText - $searchStart );
                                if ( $equalsLength > 0 ) {
                                        if ( $searchStart - $equalsLength == $piece->startPos ) {
                                                // This is just a single string of equals signs on its own line
-                                               // Replicate the doHeadings behaviour /={count}(.+)={count}/
+                                               // Replicate the doHeadings behavior /={count}(.+)={count}/
                                                // First find out how many equals signs there really are (don't stop at 6)
                                                $count = $equalsLength;
                                                if ( $count < 3 ) {
@@ -513,17 +622,21 @@ class Preprocessor_DOM implements Preprocessor {
                                // input pointer.
                        } elseif ( $found == 'open' ) {
                                # count opening brace characters
-                               $count = strspn( $text, $curChar, $i );
+                               $curLen = strlen( $curChar );
+                               $count = ( $curLen > 1 ) ?
+                                       # allow the final character to repeat
+                                       strspn( $text, $curChar[$curLen - 1], $i + 1 ) + 1 :
+                                       strspn( $text, $curChar, $i );
 
                                # we need to add to stack only if opening brace count is enough for one of the rules
                                if ( $count >= $rule['min'] ) {
                                        # Add it to the stack
-                                       $piece = array(
+                                       $piece = [
                                                'open' => $curChar,
                                                'close' => $rule['end'],
                                                'count' => $count,
-                                               'lineStart' => ($i > 0 && $text[$i-1] == "\n"),
-                                       );
+                                               'lineStart' => ( $i > 0 && $text[$i - 1] == "\n" ),
+                                       ];
 
                                        $stack->push( $piece );
                                        $accum =& $stack->getAccum();
@@ -538,11 +651,20 @@ class Preprocessor_DOM implements Preprocessor {
                                $piece = $stack->top;
                                # lets check if there are enough characters for closing brace
                                $maxCount = $piece->count;
-                               $count = strspn( $text, $curChar, $i, $maxCount );
+                               if ( $piece->close === '}-' && $curChar === '}' ) {
+                                       $maxCount--; # don't try to match closing '-' as a '}'
+                               }
+                               $curLen = strlen( $curChar );
+                               $count = ( $curLen > 1 ) ? $curLen :
+                                       strspn( $text, $curChar, $i, $maxCount );
 
                                # check for maximum matching characters (if there are 5 closing
                                # characters, we will probably need only 3 - depending on the rules)
-                               $rule = $rules[$piece->open];
+                               $rule = $this->rules[$piece->open];
+                               if ( $piece->close === '}-' && $piece->count > 2 ) {
+                                       # tweak for -{..{{ }}..}-
+                                       $rule = $this->rules['{'];
+                               }
                                if ( $count > $rule['max'] ) {
                                        # The specified maximum exists in the callback array, unless the caller
                                        # has made an error
@@ -557,17 +679,19 @@ class Preprocessor_DOM implements Preprocessor {
                                        }
                                }
 
-                               if ($matchingCount <= 0) {
+                               if ( $matchingCount <= 0 ) {
                                        # No matching element found in callback array
                                        # Output a literal closing brace and continue
-                                       $accum .= htmlspecialchars( str_repeat( $curChar, $count ) );
+                                       $endText = substr( $text, $i, $count );
+                                       $accum .= htmlspecialchars( $endText );
                                        $i += $count;
                                        continue;
                                }
                                $name = $rule['names'][$matchingCount];
                                if ( $name === null ) {
                                        // No element, just literal text
-                                       $element = $piece->breakSyntax( $matchingCount ) . str_repeat( $rule['end'], $matchingCount );
+                                       $endText = substr( $text, $i, $matchingCount );
+                                       $element = $piece->breakSyntax( $matchingCount ) . $endText;
                                } else {
                                        # Create XML element
                                        # Note: $parts is already XML, does not need to be encoded further
@@ -607,43 +731,41 @@ class Preprocessor_DOM implements Preprocessor {
                                $accum =& $stack->getAccum();
 
                                # Re-add the old stack element if it still has unmatched opening characters remaining
-                               if ($matchingCount < $piece->count) {
-                                       $piece->parts = array( new PPDPart );
+                               if ( $matchingCount < $piece->count ) {
+                                       $piece->parts = [ new PPDPart ];
                                        $piece->count -= $matchingCount;
                                        # do we still qualify for any callback with remaining count?
-                                       $names = $rules[$piece->open]['names'];
-                                       $skippedBraces = 0;
-                                       $enclosingAccum =& $accum;
-                                       while ( $piece->count ) {
-                                               if ( array_key_exists( $piece->count, $names ) ) {
-                                                       $stack->push( $piece );
-                                                       $accum =& $stack->getAccum();
-                                                       break;
-                                               }
-                                               --$piece->count;
-                                               $skippedBraces ++;
+                                       $min = $this->rules[$piece->open]['min'];
+                                       if ( $piece->count >= $min ) {
+                                               $stack->push( $piece );
+                                               $accum =& $stack->getAccum();
+                                       } else {
+                                               $s = substr( $piece->open, 0, -1 );
+                                               $s .= str_repeat(
+                                                       substr( $piece->open, -1 ),
+                                                       $piece->count - strlen( $s )
+                                               );
+                                               $accum .= $s;
                                        }
-                                       $enclosingAccum .= str_repeat( $piece->open, $skippedBraces );
                                }
                                $flags = $stack->getFlags();
                                extract( $flags );
 
                                # Add XML element to the enclosing accumulator
                                $accum .= $element;
-                       }
-
-                       elseif ( $found == 'pipe' ) {
+                       } elseif ( $found == 'pipe' ) {
                                $findEquals = true; // shortcut for getFlags()
                                $stack->addPart();
                                $accum =& $stack->getAccum();
                                ++$i;
-                       }
-
-                       elseif ( $found == 'equals' ) {
+                       } elseif ( $found == 'equals' ) {
                                $findEquals = false; // shortcut for getFlags()
                                $stack->getCurrentPart()->eqpos = strlen( $accum );
                                $accum .= '=';
                                ++$i;
+                       } elseif ( $found == 'dash' ) {
+                               $accum .= '-';
+                               ++$i;
                        }
                }
 
@@ -654,8 +776,6 @@ class Preprocessor_DOM implements Preprocessor {
                $stack->rootAccum .= '</root>';
                $xml = $stack->rootAccum;
 
-               wfProfileOut( __METHOD__ );
-               
                return $xml;
        }
 }
@@ -665,28 +785,36 @@ class Preprocessor_DOM implements Preprocessor {
  * @ingroup Parser
  */
 class PPDStack {
-       var $stack, $rootAccum, $top;
-       var $out;
-       var $elementClass = 'PPDStackElement';
+       public $stack, $rootAccum;
+
+       /**
+        * @var PPDStack
+        */
+       public $top;
+       public $out;
+       public $elementClass = 'PPDStackElement';
 
-       static $false = false;
+       public static $false = false;
 
-       function __construct() {
-               $this->stack = array();
+       public function __construct() {
+               $this->stack = [];
                $this->top = false;
                $this->rootAccum = '';
                $this->accum =& $this->rootAccum;
        }
 
-       function count() {
+       /**
+        * @return int
+        */
+       public function count() {
                return count( $this->stack );
        }
 
-       function &getAccum() {
+       public function &getAccum() {
                return $this->accum;
        }
 
-       function getCurrentPart() {
+       public function getCurrentPart() {
                if ( $this->top === false ) {
                        return false;
                } else {
@@ -694,25 +822,25 @@ class PPDStack {
                }
        }
 
-       function push( $data ) {
+       public function push( $data ) {
                if ( $data instanceof $this->elementClass ) {
                        $this->stack[] = $data;
                } else {
                        $class = $this->elementClass;
                        $this->stack[] = new $class( $data );
                }
-               $this->top = $this->stack[ count( $this->stack ) - 1 ];
+               $this->top = $this->stack[count( $this->stack ) - 1];
                $this->accum =& $this->top->getAccum();
        }
 
-       function pop() {
+       public function pop() {
                if ( !count( $this->stack ) ) {
-                       throw new MWException( __METHOD__.': no elements remaining' );
+                       throw new MWException( __METHOD__ . ': no elements remaining' );
                }
                $temp = array_pop( $this->stack );
 
                if ( count( $this->stack ) ) {
-                       $this->top = $this->stack[ count( $this->stack ) - 1 ];
+                       $this->top = $this->stack[count( $this->stack ) - 1];
                        $this->accum =& $this->top->getAccum();
                } else {
                        $this->top = self::$false;
@@ -721,18 +849,21 @@ class PPDStack {
                return $temp;
        }
 
-       function addPart( $s = '' ) {
+       public function addPart( $s = '' ) {
                $this->top->addPart( $s );
                $this->accum =& $this->top->getAccum();
        }
 
-       function getFlags() {
+       /**
+        * @return array
+        */
+       public function getFlags() {
                if ( !count( $this->stack ) ) {
-                       return array(
+                       return [
                                'findEquals' => false,
                                'findPipe' => false,
                                'inHeading' => false,
-                       );
+                       ];
                } else {
                        return $this->top->getFlags();
                }
@@ -743,57 +874,87 @@ class PPDStack {
  * @ingroup Parser
  */
 class PPDStackElement {
-       var $open,                      // Opening character (\n for heading)
-               $close,             // Matching closing character
-               $count,             // Number of opening characters found (number of "=" for heading)
-               $parts,             // Array of PPDPart objects describing pipe-separated parts.
-               $lineStart;         // True if the open char appeared at the start of the input line. Not set for headings.
+       /**
+        * @var string Opening character (\n for heading)
+        */
+       public $open;
 
-       var $partClass = 'PPDPart';
+       /**
+        * @var string Matching closing character
+        */
+       public $close;
 
-       function __construct( $data = array() ) {
+       /**
+        * @var int Number of opening characters found (number of "=" for heading)
+        */
+       public $count;
+
+       /**
+        * @var PPDPart[] Array of PPDPart objects describing pipe-separated parts.
+        */
+       public $parts;
+
+       /**
+        * @var bool True if the open char appeared at the start of the input line.
+        *  Not set for headings.
+        */
+       public $lineStart;
+
+       public $partClass = 'PPDPart';
+
+       public function __construct( $data = [] ) {
                $class = $this->partClass;
-               $this->parts = array( new $class );
+               $this->parts = [ new $class ];
 
                foreach ( $data as $name => $value ) {
                        $this->$name = $value;
                }
        }
 
-       function &getAccum() {
-               return $this->parts[count($this->parts) - 1]->out;
+       public function &getAccum() {
+               return $this->parts[count( $this->parts ) - 1]->out;
        }
 
-       function addPart( $s = '' ) {
+       public function addPart( $s = '' ) {
                $class = $this->partClass;
                $this->parts[] = new $class( $s );
        }
 
-       function getCurrentPart() {
-               return $this->parts[count($this->parts) - 1];
+       public function getCurrentPart() {
+               return $this->parts[count( $this->parts ) - 1];
        }
 
-       function getFlags() {
+       /**
+        * @return array
+        */
+       public function getFlags() {
                $partCount = count( $this->parts );
                $findPipe = $this->open != "\n" && $this->open != '[';
-               return array(
+               return [
                        'findPipe' => $findPipe,
                        'findEquals' => $findPipe && $partCount > 1 && !isset( $this->parts[$partCount - 1]->eqpos ),
                        'inHeading' => $this->open == "\n",
-               );
+               ];
        }
 
        /**
         * Get the output string that would result if the close is not found.
+        *
+        * @param bool|int $openingCount
+        * @return string
         */
-       function breakSyntax( $openingCount = false ) {
+       public function breakSyntax( $openingCount = false ) {
                if ( $this->open == "\n" ) {
                        $s = $this->parts[0]->out;
                } else {
                        if ( $openingCount === false ) {
                                $openingCount = $this->count;
                        }
-                       $s = str_repeat( $this->open, $openingCount );
+                       $s = substr( $this->open, 0, -1 );
+                       $s .= str_repeat(
+                               substr( $this->open, -1 ),
+                               $openingCount - strlen( $s )
+                       );
                        $first = true;
                        foreach ( $this->parts as $part ) {
                                if ( $first ) {
@@ -812,14 +973,17 @@ class PPDStackElement {
  * @ingroup Parser
  */
 class PPDPart {
-       var $out; // Output accumulator string
+       /**
+        * @var string Output accumulator string
+        */
+       public $out;
 
        // Optional member variables:
        //   eqpos        Position of equals sign in output accumulator
        //   commentEnd   Past-the-end input pointer for the last comment encountered
        //   visualEnd    Past-the-end input pointer for the end of the accumulator minus comments
 
-       function __construct( $out = '' ) {
+       public function __construct( $out = '' ) {
                $this->out = $out;
        }
 }
@@ -828,43 +992,72 @@ class PPDPart {
  * An expansion frame, used as a context to expand the result of preprocessToObj()
  * @ingroup Parser
  */
+// @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
 class PPFrame_DOM implements PPFrame {
-       var $preprocessor, $parser, $title;
-       var $titleCache;
+       // @codingStandardsIgnoreEnd
+
+       /**
+        * @var Preprocessor
+        */
+       public $preprocessor;
+
+       /**
+        * @var Parser
+        */
+       public $parser;
+
+       /**
+        * @var Title
+        */
+       public $title;
+       public $titleCache;
 
        /**
         * Hashtable listing templates which are disallowed for expansion in this frame,
         * having been encountered previously in parent frames.
         */
-       var $loopCheckHash;
+       public $loopCheckHash;
 
        /**
         * Recursion depth of this frame, top = 0
         * Note that this is NOT the same as expansion depth in expand()
         */
-       var $depth;
+       public $depth;
+
+       private $volatile = false;
+       private $ttl = null;
 
+       /**
+        * @var array
+        */
+       protected $childExpansionCache;
 
        /**
         * Construct a new preprocessor frame.
-        * @param $preprocessor Preprocessor: The parent preprocessor
+        * @param Preprocessor $preprocessor The parent preprocessor
         */
-       function __construct( $preprocessor ) {
+       public function __construct( $preprocessor ) {
                $this->preprocessor = $preprocessor;
                $this->parser = $preprocessor->parser;
                $this->title = $this->parser->mTitle;
-               $this->titleCache = array( $this->title ? $this->title->getPrefixedDBkey() : false );
-               $this->loopCheckHash = array();
+               $this->titleCache = [ $this->title ? $this->title->getPrefixedDBkey() : false ];
+               $this->loopCheckHash = [];
                $this->depth = 0;
+               $this->childExpansionCache = [];
        }
 
        /**
         * Create a new child frame
         * $args is optionally a multi-root PPNode or array containing the template arguments
+        *
+        * @param bool|array $args
+        * @param Title|bool $title
+        * @param int $indexOffset
+        * @return PPTemplateFrame_DOM
         */
-       function newChild( $args = false, $title = false ) {
-               $namedArgs = array();
-               $numberedArgs = array();
+       public function newChild( $args = false, $title = false, $indexOffset = 0 ) {
+               $namedArgs = [];
+               $numberedArgs = [];
                if ( $title === false ) {
                        $title = $this->title;
                }
@@ -874,7 +1067,10 @@ class PPFrame_DOM implements PPFrame {
                                $args = $args->node;
                        }
                        foreach ( $args as $arg ) {
-                               if ( !$xpath ) {
+                               if ( $arg instanceof PPNode ) {
+                                       $arg = $arg->node;
+                               }
+                               if ( !$xpath || $xpath->document !== $arg->ownerDocument ) {
                                        $xpath = new DOMXPath( $arg->ownerDocument );
                                }
 
@@ -883,11 +1079,26 @@ class PPFrame_DOM implements PPFrame {
                                if ( $nameNodes->item( 0 )->hasAttributes() ) {
                                        // Numbered parameter
                                        $index = $nameNodes->item( 0 )->attributes->getNamedItem( 'index' )->textContent;
+                                       $index = $index - $indexOffset;
+                                       if ( isset( $namedArgs[$index] ) || isset( $numberedArgs[$index] ) ) {
+                                               $this->parser->getOutput()->addWarning( wfMessage( 'duplicate-args-warning',
+                                                       wfEscapeWikiText( $this->title ),
+                                                       wfEscapeWikiText( $title ),
+                                                       wfEscapeWikiText( $index ) )->text() );
+                                               $this->parser->addTrackingCategory( 'duplicate-args-category' );
+                                       }
                                        $numberedArgs[$index] = $value->item( 0 );
                                        unset( $namedArgs[$index] );
                                } else {
                                        // Named parameter
                                        $name = trim( $this->expand( $nameNodes->item( 0 ), PPFrame::STRIP_COMMENTS ) );
+                                       if ( isset( $namedArgs[$name] ) || isset( $numberedArgs[$name] ) ) {
+                                               $this->parser->getOutput()->addWarning( wfMessage( 'duplicate-args-warning',
+                                                       wfEscapeWikiText( $this->title ),
+                                                       wfEscapeWikiText( $title ),
+                                                       wfEscapeWikiText( $name ) )->text() );
+                                               $this->parser->addTrackingCategory( 'duplicate-args-category' );
+                                       }
                                        $namedArgs[$name] = $value->item( 0 );
                                        unset( $numberedArgs[$name] );
                                }
@@ -896,22 +1107,49 @@ class PPFrame_DOM implements PPFrame {
                return new PPTemplateFrame_DOM( $this->preprocessor, $this, $numberedArgs, $namedArgs, $title );
        }
 
-       function expand( $root, $flags = 0 ) {
+       /**
+        * @throws MWException
+        * @param string|int $key
+        * @param string|PPNode_DOM|DOMDocument $root
+        * @param int $flags
+        * @return string
+        */
+       public function cachedExpand( $key, $root, $flags = 0 ) {
+               // we don't have a parent, so we don't have a cache
+               return $this->expand( $root, $flags );
+       }
+
+       /**
+        * @throws MWException
+        * @param string|PPNode_DOM|DOMDocument $root
+        * @param int $flags
+        * @return string
+        */
+       public function expand( $root, $flags = 0 ) {
                static $expansionDepth = 0;
                if ( is_string( $root ) ) {
                        return $root;
                }
 
-               if ( ++$this->parser->mPPNodeCount > $this->parser->mOptions->getMaxPPNodeCount() )
-               {
+               if ( ++$this->parser->mPPNodeCount > $this->parser->mOptions->getMaxPPNodeCount() ) {
+                       $this->parser->limitationWarn( 'node-count-exceeded',
+                               $this->parser->mPPNodeCount,
+                               $this->parser->mOptions->getMaxPPNodeCount()
+                       );
                        return '<span class="error">Node-count limit exceeded</span>';
                }
 
                if ( $expansionDepth > $this->parser->mOptions->getMaxPPExpandDepth() ) {
+                       $this->parser->limitationWarn( 'expansion-depth-exceeded',
+                               $expansionDepth,
+                               $this->parser->mOptions->getMaxPPExpandDepth()
+                       );
                        return '<span class="error">Expansion depth limit exceeded</span>';
                }
-               wfProfileIn( __METHOD__ );
                ++$expansionDepth;
+               if ( $expansionDepth > $this->parser->mHighestExpansionDepth ) {
+                       $this->parser->mHighestExpansionDepth = $expansionDepth;
+               }
 
                if ( $root instanceof PPNode_DOM ) {
                        $root = $root->node;
@@ -920,17 +1158,19 @@ class PPFrame_DOM implements PPFrame {
                        $root = $root->documentElement;
                }
 
-               $outStack = array( '', '' );
-               $iteratorStack = array( false, $root );
-               $indexStack = array( 0, 0 );
+               $outStack = [ '', '' ];
+               $iteratorStack = [ false, $root ];
+               $indexStack = [ 0, 0 ];
 
                while ( count( $iteratorStack ) > 1 ) {
                        $level = count( $outStack ) - 1;
-                       $iteratorNode =& $iteratorStack[ $level ];
+                       $iteratorNode =& $iteratorStack[$level];
                        $out =& $outStack[$level];
                        $index =& $indexStack[$level];
 
-                       if ( $iteratorNode instanceof PPNode_DOM ) $iteratorNode = $iteratorNode->node;
+                       if ( $iteratorNode instanceof PPNode_DOM ) {
+                               $iteratorNode = $iteratorNode->node;
+                       }
 
                        if ( is_array( $iteratorNode ) ) {
                                if ( $index >= count( $iteratorNode ) ) {
@@ -982,10 +1222,10 @@ class PPFrame_DOM implements PPFrame {
                                                $newIterator = $this->virtualBracketedImplode( '{{', '|', '}}', $title, $parts );
                                        } else {
                                                $lineStart = $contextNode->getAttribute( 'lineStart' );
-                                               $params = array(
+                                               $params = [
                                                        'title' => new PPNode_DOM( $title ),
                                                        'parts' => new PPNode_DOM( $parts ),
-                                                       'lineStart' => $lineStart );
+                                                       'lineStart' => $lineStart ];
                                                $ret = $this->parser->braceSubstitution( $params, $this );
                                                if ( isset( $ret['object'] ) ) {
                                                        $newIterator = $ret['object'];
@@ -1002,9 +1242,9 @@ class PPFrame_DOM implements PPFrame {
                                        if ( $flags & PPFrame::NO_ARGS ) {
                                                $newIterator = $this->virtualBracketedImplode( '{{{', '|', '}}}', $title, $parts );
                                        } else {
-                                               $params = array(
+                                               $params = [
                                                        'title' => new PPNode_DOM( $title ),
-                                                       'parts' => new PPNode_DOM( $parts ) );
+                                                       'parts' => new PPNode_DOM( $parts ) ];
                                                $ret = $this->parser->argSubstitution( $params, $this );
                                                if ( isset( $ret['object'] ) ) {
                                                        $newIterator = $ret['object'];
@@ -1015,19 +1255,20 @@ class PPFrame_DOM implements PPFrame {
                                } elseif ( $contextNode->nodeName == 'comment' ) {
                                        # HTML-style comment
                                        # Remove it in HTML, pre+remove and STRIP_COMMENTS modes
-                                       if ( $this->parser->ot['html']
+                                       # Not in RECOVER_COMMENTS mode (msgnw) though.
+                                       if ( ( $this->parser->ot['html']
                                                || ( $this->parser->ot['pre'] && $this->parser->mOptions->getRemoveComments() )
-                                               || ( $flags & PPFrame::STRIP_COMMENTS ) )
-                                       {
+                                               || ( $flags & PPFrame::STRIP_COMMENTS )
+                                               ) && !( $flags & PPFrame::RECOVER_COMMENTS )
+                                       ) {
                                                $out .= '';
-                                       }
-                                       # Add a strip marker in PST mode so that pstPass2() can run some old-fashioned regexes on the result
-                                       # Not in RECOVER_COMMENTS mode (extractSections) though
-                                       elseif ( $this->parser->ot['wiki'] && ! ( $flags & PPFrame::RECOVER_COMMENTS ) ) {
+                                       } elseif ( $this->parser->ot['wiki'] && !( $flags & PPFrame::RECOVER_COMMENTS ) ) {
+                                               # Add a strip marker in PST mode so that pstPass2() can
+                                               # run some old-fashioned regexes on the result.
+                                               # Not in RECOVER_COMMENTS mode (extractSections) though.
                                                $out .= $this->parser->insertStripItem( $contextNode->textContent );
-                                       }
-                                       # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
-                                       else {
+                                       } else {
+                                               # Recover the literal comment in RECOVER_COMMENTS and pre+no-remove
                                                $out .= $contextNode->textContent;
                                        }
                                } elseif ( $contextNode->nodeName == 'ignore' ) {
@@ -1035,7 +1276,9 @@ class PPFrame_DOM implements PPFrame {
                                        # OT_WIKI will only respect <ignore> in substed templates.
                                        # The other output types respect it unless NO_IGNORE is set.
                                        # extractSections() sets NO_IGNORE and so never respects it.
-                                       if ( ( !isset( $this->parent ) && $this->parser->ot['wiki'] ) || ( $flags & PPFrame::NO_IGNORE ) ) {
+                                       if ( ( !isset( $this->parent ) && $this->parser->ot['wiki'] )
+                                               || ( $flags & PPFrame::NO_IGNORE )
+                                       ) {
                                                $out .= $contextNode->textContent;
                                        } else {
                                                $out .= '';
@@ -1047,31 +1290,45 @@ class PPFrame_DOM implements PPFrame {
                                        $attrs = $xpath->query( 'attr', $contextNode );
                                        $inners = $xpath->query( 'inner', $contextNode );
                                        $closes = $xpath->query( 'close', $contextNode );
-                                       $params = array(
-                                               'name' => new PPNode_DOM( $names->item( 0 ) ),
-                                               'attr' => $attrs->length > 0 ? new PPNode_DOM( $attrs->item( 0 ) ) : null,
-                                               'inner' => $inners->length > 0 ? new PPNode_DOM( $inners->item( 0 ) ) : null,
-                                               'close' => $closes->length > 0 ? new PPNode_DOM( $closes->item( 0 ) ) : null,
-                                       );
-                                       $out .= $this->parser->extensionSubstitution( $params, $this );
+                                       if ( $flags & PPFrame::NO_TAGS ) {
+                                               $s = '<' . $this->expand( $names->item( 0 ), $flags );
+                                               if ( $attrs->length > 0 ) {
+                                                       $s .= $this->expand( $attrs->item( 0 ), $flags );
+                                               }
+                                               if ( $inners->length > 0 ) {
+                                                       $s .= '>' . $this->expand( $inners->item( 0 ), $flags );
+                                                       if ( $closes->length > 0 ) {
+                                                               $s .= $this->expand( $closes->item( 0 ), $flags );
+                                                       }
+                                               } else {
+                                                       $s .= '/>';
+                                               }
+                                               $out .= $s;
+                                       } else {
+                                               $params = [
+                                                       'name' => new PPNode_DOM( $names->item( 0 ) ),
+                                                       'attr' => $attrs->length > 0 ? new PPNode_DOM( $attrs->item( 0 ) ) : null,
+                                                       'inner' => $inners->length > 0 ? new PPNode_DOM( $inners->item( 0 ) ) : null,
+                                                       'close' => $closes->length > 0 ? new PPNode_DOM( $closes->item( 0 ) ) : null,
+                                               ];
+                                               $out .= $this->parser->extensionSubstitution( $params, $this );
+                                       }
                                } elseif ( $contextNode->nodeName == 'h' ) {
                                        # Heading
                                        $s = $this->expand( $contextNode->childNodes, $flags );
 
-                    # Insert a heading marker only for <h> children of <root>
-                    # This is to stop extractSections from going over multiple tree levels
-                    if ( $contextNode->parentNode->nodeName == 'root'
-                      && $this->parser->ot['html'] )
-                    {
+                                       # Insert a heading marker only for <h> children of <root>
+                                       # This is to stop extractSections from going over multiple tree levels
+                                       if ( $contextNode->parentNode->nodeName == 'root' && $this->parser->ot['html'] ) {
                                                # Insert heading index marker
                                                $headingIndex = $contextNode->getAttribute( 'i' );
                                                $titleText = $this->title->getPrefixedDBkey();
-                                               $this->parser->mHeadings[] = array( $titleText, $headingIndex );
+                                               $this->parser->mHeadings[] = [ $titleText, $headingIndex ];
                                                $serial = count( $this->parser->mHeadings ) - 1;
-                                               $marker = "{$this->parser->mUniqPrefix}-h-$serial-" . Parser::MARKER_SUFFIX;
+                                               $marker = Parser::MARKER_PREFIX . "-h-$serial-" . Parser::MARKER_SUFFIX;
                                                $count = $contextNode->getAttribute( 'level' );
                                                $s = substr( $s, 0, $count ) . $marker . substr( $s, $count );
-                                               $this->parser->mStripState->general->setPair( $marker, '' );
+                                               $this->parser->mStripState->addGeneral( $marker, '' );
                                        }
                                        $out .= $s;
                                } else {
@@ -1079,8 +1336,7 @@ class PPFrame_DOM implements PPFrame {
                                        $newIterator = $contextNode->childNodes;
                                }
                        } else {
-                               wfProfileOut( __METHOD__ );
-                               throw new MWException( __METHOD__.': Invalid parameter type' );
+                               throw new MWException( __METHOD__ . ': Invalid parameter type' );
                        }
 
                        if ( $newIterator !== false ) {
@@ -1103,19 +1359,26 @@ class PPFrame_DOM implements PPFrame {
                        }
                }
                --$expansionDepth;
-               wfProfileOut( __METHOD__ );
                return $outStack[0];
        }
 
-       function implodeWithFlags( $sep, $flags /*, ... */ ) {
+       /**
+        * @param string $sep
+        * @param int $flags
+        * @param string|PPNode_DOM|DOMDocument $args,...
+        * @return string
+        */
+       public function implodeWithFlags( $sep, $flags /*, ... */ ) {
                $args = array_slice( func_get_args(), 2 );
 
                $first = true;
                $s = '';
                foreach ( $args as $root ) {
-                       if ( $root instanceof PPNode_DOM ) $root = $root->node;
+                       if ( $root instanceof PPNode_DOM ) {
+                               $root = $root->node;
+                       }
                        if ( !is_array( $root ) && !( $root instanceof DOMNodeList ) ) {
-                               $root = array( $root );
+                               $root = [ $root ];
                        }
                        foreach ( $root as $node ) {
                                if ( $first ) {
@@ -1132,8 +1395,12 @@ class PPFrame_DOM implements PPFrame {
        /**
         * Implode with no flags specified
         * This previously called implodeWithFlags but has now been inlined to reduce stack depth
+        *
+        * @param string $sep
+        * @param string|PPNode_DOM|DOMDocument $args,...
+        * @return string
         */
-       function implode( $sep /*, ... */ ) {
+       public function implode( $sep /*, ... */ ) {
                $args = array_slice( func_get_args(), 1 );
 
                $first = true;
@@ -1143,7 +1410,7 @@ class PPFrame_DOM implements PPFrame {
                                $root = $root->node;
                        }
                        if ( !is_array( $root ) && !( $root instanceof DOMNodeList ) ) {
-                               $root = array( $root );
+                               $root = [ $root ];
                        }
                        foreach ( $root as $node ) {
                                if ( $first ) {
@@ -1160,10 +1427,14 @@ class PPFrame_DOM implements PPFrame {
        /**
         * Makes an object that, when expand()ed, will be the same as one obtained
         * with implode()
+        *
+        * @param string $sep
+        * @param string|PPNode_DOM|DOMDocument $args,...
+        * @return array
         */
-       function virtualImplode( $sep /*, ... */ ) {
+       public function virtualImplode( $sep /*, ... */ ) {
                $args = array_slice( func_get_args(), 1 );
-               $out = array();
+               $out = [];
                $first = true;
 
                foreach ( $args as $root ) {
@@ -1171,7 +1442,7 @@ class PPFrame_DOM implements PPFrame {
                                $root = $root->node;
                        }
                        if ( !is_array( $root ) && !( $root instanceof DOMNodeList ) ) {
-                               $root = array( $root );
+                               $root = [ $root ];
                        }
                        foreach ( $root as $node ) {
                                if ( $first ) {
@@ -1187,10 +1458,15 @@ class PPFrame_DOM implements PPFrame {
 
        /**
         * Virtual implode with brackets
+        * @param string $start
+        * @param string $sep
+        * @param string $end
+        * @param string|PPNode_DOM|DOMDocument $args,...
+        * @return array
         */
-       function virtualBracketedImplode( $start, $sep, $end /*, ... */ ) {
+       public function virtualBracketedImplode( $start, $sep, $end /*, ... */ ) {
                $args = array_slice( func_get_args(), 3 );
-               $out = array( $start );
+               $out = [ $start ];
                $first = true;
 
                foreach ( $args as $root ) {
@@ -1198,7 +1474,7 @@ class PPFrame_DOM implements PPFrame {
                                $root = $root->node;
                        }
                        if ( !is_array( $root ) && !( $root instanceof DOMNodeList ) ) {
-                               $root = array( $root );
+                               $root = [ $root ];
                        }
                        foreach ( $root as $node ) {
                                if ( $first ) {
@@ -1213,11 +1489,11 @@ class PPFrame_DOM implements PPFrame {
                return $out;
        }
 
-       function __toString() {
+       public function __toString() {
                return 'frame{}';
        }
 
-       function getPDBK( $level = false ) {
+       public function getPDBK( $level = false ) {
                if ( $level === false ) {
                        return $this->title->getPrefixedDBkey();
                } else {
@@ -1225,53 +1501,137 @@ class PPFrame_DOM implements PPFrame {
                }
        }
 
-       function getArguments() {
-               return array();
+       /**
+        * @return array
+        */
+       public function getArguments() {
+               return [];
        }
 
-       function getNumberedArguments() {
-               return array();
+       /**
+        * @return array
+        */
+       public function getNumberedArguments() {
+               return [];
        }
 
-       function getNamedArguments() {
-               return array();
+       /**
+        * @return array
+        */
+       public function getNamedArguments() {
+               return [];
        }
 
        /**
         * Returns true if there are no arguments in this frame
+        *
+        * @return bool
         */
-       function isEmpty() {
+       public function isEmpty() {
                return true;
        }
 
-       function getArgument( $name ) {
+       /**
+        * @param int|string $name
+        * @return bool Always false in this implementation.
+        */
+       public function getArgument( $name ) {
                return false;
        }
 
        /**
         * Returns true if the infinite loop check is OK, false if a loop is detected
+        *
+        * @param Title $title
+        * @return bool
         */
-       function loopCheck( $title ) {
+       public function loopCheck( $title ) {
                return !isset( $this->loopCheckHash[$title->getPrefixedDBkey()] );
        }
 
        /**
         * Return true if the frame is a template frame
+        *
+        * @return bool
         */
-       function isTemplate() {
+       public function isTemplate() {
                return false;
        }
+
+       /**
+        * Get a title of frame
+        *
+        * @return Title
+        */
+       public function getTitle() {
+               return $this->title;
+       }
+
+       /**
+        * Set the volatile flag
+        *
+        * @param bool $flag
+        */
+       public function setVolatile( $flag = true ) {
+               $this->volatile = $flag;
+       }
+
+       /**
+        * Get the volatile flag
+        *
+        * @return bool
+        */
+       public function isVolatile() {
+               return $this->volatile;
+       }
+
+       /**
+        * Set the TTL
+        *
+        * @param int $ttl
+        */
+       public function setTTL( $ttl ) {
+               if ( $ttl !== null && ( $this->ttl === null || $ttl < $this->ttl ) ) {
+                       $this->ttl = $ttl;
+               }
+       }
+
+       /**
+        * Get the TTL
+        *
+        * @return int|null
+        */
+       public function getTTL() {
+               return $this->ttl;
+       }
 }
 
 /**
  * Expansion frame with template arguments
  * @ingroup Parser
  */
+// @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
 class PPTemplateFrame_DOM extends PPFrame_DOM {
-       var $numberedArgs, $namedArgs, $parent;
-       var $numberedExpansionCache, $namedExpansionCache;
+       // @codingStandardsIgnoreEnd
+
+       public $numberedArgs, $namedArgs;
+
+       /**
+        * @var PPFrame_DOM
+        */
+       public $parent;
+       public $numberedExpansionCache, $namedExpansionCache;
 
-       function __construct( $preprocessor, $parent = false, $numberedArgs = array(), $namedArgs = array(), $title = false ) {
+       /**
+        * @param Preprocessor $preprocessor
+        * @param bool|PPFrame_DOM $parent
+        * @param array $numberedArgs
+        * @param array $namedArgs
+        * @param bool|Title $title
+        */
+       public function __construct( $preprocessor, $parent = false, $numberedArgs = [],
+               $namedArgs = [], $title = false
+       ) {
                parent::__construct( $preprocessor );
 
                $this->parent = $parent;
@@ -1286,10 +1646,10 @@ class PPTemplateFrame_DOM extends PPFrame_DOM {
                        $this->loopCheckHash[$pdbk] = true;
                }
                $this->depth = $parent->depth + 1;
-               $this->numberedExpansionCache = $this->namedExpansionCache = array();
+               $this->numberedExpansionCache = $this->namedExpansionCache = [];
        }
 
-       function __toString() {
+       public function __toString() {
                $s = 'tplframe{';
                $first = true;
                $args = $this->numberedArgs + $this->namedArgs;
@@ -1305,51 +1665,83 @@ class PPTemplateFrame_DOM extends PPFrame_DOM {
                $s .= '}';
                return $s;
        }
+
+       /**
+        * @throws MWException
+        * @param string|int $key
+        * @param string|PPNode_DOM|DOMDocument $root
+        * @param int $flags
+        * @return string
+        */
+       public function cachedExpand( $key, $root, $flags = 0 ) {
+               if ( isset( $this->parent->childExpansionCache[$key] ) ) {
+                       return $this->parent->childExpansionCache[$key];
+               }
+               $retval = $this->expand( $root, $flags );
+               if ( !$this->isVolatile() ) {
+                       $this->parent->childExpansionCache[$key] = $retval;
+               }
+               return $retval;
+       }
+
        /**
         * Returns true if there are no arguments in this frame
+        *
+        * @return bool
         */
-       function isEmpty() {
+       public function isEmpty() {
                return !count( $this->numberedArgs ) && !count( $this->namedArgs );
        }
 
-       function getArguments() {
-               $arguments = array();
+       public function getArguments() {
+               $arguments = [];
                foreach ( array_merge(
-                               array_keys($this->numberedArgs),
-                               array_keys($this->namedArgs)) as $key ) {
-                       $arguments[$key] = $this->getArgument($key);
+                               array_keys( $this->numberedArgs ),
+                               array_keys( $this->namedArgs ) ) as $key ) {
+                       $arguments[$key] = $this->getArgument( $key );
                }
                return $arguments;
        }
-       
-       function getNumberedArguments() {
-               $arguments = array();
-               foreach ( array_keys($this->numberedArgs) as $key ) {
-                       $arguments[$key] = $this->getArgument($key);
+
+       public function getNumberedArguments() {
+               $arguments = [];
+               foreach ( array_keys( $this->numberedArgs ) as $key ) {
+                       $arguments[$key] = $this->getArgument( $key );
                }
                return $arguments;
        }
-       
-       function getNamedArguments() {
-               $arguments = array();
-               foreach ( array_keys($this->namedArgs) as $key ) {
-                       $arguments[$key] = $this->getArgument($key);
+
+       public function getNamedArguments() {
+               $arguments = [];
+               foreach ( array_keys( $this->namedArgs ) as $key ) {
+                       $arguments[$key] = $this->getArgument( $key );
                }
                return $arguments;
        }
 
-       function getNumberedArgument( $index ) {
+       /**
+        * @param int $index
+        * @return string|bool
+        */
+       public function getNumberedArgument( $index ) {
                if ( !isset( $this->numberedArgs[$index] ) ) {
                        return false;
                }
                if ( !isset( $this->numberedExpansionCache[$index] ) ) {
                        # No trimming for unnamed arguments
-                       $this->numberedExpansionCache[$index] = $this->parent->expand( $this->numberedArgs[$index], PPFrame::STRIP_COMMENTS );
+                       $this->numberedExpansionCache[$index] = $this->parent->expand(
+                               $this->numberedArgs[$index],
+                               PPFrame::STRIP_COMMENTS
+                       );
                }
                return $this->numberedExpansionCache[$index];
        }
 
-       function getNamedArgument( $name ) {
+       /**
+        * @param string $name
+        * @return string|bool
+        */
+       public function getNamedArgument( $name ) {
                if ( !isset( $this->namedArgs[$name] ) ) {
                        return false;
                }
@@ -1361,7 +1753,11 @@ class PPTemplateFrame_DOM extends PPFrame_DOM {
                return $this->namedExpansionCache[$name];
        }
 
-       function getArgument( $name ) {
+       /**
+        * @param int|string $name
+        * @return string|bool
+        */
+       public function getArgument( $name ) {
                $text = $this->getNumberedArgument( $name );
                if ( $text === false ) {
                        $text = $this->getNamedArgument( $name );
@@ -1371,25 +1767,40 @@ class PPTemplateFrame_DOM extends PPFrame_DOM {
 
        /**
         * Return true if the frame is a template frame
+        *
+        * @return bool
         */
-       function isTemplate() {
+       public function isTemplate() {
                return true;
        }
+
+       public function setVolatile( $flag = true ) {
+               parent::setVolatile( $flag );
+               $this->parent->setVolatile( $flag );
+       }
+
+       public function setTTL( $ttl ) {
+               parent::setTTL( $ttl );
+               $this->parent->setTTL( $ttl );
+       }
 }
 
 /**
  * Expansion frame with custom arguments
  * @ingroup Parser
  */
+// @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
 class PPCustomFrame_DOM extends PPFrame_DOM {
-       var $args;
+       // @codingStandardsIgnoreEnd
 
-       function __construct( $preprocessor, $args ) {
+       public $args;
+
+       public function __construct( $preprocessor, $args ) {
                parent::__construct( $preprocessor );
                $this->args = $args;
        }
 
-       function __toString() {
+       public function __toString() {
                $s = 'cstmframe{';
                $first = true;
                foreach ( $this->args as $name => $value ) {
@@ -1405,36 +1816,57 @@ class PPCustomFrame_DOM extends PPFrame_DOM {
                return $s;
        }
 
-       function isEmpty() {
+       /**
+        * @return bool
+        */
+       public function isEmpty() {
                return !count( $this->args );
        }
 
-       function getArgument( $index ) {
+       /**
+        * @param int|string $index
+        * @return string|bool
+        */
+       public function getArgument( $index ) {
                if ( !isset( $this->args[$index] ) ) {
                        return false;
                }
                return $this->args[$index];
        }
+
+       public function getArguments() {
+               return $this->args;
+       }
 }
 
 /**
  * @ingroup Parser
  */
+// @codingStandardsIgnoreStart Squiz.Classes.ValidClassName.NotCamelCaps
 class PPNode_DOM implements PPNode {
-       var $node;
+       // @codingStandardsIgnoreEnd
 
-       function __construct( $node, $xpath = false ) {
+       /**
+        * @var DOMElement
+        */
+       public $node;
+       public $xpath;
+
+       public function __construct( $node, $xpath = false ) {
                $this->node = $node;
        }
 
-       function __get( $name ) {
-               if ( $name == 'xpath' ) {
+       /**
+        * @return DOMXPath
+        */
+       public function getXPath() {
+               if ( $this->xpath === null ) {
                        $this->xpath = new DOMXPath( $this->node->ownerDocument );
                }
                return $this->xpath;
        }
 
-       function __toString() {
+       public function __toString() {
                if ( $this->node instanceof DOMNodeList ) {
                        $s = '';
                        foreach ( $this->node as $node ) {
@@ -1446,23 +1878,40 @@ class PPNode_DOM implements PPNode {
                return $s;
        }
 
-       function getChildren() {
+       /**
+        * @return bool|PPNode_DOM
+        */
+       public function getChildren() {
                return $this->node->childNodes ? new self( $this->node->childNodes ) : false;
        }
 
-       function getFirstChild() {
+       /**
+        * @return bool|PPNode_DOM
+        */
+       public function getFirstChild() {
                return $this->node->firstChild ? new self( $this->node->firstChild ) : false;
        }
 
-       function getNextSibling() {
+       /**
+        * @return bool|PPNode_DOM
+        */
+       public function getNextSibling() {
                return $this->node->nextSibling ? new self( $this->node->nextSibling ) : false;
        }
 
-       function getChildrenOfType( $type ) {
-               return new self( $this->xpath->query( $type, $this->node ) );
+       /**
+        * @param string $type
+        *
+        * @return bool|PPNode_DOM
+        */
+       public function getChildrenOfType( $type ) {
+               return new self( $this->getXPath()->query( $type, $this->node ) );
        }
 
-       function getLength() {
+       /**
+        * @return int
+        */
+       public function getLength() {
                if ( $this->node instanceof DOMNodeList ) {
                        return $this->node->length;
                } else {
@@ -1470,12 +1919,19 @@ class PPNode_DOM implements PPNode {
                }
        }
 
-       function item( $i ) {
+       /**
+        * @param int $i
+        * @return bool|PPNode_DOM
+        */
+       public function item( $i ) {
                $item = $this->node->item( $i );
                return $item ? new self( $item ) : false;
        }
 
-       function getName() {
+       /**
+        * @return string
+        */
+       public function getName() {
                if ( $this->node instanceof DOMNodeList ) {
                        return '#nodelist';
                } else {
@@ -1484,40 +1940,48 @@ class PPNode_DOM implements PPNode {
        }
 
        /**
-        * Split a <part> node into an associative array containing:
-        *    name          PPNode name
-        *    index         String index
-        *    value         PPNode value
+        * Split a "<part>" node into an associative array containing:
+        *  - name          PPNode name
+        *  - index         String index
+        *  - value         PPNode value
+        *
+        * @throws MWException
+        * @return array
         */
-       function splitArg() {
-               $names = $this->xpath->query( 'name', $this->node );
-               $values = $this->xpath->query( 'value', $this->node );
+       public function splitArg() {
+               $xpath = $this->getXPath();
+               $names = $xpath->query( 'name', $this->node );
+               $values = $xpath->query( 'value', $this->node );
                if ( !$names->length || !$values->length ) {
                        throw new MWException( 'Invalid brace node passed to ' . __METHOD__ );
                }
                $name = $names->item( 0 );
                $index = $name->getAttribute( 'index' );
-               return array(
+               return [
                        'name' => new self( $name ),
                        'index' => $index,
-                       'value' => new self( $values->item( 0 ) ) );
+                       'value' => new self( $values->item( 0 ) ) ];
        }
 
        /**
-        * Split an <ext> node into an associative array containing name, attr, inner and close
+        * Split an "<ext>" node into an associative array containing name, attr, inner and close
         * All values in the resulting array are PPNodes. Inner and close are optional.
+        *
+        * @throws MWException
+        * @return array
         */
-       function splitExt() {
-               $names = $this->xpath->query( 'name', $this->node );
-               $attrs = $this->xpath->query( 'attr', $this->node );
-               $inners = $this->xpath->query( 'inner', $this->node );
-               $closes = $this->xpath->query( 'close', $this->node );
+       public function splitExt() {
+               $xpath = $this->getXPath();
+               $names = $xpath->query( 'name', $this->node );
+               $attrs = $xpath->query( 'attr', $this->node );
+               $inners = $xpath->query( 'inner', $this->node );
+               $closes = $xpath->query( 'close', $this->node );
                if ( !$names->length || !$attrs->length ) {
                        throw new MWException( 'Invalid ext node passed to ' . __METHOD__ );
                }
-               $parts = array(
+               $parts = [
                        'name' => new self( $names->item( 0 ) ),
-                       'attr' => new self( $attrs->item( 0 ) ) );
+                       'attr' => new self( $attrs->item( 0 ) ) ];
                if ( $inners->length ) {
                        $parts['inner'] = new self( $inners->item( 0 ) );
                }
@@ -1528,16 +1992,18 @@ class PPNode_DOM implements PPNode {
        }
 
        /**
-        * Split a <h> node
+        * Split a "<h>" node
+        * @throws MWException
+        * @return array
         */
-       function splitHeading() {
-               if ( !$this->nodeName == 'h' ) {
+       public function splitHeading() {
+               if ( $this->getName() !== 'h' ) {
                        throw new MWException( 'Invalid h node passed to ' . __METHOD__ );
                }
-               return array(
+               return [
                        'i' => $this->node->getAttribute( 'i' ),
                        'level' => $this->node->getAttribute( 'level' ),
                        'contents' => $this->getChildren()
-               );
+               ];
        }
 }