From 4158cb5b9b6a5d8c76d446878149050a97c3074e Mon Sep 17 00:00:00 2001 From: Amir Sarabadani Date: Thu, 15 Mar 2018 18:03:24 +0100 Subject: [PATCH] Add ability to filter based on rc_title in API Bug: T57377 Change-Id: I0b14bde58a4012816f2998d663d88ab035c927b7 --- includes/api/ApiQueryRecentChanges.php | 11 ++++ includes/api/i18n/en.json | 1 + includes/api/i18n/qqq.json | 1 + .../ApiQueryRecentChangesIntegrationTest.php | 59 +++++++++++++++++++ 4 files changed, 72 insertions(+) diff --git a/includes/api/ApiQueryRecentChanges.php b/includes/api/ApiQueryRecentChanges.php index e431202bc8..65541db549 100644 --- a/includes/api/ApiQueryRecentChanges.php +++ b/includes/api/ApiQueryRecentChanges.php @@ -181,6 +181,16 @@ class ApiQueryRecentChanges extends ApiQueryGeneratorBase { } } + $title = $params['title']; + if ( !is_null( $title ) ) { + $titleObj = Title::newFromText( $title ); + if ( is_null( $titleObj ) ) { + $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $title ) ] ); + } + $this->addWhereFld( 'rc_namespace', $titleObj->getNamespace() ); + $this->addWhereFld( 'rc_title', $titleObj->getDBkey() ); + } + if ( !is_null( $params['show'] ) ) { $show = array_flip( $params['show'] ); @@ -710,6 +720,7 @@ class ApiQueryRecentChanges extends ApiQueryGeneratorBase { ApiBase::PARAM_TYPE => RecentChange::getChangeTypes() ], 'toponly' => false, + 'title' => null, 'continue' => [ ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', ], diff --git a/includes/api/i18n/en.json b/includes/api/i18n/en.json index 756852650d..0764d8afe2 100644 --- a/includes/api/i18n/en.json +++ b/includes/api/i18n/en.json @@ -1079,6 +1079,7 @@ "apihelp-query+recentchanges-param-limit": "How many total changes to return.", "apihelp-query+recentchanges-param-type": "Which types of changes to show.", "apihelp-query+recentchanges-param-toponly": "Only list changes which are the latest revision.", + "apihelp-query+recentchanges-param-title": "Filter entries to those related to a page.", "apihelp-query+recentchanges-param-generaterevisions": "When being used as a generator, generate revision IDs rather than titles. Recent change entries without associated revision IDs (e.g. most log entries) will generate nothing.", "apihelp-query+recentchanges-example-simple": "List recent changes.", "apihelp-query+recentchanges-example-generator": "Get page info about recent unpatrolled changes.", diff --git a/includes/api/i18n/qqq.json b/includes/api/i18n/qqq.json index fc0de4e333..8b49fdc19e 100644 --- a/includes/api/i18n/qqq.json +++ b/includes/api/i18n/qqq.json @@ -1007,6 +1007,7 @@ "apihelp-query+recentchanges-param-limit": "{{doc-apihelp-param|query+recentchanges|limit}}", "apihelp-query+recentchanges-param-type": "{{doc-apihelp-param|query+recentchanges|type}}", "apihelp-query+recentchanges-param-toponly": "{{doc-apihelp-param|query+recentchanges|toponly}}", + "apihelp-query+recentchanges-param-title": "{{doc-apihelp-param|query+recentchanges|title}}", "apihelp-query+recentchanges-param-generaterevisions": "{{doc-apihelp-param|query+recentchanges|generaterevisions}}", "apihelp-query+recentchanges-example-simple": "{{doc-apihelp-example|query+recentchanges}}", "apihelp-query+recentchanges-example-generator": "{{doc-apihelp-example|query+recentchanges}}", diff --git a/tests/phpunit/includes/api/ApiQueryRecentChangesIntegrationTest.php b/tests/phpunit/includes/api/ApiQueryRecentChangesIntegrationTest.php index 24b7500983..a89c4ad40a 100644 --- a/tests/phpunit/includes/api/ApiQueryRecentChangesIntegrationTest.php +++ b/tests/phpunit/includes/api/ApiQueryRecentChangesIntegrationTest.php @@ -860,6 +860,65 @@ class ApiQueryRecentChangesIntegrationTest extends ApiTestCase { ); } + public function testTitleParams() { + $page1 = new TitleValue( 0, 'ApiQueryRecentChangesIntegrationTestPage' ); + $page2 = new TitleValue( 1, 'ApiQueryRecentChangesIntegrationTestPage2' ); + $page3 = new TitleValue( 0, 'ApiQueryRecentChangesIntegrationTestPage3' ); + $this->doPageEdits( + $this->getLoggedInTestUser(), + [ + [ + 'target' => $page1, + 'summary' => 'Create the page', + ], + [ + 'target' => $page2, + 'summary' => 'Create the page', + ], + [ + 'target' => $page3, + 'summary' => 'Create the page', + ], + ] + ); + + $result = $this->doListRecentChangesRequest( + [ + 'rctitle' => 'ApiQueryRecentChangesIntegrationTestPage', + 'rcprop' => 'title' + ] + ); + + $result2 = $this->doListRecentChangesRequest( + [ + 'rctitle' => 'Talk:ApiQueryRecentChangesIntegrationTestPage2', + 'rcprop' => 'title' + ] + ); + + $this->assertEquals( + [ + [ + 'type' => 'new', + 'ns' => $page1->getNamespace(), + 'title' => $this->getPrefixedText( $page1 ) + ], + ], + $this->getItemsFromApiResponse( $result ) + ); + + $this->assertEquals( + [ + [ + 'type' => 'new', + 'ns' => $page2->getNamespace(), + 'title' => $this->getPrefixedText( $page2 ) + ], + ], + $this->getItemsFromApiResponse( $result2 ) + ); + } + public function testStartEndParams() { $target = new TitleValue( 0, 'ApiQueryRecentChangesIntegrationTestPage' ); $this->doPageEdit( $this->getLoggedInTestUser(), $target, 'Create the page' ); -- 2.20.1