| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Watchlist; |
| 24 |
|
| 25 |
use MediaWiki\HookContainer\HookContainer; |
| 26 |
use MediaWiki\HookContainer\HookRunner; |
| 27 |
use MediaWiki\Linker\LinkTarget; |
| 28 |
use MediaWiki\Page\PageIdentity; |
| 29 |
use MediaWiki\Page\PageReference; |
| 30 |
use MediaWiki\Page\WikiPageFactory; |
| 31 |
use MediaWiki\Permissions\Authority; |
| 32 |
use MediaWiki\Revision\RevisionLookup; |
| 33 |
use MediaWiki\Revision\RevisionRecord; |
| 34 |
use MediaWiki\Status\Status; |
| 35 |
use MediaWiki\Title\NamespaceInfo; |
| 36 |
use MediaWiki\User\TalkPageNotificationManager; |
| 37 |
use MediaWiki\User\User; |
| 38 |
use MediaWiki\User\UserFactory; |
| 39 |
use MediaWiki\User\UserIdentity; |
| 40 |
use StatusValue; |
| 41 |
use Wikimedia\ParamValidator\TypeDef\ExpiryDef; |
| 42 |
use Wikimedia\Rdbms\ReadOnlyMode; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
class WatchlistManager { |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public const OPTION_ENOTIF = 'isEnotifEnabled'; |
| 55 |
|
| 56 |
|
| 57 |
private $isEnotifEnabled; |
| 58 |
|
| 59 |
|
| 60 |
private $hookRunner; |
| 61 |
|
| 62 |
|
| 63 |
private $readOnlyMode; |
| 64 |
|
| 65 |
|
| 66 |
private $revisionLookup; |
| 67 |
|
| 68 |
|
| 69 |
private $talkPageNotificationManager; |
| 70 |
|
| 71 |
|
| 72 |
private $watchedItemStore; |
| 73 |
|
| 74 |
|
| 75 |
private $userFactory; |
| 76 |
|
| 77 |
|
| 78 |
private $nsInfo; |
| 79 |
|
| 80 |
|
| 81 |
private $wikiPageFactory; |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
⧽` |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
key⧽` |
| 95 |
|
| 96 |
|
| 97 |
tamp or false⧽ |
| 98 |
|
| 99 |
private $notificationTimestampCache = []; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public function __construct( |
| 113 |
array $options, |
| 114 |
HookContainer $hookContainer, |
| 115 |
ReadOnlyMode $readOnlyMode, |
| 116 |
RevisionLookup $revisionLookup, |
| 117 |
TalkPageNotificationManager $talkPageNotificationManager, |
| 118 |
WatchedItemStoreInterface $watchedItemStore, |
| 119 |
UserFactory $userFactory, |
| 120 |
NamespaceInfo $nsInfo, |
| 121 |
WikiPageFactory $wikiPageFactory |
| 122 |
) { |
| 123 |
$this->isEnotifEnabled = $options[ self::OPTION_ENOTIF ]; |
| 124 |
$this->hookRunner = new HookRunner( $hookContainer ); |
| 125 |
$this->readOnlyMode = $readOnlyMode; |
| 126 |
$this->revisionLookup = $revisionLookup; |
| 127 |
$this->talkPageNotificationManager = $talkPageNotificationManager; |
| 128 |
$this->watchedItemStore = $watchedItemStore; |
| 129 |
$this->userFactory = $userFactory; |
| 130 |
$this->nsInfo = $nsInfo; |
| 131 |
$this->wikiPageFactory = $wikiPageFactory; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public function clearAllUserNotifications( $performer ) { |
| 144 |
if ( $this->readOnlyMode->isReadOnly() ) { |
| 145 |
|
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
if ( !$performer instanceof Authority ) { |
| 150 |
$performer = $this->userFactory->newFromUserIdentity( $performer ); |
| 151 |
} |
| 152 |
|
| 153 |
$user = $performer->getUser(); |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
if ( !$this->isEnotifEnabled ) { |
| 158 |
$this->talkPageNotificationManager->removeUserHasNewMessages( $user ); |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
if ( !$performer->isAllowed( 'editmywatchlist' ) ) { |
| 163 |
|
| 164 |
return; |
| 165 |
} |
| 166 |
|
| 167 |
if ( !$user->isRegistered() ) { |
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
$this->watchedItemStore->resetAllNotificationTimestampsForUser( $user ); |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public function clearTitleUserNotifications( |
| 191 |
$performer, |
| 192 |
$title, |
| 193 |
int $oldid = 0, |
| 194 |
?RevisionRecord $oldRev = null |
| 195 |
) { |
| 196 |
if ( $this->readOnlyMode->isReadOnly() ) { |
| 197 |
|
| 198 |
return; |
| 199 |
} |
| 200 |
|
| 201 |
if ( !$performer instanceof Authority ) { |
| 202 |
$performer = $this->userFactory->newFromUserIdentity( $performer ); |
| 203 |
} |
| 204 |
|
| 205 |
$userIdentity = $performer->getUser(); |
| 206 |
$userTalkPage = ( |
| 207 |
$title->getNamespace() === NS_USER_TALK && |
| 208 |
$title->getDBkey() === strtr( $userIdentity->getName(), ' ', '_' ) |
| 209 |
); |
| 210 |
|
| 211 |
if ( $userTalkPage ) { |
| 212 |
if ( !$oldid ) { |
| 213 |
$oldRev = null; |
| 214 |
} elseif ( !$oldRev ) { |
| 215 |
$oldRev = $this->revisionLookup->getRevisionById( $oldid ); |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
$this->talkPageNotificationManager->clearForPageView( $userIdentity, $oldRev ); |
| 221 |
} |
| 222 |
|
| 223 |
if ( !$this->isEnotifEnabled ) { |
| 224 |
return; |
| 225 |
} |
| 226 |
|
| 227 |
if ( !$userIdentity->isRegistered() ) { |
| 228 |
|
| 229 |
return; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
if ( !$performer->isAllowed( 'editmywatchlist' ) ) { |
| 236 |
|
| 237 |
return; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
$force = $userTalkPage ? 'force' : ''; |
| 245 |
$this->watchedItemStore->resetNotificationTimestamp( $userIdentity, $title, $force, $oldid ); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
public function getTitleNotificationTimestamp( UserIdentity $user, $title ) { |
| 256 |
if ( !$user->isRegistered() ) { |
| 257 |
return false; |
| 258 |
} |
| 259 |
|
| 260 |
$cacheKey = 'u' . $user->getId() . '-' . |
| 261 |
$title->getNamespace() . ':' . $title->getDBkey(); |
| 262 |
|
| 263 |
|
| 264 |
if ( array_key_exists( $cacheKey, $this->notificationTimestampCache ) ) { |
| 265 |
return $this->notificationTimestampCache[ $cacheKey ]; |
| 266 |
} |
| 267 |
|
| 268 |
$watchedItem = $this->watchedItemStore->getWatchedItem( $user, $title ); |
| 269 |
if ( $watchedItem ) { |
| 270 |
$timestamp = $watchedItem->getNotificationTimestamp(); |
| 271 |
} else { |
| 272 |
$timestamp = false; |
| 273 |
} |
| 274 |
|
| 275 |
$this->notificationTimestampCache[ $cacheKey ] = $timestamp; |
| 276 |
return $timestamp; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
public function isWatchable( PageReference $target ): bool { |
| 285 |
if ( !$this->nsInfo->isWatchable( $target->getNamespace() ) ) { |
| 286 |
return false; |
| 287 |
} |
| 288 |
|
| 289 |
if ( $target instanceof PageIdentity && !$target->canExist() ) { |
| 290 |
|
| 291 |
return false; |
| 292 |
} |
| 293 |
|
| 294 |
return true; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
public function isWatchedIgnoringRights( UserIdentity $userIdentity, PageIdentity $target ): bool { |
| 305 |
if ( $this->isWatchable( $target ) ) { |
| 306 |
return $this->watchedItemStore->isWatched( $userIdentity, $target ); |
| 307 |
} |
| 308 |
return false; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
public function isWatched( Authority $performer, PageIdentity $target ): bool { |
| 320 |
if ( $performer->isAllowed( 'viewmywatchlist' ) ) { |
| 321 |
return $this->isWatchedIgnoringRights( $performer->getUser(), $target ); |
| 322 |
} |
| 323 |
return false; |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
public function isTempWatchedIgnoringRights( UserIdentity $userIdentity, PageIdentity $target ): bool { |
| 334 |
if ( $this->isWatchable( $target ) ) { |
| 335 |
return $this->watchedItemStore->isTempWatched( $userIdentity, $target ); |
| 336 |
} |
| 337 |
return false; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
public function isTempWatched( Authority $performer, PageIdentity $target ): bool { |
| 349 |
if ( $performer->isAllowed( 'viewmywatchlist' ) ) { |
| 350 |
return $this->isTempWatchedIgnoringRights( $performer->getUser(), $target ); |
| 351 |
} |
| 352 |
return false; |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
public function addWatchIgnoringRights( |
| 365 |
UserIdentity $userIdentity, |
| 366 |
PageIdentity $target, |
| 367 |
?string $expiry = null |
| 368 |
): StatusValue { |
| 369 |
if ( !$this->isWatchable( $target ) ) { |
| 370 |
return StatusValue::newFatal( 'watchlistnotwatchable' ); |
| 371 |
} |
| 372 |
|
| 373 |
$wikiPage = $this->wikiPageFactory->newFromTitle( $target ); |
| 374 |
$title = $wikiPage->getTitle(); |
| 375 |
|
| 376 |
|
| 377 |
$status = Status::newFatal( 'hookaborted' ); |
| 378 |
$user = $this->userFactory->newFromUserIdentity( $userIdentity ); |
| 379 |
if ( $this->hookRunner->onWatchArticle( $user, $wikiPage, $status, $expiry ) ) { |
| 380 |
$status = StatusValue::newGood(); |
| 381 |
$this->watchedItemStore->addWatch( $userIdentity, $this->nsInfo->getSubjectPage( $title ), $expiry ); |
| 382 |
if ( $this->nsInfo->canHaveTalkPage( $title ) ) { |
| 383 |
$this->watchedItemStore->addWatch( $userIdentity, $this->nsInfo->getTalkPage( $title ), $expiry ); |
| 384 |
} |
| 385 |
$this->hookRunner->onWatchArticleComplete( $user, $wikiPage ); |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
$user->invalidateCache(); |
| 390 |
|
| 391 |
return $status; |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
public function addWatch( |
| 405 |
Authority $performer, |
| 406 |
PageIdentity $target, |
| 407 |
?string $expiry = null |
| 408 |
): StatusValue { |
| 409 |
if ( !$performer->isAllowed( 'editmywatchlist' ) ) { |
| 410 |
|
| 411 |
return User::newFatalPermissionDeniedStatus( 'editmywatchlist' ); |
| 412 |
} |
| 413 |
|
| 414 |
return $this->addWatchIgnoringRights( $performer->getUser(), $target, $expiry ); |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
public function removeWatchIgnoringRights( |
| 425 |
UserIdentity $userIdentity, |
| 426 |
PageIdentity $target |
| 427 |
): StatusValue { |
| 428 |
if ( !$this->isWatchable( $target ) ) { |
| 429 |
return StatusValue::newFatal( 'watchlistnotwatchable' ); |
| 430 |
} |
| 431 |
|
| 432 |
$wikiPage = $this->wikiPageFactory->newFromTitle( $target ); |
| 433 |
$title = $wikiPage->getTitle(); |
| 434 |
|
| 435 |
|
| 436 |
$status = Status::newFatal( 'hookaborted' ); |
| 437 |
$user = $this->userFactory->newFromUserIdentity( $userIdentity ); |
| 438 |
if ( $this->hookRunner->onUnwatchArticle( $user, $wikiPage, $status ) ) { |
| 439 |
$status = StatusValue::newGood(); |
| 440 |
$this->watchedItemStore->removeWatch( $userIdentity, $this->nsInfo->getSubjectPage( $title ) ); |
| 441 |
if ( $this->nsInfo->canHaveTalkPage( $title ) ) { |
| 442 |
$this->watchedItemStore->removeWatch( $userIdentity, $this->nsInfo->getTalkPage( $title ) ); |
| 443 |
} |
| 444 |
$this->hookRunner->onUnwatchArticleComplete( $user, $wikiPage ); |
| 445 |
} |
| 446 |
|
| 447 |
|
| 448 |
$user->invalidateCache(); |
| 449 |
|
| 450 |
return $status; |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
public function removeWatch( |
| 462 |
Authority $performer, |
| 463 |
PageIdentity $target |
| 464 |
): StatusValue { |
| 465 |
if ( !$performer->isAllowed( 'editmywatchlist' ) ) { |
| 466 |
|
| 467 |
return User::newFatalPermissionDeniedStatus( 'editmywatchlist' ); |
| 468 |
} |
| 469 |
|
| 470 |
return $this->removeWatchIgnoringRights( $performer->getUser(), $target ); |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
public function setWatch( |
| 487 |
bool $watch, |
| 488 |
Authority $performer, |
| 489 |
PageIdentity $target, |
| 490 |
?string $expiry = null |
| 491 |
): StatusValue { |
| 492 |
|
| 493 |
if ( !$performer->getUser()->isRegistered() || $performer->isTemp() ) { |
| 494 |
return StatusValue::newGood(); |
| 495 |
} |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
$oldWatchedItem = $this->watchedItemStore->getWatchedItem( $performer->getUser(), $target ); |
| 501 |
$changingWatchStatus = (bool)$oldWatchedItem !== $watch; |
| 502 |
if ( $oldWatchedItem && $expiry !== null ) { |
| 503 |
|
| 504 |
$oldWatchPeriod = $oldWatchedItem->getExpiry() ?? 'infinity'; |
| 505 |
$changingWatchStatus = $changingWatchStatus || |
| 506 |
$oldWatchPeriod !== ExpiryDef::normalizeExpiry( $expiry, TS_MW ); |
| 507 |
} |
| 508 |
|
| 509 |
if ( $changingWatchStatus ) { |
| 510 |
|
| 511 |
|
| 512 |
if ( $watch ) { |
| 513 |
return $this->addWatchIgnoringRights( $performer->getUser(), $target, $expiry ); |
| 514 |
} else { |
| 515 |
return $this->removeWatch( $performer, $target ); |
| 516 |
} |
| 517 |
} |
| 518 |
|
| 519 |
return StatusValue::newGood(); |
| 520 |
} |
| 521 |
} |