2015-11-25 04:07:54 +00:00
|
|
|
<?php
|
|
|
|
|
2021-12-01 18:29:00 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
2016-04-27 05:40:07 +00:00
|
|
|
/**
|
|
|
|
* Caches the result of EchoUnreadWikis::getUnreadCounts() and interprets the results in various useful ways.
|
|
|
|
*
|
2020-06-27 10:05:03 +00:00
|
|
|
* If the user has disabled cross-wiki notifications in their preferences
|
|
|
|
* (see {@see EchoForeignNotifications::isEnabledByUser}), this class
|
2016-04-27 05:40:07 +00:00
|
|
|
* won't do anything and will behave as if the user has no foreign notifications. For example, getCount() will
|
|
|
|
* return 0. If you need to get foreign notification information for a user even though they may not have
|
|
|
|
* enabled the preference, set $forceEnable=true in the constructor.
|
|
|
|
*/
|
2015-11-25 04:07:54 +00:00
|
|
|
class EchoForeignNotifications {
|
|
|
|
/**
|
2016-04-27 05:40:07 +00:00
|
|
|
* @var User
|
2015-11-25 04:07:54 +00:00
|
|
|
*/
|
2016-04-27 05:40:07 +00:00
|
|
|
protected $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $enabled = false;
|
2015-11-25 04:07:54 +00:00
|
|
|
|
|
|
|
/**
|
2018-08-13 07:25:22 +00:00
|
|
|
* @var int[] [(str) section => (int) count, ...]
|
2015-11-25 04:07:54 +00:00
|
|
|
*/
|
2016-12-05 18:51:07 +00:00
|
|
|
protected $counts = [ EchoAttributeManager::ALERT => 0, EchoAttributeManager::MESSAGE => 0 ];
|
2015-11-25 04:07:54 +00:00
|
|
|
|
|
|
|
/**
|
2018-08-13 07:25:22 +00:00
|
|
|
* @var array[] [(str) section => (string[]) wikis, ...]
|
2015-11-25 04:07:54 +00:00
|
|
|
*/
|
2016-12-05 18:51:07 +00:00
|
|
|
protected $wikis = [ EchoAttributeManager::ALERT => [], EchoAttributeManager::MESSAGE => [] ];
|
2015-11-25 04:07:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array [(str) section => (MWTimestamp) timestamp, ...]
|
|
|
|
*/
|
2016-12-05 18:51:07 +00:00
|
|
|
protected $timestamps = [ EchoAttributeManager::ALERT => false, EchoAttributeManager::MESSAGE => false ];
|
2015-11-25 04:07:54 +00:00
|
|
|
|
2016-02-27 05:43:46 +00:00
|
|
|
/**
|
2018-08-13 07:25:22 +00:00
|
|
|
* @var array[] [(str) wiki => [ (str) section => (MWTimestamp) timestamp, ...], ...]
|
2016-02-27 05:43:46 +00:00
|
|
|
*/
|
2016-12-05 18:51:07 +00:00
|
|
|
protected $wikiTimestamps = [];
|
2016-02-27 05:43:46 +00:00
|
|
|
|
2015-11-25 04:07:54 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $populated = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user
|
2016-04-27 05:40:07 +00:00
|
|
|
* @param bool $forceEnable Ignore the user's preferences and act as if they've enabled cross-wiki notifications
|
2015-11-25 04:07:54 +00:00
|
|
|
*/
|
2016-04-27 05:40:07 +00:00
|
|
|
public function __construct( User $user, $forceEnable = false ) {
|
|
|
|
$this->user = $user;
|
|
|
|
$this->enabled = $forceEnable || $this->isEnabledByUser();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the user has enabled cross-wiki notifications.
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isEnabledByUser() {
|
2021-12-01 18:29:00 +00:00
|
|
|
$userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
|
|
|
|
return (bool)$userOptionsLookup->getOption( $this->user, 'echo-cross-wiki-notifications' );
|
2015-11-25 04:07:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-27 04:18:28 +00:00
|
|
|
* @param string $section Name of section
|
2015-11-25 04:07:54 +00:00
|
|
|
* @return int
|
|
|
|
*/
|
2016-04-27 04:18:28 +00:00
|
|
|
public function getCount( $section = EchoAttributeManager::ALL ) {
|
2015-11-25 04:07:54 +00:00
|
|
|
$this->populate();
|
|
|
|
|
2016-04-27 04:18:28 +00:00
|
|
|
if ( $section === EchoAttributeManager::ALL ) {
|
2016-09-09 22:44:40 +00:00
|
|
|
$count = array_sum( $this->counts );
|
|
|
|
} else {
|
2019-03-12 20:04:00 +00:00
|
|
|
$count = $this->counts[$section] ?? 0;
|
2015-11-25 04:07:54 +00:00
|
|
|
}
|
|
|
|
|
2016-09-09 22:44:40 +00:00
|
|
|
return MWEchoNotifUser::capNotificationCount( $count );
|
2015-11-25 04:07:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-27 04:18:28 +00:00
|
|
|
* @param string $section Name of section
|
2015-11-25 04:07:54 +00:00
|
|
|
* @return MWTimestamp|false
|
|
|
|
*/
|
2016-04-27 04:18:28 +00:00
|
|
|
public function getTimestamp( $section = EchoAttributeManager::ALL ) {
|
2015-11-25 04:07:54 +00:00
|
|
|
$this->populate();
|
|
|
|
|
2016-04-27 04:18:28 +00:00
|
|
|
if ( $section === EchoAttributeManager::ALL ) {
|
2015-11-25 04:07:54 +00:00
|
|
|
$max = false;
|
|
|
|
/** @var MWTimestamp $timestamp */
|
|
|
|
foreach ( $this->timestamps as $timestamp ) {
|
|
|
|
// $timestamp < $max = invert 0
|
|
|
|
// $timestamp > $max = invert 1
|
2016-04-28 17:16:38 +00:00
|
|
|
if ( $timestamp !== false && ( $max === false || $timestamp->diff( $max )->invert === 1 ) ) {
|
2015-11-25 04:07:54 +00:00
|
|
|
$max = $timestamp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 22:18:49 +00:00
|
|
|
return $max;
|
2015-11-25 04:07:54 +00:00
|
|
|
}
|
|
|
|
|
2019-03-12 20:04:00 +00:00
|
|
|
return $this->timestamps[$section] ?? false;
|
2015-11-25 04:07:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-27 04:18:28 +00:00
|
|
|
* @param string $section Name of section
|
2015-11-25 04:07:54 +00:00
|
|
|
* @return string[]
|
|
|
|
*/
|
2016-04-27 04:18:28 +00:00
|
|
|
public function getWikis( $section = EchoAttributeManager::ALL ) {
|
2015-11-25 04:07:54 +00:00
|
|
|
$this->populate();
|
|
|
|
|
2016-04-27 04:18:28 +00:00
|
|
|
if ( $section === EchoAttributeManager::ALL ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$all = [];
|
2015-11-25 04:07:54 +00:00
|
|
|
foreach ( $this->wikis as $wikis ) {
|
|
|
|
$all = array_merge( $all, $wikis );
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_unique( $all );
|
|
|
|
}
|
|
|
|
|
2019-03-12 20:04:00 +00:00
|
|
|
return $this->wikis[$section] ?? [];
|
2015-11-25 04:07:54 +00:00
|
|
|
}
|
|
|
|
|
2016-04-27 04:18:28 +00:00
|
|
|
public function getWikiTimestamp( $wiki, $section = EchoAttributeManager::ALL ) {
|
2016-02-27 05:43:46 +00:00
|
|
|
$this->populate();
|
|
|
|
if ( !isset( $this->wikiTimestamps[$wiki] ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-27 04:18:28 +00:00
|
|
|
if ( $section === EchoAttributeManager::ALL ) {
|
2016-02-27 05:43:46 +00:00
|
|
|
$max = false;
|
|
|
|
foreach ( $this->wikiTimestamps[$wiki] as $section => $ts ) {
|
|
|
|
// $ts < $max = invert 0
|
|
|
|
// $ts > $max = invert 1
|
|
|
|
if ( $max === false || $ts->diff( $max )->invert === 1 ) {
|
|
|
|
$max = $ts;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $max;
|
|
|
|
}
|
2019-03-12 20:04:00 +00:00
|
|
|
return $this->wikiTimestamps[$wiki][$section] ?? false;
|
2016-02-27 05:43:46 +00:00
|
|
|
}
|
|
|
|
|
2015-11-25 04:07:54 +00:00
|
|
|
protected function populate() {
|
|
|
|
if ( $this->populated ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-27 05:40:07 +00:00
|
|
|
if ( !$this->enabled ) {
|
2015-11-25 04:07:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-04-27 05:40:07 +00:00
|
|
|
$unreadWikis = EchoUnreadWikis::newFromUser( $this->user );
|
|
|
|
if ( !$unreadWikis ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$unreadCounts = $unreadWikis->getUnreadCounts();
|
2015-11-25 04:07:54 +00:00
|
|
|
if ( !$unreadCounts ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $unreadCounts as $wiki => $sections ) {
|
|
|
|
// exclude current wiki
|
2021-12-21 00:47:31 +00:00
|
|
|
if ( $wiki === WikiMap::getCurrentWikiId() ) {
|
2015-11-25 04:07:54 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $sections as $section => $data ) {
|
|
|
|
if ( $data['count'] > 0 ) {
|
|
|
|
$this->counts[$section] += $data['count'];
|
|
|
|
$this->wikis[$section][] = $wiki;
|
2016-02-26 21:30:34 +00:00
|
|
|
|
|
|
|
$timestamp = new MWTimestamp( $data['ts'] );
|
2016-02-27 05:43:46 +00:00
|
|
|
$this->wikiTimestamps[$wiki][$section] = $timestamp;
|
|
|
|
|
2016-02-26 21:30:34 +00:00
|
|
|
// We need $this->timestamp[$section] to be the max timestamp
|
|
|
|
// across all wikis.
|
|
|
|
// $timestamp < $this->timestamps[$section] = invert 0
|
|
|
|
// $timestamp > $this->timestamps[$section] = invert 1
|
|
|
|
if (
|
|
|
|
$this->timestamps[$section] === false ||
|
|
|
|
$timestamp->diff( $this->timestamps[$section] )->invert === 1
|
|
|
|
) {
|
|
|
|
$this->timestamps[$section] = new MWTimestamp( $data['ts'] );
|
|
|
|
}
|
|
|
|
|
2015-11-25 04:07:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->populated = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string[] $wikis
|
|
|
|
* @return array[] [(string) wiki => (array) data]
|
|
|
|
*/
|
2016-04-27 00:25:14 +00:00
|
|
|
public static function getApiEndpoints( array $wikis ) {
|
2015-11-25 04:07:54 +00:00
|
|
|
global $wgConf;
|
2016-01-15 05:28:02 +00:00
|
|
|
$wgConf->loadFullData();
|
2015-11-25 04:07:54 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$data = [];
|
2015-11-25 04:07:54 +00:00
|
|
|
foreach ( $wikis as $wiki ) {
|
2016-01-26 08:23:32 +00:00
|
|
|
$siteFromDB = $wgConf->siteFromDB( $wiki );
|
|
|
|
list( $major, $minor ) = $siteFromDB;
|
2016-12-05 18:51:07 +00:00
|
|
|
$server = $wgConf->get( 'wgServer', $wiki, $major, [ 'lang' => $minor, 'site' => $major ] );
|
|
|
|
$scriptPath = $wgConf->get( 'wgScriptPath', $wiki, $major, [ 'lang' => $minor, 'site' => $major ] );
|
|
|
|
$articlePath = $wgConf->get( 'wgArticlePath', $wiki, $major, [ 'lang' => $minor, 'site' => $major ] );
|
2016-01-26 08:23:32 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$data[$wiki] = [
|
2016-04-27 00:25:14 +00:00
|
|
|
'title' => static::getWikiTitle( $wiki, $siteFromDB ),
|
2016-04-21 11:16:21 +00:00
|
|
|
'url' => wfExpandUrl( $server . $scriptPath . '/api.php', PROTO_INTERNAL ),
|
2016-05-14 00:35:47 +00:00
|
|
|
// We need this to link to Special:Notifications page
|
|
|
|
'base' => wfExpandUrl( $server . $articlePath, PROTO_INTERNAL ),
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2015-11-25 04:07:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2016-01-26 08:23:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $wikiId
|
2018-04-04 15:11:39 +00:00
|
|
|
* @param array|null $siteFromDB $wgConf->siteFromDB( $wikiId ) result
|
2016-01-26 08:23:32 +00:00
|
|
|
* @return mixed|string
|
|
|
|
*/
|
2016-04-27 00:25:14 +00:00
|
|
|
protected static function getWikiTitle( $wikiId, array $siteFromDB = null ) {
|
2016-01-26 08:23:32 +00:00
|
|
|
global $wgConf, $wgLang;
|
|
|
|
|
2018-09-03 01:46:10 +00:00
|
|
|
$msg = wfMessage( 'project-localized-name-' . $wikiId );
|
2016-01-26 08:23:32 +00:00
|
|
|
// check if WikimediaMessages localized project names are available
|
|
|
|
if ( $msg->exists() ) {
|
|
|
|
return $msg->text();
|
|
|
|
} else {
|
|
|
|
// don't fetch $site, $langCode if known already
|
|
|
|
if ( $siteFromDB === null ) {
|
|
|
|
$siteFromDB = $wgConf->siteFromDB( $wikiId );
|
|
|
|
}
|
|
|
|
list( $site, $langCode ) = $siteFromDB;
|
|
|
|
|
|
|
|
// try to fetch site name for this specific wiki, or fallback to the
|
|
|
|
// general project's sitename if there is no override
|
|
|
|
$wikiName = $wgConf->get( 'wgSitename', $wikiId ) ?: $wgConf->get( 'wgSitename', $site );
|
2016-03-10 23:12:14 +00:00
|
|
|
$langName = Language::fetchLanguageName( $langCode, $wgLang->getCode() );
|
2016-01-26 08:23:32 +00:00
|
|
|
|
|
|
|
if ( !$langName ) {
|
|
|
|
// if we can't find a language name (in language-agnostic
|
|
|
|
// project like mediawikiwiki), including the language name
|
|
|
|
// doesn't make much sense
|
|
|
|
return $wikiName;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ... or use generic fallback
|
|
|
|
return wfMessage( 'echo-foreign-wiki-lang', $wikiName, $langName )->text();
|
|
|
|
}
|
|
|
|
}
|
2015-11-25 04:07:54 +00:00
|
|
|
}
|