summaryrefslogtreecommitdiff
blob: 4769a4ca283f1d670d8e69fc4c3dacc4f7894205 (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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
<?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 ConfigException;
use FatalError;
use Html;
use IContextSource;
use MediaWiki\Linker\LinkRenderer;
use MediaWiki\User\UserIdentity;
use MWException;
use MWTimestamp;
use PageProps;
use Title;
use User;
use Wikimedia\Assert\Assert;
use WikiPage;

class Comment {
	public const CONSTRUCTOR_OPTIONS = [
		'CommentStreamsUserAvatarPropertyName',
		'CommentStreamsUserRealNamePropertyName',
		'CommentStreamsEnableVoting'
	];

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

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

	/**
	 * @var CommentStreamsSMWInterface
	 */
	private $smwInterface;

	/**
	 * @var CommentStreamsSocialProfileInterface
	 */
	private $socialProfileInterface;

	/**
	 * @var LinkRenderer
	 */
	private $linkRenderer;

	/**
	 * @var string
	 */
	private $userAvatarPropertyName;

	/**
	 * @var string
	 */
	private $userRealNamePropertyName;

	/**
	 * @var mixed
	 */
	private $enableVoting;

	/**
	 * wiki page object for this comment wiki page
	 * @var WikiPage
	 */
	private $wikipage;

	/**
	 * unique id to identify comment block in a page
	 * @var ?string
	 */
	private $comment_block_id;

	/**
	 * page ID for the wiki page this comment is on
	 * @var int
	 */
	private $assoc_page_id;

	/**
	 * page ID for the wiki page this comment is in reply to or null
	 * @var ?int
	 */
	private $parent_page_id;

	/**
	 * title of comment
	 * @var ?string
	 */
	private $comment_title;

	/**
	 * wikitext of comment
	 * @var ?string
	 */
	private $wikitext;

	/**
	 * number of replies to this comment
	 * @var ?int
	 */
	private $num_replies;

	/**
	 * user object for the author of this comment
	 * @var User
	 */
	private $author;

	/**
	 * user object for the last editor of this comment
	 * @var ?UserIdentity
	 */
	private $lastEditor;

	/**
	 * Avatar for author of this comment
	 * @var ?string
	 */
	private $avatar;

	/**
	 * @var array
	 */
	private static $avatarCache = [];

	/**
	 * the earliest revision date for this comment
	 * @var ?MWTimestamp
	 */
	private $creation_timestamp;

	/**
	 * the latest revision date for this comment
	 * @var ?MWTimestamp
	 */
	private $modification_timestamp;

	/**
	 * Do not instantiate directly. Use CommentStreamsFactory instead.
	 * @param \MediaWiki\Config\ServiceOptions|\Config $options
	 * @param CommentStreamsStore $commentStreamsStore
	 * @param CommentStreamsEchoInterface $echoInterface
	 * @param CommentStreamsSMWInterface $smwInterface
	 * @param CommentStreamsSocialProfileInterface $socialProfileInterface
	 * @param LinkRenderer $linkRenderer
	 * @param WikiPage $wikipage WikiPage object corresponding to comment page
	 * @param ?string $comment_block_id
	 * @param int $assoc_page_id
	 * @param ?int $parent_page_id
	 * @param ?string $comment_title
	 * @param string $wikitext
	 * @throws ConfigException
	 */
	public function __construct(
		$options,
		CommentStreamsStore $commentStreamsStore,
		CommentStreamsEchoInterface $echoInterface,
		CommentStreamsSMWInterface $smwInterface,
		CommentStreamsSocialProfileInterface $socialProfileInterface,
		LinkRenderer $linkRenderer,
		WikiPage $wikipage,
		?string $comment_block_id,
		int $assoc_page_id,
		?int $parent_page_id,
		?string $comment_title,
		string $wikitext
	) {
		if ( class_exists( '\MediaWiki\Config\ServiceOptions' ) ) {
			$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
		}
		$this->userAvatarPropertyName = $options->get( 'CommentStreamsUserAvatarPropertyName' );
		$this->userRealNamePropertyName = $options->get( 'CommentStreamsUserRealNamePropertyName' );
		$this->enableVoting = (bool)$options->get( 'CommentStreamsEnableVoting' );
		$this->commentStreamsStore = $commentStreamsStore;
		$this->echoInterface = $echoInterface;
		$this->smwInterface = $smwInterface;
		$this->socialProfileInterface = $socialProfileInterface;
		$this->linkRenderer = $linkRenderer;
		$this->wikipage = $wikipage;
		$this->comment_block_id = $comment_block_id;
		$this->assoc_page_id = $assoc_page_id;
		$this->parent_page_id = $parent_page_id;
		$this->comment_title = $comment_title;
		$this->wikitext = $wikitext;
		$this->num_replies = $commentStreamsStore->getNumReplies( $wikipage->getId() );
		$title = $wikipage->getTitle();
		$user = CommentStreamsUtils::getAuthor( $title );
		if ( $user !== null ) {
			$this->author = $user;
		}
		$this->setAvatar();
		$this->lastEditor = CommentStreamsUtils::getLastEditor( $wikipage ) ?: $this->author;
		$timestamp = CommentStreamsUtils::getCreationTimestamp( $title );
		if ( $timestamp !== null ) {
			$this->creation_timestamp = MWTimestamp::getLocalInstance( $timestamp );
		}
		$this->setModificationTimestamp();
	}

	/**
	 * @return int page ID of the comment's wiki page
	 */
	public function getId() : int {
		return $this->wikipage->getId();
	}

	/**
	 * @return Title Title object associated with this comment page
	 */
	public function getTitle() : Title {
		return $this->wikipage->getTitle();
	}

	/**
	 * @return ?string comment block id
	 */
	public function getBlockId() : ?string {
		return $this->comment_block_id;
	}

	/**
	 * @return int page ID for the wiki page this comment is on
	 */
	public function getAssociatedId() : int {
		return $this->assoc_page_id;
	}

	/**
	 * @return int|null page ID for the wiki page this comment is in reply to or
	 * null if this comment is a discussion, not a reply
	 */
	public function getParentId() : ?int {
		return $this->parent_page_id;
	}

	/**
	 * @return ?string the title of the comment
	 */
	public function getCommentTitle() : ?string {
		return $this->comment_title;
	}

	/**
	 * @return string wikitext of the comment
	 */
	public function getWikiText() : string {
		return $this->wikitext;
	}

	/**
	 * @param IContextSource $context
	 * @return string parsed HTML of the comment
	 */
	public function getHTML( IContextSource $context ) : string {
		return CommentStreamsUtils::parse( $this->wikitext, $this->wikipage, $context );
	}

	/**
	 * @return int number of replies
	 */
	public function getNumReplies() : int {
		return $this->num_replies;
	}

	/**
	 * @return ?User the author of this comment
	 */
	public function getAuthor() : ?User {
		return $this->author;
	}

	/**
	 * @return string username of the author of this comment
	 */
	public function getUsername() : string {
		return $this->author->getName();
	}

	/**
	 * @return string display name of the author of this comment linked to
	 * the user's user page if it exists
	 */
	public function getUserDisplayName() : string {
		return $this->getDisplayNameFromUser( $this->author, true );
	}

	/**
	 * @return string display name of the author of this comment
	 */
	public function getUserDisplayNameUnlinked() : string {
		return $this->getDisplayNameFromUser( $this->author, false );
	}

	/**
	 * @return UserIdentity the last editor of this comment
	 */
	public function getLastEditor() : UserIdentity {
		return $this->lastEditor;
	}

	/**
	 * @return bool true if the last edit to this comment was not done by the
	 * original author
	 */
	public function isLastEditModerated() : bool {
		return $this->author->getId() !== $this->lastEditor->getId();
	}

	/**
	 * @return MWTimestamp
	 */
	public function getCreationTimestamp() : MWTimestamp {
		return $this->creation_timestamp;
	}

	/**
	 * @return string
	 */
	public function getCreationDate() : string {
		return $this->creation_timestamp->format( "M j \a\\t g:i a" );
	}

	/**
	 * @return ?string
	 */
	public function getModificationDate() : ?string {
		return $this->modification_timestamp ?
			$this->modification_timestamp->format( "M j \a\\t g:i a" ) : null;
	}

	/**
	 * @param IContextSource $context
	 * @return array get comment data in array suitable for JSON
	 */
	public function getJSON( IContextSource $context ) : array {
		$json = [
			'pageid' => $this->wikipage->getId(),
			'commentblockid' => $this->comment_block_id,
			'associatedid' => $this->assoc_page_id,
			'parentid' => $this->parent_page_id,
			'commenttitle' => $this->comment_title,
			'wikitext' => htmlentities( $this->wikitext ),
			'html' => $this->getHTML( $context ),
			'username' => $this->getUsername(),
			'numreplies' => $this->num_replies,
			'userdisplayname' => $this->getUserDisplayName(),
			'avatar' => $this->avatar,
			'moderated' => $this->isLastEditModerated() ? "moderated" : null,
			'created' => $this->getCreationDate(),
			'created_timestamp' => $this->creation_timestamp->format( "U" ),
			'modified' => $this->getModificationDate()
		];

		$user = $context->getUser();
		if ( $this->parent_page_id === null ) {
			if ( $this->enableVoting ) {
				$json['numupvotes'] = $this->commentStreamsStore->getNumUpVotes( $this->getId() );
				$json['numdownvotes'] =
					$this->commentStreamsStore->getNumDownVotes( $this->getId() );
				$json['vote'] = $this->getVote( $user );
			}
			if ( $this->echoInterface->isLoaded() ) {
				$json['watching'] = $this->isWatching( $user ) ? 1 : 0;
			}
		}

		return $json;
	}

	/**
	 * record a vote
	 *
	 * @param string $vote 1 for up vote, -1 for down vote, 0 for no vote
	 * @param User $user the user voting on the comment
	 * @return bool database status code
	 */
	public function vote( string $vote, User $user ) : bool {
		Assert::parameter( $vote === "-1" || $vote === "0" || $vote === "1", '$vote',
			'must be "-1", "0", or "1"' );
		$result = $this->commentStreamsStore->vote( (int)$vote, $this->getId(), $user->getId() );
		$this->smwInterface->update( $this->getTitle() );
		return $result;
	}

	/**
	 * @param User $user
	 * @return int
	 */
	public function getVote( User $user ) : int {
		return $this->commentStreamsStore->getVote( $this->getId(), $user->getId() );
	}

	/**
	 * @param int $user_id
	 * @return bool
	 */
	public function watch( int $user_id ) : bool {
		return $this->commentStreamsStore->watch( $this->getId(), $user_id );
	}

	/**
	 * @param int $user_id
	 * @return bool
	 */
	public function unwatch( int $user_id ) : bool {
		return $this->commentStreamsStore->unwatch( $this->getId(), $user_id );
	}

	/**
	 * @param User $user
	 * @return bool
	 */
	public function isWatching( User $user ) : bool {
		return $this->commentStreamsStore->isWatching( $this->getId(), $user->getId() );
	}

	/**
	 * update comment in database
	 * NOTE: since only head comments can contain a comment title,
	 * $comment_title may only be non null if this comment has a null parent id
	 * and vice versa
	 *
	 * @param ?string $comment_title the new title for the comment
	 * @param string $wikitext the wikitext to add
	 * @param User $user the author of the edit
	 * @return bool true if successful
	 * @throws MWException
	 */
	public function update(
		?string $comment_title,
		string $wikitext,
		User $user
	) : bool {
		Assert::parameter(
			( $comment_title === null && $this->parent_page_id !== null ) ||
			( $comment_title !== null && $this->parent_page_id === null ),
			'$comment_title',
			'must be null if parent page ID is non-null or non-null if parent page ID is null'
		);
		$result = $this->commentStreamsStore->updateComment(
			$this->wikipage,
			$comment_title,
			$wikitext,
			$user
		);
		if ( !$result ) {
			return false;
		}
		$this->comment_title = $comment_title;
		$this->wikitext = $wikitext;
		$this->modification_timestamp = null;
		$wikipage = CommentStreamsUtils::newWikiPageFromId( $this->wikipage->getId(),
			'fromdbmaster' );
		if ( $wikipage !== null ) {
			$this->wikipage = $wikipage;
		}
		if ( $this->parent_page_id === null ) {
			$this->smwInterface->update( $this->getTitle() );
		}
		return true;
	}

	/**
	 * delete comment from database
	 *
	 * @param User $deleter
	 * @return bool true if successful
	 * @throws FatalError
	 * @throws MWException
	 */
	public function delete( User $deleter ) : bool {
		return $this->commentStreamsStore->deleteComment( $this->wikipage, $deleter );
	}

	private function setAvatar() {
		if ( array_key_exists( $this->author->getId(), self::$avatarCache ) ) {
			$this->avatar = self::$avatarCache[ $this->author->getId() ];
			return;
		}

		$this->avatar = $this->socialProfileInterface->getAvatar( $this->author );

		if ( $this->avatar === null && $this->userAvatarPropertyName !== null ) {
			$title = $this->smwInterface->getUserProperty( $this->author,
				$this->userAvatarPropertyName );
			if ( $title !== null ) {
				if ( is_string( $title ) ) {
					$title = Title::newFromText( $title );
				}
				if ( $title->isKnown() && $title->getNamespace() === NS_FILE ) {
					$file = CommentStreamsUtils::findFile( $title->getText() );
					if ( $file ) {
						$this->avatar = $file->createThumb( 48, 48 );
					}
				}
			}
		}

		self::$avatarCache[ $this->author->getId() ] = $this->avatar;
	}

	private function setModificationTimestamp() {
		$title = $this->wikipage->getTitle();
		$firstRevisionId = CommentStreamsUtils::getFirstRevisionId( $title );
		if ( $firstRevisionId === null ) {
			return;
		}

		$latestRevisionId = $title->getLatestRevId();
		if ( $firstRevisionId === $latestRevisionId ) {
			return;
		}

		$timestamp = CommentStreamsUtils::getTimestampFromId( $latestRevisionId, $title );
		if ( $timestamp !== false ) {
			$this->modification_timestamp = MWTimestamp::getLocalInstance( $timestamp );
		}
	}

	/**
	 * return the text to use to represent the user at the top of a comment
	 *
	 * @param User $user the user
	 * @param bool $linked whether to link the display name to the user page,
	 *        if it exists
	 * @return string display name for user
	 */
	private function getDisplayNameFromUser(
		User $user,
		bool $linked
	) : string {
		if ( $user->isAnon() ) {
			return Html::openElement( 'span', [
					'class' => 'cs-comment-author-anonymous'
				] )
				. wfMessage( 'commentstreams-author-anonymous' )
				. Html::closeElement( 'span' );
		}
		$userpage = $user->getUserPage();
		$displayname = null;
		if ( $this->userRealNamePropertyName !== null ) {
			$displayname = $this->smwInterface->getUserProperty(
				$user,
				$this->userRealNamePropertyName
			);
		}
		if ( $displayname === null || strlen( $displayname ) == 0 ) {
			$values = PageProps::getInstance()->getProperties( $userpage,
				'displaytitle' );
			if ( array_key_exists( $userpage->getArticleID(), $values ) ) {
				$displayname = $values[$userpage->getArticleID()];
			}
		}
		if ( $displayname === null || strlen( $displayname ) == 0 ) {
			$displayname = $user->getRealName();
		}
		if ( $displayname === null || strlen( $displayname ) == 0 ) {
			$displayname = $user->getName();
		}
		if ( $linked && $userpage->exists() ) {
			$displayname = $this->linkRenderer->makeLink( $userpage, $displayname );
		}
		return $displayname;
	}
}