Merge "ApiComparePages: Add 'fromsection' and 'tosection' parameters"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 4 Jan 2018 07:01:31 +0000 (07:01 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 4 Jan 2018 07:01:31 +0000 (07:01 +0000)
includes/api/ApiComparePages.php
includes/api/i18n/en.json
includes/api/i18n/qqq.json
tests/phpunit/includes/api/ApiComparePagesTest.php

index eb67bab..5486594 100644 (file)
@@ -94,6 +94,26 @@ class ApiComparePages extends ApiBase {
                        $this->dieWithError( 'apierror-baddiff' );
                }
 
+               // Extract sections, if told to
+               if ( isset( $params['fromsection'] ) ) {
+                       $fromContent = $fromContent->getSection( $params['fromsection'] );
+                       if ( !$fromContent ) {
+                               $this->dieWithError(
+                                       [ 'apierror-compare-nosuchfromsection', wfEscapeWikiText( $params['fromsection'] ) ],
+                                       'nosuchfromsection'
+                               );
+                       }
+               }
+               if ( isset( $params['tosection'] ) ) {
+                       $toContent = $toContent->getSection( $params['tosection'] );
+                       if ( !$toContent ) {
+                               $this->dieWithError(
+                                       [ 'apierror-compare-nosuchtosection', wfEscapeWikiText( $params['tosection'] ) ],
+                                       'nosuchtosection'
+                               );
+                       }
+               }
+
                // Get the diff
                $context = new DerivativeContext( $this->getContext() );
                if ( $relRev && $relRev->getTitle() ) {
@@ -444,6 +464,7 @@ class ApiComparePages extends ApiBase {
                        'text' => [
                                ApiBase::PARAM_TYPE => 'text'
                        ],
+                       'section' => null,
                        'pst' => false,
                        'contentformat' => [
                                ApiBase::PARAM_TYPE => ContentHandler::getAllContentFormats(),
index e1360c8..cceed01 100644 (file)
@@ -64,6 +64,7 @@
        "apihelp-compare-param-fromid": "First page ID to compare.",
        "apihelp-compare-param-fromrev": "First revision to compare.",
        "apihelp-compare-param-fromtext": "Use this text instead of the content of the revision specified by <var>fromtitle</var>, <var>fromid</var> or <var>fromrev</var>.",
+       "apihelp-compare-param-fromsection": "Only use the specified section of the specified 'from' content.",
        "apihelp-compare-param-frompst": "Do a pre-save transform on <var>fromtext</var>.",
        "apihelp-compare-param-fromcontentmodel": "Content model of <var>fromtext</var>. If not supplied, it will be guessed based on the other parameters.",
        "apihelp-compare-param-fromcontentformat": "Content serialization format of <var>fromtext</var>.",
@@ -72,6 +73,7 @@
        "apihelp-compare-param-torev": "Second revision to compare.",
        "apihelp-compare-param-torelative": "Use a revision relative to the revision determined from <var>fromtitle</var>, <var>fromid</var> or <var>fromrev</var>. All of the other 'to' options will be ignored.",
        "apihelp-compare-param-totext": "Use this text instead of the content of the revision specified by <var>totitle</var>, <var>toid</var> or <var>torev</var>.",
+       "apihelp-compare-param-tosection": "Only use the specified section of the specified 'to' content.",
        "apihelp-compare-param-topst": "Do a pre-save transform on <var>totext</var>.",
        "apihelp-compare-param-tocontentmodel": "Content model of <var>totext</var>. If not supplied, it will be guessed based on the other parameters.",
        "apihelp-compare-param-tocontentformat": "Content serialization format of <var>totext</var>.",
        "apierror-chunk-too-small": "Minimum chunk size is $1 {{PLURAL:$1|byte|bytes}} for non-final chunks.",
        "apierror-cidrtoobroad": "$1 CIDR ranges broader than /$2 are not accepted.",
        "apierror-compare-no-title": "Cannot pre-save transform without a title. Try specifying <var>fromtitle</var> or <var>totitle</var>.",
+       "apierror-compare-nosuchfromsection": "There is no section $1 in the 'from' content.",
+       "apierror-compare-nosuchtosection": "There is no section $1 in the 'to' content.",
        "apierror-compare-relative-to-nothing": "No 'from' revision for <var>torelative</var> to be relative to.",
        "apierror-contentserializationexception": "Content serialization failed: $1",
        "apierror-contenttoobig": "The content you supplied exceeds the article size limit of $1 {{PLURAL:$1|kilobyte|kilobytes}}.",
index 1724fa9..d21f29c 100644 (file)
@@ -68,6 +68,7 @@
        "apihelp-compare-param-fromid": "{{doc-apihelp-param|compare|fromid}}",
        "apihelp-compare-param-fromrev": "{{doc-apihelp-param|compare|fromrev}}",
        "apihelp-compare-param-fromtext": "{{doc-apihelp-param|compare|fromtext}}",
+       "apihelp-compare-param-fromsection": "{{doc-apihelp-param|compare|fromsection}}",
        "apihelp-compare-param-frompst": "{{doc-apihelp-param|compare|frompst}}",
        "apihelp-compare-param-fromcontentmodel": "{{doc-apihelp-param|compare|fromcontentmodel}}",
        "apihelp-compare-param-fromcontentformat": "{{doc-apihelp-param|compare|fromcontentformat}}",
@@ -76,6 +77,7 @@
        "apihelp-compare-param-torev": "{{doc-apihelp-param|compare|torev}}",
        "apihelp-compare-param-torelative": "{{doc-apihelp-param|compare|torelative}}",
        "apihelp-compare-param-totext": "{{doc-apihelp-param|compare|totext}}",
+       "apihelp-compare-param-tosection": "{{doc-apihelp-param|compare|tosection}}",
        "apihelp-compare-param-topst": "{{doc-apihelp-param|compare|topst}}",
        "apihelp-compare-param-tocontentmodel": "{{doc-apihelp-param|compare|tocontentmodel}}",
        "apihelp-compare-param-tocontentformat": "{{doc-apihelp-param|compare|tocontentformat}}",
        "apierror-chunk-too-small": "{{doc-apierror}}\n\nParameters:\n* $1 - Minimum size in bytes.",
        "apierror-cidrtoobroad": "{{doc-apierror}}\n\nParameters:\n* $1 - \"IPv4\" or \"IPv6\"\n* $2 - Minimum CIDR mask length.",
        "apierror-compare-no-title": "{{doc-apierror}}",
+       "apierror-compare-nosuchfromsection": "{{doc-apierror}}\n\nParameters:\n* $1 - Section identifier. Probably a number or \"T-\" followed by a number.",
+       "apierror-compare-nosuchtosection": "{{doc-apierror}}\n\nParameters:\n* $1 - Section identifier. Probably a number or \"T-\" followed by a number.",
        "apierror-compare-relative-to-nothing": "{{doc-apierror}}",
        "apierror-contentserializationexception": "{{doc-apierror}}\n\nParameters:\n* $1 - Exception text, may end with punctuation. Currently this is probably English, hopefully we'll fix that in the future.",
        "apierror-contenttoobig": "{{doc-apierror}}\n\nParameters:\n* $1 - Maximum article size in kilobytes.",
index 155a08e..ea13a0d 100644 (file)
@@ -70,6 +70,9 @@ class ApiComparePagesTest extends ApiTestCase {
                        'page', [ 'page_latest' => 0 ], [ 'page_id' => self::$repl['pageE'] ]
                );
 
+               self::$repl['revF1'] = $this->addPage( 'F', "== Section 1 ==\nF 1.1\n\n== Section 2 ==\nF 1.2" );
+               self::$repl['pageF'] = Title::newFromText( 'ApiComparePagesTest F' )->getArticleId();
+
                WikiPage::factory( Title::newFromText( 'ApiComparePagesTest C' ) )
                        ->doDeleteArticleReal( 'Test for ApiComparePagesTest' );
 
@@ -372,6 +375,26 @@ class ApiComparePagesTest extends ApiTestCase {
                                ],
                                false, true
                        ],
+                       'Basic diff, test with sections' => [
+                               [
+                                       'fromtitle' => 'ApiComparePagesTest F',
+                                       'fromsection' => 1,
+                                       'totext' => "== Section 1 ==\nTo text\n\n== Section 2 ==\nTo text?",
+                                       'tosection' => 2,
+                               ],
+                               [
+                                       'compare' => [
+                                               'body' => '<tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l1" >Line 1:</td>' . "\n"
+                                                       . '<td colspan="2" class="diff-lineno">Line 1:</td></tr>' . "\n"
+                                                       . '<tr><td class=\'diff-marker\'>−</td><td class=\'diff-deletedline\'><div>== Section <del class="diffchange diffchange-inline">1 </del>==</div></td><td class=\'diff-marker\'>+</td><td class=\'diff-addedline\'><div>== Section <ins class="diffchange diffchange-inline">2 </ins>==</div></td></tr>' . "\n"
+                                                       . '<tr><td class=\'diff-marker\'>−</td><td class=\'diff-deletedline\'><div><del class="diffchange diffchange-inline">F 1.1</del></div></td><td class=\'diff-marker\'>+</td><td class=\'diff-addedline\'><div><ins class="diffchange diffchange-inline">To text?</ins></div></td></tr>' . "\n",
+                                               'fromid' => '{{REPL:pageF}}',
+                                               'fromrevid' => '{{REPL:revF1}}',
+                                               'fromns' => '0',
+                                               'fromtitle' => 'ApiComparePagesTest F',
+                                       ]
+                               ],
+                       ],
                        'Diff with all props' => [
                                [
                                        'fromrev' => '{{REPL:revB1}}',
@@ -568,6 +591,26 @@ class ApiComparePagesTest extends ApiTestCase {
                                [],
                                'compare-no-title',
                        ],
+                       'Error, test with invalid from section ID' => [
+                               [
+                                       'fromtitle' => 'ApiComparePagesTest F',
+                                       'fromsection' => 5,
+                                       'totext' => "== Section 1 ==\nTo text\n\n== Section 2 ==\nTo text?",
+                                       'tosection' => 2,
+                               ],
+                               [],
+                               'nosuchfromsection',
+                       ],
+                       'Error, test with invalid to section ID' => [
+                               [
+                                       'fromtitle' => 'ApiComparePagesTest F',
+                                       'fromsection' => 1,
+                                       'totext' => "== Section 1 ==\nTo text\n\n== Section 2 ==\nTo text?",
+                                       'tosection' => 5,
+                               ],
+                               [],
+                               'nosuchtosection',
+                       ],
                        'Error, Relative diff, no from revision' => [
                                [
                                        'fromtext' => 'Foo',