summaryrefslogtreecommitdiff
blob: 54f7302cb80dc1d7faabd091c5cff97675c8e83d (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<?php
/*
 * Copyright (c) 2016 The MITRE Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

namespace MediaWiki\Extension\CommentStreams;

use Action;
use ConfigException;
use ExtensionRegistry;
use MWException;
use OutputPage;

class CommentStreamsHandler {
	const COMMENTS_ENABLED = 1;
	const COMMENTS_DISABLED = -1;
	const COMMENTS_INHERITED = 0;

	/**
	 * no CommentStreams flag
	 */
	private $areCommentsEnabled = self::COMMENTS_INHERITED;

	/**
	 * @var bool true if enabled due to wgCommentStreamsAllowedNamespaces
	 */
	private $isNamespaceEnabled = false;

	/**
	 * @var CommentStreamsFactory
	 */
	private $commentStreamsFactory;

	/**
	 * @var CommentStreamsStore
	 */
	private $commentStreamsStore;

	/**
	 * @var CommentStreamsEchoInterface
	 */
	private $echoInterface;

	/**
	 * @param CommentStreamsFactory $commentStreamsFactory
	 * @param CommentStreamsStore $commentStreamsStore
	 * @param CommentStreamsEchoInterface $echoInterface
	 */
	public function __construct(
		CommentStreamsFactory $commentStreamsFactory,
		CommentStreamsStore $commentStreamsStore,
		CommentStreamsEchoInterface $echoInterface
	) {
		$this->commentStreamsFactory = $commentStreamsFactory;
		$this->commentStreamsStore = $commentStreamsStore;
		$this->echoInterface = $echoInterface;
	}

	/**
	 * enables the display of comments on the current page
	 */
	public function enableCommentsOnPage() {
		$this->areCommentsEnabled = self::COMMENTS_ENABLED;
	}

	/**
	 * disables the display of comments on the current page
	 */
	public function disableCommentsOnPage() {
		$this->areCommentsEnabled = self::COMMENTS_DISABLED;
	}

	/**
	 * initially collapse CommentStreams flag
	 */
	private $initiallyCollapseCommentStreams = false;

	/**
	 * makes the comments appear initially collapsed when the current page
	 * is viewed
	 */
	public function initiallyCollapseCommentsOnPage() {
		$this->initiallyCollapseCommentStreams = true;
	}

	/**
	 * initializes the display of comments
	 *
	 * @param OutputPage $output OutputPage object
	 * @throws ConfigException
	 * @throws MWException
	 */
	public function init( OutputPage $output ) {
		if ( $this->checkDisplayComments( $output ) ) {
			$comments = $this->getComments( $output );
			$this->initJS( $output, $comments );
		}
	}

	/**
	 * checks to see if comments should be displayed on this page
	 *
	 * @param OutputPage $output the OutputPage object
	 * @return bool true if comments should be displayed on this page
	 * @throws ConfigException
	 */
	private function checkDisplayComments( OutputPage $output ) : bool {
		// don't display comments on this page if they are explicitly disabled
		if ( $this->areCommentsEnabled === self::COMMENTS_DISABLED ) {
			return false;
		}

		// don't display comments on any page action other than view action
		if ( Action::getActionName( $output->getContext() ) !== "view" ) {
			return false;
		}

		// if $wgCommentStreamsAllowedNamespaces is not set, display comments
		// in all content namespaces and if set to -1, don't display comments
		$config = $output->getConfig();
		$csAllowedNamespaces = $config->get( 'CommentStreamsAllowedNamespaces' );
		if ( $csAllowedNamespaces === null ) {
			$csAllowedNamespaces = $config->get( 'ContentNamespaces' );
		} elseif ( $csAllowedNamespaces === self::COMMENTS_DISABLED ) {
			return false;
		} elseif ( !is_array( $csAllowedNamespaces ) ) {
			$csAllowedNamespaces = [ $csAllowedNamespaces ];
		}

		$title = $output->getTitle();
		$namespace = $title->getNamespace();

		// don't display comments in CommentStreams namespace
		if ( $namespace === NS_COMMENTSTREAMS ) {
			return false;
		}

		// don't display comments on pages that do not exist
		if ( !$title->exists() ) {
			return false;
		}

		// don't display comments on redirect pages
		if ( $title->isRedirect() ) {
			return false;
		}

		// if this namespace is one of the explicitly allowed namespace, set flag to enable
		// default comment block
		if ( in_array( $namespace, $csAllowedNamespaces ) ) {
			$this->isNamespaceEnabled = true;
		}

		// display comments on this page if they are explicitly enabled
		if ( $this->areCommentsEnabled === self::COMMENTS_ENABLED ) {
			return true;
		}

		// don't display comments in a talk namespace unless:
		// 1) $wgCommentStreamsEnableTalk is true, OR
		// 2) the namespace is a talk namespace for a namespace in the array of
		// allowed namespaces
		// 3) comments have been explicitly enabled on that namespace with
		// <comment-streams/>
		if ( $title->isTalkPage() ) {
			$subject_namespace = CommentStreamsUtils::getSubjectNamespace( $namespace );
			if ( !$config->get( 'CommentStreamsEnableTalk' ) &&
				!in_array( $subject_namespace, $csAllowedNamespaces ) ) {
				return false;
			}
		}

		return $this->isNamespaceEnabled;
	}

	/**
	 * retrieve all comments for the current page
	 *
	 * @param OutputPage $output the OutputPage object for the current page
	 * @return Comment[] array of comments
	 * @throws MWException
	 * @throws ConfigException
	 */
	private function getComments( OutputPage $output ) : array {
		$commentData = [];
		$pageId = $output->getTitle()->getArticleID();
		$comment_page_ids = $this->commentStreamsStore->getAssociatedComments( $pageId );
		$allComments = [];
		foreach ( $comment_page_ids as $id ) {
			$wikipage = CommentStreamsUtils::newWikiPageFromId( $id );
			if ( $wikipage !== null ) {
				$comment = $this->commentStreamsFactory->newFromWikiPage( $wikipage );
				if ( $comment !== null ) {
					$allComments[] = $comment;
				}
			}
		}

		$config = $output->getConfig();
		$newestStreamsOnTop = $config->get( 'CommentStreamsNewestStreamsOnTop' );
		$votingEnabled = $config->get( 'CommentStreamsEnableVoting' );
		$parentComments = $this->getDiscussions(
			$allComments,
			$newestStreamsOnTop,
			$votingEnabled
		);
		foreach ( $parentComments as $parentComment ) {
			$parentJSON = $parentComment->getJSON( $output );
			if ( $votingEnabled ) {
				$parentJSON['vote'] = $parentComment->getVote( $output->getUser() );
			}
			if ( $this->echoInterface->isLoaded() ) {
				$parentJSON['watching'] = $parentComment->isWatching( $output->getUser() );
			}
			$childComments = $this->getReplies( $allComments, $parentComment->getId() );
			foreach ( $childComments as $childComment ) {
				$childJSON = $childComment->getJSON( $output );
				$parentJSON['children'][] = $childJSON;
			}
			$commentData[] = $parentJSON;
		}
		return $commentData;
	}

	/**
	 * initialize JavaScript
	 *
	 * @param OutputPage $output the OutputPage object
	 * @param Comment[] $comments array of comments on the current page
	 * @throws ConfigException
	 */
	private function initJS( OutputPage $output, array $comments ) {
		// determine if comments should be initially collapsed or expanded
		// if the namespace is a talk namespace, use state of its subject namespace
		$title = $output->getTitle();
		$namespace = $title->getNamespace();
		if ( $title->isTalkPage() ) {
			$namespace = CommentStreamsUtils::getSubjectNamespace( $namespace );
		}

		$config = $output->getConfig();

		if ( $this->initiallyCollapseCommentStreams ) {
			$initiallyCollapsed = true;
		} else {
			$initiallyCollapsedNamespaces =
				$config->get( 'CommentStreamsInitiallyCollapsedNamespaces' );
			$initiallyCollapsed = in_array( $namespace, $initiallyCollapsedNamespaces );
		}

		$canComment = true;
		if ( !CommentStreamsUtils::userHasRight( $output->getUser(), 'cs-comment' ) ) {
			$canComment = false;
		}

		$commentStreamsParams = [
			'canComment' => $canComment,
			'moderatorEdit' => CommentStreamsUtils::userHasRight( $output->getUser(),
				'cs-moderator-edit' ),
			'moderatorDelete' => CommentStreamsUtils::userHasRight( $output->getUser(),
				'cs-moderator-delete' ),
			'moderatorFastDelete' => $config->get( 'CommentStreamsModeratorFastDelete' ) ? 1 : 0,
			'showLabels' => $config->get( 'CommentStreamsShowLabels' ) ? 1 : 0,
			'newestStreamsOnTop' => $config->get( 'CommentStreamsNewestStreamsOnTop' ) ? 1 : 0,
			'initiallyCollapsed' => $initiallyCollapsed,
			'isNamespaceEnabled' => $this->isNamespaceEnabled,
			'enableVoting' => $config->get( 'CommentStreamsEnableVoting' ) ? 1 : 0,
			'enableWatchlist' => $this->echoInterface->isLoaded() ? 1 : 0,
			'comments' => $comments
		];
		$output->addJsConfigVars( 'CommentStreams', $commentStreamsParams );
		$output->addModules( 'ext.CommentStreams' );
		if ( ExtensionRegistry::getInstance()->isLoaded( 'VEForAll' ) ) {
			$output->addModules( 'ext.veforall.main' );
		}
	}

	/**
	 * return all discussions (top level comments) in an array of comments
	 *
	 * @param array $allComments an array of all comments on a page
	 * @param bool $newestOnTop true if array should be sorted from newest to
	 * @param bool $enableVoting
	 * @return array an array of all discussions
	 * oldest
	 */
	private function getDiscussions(
		array $allComments,
		bool $newestOnTop,
		bool $enableVoting
	) : array {
		$array = array_filter(
			$allComments, static function ( $comment ) {
				return $comment->getParentId() === null;
			}
		);
		usort( $array, function ( $comment1, $comment2 ) use ( $newestOnTop, $enableVoting ) {
			$date1 = $comment1->getCreationTimestamp()->timestamp;
			$date2 = $comment2->getCreationTimestamp()->timestamp;
			if ( $enableVoting ) {
				$upvotes1 = $this->commentStreamsStore->getNumUpVotes( $comment1->getId() );
				$downvotes1 = $this->commentStreamsStore->getNumDownVotes( $comment1->getId() );
				$votediff1 = $upvotes1 - $downvotes1;
				$upvotes2 = $this->commentStreamsStore->getNumUpVotes( $comment2->getId() );
				$downvotes2 = $this->commentStreamsStore->getNumDownVotes( $comment2->getId() );
				$votediff2 = $upvotes2 - $downvotes2;
				if ( $votediff1 === $votediff2 ) {
					if ( $upvotes1 === $upvotes2 ) {
						if ( $newestOnTop ) {
							return $date1 > $date2 ? -1 : 1;
						} else {
							return $date1 < $date2 ? -1 : 1;
						}
					} else {
						return $upvotes1 > $upvotes2 ? -1 : 1;
					}
				} else {
					return $votediff1 > $votediff2 ? -1 : 1;
				}
			} else {
				if ( $newestOnTop ) {
					return $date1 > $date2 ? -1 : 1;
				} else {
					return $date1 < $date2 ? -1 : 1;
				}
			}
		} );
		return $array;
	}

	/**
	 * return all replies for a given discussion in an array of comments
	 *
	 * @param array $allComments an array of all comments on a page
	 * @param int $parentId the page ID of the discussion to get replies for
	 * @return array an array of replies for the given discussion
	 */
	private function getReplies( array $allComments, int $parentId ) : array {
		$array = array_filter(
			$allComments, static function ( $comment ) use ( $parentId ) {
				if ( $comment->getParentId() === $parentId ) {
					return true;
				}
				return false;
			}
		);
		usort(
			$array, static function ( $comment1, $comment2 ) {
				$date1 = $comment1->getCreationTimestamp()->timestamp;
				$date2 = $comment2->getCreationTimestamp()->timestamp;
				return $date1 < $date2 ? -1 : 1;
			}
		);
		return $array;
	}
}