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,
"AutoloadClasses": {
"ApiCrossWikiBase": "includes/api/ApiCrossWikiBase.php",
"ApiCrossWiki": "includes/api/ApiCrossWiki.php",
"ApiEchoArticleReminder": "includes/api/ApiEchoArticleReminder.php",
"ApiEchoMarkRead": "includes/api/ApiEchoMarkRead.php",
"ApiEchoMarkReadTest": "tests/phpunit/api/ApiEchoMarkReadTest.php",

View file

@ -1,24 +1,21 @@
<?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
*/
protected $foreignNotifications;
/**
* @param ApiQuery $queryModule
* @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
* This will take the current API call (with all of its params) and execute
* 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().
@ -27,6 +24,10 @@ abstract class ApiCrossWikiBase extends ApiQueryBase {
* @throws Exception
*/
protected function getFromForeign( $wikis = null, array $paramOverrides = [] ) {
$wikis = $wikis ?? $this->getRequestedForeignWikis();
if ( $wikis === [] ) {
return [];
}
$foreignReq = new EchoForeignWikiRequest(
$this->getUser(),
$paramOverrides + $this->getForeignQueryParams(),
@ -38,8 +39,8 @@ abstract class ApiCrossWikiBase extends ApiQueryBase {
/**
* Get the query parameters to use for the foreign API requests.
* Subclasses should override this if they need to customize the
* parameters.
* Implementing classes should override this if they need to customize
* the parameters.
* @return array Query parameters
*/
protected function getForeignQueryParams() {
@ -91,17 +92,27 @@ abstract class ApiCrossWikiBase extends ApiQueryBase {
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
*/
protected function getForeignWikisWithUnreadNotifications() {
return $this->foreignNotifications->getWikis();
return $this->getForeignNotifications()->getWikis();
}
/**
* @return array[]
*/
public function getAllowedParams() {
public function getCrossWikiParams() {
global $wgConf;
$params = [];

View file

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

View file

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