2012-04-27 15:14:24 +00:00
|
|
|
<?php
|
|
|
|
|
2016-05-26 20:41:25 +00:00
|
|
|
class ApiEchoNotifications extends ApiCrossWikiBase {
|
2016-04-21 10:28:32 +00:00
|
|
|
/**
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $crossWikiSummary = false;
|
2013-12-02 23:11:59 +00:00
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
public function __construct( $query, $moduleName ) {
|
|
|
|
parent::__construct( $query, $moduleName, 'not' );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute() {
|
2016-06-21 11:46:06 +00:00
|
|
|
|
2013-07-12 22:46:37 +00:00
|
|
|
// To avoid API warning, register the parameter used to bust browser cache
|
|
|
|
$this->getMain()->getVal( '_' );
|
|
|
|
|
2016-04-21 11:16:21 +00:00
|
|
|
if ( $this->getUser()->isAnon() ) {
|
2012-12-07 01:08:33 +00:00
|
|
|
$this->dieUsage( 'Login is required', 'login-required' );
|
|
|
|
}
|
2012-06-01 10:57:09 +00:00
|
|
|
|
2012-12-07 01:08:33 +00:00
|
|
|
$params = $this->extractRequestParams();
|
2012-04-27 15:14:24 +00:00
|
|
|
|
2016-01-18 06:27:17 +00:00
|
|
|
/* @deprecated */
|
|
|
|
if ( $params['format'] === 'flyout' ) {
|
|
|
|
$this->setWarning(
|
|
|
|
"notformat=flyout has been deprecated and will be removed soon.\n".
|
|
|
|
"Use notformat=model to get the raw data or notformat=special\n".
|
|
|
|
"for pre-rendered HTML."
|
|
|
|
);
|
|
|
|
} elseif ( $params['format'] === 'html' ) {
|
|
|
|
$this->setWarning(
|
|
|
|
"notformat=html has been deprecated and will be removed soon.\n".
|
|
|
|
"Use notformat=special instead."
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-05-26 20:41:25 +00:00
|
|
|
if ( $this->allowCrossWikiNotifications() ) {
|
2016-05-13 20:48:03 +00:00
|
|
|
$this->crossWikiSummary = $params['crosswikisummary'];
|
|
|
|
}
|
2015-11-25 04:07:54 +00:00
|
|
|
|
2016-04-21 11:16:21 +00:00
|
|
|
$results = array();
|
2016-06-29 13:11:21 +00:00
|
|
|
if ( in_array( wfWikiID(), $this->getRequestedWikis() ) ) {
|
|
|
|
$results[wfWikiID()] = $this->getLocalNotifications( $params );
|
2016-04-21 11:16:21 +00:00
|
|
|
}
|
|
|
|
|
2016-05-27 21:10:04 +00:00
|
|
|
if ( $this->getRequestedForeignWikis() ) {
|
2016-05-26 20:41:25 +00:00
|
|
|
$foreignResults = $this->getFromForeign();
|
|
|
|
foreach ( $foreignResults as $wiki => $result ) {
|
|
|
|
if ( isset( $result['query']['notifications'] ) ) {
|
|
|
|
$results[$wiki] = $result['query']['notifications'];
|
|
|
|
}
|
|
|
|
}
|
2016-04-21 11:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// after getting local & foreign results, merge them all together
|
|
|
|
$result = $this->mergeResults( $results, $params );
|
|
|
|
if ( $params['groupbysection'] ) {
|
|
|
|
foreach ( $params['sections'] as $section ) {
|
|
|
|
if ( in_array( 'list', $params['prop'] ) ) {
|
|
|
|
$this->getResult()->setIndexedTagName( $result[$section]['list'], 'notification' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( in_array( 'list', $params['prop'] ) ) {
|
|
|
|
$this->getResult()->setIndexedTagName( $result['list'], 'notification' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->getResult()->addValue( 'query', $this->getModuleName(), $result );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $params
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getLocalNotifications( array $params ) {
|
|
|
|
$user = $this->getUser();
|
|
|
|
$prop = $params['prop'];
|
2016-06-07 18:48:33 +00:00
|
|
|
$titles = null;
|
|
|
|
if ( $params['titles'] ) {
|
|
|
|
$titles = array_values( array_filter( array_map( 'Title::newFromText', $params['titles'] ) ) );
|
2016-06-23 22:32:18 +00:00
|
|
|
if ( in_array( '[]', $params['titles'] ) ) {
|
|
|
|
$titles[] = null;
|
|
|
|
}
|
2016-06-07 18:48:33 +00:00
|
|
|
}
|
2016-04-21 11:16:21 +00:00
|
|
|
|
2012-12-07 01:08:33 +00:00
|
|
|
$result = array();
|
2012-08-30 16:04:39 +00:00
|
|
|
if ( in_array( 'list', $prop ) ) {
|
2014-08-05 21:50:54 +00:00
|
|
|
// Group notification results by section
|
|
|
|
if ( $params['groupbysection'] ) {
|
|
|
|
foreach ( $params['sections'] as $section ) {
|
|
|
|
$result[$section] = $this->getSectionPropList(
|
2015-12-10 16:18:30 +00:00
|
|
|
$user, $section, $params['filter'], $params['limit'],
|
2016-06-07 18:48:33 +00:00
|
|
|
$params[$section . 'continue'], $params['format'],
|
2016-03-04 19:23:02 +00:00
|
|
|
$titles, $params[$section . 'unreadfirst'], $params['bundle']
|
2014-08-05 21:50:54 +00:00
|
|
|
);
|
2015-11-25 04:07:54 +00:00
|
|
|
|
2016-05-29 20:54:15 +00:00
|
|
|
if ( $this->crossWikiSummary ) {
|
2015-11-25 04:07:54 +00:00
|
|
|
// insert fake notification for foreign notifications
|
2016-05-29 20:54:15 +00:00
|
|
|
$foreignNotification = $this->makeForeignNotification( $user, $params['format'], $section );
|
|
|
|
if ( $foreignNotification ) {
|
|
|
|
array_unshift( $result[$section]['list'], $foreignNotification );
|
|
|
|
}
|
2015-11-25 04:07:54 +00:00
|
|
|
}
|
2014-08-05 21:50:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$attributeManager = EchoAttributeManager::newFromGlobalVars();
|
|
|
|
$result = $this->getPropList(
|
|
|
|
$user,
|
|
|
|
$attributeManager->getUserEnabledEventsbySections( $user, 'web', $params['sections'] ),
|
2016-06-07 18:48:33 +00:00
|
|
|
$params['filter'], $params['limit'], $params['continue'], $params['format'],
|
2016-03-04 19:23:02 +00:00
|
|
|
$titles, $params['unreadfirst'], $params['bundle']
|
2014-08-05 21:50:54 +00:00
|
|
|
);
|
2015-11-25 04:07:54 +00:00
|
|
|
|
2016-04-21 10:28:32 +00:00
|
|
|
// if exactly 1 section is specified, we consider only that section, otherwise
|
2016-04-27 04:18:28 +00:00
|
|
|
// we pass ALL to consider all foreign notifications
|
|
|
|
$section = count( $params['sections'] ) === 1 ? reset( $params['sections'] ) : EchoAttributeManager::ALL;
|
2016-05-29 20:54:15 +00:00
|
|
|
if ( $this->crossWikiSummary ) {
|
|
|
|
$foreignNotification = $this->makeForeignNotification( $user, $params['format'], $section );
|
|
|
|
if ( $foreignNotification ) {
|
|
|
|
array_unshift( $result['list'], $foreignNotification );
|
|
|
|
}
|
2015-11-25 04:07:54 +00:00
|
|
|
}
|
2014-07-30 22:09:22 +00:00
|
|
|
}
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2012-08-30 16:04:39 +00:00
|
|
|
if ( in_array( 'count', $prop ) ) {
|
2014-08-14 18:46:26 +00:00
|
|
|
$result = array_merge_recursive(
|
|
|
|
$result,
|
2016-04-21 10:28:32 +00:00
|
|
|
$this->getPropCount( $user, $params['sections'], $params['groupbysection'] )
|
2014-08-14 18:46:26 +00:00
|
|
|
);
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2016-04-21 11:16:21 +00:00
|
|
|
return $result;
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2014-07-30 22:09:22 +00:00
|
|
|
/**
|
2014-08-05 21:50:54 +00:00
|
|
|
* Internal method for getting the property 'list' data for individual section
|
|
|
|
* @param User $user
|
2015-08-15 01:51:11 +00:00
|
|
|
* @param string $section 'alert' or 'message'
|
2015-12-10 16:18:30 +00:00
|
|
|
* @param string $filter 'all', 'read' or 'unread'
|
2014-08-05 21:50:54 +00:00
|
|
|
* @param int $limit
|
|
|
|
* @param string $continue
|
|
|
|
* @param string $format
|
2016-06-07 18:48:33 +00:00
|
|
|
* @param Title[] $titles
|
2014-08-14 00:01:35 +00:00
|
|
|
* @param boolean $unreadFirst
|
2016-03-04 19:23:02 +00:00
|
|
|
* @param bool $bundle
|
2014-08-05 21:50:54 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2016-03-04 19:23:02 +00:00
|
|
|
protected function getSectionPropList( User $user, $section, $filter, $limit, $continue, $format, array $titles = null, $unreadFirst = false, $bundle = false ) {
|
2014-08-05 21:50:54 +00:00
|
|
|
$attributeManager = EchoAttributeManager::newFromGlobalVars();
|
|
|
|
$sectionEvents = $attributeManager->getUserEnabledEventsbySections( $user, 'web', array( $section ) );
|
2015-08-15 01:51:11 +00:00
|
|
|
|
|
|
|
if ( !$sectionEvents ) {
|
2014-08-05 21:50:54 +00:00
|
|
|
$result = array(
|
|
|
|
'list' => array(),
|
|
|
|
'continue' => null
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$result = $this->getPropList(
|
2016-03-04 19:23:02 +00:00
|
|
|
$user, $sectionEvents, $filter, $limit, $continue, $format, $titles, $unreadFirst, $bundle
|
2014-08-05 21:50:54 +00:00
|
|
|
);
|
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-07-30 22:09:22 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-08-05 21:50:54 +00:00
|
|
|
* Internal helper method for getting property 'list' data, this is based
|
|
|
|
* on the event types specified in the arguments and it could be event types
|
|
|
|
* of a set of sections or a single section
|
|
|
|
* @param User $user
|
2014-08-14 00:01:35 +00:00
|
|
|
* @param string[] $eventTypes
|
2015-12-10 16:18:30 +00:00
|
|
|
* @param string $filter 'all', 'read' or 'unread'
|
2014-08-05 21:50:54 +00:00
|
|
|
* @param int $limit
|
|
|
|
* @param string $continue
|
|
|
|
* @param string $format
|
2016-06-07 18:48:33 +00:00
|
|
|
* @param Title[] $titles
|
2014-08-14 00:01:35 +00:00
|
|
|
* @param boolean $unreadFirst
|
2016-03-04 19:23:02 +00:00
|
|
|
* @param bool $bundle
|
2014-08-05 21:50:54 +00:00
|
|
|
* @return array
|
2014-07-30 22:09:22 +00:00
|
|
|
*/
|
2016-03-04 19:23:02 +00:00
|
|
|
protected function getPropList( User $user, array $eventTypes, $filter, $limit, $continue, $format, array $titles = null, $unreadFirst = false, $bundle = false ) {
|
2014-07-30 22:09:22 +00:00
|
|
|
$result = array(
|
|
|
|
'list' => array(),
|
|
|
|
'continue' => null
|
|
|
|
);
|
|
|
|
|
2014-08-14 18:46:26 +00:00
|
|
|
$notifMapper = new EchoNotificationMapper();
|
2014-08-14 00:01:35 +00:00
|
|
|
|
2015-12-10 16:18:30 +00:00
|
|
|
// check if we want both read & unread...
|
|
|
|
if ( in_array( 'read', $filter ) && in_array( '!read', $filter ) ) {
|
|
|
|
// Prefer unread notifications. We don't care about next offset in this case
|
|
|
|
if ( $unreadFirst ) {
|
|
|
|
// query for unread notifications past 'continue' (offset)
|
2016-06-07 18:48:33 +00:00
|
|
|
$notifs = $notifMapper->fetchUnreadByUser( $user, $limit + 1, $continue, $eventTypes, $titles );
|
2015-12-09 18:41:40 +00:00
|
|
|
|
2015-12-10 16:18:30 +00:00
|
|
|
/*
|
|
|
|
* 'continue' has a timestamp & id (to start with, in case
|
|
|
|
* there would be multiple events with that same timestamp)
|
|
|
|
* Unread notifications should always load first, but may be
|
|
|
|
* older than read ones, but we can work with current
|
|
|
|
* 'continue' format:
|
|
|
|
* * if there's no continue, first load unread notifications
|
|
|
|
* * if there's a continue, fetch unread notifications first
|
|
|
|
* * if there are no unread ones, continue must've been
|
|
|
|
* about read notifications: fetch 'em
|
|
|
|
* * if there are unread ones but first one doesn't match
|
|
|
|
* continue id, it must've been about read notifications:
|
|
|
|
* discard unread & fetch read
|
|
|
|
*/
|
|
|
|
if ( $notifs && $continue ) {
|
|
|
|
/** @var EchoNotification $first */
|
|
|
|
$first = reset( $notifs );
|
|
|
|
$continueId = intval( trim( strrchr( $continue, '|' ), '|' ) );
|
|
|
|
if ( $first->getEvent()->getID() !== $continueId ) {
|
|
|
|
// notification doesn't match continue id, it must've been
|
|
|
|
// about read notifications: discard all unread ones
|
|
|
|
$notifs = array();
|
|
|
|
}
|
2015-12-09 18:41:40 +00:00
|
|
|
}
|
|
|
|
|
2015-12-10 16:18:30 +00:00
|
|
|
// If there are less unread notifications than we requested,
|
|
|
|
// then fill the result with some read notifications
|
|
|
|
$count = count( $notifs );
|
|
|
|
// we need 1 more than $limit, so we can respond 'continue'
|
|
|
|
if ( $count <= $limit ) {
|
|
|
|
// Query planner should be smart enough that passing a short list of ids to exclude
|
|
|
|
// will only visit at most that number of extra rows.
|
|
|
|
$mixedNotifs = $notifMapper->fetchByUser(
|
|
|
|
$user,
|
|
|
|
$limit - $count + 1,
|
|
|
|
// if there were unread notifications, 'continue' was for
|
|
|
|
// unread notifications and we should start fetching read
|
|
|
|
// notifications from start
|
|
|
|
$count > 0 ? null : $continue,
|
|
|
|
$eventTypes,
|
2016-06-07 18:48:33 +00:00
|
|
|
array_keys( $notifs ),
|
|
|
|
$titles
|
2015-12-10 16:18:30 +00:00
|
|
|
);
|
|
|
|
foreach ( $mixedNotifs as $notif ) {
|
|
|
|
$notifs[$notif->getEvent()->getId()] = $notif;
|
|
|
|
}
|
2014-08-14 00:01:35 +00:00
|
|
|
}
|
2015-12-10 16:18:30 +00:00
|
|
|
} else {
|
2016-06-07 18:48:33 +00:00
|
|
|
$notifs = $notifMapper->fetchByUser( $user, $limit + 1, $continue, $eventTypes, array(), $titles );
|
2014-08-14 00:01:35 +00:00
|
|
|
}
|
2015-12-10 16:18:30 +00:00
|
|
|
} elseif ( in_array( 'read', $filter ) ) {
|
2016-06-07 18:48:33 +00:00
|
|
|
$notifs = $notifMapper->fetchReadByUser( $user, $limit + 1, $continue, $eventTypes, $titles );
|
2015-12-10 16:18:30 +00:00
|
|
|
} else { // = if ( in_array( '!read', $filter ) ) {
|
2016-06-07 18:48:33 +00:00
|
|
|
$notifs = $notifMapper->fetchUnreadByUser( $user, $limit + 1, $continue, $eventTypes, $titles );
|
2014-08-14 00:01:35 +00:00
|
|
|
}
|
2014-08-27 23:59:49 +00:00
|
|
|
|
2016-03-04 19:23:02 +00:00
|
|
|
if ( $bundle ) {
|
|
|
|
$bundler = new Bundler();
|
|
|
|
$notifs = $bundler->bundle( $notifs );
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var EchoNotification $notif */
|
2014-07-30 22:09:22 +00:00
|
|
|
foreach ( $notifs as $notif ) {
|
2015-10-28 17:15:25 +00:00
|
|
|
$output = EchoDataOutputFormatter::formatOutput( $notif, $format, $user, $this->getLanguage() );
|
|
|
|
if ( $output !== false ) {
|
2016-04-21 09:18:59 +00:00
|
|
|
$result['list'][] = $output;
|
2015-10-28 17:15:25 +00:00
|
|
|
}
|
2014-07-30 22:09:22 +00:00
|
|
|
}
|
|
|
|
|
2014-08-26 20:07:11 +00:00
|
|
|
// Generate offset if necessary
|
2015-12-09 18:41:40 +00:00
|
|
|
if ( count( $result['list'] ) > $limit ) {
|
|
|
|
$lastItem = array_pop( $result['list'] );
|
2016-04-21 11:16:21 +00:00
|
|
|
// @todo: what to do with this when fetching from multiple wikis?
|
2015-12-09 18:41:40 +00:00
|
|
|
$result['continue'] = $lastItem['timestamp']['utcunix'] . '|' . $lastItem['id'];
|
2014-08-26 20:07:11 +00:00
|
|
|
}
|
|
|
|
|
2014-07-30 22:09:22 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2014-08-05 21:50:54 +00:00
|
|
|
/**
|
|
|
|
* Internal helper method for getting property 'count' data
|
|
|
|
* @param User $user
|
|
|
|
* @param string[] $sections
|
|
|
|
* @param boolean $groupBySection
|
2015-08-19 20:13:07 +00:00
|
|
|
* @return array
|
2014-08-05 21:50:54 +00:00
|
|
|
*/
|
2016-04-21 10:28:32 +00:00
|
|
|
protected function getPropCount( User $user, array $sections, $groupBySection ) {
|
2014-08-05 21:50:54 +00:00
|
|
|
$result = array();
|
|
|
|
$notifUser = MWEchoNotifUser::newFromUser( $user );
|
NotifUser: Refactor getNotificationCount() and friends, add caching for global counts
Previously, getNotificationCount() only looked at local notifications,
and foreign notifications were added in separately by getMessageCount()
and getAlertCount(). This didn't make any sense and resulted in
counter-intuitive things like I4d49b543.
Instead, add a $global flag to getNotificationCount(). If $global=false,
the local count is returned as before, but if $global=true, the
global count (=local+foreign) is returned. If $global is omitted,
the user's cross-wiki notification preference determines which is returned.
Update getLastUnreadNotificationCount() in the same way, since it had
the same issues.
Also add caching for global counts and timestamps, using a global
memc key.
Bug: T133623
Change-Id: If78bfc710acd91a075771b565cc99f4c302a104d
2016-04-27 07:12:32 +00:00
|
|
|
$global = $this->crossWikiSummary ? 'preference' : false;
|
2016-07-05 20:36:36 +00:00
|
|
|
|
|
|
|
$totalRawCount = 0;
|
|
|
|
foreach ( $sections as $section ) {
|
|
|
|
$rawCount = $notifUser->getNotificationCount( /* $tryCache = */true, DB_SLAVE, $section, $global );
|
|
|
|
if ( $groupBySection ) {
|
2014-08-05 21:50:54 +00:00
|
|
|
$result[$section]['rawcount'] = $rawCount;
|
|
|
|
$result[$section]['count'] = EchoNotificationController::formatNotificationCount( $rawCount );
|
|
|
|
}
|
2016-07-05 20:36:36 +00:00
|
|
|
$totalRawCount += $rawCount;
|
2014-08-05 21:50:54 +00:00
|
|
|
}
|
2016-07-05 20:36:36 +00:00
|
|
|
$result['rawcount'] = $totalRawCount;
|
|
|
|
$result['count'] = EchoNotificationController::formatNotificationCount( $totalRawCount );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-08-05 21:50:54 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2016-05-29 20:54:15 +00:00
|
|
|
/**
|
|
|
|
* Build and format a "fake" notification to represent foreign notifications.
|
|
|
|
* @param User $user
|
|
|
|
* @param string $format
|
|
|
|
* @param string $section
|
|
|
|
* @return array|false A formatted notification, or false if there are no foreign notifications
|
|
|
|
*/
|
2016-04-27 04:18:28 +00:00
|
|
|
protected function makeForeignNotification( User $user, $format, $section = EchoAttributeManager::ALL ) {
|
2016-05-29 20:54:15 +00:00
|
|
|
global $wgEchoSectionTransition, $wgEchoBundleTransition;
|
|
|
|
if (
|
|
|
|
( $wgEchoSectionTransition && $section !== EchoAttributeManager::ALL ) ||
|
|
|
|
$wgEchoBundleTransition
|
|
|
|
) {
|
|
|
|
// In section transition mode we trust that echo_unread_wikis is accurate for the total of alerts+messages,
|
|
|
|
// but not for each section individually (i.e. we don't trust that notifications won't be misclassified).
|
|
|
|
// We get all wikis that have any notifications at all according to the euw table,
|
|
|
|
// 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( $wgEchoSectionTransition ? EchoAttributeManager::ALL : $section );
|
|
|
|
if ( !$potentialWikis ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$foreignResults = $this->getFromForeign( $potentialWikis, array( $this->getModulePrefix() . 'filter' => '!read' ) );
|
|
|
|
|
|
|
|
$countsByWiki = array();
|
|
|
|
$timestampsByWiki = array();
|
|
|
|
foreach ( $foreignResults as $wiki => $result ) {
|
|
|
|
if ( isset( $result['query']['notifications']['list'] ) ) {
|
|
|
|
$notifs = $result['query']['notifications']['list'];
|
2016-06-29 13:09:55 +00:00
|
|
|
$countsByWiki[$wiki] = intval( $result['query']['notifications']['count'] );
|
2016-05-29 20:54:15 +00:00
|
|
|
} elseif ( isset( $result['query']['notifications'][$section]['list'] ) ) {
|
|
|
|
$notifs = $result['query']['notifications'][$section]['list'];
|
2016-06-29 13:09:55 +00:00
|
|
|
$countsByWiki[$wiki] = intval( $result['query']['notifications'][$section]['count'] );
|
2016-05-29 20:54:15 +00:00
|
|
|
} else {
|
|
|
|
$notifs = false;
|
2016-06-29 13:09:55 +00:00
|
|
|
$countsByWiki[$wiki] = 0;
|
2016-05-29 20:54:15 +00:00
|
|
|
}
|
|
|
|
if ( $notifs ) {
|
|
|
|
$timestampsByWiki[$wiki] = max( array_map( function ( $n ) {
|
|
|
|
return $n['timestamp']['mw'];
|
|
|
|
}, $notifs ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-01 19:17:29 +00:00
|
|
|
$wikis = array_keys( $timestampsByWiki );
|
2016-05-29 20:54:15 +00:00
|
|
|
$count = array_sum( $countsByWiki );
|
|
|
|
$maxTimestamp = new MWTimestamp( max( $timestampsByWiki ) );
|
|
|
|
$timestampsByWiki = array_map( function ( $ts ) {
|
|
|
|
return new MWTimestamp( $ts );
|
|
|
|
}, $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 );
|
|
|
|
$timestampsByWiki = array();
|
|
|
|
foreach ( $wikis as $wiki ) {
|
|
|
|
$timestampsByWiki[$wiki] = $this->foreignNotifications->getWikiTimestamp( $wiki, $section );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $count === 0 || count( $wikis ) === 0 ) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-25 04:07:54 +00:00
|
|
|
|
2016-02-27 05:43:46 +00:00
|
|
|
// Sort wikis by timestamp, in descending order (newest first)
|
2016-05-29 20:54:15 +00:00
|
|
|
usort( $wikis, function ( $a, $b ) use ( $section, $timestampsByWiki ) {
|
|
|
|
return $timestampsByWiki[$b]->getTimestamp( TS_UNIX ) - $timestampsByWiki[$a]->getTimestamp( TS_UNIX );
|
2016-02-27 05:43:46 +00:00
|
|
|
} );
|
|
|
|
|
2015-11-25 04:07:54 +00:00
|
|
|
$row = new StdClass;
|
|
|
|
$row->event_id = -1;
|
|
|
|
$row->event_type = 'foreign';
|
|
|
|
$row->event_variant = null;
|
|
|
|
$row->event_agent_id = $user->getId();
|
|
|
|
$row->event_agent_ip = null;
|
|
|
|
$row->event_page_id = null;
|
|
|
|
$row->event_page_namespace = null;
|
|
|
|
$row->event_page_title = null;
|
|
|
|
$row->event_extra = serialize( array(
|
2016-04-15 19:45:48 +00:00
|
|
|
'section' => $section ?: 'all',
|
2015-11-25 04:07:54 +00:00
|
|
|
'wikis' => $wikis,
|
|
|
|
'count' => $count
|
|
|
|
) );
|
|
|
|
|
|
|
|
$row->notification_user = $user->getId();
|
2016-05-29 20:54:15 +00:00
|
|
|
$row->notification_timestamp = $maxTimestamp;
|
2015-11-25 04:07:54 +00:00
|
|
|
$row->notification_read_timestamp = null;
|
|
|
|
$row->notification_bundle_base = 1;
|
|
|
|
$row->notification_bundle_hash = md5( 'bogus' );
|
|
|
|
$row->notification_bundle_display_hash = md5( 'also-bogus' );
|
|
|
|
|
2016-02-27 05:43:46 +00:00
|
|
|
// Format output like any other notification
|
2015-11-25 04:07:54 +00:00
|
|
|
$notif = EchoNotification::newFromRow( $row );
|
|
|
|
$output = EchoDataOutputFormatter::formatOutput( $notif, $format, $user, $this->getLanguage() );
|
|
|
|
|
2016-02-27 05:43:46 +00:00
|
|
|
// Add cross-wiki-specific data
|
2016-04-15 19:45:48 +00:00
|
|
|
$output['section'] = $section ?: 'all';
|
2015-11-25 04:07:54 +00:00
|
|
|
$output['count'] = $count;
|
2016-04-27 00:25:14 +00:00
|
|
|
$output['sources'] = EchoForeignNotifications::getApiEndpoints( $wikis );
|
2016-02-27 05:43:46 +00:00
|
|
|
// Add timestamp information
|
|
|
|
foreach ( $output['sources'] as $wiki => &$data ) {
|
2016-05-29 20:54:15 +00:00
|
|
|
$data['ts'] = $timestampsByWiki[$wiki]->getTimestamp( TS_MW );
|
2016-02-27 05:43:46 +00:00
|
|
|
}
|
2015-11-25 04:07:54 +00:00
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2016-06-15 22:39:07 +00:00
|
|
|
protected function getForeignQueryParams() {
|
|
|
|
$params = parent::getForeignQueryParams();
|
2016-04-21 11:16:21 +00:00
|
|
|
|
2016-05-26 20:41:25 +00:00
|
|
|
// don't request cross-wiki notification summaries
|
2016-05-04 00:46:59 +00:00
|
|
|
unset( $params['notcrosswikisummary'] );
|
2016-04-21 11:16:21 +00:00
|
|
|
|
2016-05-26 20:41:25 +00:00
|
|
|
return $params;
|
2016-04-21 11:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $results
|
|
|
|
* @param array $params
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function mergeResults( array $results, array $params ) {
|
|
|
|
$master = array_shift( $results );
|
2016-05-09 18:08:00 +00:00
|
|
|
if ( !$master ) {
|
|
|
|
$master = array();
|
|
|
|
}
|
2016-04-21 11:16:21 +00:00
|
|
|
|
|
|
|
if ( in_array( 'list', $params['prop'] ) ) {
|
|
|
|
$master = $this->mergeList( $master, $results, $params['groupbysection'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( in_array( 'count', $params['prop'] ) && !$this->crossWikiSummary ) {
|
|
|
|
// if crosswiki data was requested, the count in $master
|
|
|
|
// is accurate already
|
|
|
|
// otherwise, we'll want to combine counts for all wikis
|
|
|
|
$master = $this->mergeCount( $master, $results, $params['groupbysection'] );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $master;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $master
|
|
|
|
* @param array $results
|
|
|
|
* @param bool $groupBySection
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function mergeList( array $master, array $results, $groupBySection ) {
|
|
|
|
// sort all notifications by timestamp: most recent first
|
|
|
|
$sort = function( $a, $b ) {
|
|
|
|
return $a['timestamp']['utcunix'] - $b['timestamp']['utcunix'];
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( $groupBySection ) {
|
|
|
|
foreach ( EchoAttributeManager::$sections as $section ) {
|
2016-05-09 18:08:00 +00:00
|
|
|
if ( !isset( $master[$section]['list'] ) ) {
|
|
|
|
$master[$section]['list'] = array();
|
|
|
|
}
|
2016-04-21 11:16:21 +00:00
|
|
|
foreach ( $results as $result ) {
|
|
|
|
$master[$section]['list'] = array_merge( $master[$section]['list'], $result[$section]['list'] );
|
|
|
|
}
|
|
|
|
usort( $master[$section]['list'], $sort );
|
|
|
|
}
|
|
|
|
} else {
|
2016-05-13 00:55:51 +00:00
|
|
|
if ( !isset( $master['list'] ) || !is_array( $master['list'] ) ) {
|
2016-05-09 18:08:00 +00:00
|
|
|
$master['list'] = array();
|
|
|
|
}
|
2016-04-21 11:16:21 +00:00
|
|
|
foreach ( $results as $result ) {
|
|
|
|
$master['list'] = array_merge( $master['list'], $result['list'] );
|
|
|
|
}
|
|
|
|
usort( $master['list'], $sort );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $master;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $master
|
|
|
|
* @param array $results
|
|
|
|
* @param bool $groupBySection
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function mergeCount( array $master, array $results, $groupBySection ) {
|
|
|
|
if ( $groupBySection ) {
|
|
|
|
foreach ( EchoAttributeManager::$sections as $section ) {
|
2016-05-09 18:08:00 +00:00
|
|
|
if ( !isset( $master[$section]['rawcount'] ) ) {
|
|
|
|
$master[$section]['rawcount'] = 0;
|
|
|
|
}
|
2016-04-21 11:16:21 +00:00
|
|
|
foreach ( $results as $result ) {
|
|
|
|
$master[$section]['rawcount'] += $result[$section]['rawcount'];
|
|
|
|
}
|
|
|
|
$master[$section]['count'] = EchoNotificationController::formatNotificationCount( $master[$section]['rawcount'] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-09 18:08:00 +00:00
|
|
|
if ( !isset( $master['rawcount'] ) ) {
|
|
|
|
$master['rawcount'] = 0;
|
|
|
|
}
|
2016-04-21 11:16:21 +00:00
|
|
|
foreach ( $results as $result ) {
|
|
|
|
// regardless of groupbysection, totals are always included
|
|
|
|
$master['rawcount'] += $result['rawcount'];
|
|
|
|
}
|
|
|
|
$master['count'] = EchoNotificationController::formatNotificationCount( $master['rawcount'] );
|
|
|
|
|
|
|
|
return $master;
|
|
|
|
}
|
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
public function getAllowedParams() {
|
2014-08-05 21:50:54 +00:00
|
|
|
$sections = EchoAttributeManager::$sections;
|
2016-05-26 20:41:25 +00:00
|
|
|
|
|
|
|
$params = parent::getAllowedParams();
|
|
|
|
$params += array(
|
2015-12-10 16:18:30 +00:00
|
|
|
'filter' => array(
|
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
|
|
|
ApiBase::PARAM_DFLT => 'read|!read',
|
|
|
|
ApiBase::PARAM_TYPE => array(
|
|
|
|
'read',
|
|
|
|
'!read',
|
|
|
|
),
|
|
|
|
),
|
2012-04-27 15:14:24 +00:00
|
|
|
'prop' => array(
|
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
|
|
|
ApiBase::PARAM_TYPE => array(
|
2012-08-31 21:50:46 +00:00
|
|
|
'list',
|
|
|
|
'count',
|
|
|
|
),
|
2012-04-27 15:14:24 +00:00
|
|
|
ApiBase::PARAM_DFLT => 'list',
|
|
|
|
),
|
2014-08-05 21:50:54 +00:00
|
|
|
'sections' => array(
|
|
|
|
ApiBase::PARAM_DFLT => implode( '|', $sections ),
|
|
|
|
ApiBase::PARAM_TYPE => $sections,
|
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
|
|
|
),
|
2014-08-14 00:01:35 +00:00
|
|
|
'groupbysection' => array(
|
|
|
|
ApiBase::PARAM_TYPE => 'boolean',
|
|
|
|
ApiBase::PARAM_DFLT => false,
|
|
|
|
),
|
2012-04-27 15:14:24 +00:00
|
|
|
'format' => array(
|
|
|
|
ApiBase::PARAM_TYPE => array(
|
2012-08-31 21:50:46 +00:00
|
|
|
'text',
|
2015-11-16 16:05:07 +00:00
|
|
|
'model',
|
2015-11-11 02:23:03 +00:00
|
|
|
'special',
|
2016-01-18 06:27:17 +00:00
|
|
|
'flyout', /* @deprecated */
|
|
|
|
'html', /* @deprecated */
|
2012-08-31 21:50:46 +00:00
|
|
|
),
|
2016-01-18 06:27:17 +00:00
|
|
|
ApiBase::PARAM_HELP_MSG_PER_VALUE => array(),
|
2012-04-27 15:14:24 +00:00
|
|
|
),
|
2012-08-01 19:53:05 +00:00
|
|
|
'limit' => array(
|
2013-05-02 17:56:46 +00:00
|
|
|
ApiBase::PARAM_TYPE => 'limit',
|
2012-08-01 19:53:05 +00:00
|
|
|
ApiBase::PARAM_DFLT => 20,
|
|
|
|
ApiBase::PARAM_MIN => 1,
|
2013-05-02 17:56:46 +00:00
|
|
|
ApiBase::PARAM_MAX => ApiBase::LIMIT_SML1,
|
|
|
|
ApiBase::PARAM_MAX2 => ApiBase::LIMIT_SML2,
|
2012-08-01 19:53:05 +00:00
|
|
|
),
|
2014-10-28 19:42:41 +00:00
|
|
|
'continue' => array(
|
2015-10-26 15:41:57 +00:00
|
|
|
ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
|
2014-10-28 19:42:41 +00:00
|
|
|
),
|
2016-03-02 01:11:27 +00:00
|
|
|
'unreadfirst' => array(
|
|
|
|
ApiBase::PARAM_TYPE => 'boolean',
|
|
|
|
ApiBase::PARAM_DFLT => false,
|
|
|
|
),
|
2016-06-07 18:48:33 +00:00
|
|
|
'titles' => array(
|
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
|
|
|
),
|
2016-03-04 19:23:02 +00:00
|
|
|
'bundle' => array(
|
|
|
|
ApiBase::PARAM_TYPE => 'boolean',
|
|
|
|
ApiBase::PARAM_DFLT => false,
|
|
|
|
),
|
2012-04-27 15:14:24 +00:00
|
|
|
);
|
2014-08-05 21:50:54 +00:00
|
|
|
foreach ( $sections as $section ) {
|
|
|
|
$params[$section . 'continue'] = null;
|
2014-08-14 00:01:35 +00:00
|
|
|
$params[$section . 'unreadfirst'] = array(
|
|
|
|
ApiBase::PARAM_TYPE => 'boolean',
|
|
|
|
ApiBase::PARAM_DFLT => false,
|
|
|
|
);
|
2014-08-05 21:50:54 +00:00
|
|
|
}
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2016-05-26 20:41:25 +00:00
|
|
|
if ( $this->allowCrossWikiNotifications() ) {
|
2016-05-13 20:48:03 +00:00
|
|
|
$params += array(
|
|
|
|
// create "x notifications from y wikis" notification bundle &
|
|
|
|
// include unread counts from other wikis in prop=count results
|
|
|
|
'crosswikisummary' => array(
|
|
|
|
ApiBase::PARAM_TYPE => 'boolean',
|
|
|
|
ApiBase::PARAM_DFLT => false,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-08-05 21:50:54 +00:00
|
|
|
return $params;
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2014-10-28 19:42:41 +00:00
|
|
|
/**
|
|
|
|
* @see ApiBase::getExamplesMessages()
|
|
|
|
*/
|
|
|
|
protected function getExamplesMessages() {
|
|
|
|
return array(
|
|
|
|
'action=query&meta=notifications'
|
|
|
|
=> 'apihelp-query+notifications-example-1',
|
|
|
|
'action=query&meta=notifications¬prop=count¬sections=alert|message¬groupbysection=1'
|
|
|
|
=> 'apihelp-query+notifications-example-2',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
public function getHelpUrls() {
|
2014-08-14 21:33:23 +00:00
|
|
|
return 'https://www.mediawiki.org/wiki/Echo_(Notifications)/API';
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
2012-07-26 17:23:18 +00:00
|
|
|
}
|