summaryrefslogtreecommitdiff
blob: 95f9b5dfd6a8d7b542c00d916a7d6d5177c0f615 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php

class ApiEchoUnreadNotificationPages extends ApiQueryBase {
	use ApiCrossWiki;

	/**
	 * @var bool
	 */
	protected $crossWikiSummary = false;

	/**
	 * @param ApiQuery $query
	 * @param string $moduleName
	 */
	public function __construct( $query, $moduleName ) {
		parent::__construct( $query, $moduleName, 'unp' );
	}

	/**
	 * @throws ApiUsageException
	 */
	public function execute() {
		// To avoid API warning, register the parameter used to bust browser cache
		$this->getMain()->getVal( '_' );

		if ( $this->getUser()->isAnon() ) {
			$this->dieWithError( 'apierror-mustbeloggedin-generic', 'login-required' );
		}

		$params = $this->extractRequestParams();

		$result = [];
		if ( in_array( wfWikiID(), $this->getRequestedWikis() ) ) {
			$result[wfWikiID()] = $this->getFromLocal( $params['limit'], $params['grouppages'] );
		}

		if ( $this->getRequestedForeignWikis() ) {
			$result += $this->getUnreadNotificationPagesFromForeign();
		}

		$apis = $this->getForeignNotifications()->getApiEndpoints( $this->getRequestedWikis() );
		foreach ( $result as $wiki => $data ) {
			$result[$wiki]['source'] = $apis[$wiki];
			$result[$wiki]['pages'] = $data['pages'] ?: [];
		}

		$this->getResult()->addValue( 'query', $this->getModuleName(), $result );
	}

	/**
	 * @param int $limit
	 * @param bool $groupPages
	 * @return array
	 * @phan-return array{pages:array[],totalCount:int}
	 */
	protected function getFromLocal( $limit, $groupPages ) {
		$attributeManager = EchoAttributeManager::newFromGlobalVars();
		$enabledTypes = $attributeManager->getUserEnabledEvents( $this->getUser(), 'web' );

		$dbr = MWEchoDbFactory::newFromDefault()->getEchoDb( DB_REPLICA );
		// If $groupPages is true, we need to fetch all pages and apply the ORDER BY and LIMIT ourselves
		// after grouping.
		$extraOptions = $groupPages ? [] : [ 'ORDER BY' => 'count DESC', 'LIMIT' => $limit ];
		$rows = $dbr->select(
			[ 'echo_event', 'echo_notification' ],
			[ 'event_page_id', 'count' => 'COUNT(*)' ],
			[
				'notification_user' => $this->getUser()->getId(),
				'notification_read_timestamp' => null,
				'event_deleted' => 0,
				'event_type' => $enabledTypes,
			],
			__METHOD__,
			[
				'GROUP BY' => 'event_page_id',
			] + $extraOptions,
			[ 'echo_notification' => [ 'INNER JOIN', 'notification_event = event_id' ] ]
		);

		if ( $rows === false ) {
			return [
				'pages' => [],
				'totalCount' => 0,
			];
		}

		$nullCount = 0;
		$pageCounts = [];
		foreach ( $rows as $row ) {
			if ( $row->event_page_id !== null ) {
				// @phan-suppress-next-line PhanTypeMismatchDimAssignment
				$pageCounts[$row->event_page_id] = intval( $row->count );
			} else {
				$nullCount = intval( $row->count );
			}
		}

		// @phan-suppress-next-line PhanTypeMismatchArgument
		$titles = Title::newFromIDs( array_keys( $pageCounts ) );

		$groupCounts = [];
		foreach ( $titles as $title ) {
			if ( $groupPages ) {
				// If $title is a talk page, add its count to its subject page's count
				$pageName = $title->getSubjectPage()->getPrefixedText();
			} else {
				$pageName = $title->getPrefixedText();
			}

			// @phan-suppress-next-line PhanTypeMismatchDimFetch
			$count = $pageCounts[$title->getArticleID()];
			if ( isset( $groupCounts[$pageName] ) ) {
				$groupCounts[$pageName] += $count;
			} else {
				$groupCounts[$pageName] = $count;
			}
		}

		$userPageName = $this->getUser()->getUserPage()->getPrefixedText();
		if ( $nullCount > 0 && $groupPages ) {
			// Add the count for NULL (not associated with any page) to the count for the user page
			if ( isset( $groupCounts[$userPageName] ) ) {
				$groupCounts[$userPageName] += $nullCount;
			} else {
				$groupCounts[$userPageName] = $nullCount;
			}
		}

		arsort( $groupCounts );
		if ( $groupPages ) {
			$groupCounts = array_slice( $groupCounts, 0, $limit );
		}

		$result = [];
		foreach ( $groupCounts as $pageName => $count ) {
			if ( $groupPages ) {
				$title = Title::newFromText( $pageName );
				$pages = [ $title->getSubjectPage()->getPrefixedText() ];
				if ( $title->canHaveTalkPage() ) {
					$pages[] = $title->getTalkPage()->getPrefixedText();
				}
				if ( $pageName === $userPageName ) {
					$pages[] = null;
				}
				$pageDescription = [
					'ns' => $title->getNamespace(),
					'title' => $title->getPrefixedText(),
					'unprefixed' => $title->getText(),
					'pages' => $pages,
				];
			} else {
				$pageDescription = [ 'title' => $pageName ];
			}
			$result[] = $pageDescription + [
				'count' => $count,
			];
		}
		if ( !$groupPages && $nullCount > 0 ) {
			$result[] = [
				'title' => null,
				'count' => $nullCount,
			];
		}

		return [
			'pages' => $result,
			'totalCount' => MWEchoNotifUser::newFromUser( $this->getUser() )->getLocalNotificationCount(),
		];
	}

	/**
	 * @return array[]
	 */
	protected function getUnreadNotificationPagesFromForeign() {
		$result = [];
		foreach ( $this->getFromForeign() as $wiki => $data ) {
			$result[$wiki] = $data['query'][$this->getModuleName()][$wiki];
		}

		return $result;
	}

	/**
	 * @return array[]
	 */
	public function getAllowedParams() {
		global $wgEchoMaxUpdateCount;

		return $this->getCrossWikiParams() + [
			'grouppages' => [
				ApiBase::PARAM_TYPE => 'boolean',
				ApiBase::PARAM_DFLT => false,
			],
			'limit' => [
				ApiBase::PARAM_TYPE => 'limit',
				ApiBase::PARAM_DFLT => 10,
				ApiBase::PARAM_MIN => 1,
				ApiBase::PARAM_MAX => $wgEchoMaxUpdateCount,
				ApiBase::PARAM_MAX2 => $wgEchoMaxUpdateCount,
			],
			// there is no `offset` or `continue` value: the set of possible
			// notifications is small enough to allow fetching all of them at
			// once, and any sort of fetching would be unreliable because
			// they're sorted based on count of notifications, which could
			// change in between requests
		];
	}

	/**
	 * @see ApiBase::getExamplesMessages()
	 * @return string[]
	 */
	protected function getExamplesMessages() {
		return [
			'action=query&meta=unreadnotificationpages' => 'apihelp-query+unreadnotificationpages-example-1',
		];
	}

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