2013-09-18 21:10:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class ApiEchoMarkRead extends ApiBase {
|
2018-08-30 00:30:06 +00:00
|
|
|
use ApiCrossWiki;
|
2013-09-18 21:10:37 +00:00
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
// To avoid API warning, register the parameter used to bust browser cache
|
|
|
|
$this->getMain()->getVal( '_' );
|
|
|
|
|
|
|
|
$user = $this->getUser();
|
|
|
|
if ( $user->isAnon() ) {
|
2016-11-03 19:16:56 +00:00
|
|
|
$this->dieWithError( 'apierror-mustbeloggedin-generic', 'login-required' );
|
2018-02-22 03:07:45 +00:00
|
|
|
} elseif ( MWEchoDbFactory::newFromDefault()->isReadOnly() ) {
|
|
|
|
$this->dieReadOnly();
|
2013-09-18 21:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$notifUser = MWEchoNotifUser::newFromUser( $user );
|
|
|
|
|
|
|
|
$params = $this->extractRequestParams();
|
|
|
|
|
2018-08-30 00:30:06 +00:00
|
|
|
// Mark as read/unread locally, if requested
|
|
|
|
if ( in_array( wfWikiID(), $this->getRequestedWikis() ) ) {
|
|
|
|
// There is no need to trigger markRead if all notifications are read
|
|
|
|
if ( $notifUser->getLocalNotificationCount() > 0 ) {
|
|
|
|
if ( $params['list'] ) {
|
|
|
|
// Make sure there is a limit to the update
|
|
|
|
$notifUser->markRead( array_slice( $params['list'], 0, ApiBase::LIMIT_SML2 ) );
|
|
|
|
// Mark all as read
|
|
|
|
} elseif ( $params['all'] ) {
|
|
|
|
$notifUser->markAllRead();
|
|
|
|
// Mark all as read for sections
|
|
|
|
} elseif ( $params['sections'] ) {
|
|
|
|
$notifUser->markAllRead( $params['sections'] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark as unread
|
|
|
|
if ( $params['unreadlist'] !== null && $params['unreadlist'] !== [] ) {
|
2013-09-18 21:10:37 +00:00
|
|
|
// Make sure there is a limit to the update
|
2018-08-30 00:30:06 +00:00
|
|
|
$notifUser->markUnRead( array_slice( $params['unreadlist'], 0, ApiBase::LIMIT_SML2 ) );
|
2013-09-18 21:10:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-30 00:30:06 +00:00
|
|
|
$foreignResults = $this->getFromForeign();
|
2016-03-04 22:44:22 +00:00
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$result = [
|
2014-08-05 21:50:54 +00:00
|
|
|
'result' => 'success'
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2018-08-30 00:30:06 +00:00
|
|
|
|
|
|
|
foreach ( $foreignResults as $wiki => $foreignResult ) {
|
|
|
|
if ( isset( $foreignResult['error'] ) ) {
|
|
|
|
$result['errors'][$wiki] = $foreignResult['error'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-05 21:50:54 +00:00
|
|
|
$rawCount = 0;
|
|
|
|
foreach ( EchoAttributeManager::$sections as $section ) {
|
2018-05-31 23:11:57 +00:00
|
|
|
$rawSectionCount = $notifUser->getNotificationCount( $section );
|
2014-08-05 21:50:54 +00:00
|
|
|
$result[$section]['rawcount'] = $rawSectionCount;
|
|
|
|
$result[$section]['count'] = EchoNotificationController::formatNotificationCount( $rawSectionCount );
|
|
|
|
$rawCount += $rawSectionCount;
|
|
|
|
}
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$result += [
|
2013-09-25 00:18:02 +00:00
|
|
|
'rawcount' => $rawCount,
|
|
|
|
'count' => EchoNotificationController::formatNotificationCount( $rawCount ),
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2018-08-30 00:30:06 +00:00
|
|
|
|
2013-09-18 21:10:37 +00:00
|
|
|
$this->getResult()->addValue( 'query', $this->getModuleName(), $result );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAllowedParams() {
|
2018-08-30 00:30:06 +00:00
|
|
|
return $this->getCrossWikiParams() + [
|
2016-12-05 18:51:07 +00:00
|
|
|
'list' => [
|
2013-09-18 21:10:37 +00:00
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
'unreadlist' => [
|
2016-03-04 22:44:22 +00:00
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
'all' => [
|
2013-09-18 21:10:37 +00:00
|
|
|
ApiBase::PARAM_REQUIRED => false,
|
|
|
|
ApiBase::PARAM_TYPE => 'boolean'
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
'sections' => [
|
2014-08-13 22:00:25 +00:00
|
|
|
ApiBase::PARAM_TYPE => EchoAttributeManager::$sections,
|
|
|
|
ApiBase::PARAM_ISMULTI => true,
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
'token' => [
|
2013-09-18 21:10:37 +00:00
|
|
|
ApiBase::PARAM_REQUIRED => true,
|
2018-08-30 00:30:06 +00:00
|
|
|
]
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2013-09-18 21:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function needsToken() {
|
2014-08-09 13:07:07 +00:00
|
|
|
return 'csrf';
|
2013-09-18 21:10:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function mustBePosted() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function isWriteMode() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-10-28 19:42:41 +00:00
|
|
|
/**
|
|
|
|
* @see ApiBase::getExamplesMessages()
|
2018-08-13 07:25:22 +00:00
|
|
|
* @return string[]
|
2014-10-28 19:42:41 +00:00
|
|
|
*/
|
|
|
|
protected function getExamplesMessages() {
|
2016-12-05 18:51:07 +00:00
|
|
|
return [
|
2014-10-28 19:42:41 +00:00
|
|
|
'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',
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2014-10-28 19:42:41 +00:00
|
|
|
}
|
|
|
|
|
2013-09-18 21:10:37 +00:00
|
|
|
public function getHelpUrls() {
|
2019-08-27 06:13:35 +00:00
|
|
|
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/Echo_(Notifications)/API';
|
2013-09-18 21:10:37 +00:00
|
|
|
}
|
|
|
|
}
|