From: jenkins-bot Date: Thu, 4 Jan 2018 07:01:31 +0000 (+0000) Subject: Merge "ApiComparePages: Add 'fromsection' and 'tosection' parameters" X-Git-Tag: 1.31.0-rc.0~1004 X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=commitdiff_plain;h=ba00b23a1d6fea3f05c617d3df73d60ab28dfdf4;hp=689acef53fb969b9985743c821320724e13e7704 Merge "ApiComparePages: Add 'fromsection' and 'tosection' parameters" --- diff --git a/includes/api/ApiComparePages.php b/includes/api/ApiComparePages.php index eb67babf2a..54865948a8 100644 --- a/includes/api/ApiComparePages.php +++ b/includes/api/ApiComparePages.php @@ -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(), diff --git a/includes/api/i18n/en.json b/includes/api/i18n/en.json index e1360c8ad8..cceed01dcf 100644 --- a/includes/api/i18n/en.json +++ b/includes/api/i18n/en.json @@ -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 fromtitle, fromid or fromrev.", + "apihelp-compare-param-fromsection": "Only use the specified section of the specified 'from' content.", "apihelp-compare-param-frompst": "Do a pre-save transform on fromtext.", "apihelp-compare-param-fromcontentmodel": "Content model of fromtext. If not supplied, it will be guessed based on the other parameters.", "apihelp-compare-param-fromcontentformat": "Content serialization format of fromtext.", @@ -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 fromtitle, fromid or fromrev. 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 totitle, toid or torev.", + "apihelp-compare-param-tosection": "Only use the specified section of the specified 'to' content.", "apihelp-compare-param-topst": "Do a pre-save transform on totext.", "apihelp-compare-param-tocontentmodel": "Content model of totext. If not supplied, it will be guessed based on the other parameters.", "apihelp-compare-param-tocontentformat": "Content serialization format of totext.", @@ -1675,6 +1677,8 @@ "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 fromtitle or totitle.", + "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 torelative 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}}.", diff --git a/includes/api/i18n/qqq.json b/includes/api/i18n/qqq.json index 1724fa905b..d21f29c7a9 100644 --- a/includes/api/i18n/qqq.json +++ b/includes/api/i18n/qqq.json @@ -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}}", @@ -1564,6 +1566,8 @@ "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.", diff --git a/tests/phpunit/includes/api/ApiComparePagesTest.php b/tests/phpunit/includes/api/ApiComparePagesTest.php index 155a08ed06..ea13a0d38e 100644 --- a/tests/phpunit/includes/api/ApiComparePagesTest.php +++ b/tests/phpunit/includes/api/ApiComparePagesTest.php @@ -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' => 'Line 1:' . "\n" + . 'Line 1:' . "\n" + . '−
== Section 1 ==
+
== Section 2 ==
' . "\n" + . '−
F 1.1
+
To text?
' . "\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',