2016-05-26 20:41:25 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use MediaWiki\Logger\LoggerFactory;
|
|
|
|
|
|
|
|
abstract class ApiCrossWikiBase extends ApiQueryBase {
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
* it on all foreign wikis, returning an array of results per wiki.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
protected function getFromForeign() {
|
2016-06-08 20:30:51 +00:00
|
|
|
$foreignReq = new EchoForeignWikiRequest(
|
|
|
|
$this->getUser(),
|
|
|
|
$this->getRequest()->getValues(),
|
|
|
|
$this->getRequestedForeignWikis(),
|
|
|
|
$this->getModulePrefix() . 'wikis'
|
|
|
|
);
|
|
|
|
return $foreignReq->execute();
|
2016-05-26 20:41:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function allowCrossWikiNotifications() {
|
|
|
|
global $wgEchoCrossWikiNotifications;
|
|
|
|
return $wgEchoCrossWikiNotifications;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-27 21:10:04 +00:00
|
|
|
* This is basically equivalent to $params['wikis'], but some added checks:
|
|
|
|
* - `*` will expand to "all wikis with unread notifications"
|
|
|
|
* - if `$wgEchoCrossWikiNotifications` is off, foreign wikis will be excluded
|
|
|
|
*
|
|
|
|
* @return array
|
2016-05-26 20:41:25 +00:00
|
|
|
*/
|
2016-05-27 21:10:04 +00:00
|
|
|
protected function getRequestedWikis() {
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
|
|
|
|
// if wiki is omitted from params, that's because crosswiki is/was not
|
|
|
|
// available, and it'll default to current wiki
|
|
|
|
$wikis = isset( $params['wikis'] ) ? $params['wikis'] : array( wfWikiID() );
|
|
|
|
|
|
|
|
if ( array_search( '*', $wikis ) !== false ) {
|
|
|
|
// expand `*` to all foreign wikis with unread notifications + local
|
|
|
|
$wikis = array_merge(
|
|
|
|
array( wfWikiID() ),
|
|
|
|
$this->getForeignWikisWithUnreadNotifications()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-05-26 20:41:25 +00:00
|
|
|
if ( !$this->allowCrossWikiNotifications() ) {
|
2016-05-27 21:10:04 +00:00
|
|
|
// exclude foreign wikis if x-wiki is not enabled
|
|
|
|
$wikis = array_intersect_key( array( wfWikiID() ), $wikis );
|
2016-05-26 20:41:25 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 21:10:04 +00:00
|
|
|
return $wikis;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array Wiki names
|
|
|
|
*/
|
|
|
|
protected function getRequestedForeignWikis() {
|
|
|
|
return array_diff( $this->getRequestedWikis(), array( wfWikiId() ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array Wiki names
|
|
|
|
*/
|
|
|
|
protected function getForeignWikisWithUnreadNotifications() {
|
|
|
|
return $this->foreignNotifications->getWikis();
|
2016-05-26 20:41:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getAllowedParams() {
|
|
|
|
global $wgConf;
|
|
|
|
|
|
|
|
$params = array();
|
|
|
|
|
|
|
|
if ( $this->allowCrossWikiNotifications() ) {
|
|
|
|
$params += array(
|
|
|
|
// fetch notifications from multiple wikis
|
|
|
|
'wikis' => array(
|
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
|
|
|
ApiBase::PARAM_DFLT => wfWikiId(),
|
2016-05-27 21:10:04 +00:00
|
|
|
// `*` will let you immediately fetch from all wikis that have
|
|
|
|
// unread notifications, without having to look them up first
|
|
|
|
ApiBase::PARAM_TYPE => array_unique( array_merge( $wgConf->wikis, array( wfWikiId(), '*' ) ) ),
|
2016-05-26 20:41:25 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
}
|