Merge "Rename ApiCrossWikiBase -> ApiCrossWiki and make it a trait"

This commit is contained in:
jenkins-bot 2018-09-05 12:35:29 +00:00 committed by Gerrit Code Review
commit 9cc47ceac8
4 changed files with 44 additions and 30 deletions

View file

@ -903,7 +903,7 @@
}, },
"manifest_version": 2, "manifest_version": 2,
"AutoloadClasses": { "AutoloadClasses": {
"ApiCrossWikiBase": "includes/api/ApiCrossWikiBase.php", "ApiCrossWiki": "includes/api/ApiCrossWiki.php",
"ApiEchoArticleReminder": "includes/api/ApiEchoArticleReminder.php", "ApiEchoArticleReminder": "includes/api/ApiEchoArticleReminder.php",
"ApiEchoMarkRead": "includes/api/ApiEchoMarkRead.php", "ApiEchoMarkRead": "includes/api/ApiEchoMarkRead.php",
"ApiEchoMarkReadTest": "tests/phpunit/api/ApiEchoMarkReadTest.php", "ApiEchoMarkReadTest": "tests/phpunit/api/ApiEchoMarkReadTest.php",

View file

@ -1,24 +1,21 @@
<?php <?php
abstract class ApiCrossWikiBase extends ApiQueryBase { /**
* Trait that adds cross-wiki functionality to an API module. For mixing into ApiBase subclasses.
*
* In addition to mixing in this trait, you have to do the following in your API module:
* - In your getAllowedParams() method, merge in the return value of getCrossWikiParams()
* - In your execute() method, call getFromForeign() somewhere and do something with the result
* - Optionally, override getForeignQueryParams() to customize what is sent to the foreign wikis
*/
trait ApiCrossWiki {
/** /**
* @var EchoForeignNotifications * @var EchoForeignNotifications
*/ */
protected $foreignNotifications; protected $foreignNotifications;
/** /**
* @param ApiQuery $queryModule * This will take the current API call (with all of its params) and execute
* @param string $moduleName
* @param string $paramPrefix
*/
public function __construct( ApiQuery $queryModule, $moduleName, $paramPrefix = '' ) {
parent::__construct( $queryModule, $moduleName, $paramPrefix );
$this->foreignNotifications = new EchoForeignNotifications( $this->getUser() );
}
/**
* This will turn the current API call (with all of it's params) and execute
* it on all foreign wikis, returning an array of results per wiki. * it on all foreign wikis, returning an array of results per wiki.
* *
* @param array|null $wikis List of wikis to query. Defaults to the result of getRequestedForeignWikis(). * @param array|null $wikis List of wikis to query. Defaults to the result of getRequestedForeignWikis().
@ -27,6 +24,10 @@ abstract class ApiCrossWikiBase extends ApiQueryBase {
* @throws Exception * @throws Exception
*/ */
protected function getFromForeign( $wikis = null, array $paramOverrides = [] ) { protected function getFromForeign( $wikis = null, array $paramOverrides = [] ) {
$wikis = $wikis ?? $this->getRequestedForeignWikis();
if ( $wikis === [] ) {
return [];
}
$foreignReq = new EchoForeignWikiRequest( $foreignReq = new EchoForeignWikiRequest(
$this->getUser(), $this->getUser(),
$paramOverrides + $this->getForeignQueryParams(), $paramOverrides + $this->getForeignQueryParams(),
@ -38,8 +39,8 @@ abstract class ApiCrossWikiBase extends ApiQueryBase {
/** /**
* Get the query parameters to use for the foreign API requests. * Get the query parameters to use for the foreign API requests.
* Subclasses should override this if they need to customize the * Implementing classes should override this if they need to customize
* parameters. * the parameters.
* @return array Query parameters * @return array Query parameters
*/ */
protected function getForeignQueryParams() { protected function getForeignQueryParams() {
@ -91,17 +92,27 @@ abstract class ApiCrossWikiBase extends ApiQueryBase {
return array_diff( $this->getRequestedWikis(), [ wfWikiID() ] ); return array_diff( $this->getRequestedWikis(), [ wfWikiID() ] );
} }
/**
* @return EchoForeignNotifications
*/
protected function getForeignNotifications() {
if ( $this->foreignNotifications === null ) {
$this->foreignNotifications = new EchoForeignNotifications( $this->getUser() );
}
return $this->foreignNotifications;
}
/** /**
* @return string[] Wiki names * @return string[] Wiki names
*/ */
protected function getForeignWikisWithUnreadNotifications() { protected function getForeignWikisWithUnreadNotifications() {
return $this->foreignNotifications->getWikis(); return $this->getForeignNotifications()->getWikis();
} }
/** /**
* @return array[] * @return array[]
*/ */
public function getAllowedParams() { public function getCrossWikiParams() {
global $wgConf; global $wgConf;
$params = []; $params = [];

View file

@ -1,6 +1,8 @@
<?php <?php
class ApiEchoNotifications extends ApiCrossWikiBase { class ApiEchoNotifications extends ApiQueryBase {
use ApiCrossWiki;
/** /**
* @var bool * @var bool
*/ */
@ -380,7 +382,7 @@ class ApiEchoNotifications extends ApiCrossWikiBase {
// and query them to find out what's really there. // and query them to find out what's really there.
// In bundle transition mode, we trust that notifications are classified correctly, but we don't // In bundle transition mode, we trust that notifications are classified correctly, but we don't
// trust the counts in the table. // trust the counts in the table.
$potentialWikis = $this->foreignNotifications->getWikis( $potentialWikis = $this->getForeignNotifications()->getWikis(
$wgEchoSectionTransition ? EchoAttributeManager::ALL : $section ); $wgEchoSectionTransition ? EchoAttributeManager::ALL : $section );
if ( !$potentialWikis ) { if ( !$potentialWikis ) {
return false; return false;
@ -417,12 +419,12 @@ class ApiEchoNotifications extends ApiCrossWikiBase {
}, $timestampsByWiki ); }, $timestampsByWiki );
} else { } else {
// In non-transition mode, or when querying all sections, we can trust the euw table // In non-transition mode, or when querying all sections, we can trust the euw table
$wikis = $this->foreignNotifications->getWikis( $section ); $wikis = $this->getForeignNotifications()->getWikis( $section );
$count = $this->foreignNotifications->getCount( $section ); $count = $this->getForeignNotifications()->getCount( $section );
$maxTimestamp = $this->foreignNotifications->getTimestamp( $section ); $maxTimestamp = $this->getForeignNotifications()->getTimestamp( $section );
$timestampsByWiki = []; $timestampsByWiki = [];
foreach ( $wikis as $wiki ) { foreach ( $wikis as $wiki ) {
$timestampsByWiki[$wiki] = $this->foreignNotifications->getWikiTimestamp( $wiki, $section ); $timestampsByWiki[$wiki] = $this->getForeignNotifications()->getWikiTimestamp( $wiki, $section );
} }
} }
@ -474,7 +476,7 @@ class ApiEchoNotifications extends ApiCrossWikiBase {
} }
protected function getForeignQueryParams() { protected function getForeignQueryParams() {
$params = parent::getForeignQueryParams(); $params = $this->getRequest()->getValues();
// don't request cross-wiki notification summaries // don't request cross-wiki notification summaries
unset( $params['notcrosswikisummary'] ); unset( $params['notcrosswikisummary'] );
@ -577,8 +579,7 @@ class ApiEchoNotifications extends ApiCrossWikiBase {
public function getAllowedParams() { public function getAllowedParams() {
$sections = EchoAttributeManager::$sections; $sections = EchoAttributeManager::$sections;
$params = parent::getAllowedParams(); $params = $this->getCrossWikiParams() + [
$params += [
'filter' => [ 'filter' => [
ApiBase::PARAM_ISMULTI => true, ApiBase::PARAM_ISMULTI => true,
ApiBase::PARAM_DFLT => 'read|!read', ApiBase::PARAM_DFLT => 'read|!read',

View file

@ -1,6 +1,8 @@
<?php <?php
class ApiEchoUnreadNotificationPages extends ApiCrossWikiBase { class ApiEchoUnreadNotificationPages extends ApiQueryBase {
use ApiCrossWiki;
/** /**
* @var bool * @var bool
*/ */
@ -36,7 +38,7 @@ class ApiEchoUnreadNotificationPages extends ApiCrossWikiBase {
$result += $this->getUnreadNotificationPagesFromForeign(); $result += $this->getUnreadNotificationPagesFromForeign();
} }
$apis = $this->foreignNotifications->getApiEndpoints( $this->getRequestedWikis() ); $apis = $this->getForeignNotifications()->getApiEndpoints( $this->getRequestedWikis() );
foreach ( $result as $wiki => $data ) { foreach ( $result as $wiki => $data ) {
$result[$wiki]['source'] = $apis[$wiki]; $result[$wiki]['source'] = $apis[$wiki];
$result[$wiki]['pages'] = $data['pages'] ?: []; $result[$wiki]['pages'] = $data['pages'] ?: [];
@ -177,7 +179,7 @@ class ApiEchoUnreadNotificationPages extends ApiCrossWikiBase {
public function getAllowedParams() { public function getAllowedParams() {
global $wgEchoMaxUpdateCount; global $wgEchoMaxUpdateCount;
return parent::getAllowedParams() + [ return $this->getCrossWikiParams() + [
'grouppages' => [ 'grouppages' => [
ApiBase::PARAM_TYPE => 'boolean', ApiBase::PARAM_TYPE => 'boolean',
ApiBase::PARAM_DFLT => false, ApiBase::PARAM_DFLT => false,