Merge "Warn if stateful ParserOutput transforms are used"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Fri, 2 Feb 2018 02:45:27 +0000 (02:45 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 2 Feb 2018 02:45:27 +0000 (02:45 +0000)
1  2 
includes/parser/ParserOutput.php
tests/phpunit/includes/parser/ParserOutputTest.php

   */
  class ParserOutput extends CacheTime {
        /**
 -       * Feature flag to indicate to extensions that MediaWiki core supports and
 +       * Feature flags to indicate to extensions that MediaWiki core supports and
         * uses getText() stateless transforms.
         */
        const SUPPORTS_STATELESS_TRANSFORMS = 1;
 +      const SUPPORTS_UNWRAP_TRANSFORM = 1;
  
        /**
         * @var string $mText The output text
         *     to generate one and `__NOTOC__` wasn't used. Default is true,
         *     but might be statefully overridden.
         *  - enableSectionEditLinks: (bool) Include section edit links, assuming
 -       *    section edit link tokens are present in the HTML. Default is true,
 +       *     section edit link tokens are present in the HTML. Default is true,
         *     but might be statefully overridden.
 +       *  - unwrap: (bool) Remove a wrapping mw-parser-output div. Default is false.
         * @return string HTML
         */
        public function getText( $options = [] ) {
-               // @todo Warn if !array_key_exists( 'allowTOC', $options ) && empty( $this->mTOCEnabled )
+               if ( !array_key_exists( 'allowTOC', $options ) && empty( $this->mTOCEnabled ) ) {
+                       wfDeprecated( 'ParserOutput stateful allowTOC', '1.31' );
+               }
  
-               // @todo Warn if !array_key_exists( 'enableSectionEditLinks', $options )
-               //     && !$this->mEditSectionTokens
                //  Note that while $this->mEditSectionTokens formerly defaulted to false,
                //  ParserOptions->getEditSection() defaults to true and Parser copies
                //  that to us so true makes more sense as the stateless default.
+               if ( !array_key_exists( 'enableSectionEditLinks', $options ) && !$this->mEditSectionTokens ) {
+                       wfDeprecated( 'ParserOutput stateful enableSectionEditLinks', '1.31' );
+               }
  
                $options += [
                        // empty() here because old cached versions might lack the field somehow.
                        // In that situation, the historical behavior (possibly buggy) is to remove the TOC.
                        'allowTOC' => !empty( $this->mTOCEnabled ),
                        'enableSectionEditLinks' => $this->mEditSectionTokens,
 +                      'unwrap' => false,
                ];
                $text = $this->mText;
  
                Hooks::runWithoutAbort( 'ParserOutputPostCacheTransform', [ $this, &$text, &$options ] );
  
 +              if ( $options['unwrap'] !== false ) {
 +                      $start = Html::openElement( 'div', [
 +                              'class' => 'mw-parser-output'
 +                      ] );
 +                      $startLen = strlen( $start );
 +                      $end = Html::closeElement( 'div' );
 +                      $endLen = strlen( $end );
 +
 +                      if ( substr( $text, 0, $startLen ) === $start && substr( $text, -$endLen ) === $end ) {
 +                              $text = substr( $text, $startLen, -$endLen );
 +                      }
 +              }
 +
                if ( $options['enableSectionEditLinks'] ) {
                        $text = preg_replace_callback(
                                self::EDITSECTION_REGEX,
  
                # Replace unnecessary URL escape codes with the referenced character
                # This prevents spammers from hiding links from the filters
 -              $url = parser::normalizeLinkUrl( $url );
 +              $url = Parser::normalizeLinkUrl( $url );
  
                $registerExternalLink = true;
                if ( !$wgRegisterInternalExternals ) {
@@@ -105,6 -105,8 +105,8 @@@ class ParserOutputTest extends MediaWik
                        'wgScriptPath' => '/w',
                        'wgScript' => '/w/index.php',
                ] );
+               $this->hideDeprecated( 'ParserOutput stateful allowTOC' );
+               $this->hideDeprecated( 'ParserOutput stateful enableSectionEditLinks' );
  
                $po = new ParserOutput( $text );
  
        }
  
        public static function provideGetText() {
 -              // @codingStandardsIgnoreStart Generic.Files.LineLength
 +              // phpcs:disable Generic.Files.LineLength
                $text = <<<EOF
 -<p>Test document.
 +<div class="mw-parser-output"><p>Test document.
  </p>
  <mw:toc><div id="toc" class="toc"><div class="toctitle"><h2>Contents</h2></div>
  <ul>
  </p>
  <h2><span class="mw-headline" id="Section_3">Section 3</span><mw:editsection page="Test Page" section="4">Section 3</mw:editsection></h2>
  <p>Three
 -</p>
 +</p></div>
  EOF;
  
                return [
                        'No stateless options, default state' => [
                                [], [], $text, <<<EOF
 -<p>Test document.
 +<div class="mw-parser-output"><p>Test document.
  </p>
  <div id="toc" class="toc"><div class="toctitle"><h2>Contents</h2></div>
  <ul>
  </p>
  <h2><span class="mw-headline" id="Section_3">Section 3</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&amp;action=edit&amp;section=4" title="Edit section: Section 3">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
  <p>Three
 -</p>
 +</p></div>
  EOF
                        ],
                        'No stateless options, TOC statefully disabled' => [
                                [], [ 'mTOCEnabled' => false ], $text, <<<EOF
 -<p>Test document.
 +<div class="mw-parser-output"><p>Test document.
  </p>
  
  <h2><span class="mw-headline" id="Section_1">Section 1</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&amp;action=edit&amp;section=1" title="Edit section: Section 1">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
  </p>
  <h2><span class="mw-headline" id="Section_3">Section 3</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&amp;action=edit&amp;section=4" title="Edit section: Section 3">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
  <p>Three
 -</p>
 +</p></div>
  EOF
                        ],
                        'No stateless options, section edits statefully disabled' => [
                                [], [ 'mEditSectionTokens' => false ], $text, <<<EOF
 -<p>Test document.
 +<div class="mw-parser-output"><p>Test document.
  </p>
  <div id="toc" class="toc"><div class="toctitle"><h2>Contents</h2></div>
  <ul>
  </p>
  <h2><span class="mw-headline" id="Section_3">Section 3</span></h2>
  <p>Three
 -</p>
 +</p></div>
  EOF
                        ],
                        'Stateless options override stateful settings' => [
                                [ 'allowTOC' => true, 'enableSectionEditLinks' => true ],
                                [ 'mTOCEnabled' => false, 'mEditSectionTokens' => false ],
                                $text, <<<EOF
 -<p>Test document.
 +<div class="mw-parser-output"><p>Test document.
  </p>
  <div id="toc" class="toc"><div class="toctitle"><h2>Contents</h2></div>
  <ul>
  </p>
  <h2><span class="mw-headline" id="Section_3">Section 3</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&amp;action=edit&amp;section=4" title="Edit section: Section 3">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
  <p>Three
 -</p>
 +</p></div>
  EOF
                        ],
                        'Statelessly disable section edit links' => [
                                [ 'enableSectionEditLinks' => false ], [], $text, <<<EOF
 -<p>Test document.
 +<div class="mw-parser-output"><p>Test document.
  </p>
  <div id="toc" class="toc"><div class="toctitle"><h2>Contents</h2></div>
  <ul>
  </p>
  <h2><span class="mw-headline" id="Section_3">Section 3</span></h2>
  <p>Three
 -</p>
 +</p></div>
  EOF
                        ],
                        'Statelessly disable TOC' => [
                                [ 'allowTOC' => false ], [], $text, <<<EOF
 +<div class="mw-parser-output"><p>Test document.
 +</p>
 +
 +<h2><span class="mw-headline" id="Section_1">Section 1</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&amp;action=edit&amp;section=1" title="Edit section: Section 1">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
 +<p>One
 +</p>
 +<h2><span class="mw-headline" id="Section_2">Section 2</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&amp;action=edit&amp;section=2" title="Edit section: Section 2">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
 +<p>Two
 +</p>
 +<h3><span class="mw-headline" id="Section_2.1">Section 2.1</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&amp;action=edit&amp;section=3" title="Edit section: Section 2.1">edit</a><span class="mw-editsection-bracket">]</span></span></h3>
 +<p>Two point one
 +</p>
 +<h2><span class="mw-headline" id="Section_3">Section 3</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&amp;action=edit&amp;section=4" title="Edit section: Section 3">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
 +<p>Three
 +</p></div>
 +EOF
 +                      ],
 +                      'Statelessly unwrap text' => [
 +                              [ 'unwrap' => true ], [], $text, <<<EOF
  <p>Test document.
  </p>
 +<div id="toc" class="toc"><div class="toctitle"><h2>Contents</h2></div>
 +<ul>
 +<li class="toclevel-1 tocsection-1"><a href="#Section_1"><span class="tocnumber">1</span> <span class="toctext">Section 1</span></a></li>
 +<li class="toclevel-1 tocsection-2"><a href="#Section_2"><span class="tocnumber">2</span> <span class="toctext">Section 2</span></a>
 +<ul>
 +<li class="toclevel-2 tocsection-3"><a href="#Section_2.1"><span class="tocnumber">2.1</span> <span class="toctext">Section 2.1</span></a></li>
 +</ul>
 +</li>
 +<li class="toclevel-1 tocsection-4"><a href="#Section_3"><span class="tocnumber">3</span> <span class="toctext">Section 3</span></a></li>
 +</ul>
 +</div>
  
  <h2><span class="mw-headline" id="Section_1">Section 1</span><span class="mw-editsection"><span class="mw-editsection-bracket">[</span><a href="/w/index.php?title=Test_Page&amp;action=edit&amp;section=1" title="Edit section: Section 1">edit</a><span class="mw-editsection-bracket">]</span></span></h2>
  <p>One
  </p>
  EOF
                        ],
 +                      'Unwrap without a mw-parser-output wrapper' => [
 +                              [ 'unwrap' => true ], [], '<div class="foobar">Content</div>', '<div class="foobar">Content</div>'
 +                      ],
                ];
 -              // @codingStandardsIgnoreEnd
 +              // phpcs:enable
        }
  
  }