2015-11-25 04:07:54 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class EchoForeignNotifications {
|
|
|
|
/**
|
|
|
|
* @var bool|EchoUnreadWikis
|
|
|
|
*/
|
|
|
|
protected $unreadWikis = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array [(str) section => (int) count, ...]
|
|
|
|
*/
|
|
|
|
protected $counts = array( EchoAttributeManager::ALERT => 0, EchoAttributeManager::MESSAGE => 0 );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array [(str) section => (string[]) wikis, ...]
|
|
|
|
*/
|
|
|
|
protected $wikis = array( EchoAttributeManager::ALERT => array(), EchoAttributeManager::MESSAGE => array() );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array [(str) section => (MWTimestamp) timestamp, ...]
|
|
|
|
*/
|
|
|
|
protected $timestamps = array( EchoAttributeManager::ALERT => false, EchoAttributeManager::MESSAGE => false );
|
|
|
|
|
2016-02-27 05:43:46 +00:00
|
|
|
/**
|
|
|
|
* @var array [(str) wiki => [ (str) section => (MWTimestamp) timestamp, ...], ...]
|
|
|
|
*/
|
|
|
|
protected $wikiTimestamps = array();
|
|
|
|
|
2015-11-25 04:07:54 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $populated = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param User $user
|
|
|
|
*/
|
|
|
|
public function __construct( User $user ) {
|
|
|
|
if ( $user->getOption( 'echo-cross-wiki-notifications' ) ) {
|
|
|
|
$this->unreadWikis = EchoUnreadWikis::newFromUser( $user );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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 ) {
|
2015-11-25 04:07:54 +00:00
|
|
|
return array_sum( $this->counts );
|
|
|
|
}
|
|
|
|
|
|
|
|
return isset( $this->counts[$section] ) ? $this->counts[$section] : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
if ( $max === false || $timestamp->diff( $max )->invert === 1 ) {
|
|
|
|
$max = $timestamp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $timestamp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return isset( $this->timestamps[$section] ) ? $this->timestamps[$section] : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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 ) {
|
2015-11-25 04:07:54 +00:00
|
|
|
$all = array();
|
|
|
|
foreach ( $this->wikis as $wikis ) {
|
|
|
|
$all = array_merge( $all, $wikis );
|
|
|
|
}
|
|
|
|
|
|
|
|
return array_unique( $all );
|
|
|
|
}
|
|
|
|
|
|
|
|
return isset( $this->wikis[$section] ) ? $this->wikis[$section] : array();
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
return isset( $this->wikiTimestamps[$wiki][$section] ) ? $this->wikiTimestamps[$wiki][$section] : false;
|
|
|
|
}
|
|
|
|
|
2015-11-25 04:07:54 +00:00
|
|
|
protected function populate() {
|
|
|
|
if ( $this->populated ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $this->unreadWikis === false ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$unreadCounts = $this->unreadWikis->getUnreadCounts();
|
|
|
|
if ( !$unreadCounts ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $unreadCounts as $wiki => $sections ) {
|
|
|
|
// exclude current wiki
|
|
|
|
if ( $wiki === wfWikiID() ) {
|
|
|
|
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
|
|
|
|
|
|
|
$data = array();
|
|
|
|
foreach ( $wikis as $wiki ) {
|
2016-01-26 08:23:32 +00:00
|
|
|
$siteFromDB = $wgConf->siteFromDB( $wiki );
|
|
|
|
list( $major, $minor ) = $siteFromDB;
|
2016-01-15 05:28:02 +00:00
|
|
|
$server = $wgConf->get( 'wgServer', $wiki, $major, array( 'lang' => $minor, 'site' => $major ) );
|
|
|
|
$scriptPath = $wgConf->get( 'wgScriptPath', $wiki, $major, array( 'lang' => $minor, 'site' => $major ) );
|
2016-01-26 08:23:32 +00:00
|
|
|
|
2015-11-25 04:07:54 +00:00
|
|
|
$data[$wiki] = array(
|
2016-04-27 00:25:14 +00:00
|
|
|
'title' => static::getWikiTitle( $wiki, $siteFromDB ),
|
2016-01-15 05:28:02 +00:00
|
|
|
'url' => $server . $scriptPath . '/api.php',
|
2015-11-25 04:07:54 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2016-01-26 08:23:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $wikiId
|
|
|
|
* @param array $siteFromDB $wgConf->siteFromDB( $wikiId ) result
|
|
|
|
* @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;
|
|
|
|
|
|
|
|
$msg = wfMessage( 'project-localized-name-'.$wikiId );
|
|
|
|
// 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
|
|
|
}
|