Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
70.15% |
47 / 67 |
|
57.14% |
4 / 7 |
CRAP | |
0.00% |
0 / 1 |
ApiEchoMarkRead | |
70.15% |
47 / 67 |
|
57.14% |
4 / 7 |
28.60 | |
0.00% |
0 / 1 |
execute | |
69.44% |
25 / 36 |
|
0.00% |
0 / 1 |
17.82 | |||
getAllowedParams | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
1 | |||
needsToken | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
mustBePosted | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isWriteMode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getExamplesMessages | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
getHelpUrls | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\Notifications\Api; |
4 | |
5 | use ApiBase; |
6 | use MediaWiki\Extension\Notifications\AttributeManager; |
7 | use MediaWiki\Extension\Notifications\Controller\NotificationController; |
8 | use MediaWiki\Extension\Notifications\DbFactory; |
9 | use MediaWiki\Extension\Notifications\NotifUser; |
10 | use MediaWiki\WikiMap\WikiMap; |
11 | use Wikimedia\ParamValidator\ParamValidator; |
12 | |
13 | class ApiEchoMarkRead extends ApiBase { |
14 | use ApiCrossWiki; |
15 | |
16 | public function execute() { |
17 | // To avoid API warning, register the parameter used to bust browser cache |
18 | $this->getMain()->getVal( '_' ); |
19 | |
20 | $user = $this->getUser(); |
21 | if ( !$user->isRegistered() ) { |
22 | $this->dieWithError( 'apierror-mustbeloggedin-generic', 'login-required' ); |
23 | } elseif ( DbFactory::newFromDefault()->isReadOnly() ) { |
24 | $this->dieReadOnly(); |
25 | } |
26 | |
27 | $notifUser = NotifUser::newFromUser( $user ); |
28 | |
29 | $params = $this->extractRequestParams(); |
30 | |
31 | // Mark as read/unread locally, if requested |
32 | if ( in_array( WikiMap::getCurrentWikiId(), $this->getRequestedWikis() ) ) { |
33 | // There is no need to trigger markRead if all notifications are read |
34 | if ( $notifUser->getLocalNotificationCount() > 0 ) { |
35 | if ( $params['list'] ) { |
36 | // Make sure there is a limit to the update |
37 | $notifUser->markRead( array_slice( $params['list'], 0, ApiBase::LIMIT_SML2 ) ); |
38 | // Mark all as read |
39 | } elseif ( $params['all'] ) { |
40 | $notifUser->markAllRead(); |
41 | // Mark all as read for sections |
42 | } elseif ( $params['sections'] ) { |
43 | $notifUser->markAllRead( $params['sections'] ); |
44 | } |
45 | } |
46 | |
47 | // Mark as unread |
48 | if ( $params['unreadlist'] !== null && $params['unreadlist'] !== [] ) { |
49 | // Make sure there is a limit to the update |
50 | $notifUser->markUnRead( array_slice( $params['unreadlist'], 0, ApiBase::LIMIT_SML2 ) ); |
51 | } |
52 | } |
53 | |
54 | $foreignResults = $this->getFromForeign(); |
55 | |
56 | $result = [ |
57 | 'result' => 'success' |
58 | ]; |
59 | |
60 | foreach ( $foreignResults as $wiki => $foreignResult ) { |
61 | if ( isset( $foreignResult['error'] ) ) { |
62 | $result['errors'][$wiki] = $foreignResult['error']; |
63 | } |
64 | } |
65 | |
66 | $rawCount = 0; |
67 | foreach ( AttributeManager::$sections as $section ) { |
68 | $rawSectionCount = $notifUser->getNotificationCount( $section ); |
69 | $result[$section]['rawcount'] = $rawSectionCount; |
70 | $result[$section]['count'] = NotificationController::formatNotificationCount( $rawSectionCount ); |
71 | $rawCount += $rawSectionCount; |
72 | } |
73 | |
74 | $result += [ |
75 | 'rawcount' => $rawCount, |
76 | 'count' => NotificationController::formatNotificationCount( $rawCount ), |
77 | ]; |
78 | |
79 | $this->getResult()->addValue( 'query', $this->getModuleName(), $result ); |
80 | } |
81 | |
82 | public function getAllowedParams() { |
83 | return $this->getCrossWikiParams() + [ |
84 | 'list' => [ |
85 | ParamValidator::PARAM_ISMULTI => true, |
86 | ], |
87 | 'unreadlist' => [ |
88 | ParamValidator::PARAM_ISMULTI => true, |
89 | ], |
90 | 'all' => [ |
91 | ParamValidator::PARAM_REQUIRED => false, |
92 | ParamValidator::PARAM_TYPE => 'boolean' |
93 | ], |
94 | 'sections' => [ |
95 | ParamValidator::PARAM_TYPE => AttributeManager::$sections, |
96 | ParamValidator::PARAM_ISMULTI => true, |
97 | ], |
98 | 'token' => [ |
99 | ParamValidator::PARAM_REQUIRED => true, |
100 | ] |
101 | ]; |
102 | } |
103 | |
104 | public function needsToken() { |
105 | return 'csrf'; |
106 | } |
107 | |
108 | public function mustBePosted() { |
109 | return true; |
110 | } |
111 | |
112 | public function isWriteMode() { |
113 | return true; |
114 | } |
115 | |
116 | /** |
117 | * @see ApiBase::getExamplesMessages() |
118 | * @return string[] |
119 | */ |
120 | protected function getExamplesMessages() { |
121 | return [ |
122 | 'action=echomarkread&list=8' |
123 | => 'apihelp-echomarkread-example-1', |
124 | 'action=echomarkread&all=true' |
125 | => 'apihelp-echomarkread-example-2', |
126 | 'action=echomarkread&unreadlist=1' |
127 | => 'apihelp-echomarkread-example-3', |
128 | ]; |
129 | } |
130 | |
131 | public function getHelpUrls() { |
132 | return 'https://www.mediawiki.org/wiki/Special:MyLanguage/Echo_(Notifications)/API'; |
133 | } |
134 | } |