2013-09-18 21:10:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class ApiEchoMarkRead extends ApiBase {
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
// To avoid API warning, register the parameter used to bust browser cache
|
|
|
|
$this->getMain()->getVal( '_' );
|
|
|
|
|
|
|
|
$user = $this->getUser();
|
|
|
|
if ( $user->isAnon() ) {
|
|
|
|
$this->dieUsage( 'Login is required', 'login-required' );
|
|
|
|
}
|
|
|
|
|
|
|
|
$notifUser = MWEchoNotifUser::newFromUser( $user );
|
|
|
|
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
|
|
|
|
// There is no need to trigger markRead if all notifications are read
|
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
|
|
|
if ( $notifUser->getLocalNotificationCount() > 0 ) {
|
2013-09-18 21:10:37 +00:00
|
|
|
if ( count( $params['list'] ) ) {
|
|
|
|
// Make sure there is a limit to the update
|
|
|
|
$notifUser->markRead( array_slice( $params['list'], 0, ApiBase::LIMIT_SML2 ) );
|
2014-08-13 22:00:25 +00:00
|
|
|
// Mark all as read
|
2013-09-18 21:10:37 +00:00
|
|
|
} elseif ( $params['all'] ) {
|
|
|
|
$notifUser->markAllRead();
|
2014-08-13 22:00:25 +00:00
|
|
|
// Mark all as read for sections
|
|
|
|
} elseif ( $params['sections'] ) {
|
|
|
|
$notifUser->markAllRead( $params['sections'] );
|
2013-09-18 21:10:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-04 22:44:22 +00:00
|
|
|
// Mark as unread
|
|
|
|
if ( count( $params['unreadlist'] ) > 0 ) {
|
|
|
|
// Make sure there is a limit to the update
|
|
|
|
$notifUser->markUnRead( array_slice( $params['unreadlist'], 0, ApiBase::LIMIT_SML2 ) );
|
|
|
|
}
|
|
|
|
|
2013-09-25 00:18:02 +00:00
|
|
|
$result = array(
|
2014-08-05 21:50:54 +00:00
|
|
|
'result' => 'success'
|
|
|
|
);
|
|
|
|
$rawCount = 0;
|
|
|
|
foreach ( EchoAttributeManager::$sections as $section ) {
|
|
|
|
$rawSectionCount = $notifUser->getNotificationCount( /* $tryCache = */true, DB_SLAVE, $section );
|
|
|
|
$result[$section]['rawcount'] = $rawSectionCount;
|
|
|
|
$result[$section]['count'] = EchoNotificationController::formatNotificationCount( $rawSectionCount );
|
|
|
|
$rawCount += $rawSectionCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result += array(
|
2013-09-25 00:18:02 +00:00
|
|
|
'rawcount' => $rawCount,
|
|
|
|
'count' => EchoNotificationController::formatNotificationCount( $rawCount ),
|
|
|
|
);
|
2013-09-18 21:10:37 +00:00
|
|
|
$this->getResult()->addValue( 'query', $this->getModuleName(), $result );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAllowedParams() {
|
|
|
|
return array(
|
|
|
|
'list' => array(
|
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
|
|
|
),
|
2016-03-04 22:44:22 +00:00
|
|
|
'unreadlist' => array(
|
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
|
|
|
),
|
2013-09-18 21:10:37 +00:00
|
|
|
'all' => array(
|
|
|
|
ApiBase::PARAM_REQUIRED => false,
|
|
|
|
ApiBase::PARAM_TYPE => 'boolean'
|
|
|
|
),
|
2014-08-13 22:00:25 +00:00
|
|
|
'sections' => array(
|
|
|
|
ApiBase::PARAM_TYPE => EchoAttributeManager::$sections,
|
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
|
|
|
),
|
2013-09-18 21:10:37 +00:00
|
|
|
'token' => array(
|
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function needsToken() {
|
2014-08-09 13:07:07 +00:00
|
|
|
return 'csrf';
|
2013-09-18 21:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getTokenSalt() {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function mustBePosted() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isWriteMode() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-28 19:42:41 +00:00
|
|
|
/**
|
|
|
|
* @see ApiBase::getExamplesMessages()
|
|
|
|
*/
|
|
|
|
protected function getExamplesMessages() {
|
|
|
|
return array(
|
|
|
|
'action=echomarkread&list=8'
|
|
|
|
=> 'apihelp-echomarkread-example-1',
|
|
|
|
'action=echomarkread&all=true'
|
|
|
|
=> 'apihelp-echomarkread-example-2',
|
2016-03-04 22:44:22 +00:00
|
|
|
'action=echomarkread&unreadlist=1'
|
|
|
|
=> 'apihelp-echomarkread-example-3',
|
2014-10-28 19:42:41 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2013-09-18 21:10:37 +00:00
|
|
|
public function getHelpUrls() {
|
2014-08-14 21:33:23 +00:00
|
|
|
return 'https://www.mediawiki.org/wiki/Echo_(Notifications)/API';
|
2013-09-18 21:10:37 +00:00
|
|
|
}
|
|
|
|
}
|