| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Api; |
| 4 |
|
| 5 |
use MediaWiki\Api\ApiBase; |
| 6 |
use MediaWiki\Extension\Notifications\AttributeManager; |
| 7 |
use MediaWiki\Extension\Notifications\Controller\NotificationController; |
| 8 |
use MediaWiki\Extension\Notifications\NotifUser; |
| 9 |
use MediaWiki\WikiMap\WikiMap; |
| 10 |
use Wikimedia\ParamValidator\ParamValidator; |
| 11 |
|
| 12 |
class ApiEchoMarkRead extends ApiBase { |
| 13 |
use ApiCrossWiki; |
| 14 |
use ApiEchoPermissionsTrait; |
| 15 |
|
| 16 |
public function execute() { |
| 17 |
$this->checkReadNotificationsPermissions(); |
| 18 |
|
| 19 |
|
| 20 |
$this->getMain()->getVal( '_' ); |
| 21 |
|
| 22 |
$notifUser = NotifUser::newFromUser( $this->getUser() ); |
| 23 |
|
| 24 |
$params = $this->extractRequestParams(); |
| 25 |
|
| 26 |
|
| 27 |
if ( in_array( WikiMap::getCurrentWikiId(), $this->getRequestedWikis() ) ) { |
| 28 |
|
| 29 |
if ( $notifUser->getLocalNotificationCount() > 0 ) { |
| 30 |
if ( $params['list'] ) { |
| 31 |
|
| 32 |
$notifUser->markRead( array_slice( $params['list'], 0, ApiBase::LIMIT_SML2 ) ); |
| 33 |
|
| 34 |
} elseif ( $params['all'] ) { |
| 35 |
$notifUser->markAllRead(); |
| 36 |
|
| 37 |
} elseif ( $params['sections'] ) { |
| 38 |
$notifUser->markAllRead( $params['sections'] ); |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
if ( $params['unreadlist'] !== null && $params['unreadlist'] !== [] ) { |
| 44 |
|
| 45 |
$notifUser->markUnRead( array_slice( $params['unreadlist'], 0, ApiBase::LIMIT_SML2 ) ); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
$foreignResults = $this->getFromForeign(); |
| 50 |
|
| 51 |
$result = [ |
| 52 |
'result' => 'success' |
| 53 |
]; |
| 54 |
|
| 55 |
foreach ( $foreignResults as $wiki => $foreignResult ) { |
| 56 |
if ( isset( $foreignResult['error'] ) ) { |
| 57 |
$result['errors'][$wiki] = $foreignResult['error']; |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
$rawCount = 0; |
| 62 |
foreach ( AttributeManager::$sections as $section ) { |
| 63 |
$rawSectionCount = $notifUser->getNotificationCount( $section ); |
| 64 |
$result[$section]['rawcount'] = $rawSectionCount; |
| 65 |
$result[$section]['count'] = NotificationController::formatNotificationCount( $rawSectionCount ); |
| 66 |
$rawCount += $rawSectionCount; |
| 67 |
} |
| 68 |
|
| 69 |
$result += [ |
| 70 |
'rawcount' => $rawCount, |
| 71 |
'count' => NotificationController::formatNotificationCount( $rawCount ), |
| 72 |
]; |
| 73 |
|
| 74 |
$this->getResult()->addValue( 'query', $this->getModuleName(), $result ); |
| 75 |
} |
| 76 |
|
| 77 |
public function getAllowedParams() { |
| 78 |
return $this->getCrossWikiParams() + [ |
| 79 |
'list' => [ |
| 80 |
ParamValidator::PARAM_ISMULTI => true, |
| 81 |
], |
| 82 |
'unreadlist' => [ |
| 83 |
ParamValidator::PARAM_ISMULTI => true, |
| 84 |
], |
| 85 |
'all' => [ |
| 86 |
ParamValidator::PARAM_REQUIRED => false, |
| 87 |
ParamValidator::PARAM_TYPE => 'boolean' |
| 88 |
], |
| 89 |
'sections' => [ |
| 90 |
ParamValidator::PARAM_TYPE => AttributeManager::$sections, |
| 91 |
ParamValidator::PARAM_ISMULTI => true, |
| 92 |
], |
| 93 |
'token' => [ |
| 94 |
ParamValidator::PARAM_REQUIRED => true, |
| 95 |
] |
| 96 |
]; |
| 97 |
} |
| 98 |
|
| 99 |
public function needsToken() { |
| 100 |
return 'csrf'; |
| 101 |
} |
| 102 |
|
| 103 |
public function mustBePosted() { |
| 104 |
return true; |
| 105 |
} |
| 106 |
|
| 107 |
public function isWriteMode() { |
| 108 |
return true; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
protected function getExamplesMessages() { |
| 116 |
return [ |
| 117 |
'action=echomarkread&list=8' |
| 118 |
=> 'apihelp-echomarkread-example-1', |
| 119 |
'action=echomarkread&all=true' |
| 120 |
=> 'apihelp-echomarkread-example-2', |
| 121 |
'action=echomarkread&unreadlist=1' |
| 122 |
=> 'apihelp-echomarkread-example-3', |
| 123 |
]; |
| 124 |
} |
| 125 |
|
| 126 |
public function getHelpUrls() { |
| 127 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/Echo_(Notifications)/API'; |
| 128 |
} |
| 129 |
} |