summaryrefslogtreecommitdiff
blob: 1a70f236b728eb2ca856b18af3b08f30a0813689 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?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
		if ( $notifUser->getNotificationCount() > 0 ) {
			if ( count( $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'] );
			}
		}

		$rawCount = $notifUser->getNotificationCount();

		$result = array(
			'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(
			'rawcount' => $rawCount,
			'count' => EchoNotificationController::formatNotificationCount( $rawCount ),
		);
		$this->getResult()->addValue( 'query', $this->getModuleName(), $result );
	}

	public function getAllowedParams() {
		return array(
			'list' => array(
				ApiBase::PARAM_ISMULTI => true,
			),
			'all' => array(
				ApiBase::PARAM_REQUIRED => false,
				ApiBase::PARAM_TYPE => 'boolean'
			),
			'sections' => array(
				ApiBase::PARAM_TYPE => EchoAttributeManager::$sections,
				ApiBase::PARAM_ISMULTI => true,
			),
			'token' => array(
				ApiBase::PARAM_REQUIRED => true,
			),
		);
	}

	/**
	 * @deprecated since MediaWiki core 1.25
	 */
	public function getParamDescription() {
		return array(
			'list' => 'A list of notification IDs to mark as read',
			'all' => "If set to true, marks all of a user's notifications as read",
			'sections' => 'A list of sections to mark as read',
			'token' => 'edit token',
		);
	}

	public function needsToken() {
		return 'csrf';
	}

	public function getTokenSalt() {
		return '';
	}

	public function mustBePosted() {
		return true;
	}

	public function isWriteMode() {
		return true;
	}

	/**
	 * @deprecated since MediaWiki core 1.25
	 */
	public function getDescription() {
		return 'Mark notifications as read for the current user';
	}

	/**
	 * @deprecated since MediaWiki core 1.25
	 */
	public function getExamples() {
		return array(
			'api.php?action=echomarkread&list=8',
			'api.php?action=echomarkread&all=true'
		);
	}

	/**
	 * @see ApiBase::getExamplesMessages()
	 */
	protected function getExamplesMessages() {
		return array(
			'action=echomarkread&list=8'
				=> 'apihelp-echomarkread-example-1',
			'action=echomarkread&all=true'
				=> 'apihelp-echomarkread-example-2',
		);
	}

	public function getHelpUrls() {
		return 'https://www.mediawiki.org/wiki/Echo_(Notifications)/API';
	}
}