Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like WatchedItemStore often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WatchedItemStore, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class WatchedItemStore { |
||
| 14 | |||
| 15 | const SORT_DESC = 'DESC'; |
||
| 16 | const SORT_ASC = 'ASC'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var LoadBalancer |
||
| 20 | */ |
||
| 21 | private $loadBalancer; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var HashBagOStuff |
||
| 25 | */ |
||
| 26 | private $cache; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array[] Looks like $cacheIndex[Namespace ID][Target DB Key][User Id] => 'key' |
||
| 30 | * The index is needed so that on mass changes all relevant items can be un-cached. |
||
| 31 | * For example: Clearing a users watchlist of all items or updating notification timestamps |
||
| 32 | * for all users watching a single target. |
||
| 33 | */ |
||
| 34 | private $cacheIndex = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var callable|null |
||
| 38 | */ |
||
| 39 | private $deferredUpdatesAddCallableUpdateCallback; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var callable|null |
||
| 43 | */ |
||
| 44 | private $revisionGetTimestampFromIdCallback; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var self|null |
||
| 48 | */ |
||
| 49 | private static $instance; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param LoadBalancer $loadBalancer |
||
| 53 | * @param HashBagOStuff $cache |
||
| 54 | */ |
||
| 55 | public function __construct( |
||
| 56 | LoadBalancer $loadBalancer, |
||
| 57 | HashBagOStuff $cache |
||
| 58 | ) { |
||
| 59 | $this->loadBalancer = $loadBalancer; |
||
| 60 | $this->cache = $cache; |
||
| 61 | $this->deferredUpdatesAddCallableUpdateCallback = [ 'DeferredUpdates', 'addCallableUpdate' ]; |
||
| 62 | $this->revisionGetTimestampFromIdCallback = [ 'Revision', 'getTimestampFromId' ]; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Overrides the DeferredUpdates::addCallableUpdate callback |
||
| 67 | * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined. |
||
| 68 | * |
||
| 69 | * @param callable $callback |
||
| 70 | * |
||
| 71 | * @see DeferredUpdates::addCallableUpdate for callback signiture |
||
| 72 | * |
||
| 73 | * @return ScopedCallback to reset the overridden value |
||
| 74 | * @throws MWException |
||
| 75 | */ |
||
| 76 | View Code Duplication | public function overrideDeferredUpdatesAddCallableUpdateCallback( $callback ) { |
|
| 77 | if ( !defined( 'MW_PHPUNIT_TEST' ) ) { |
||
| 78 | throw new MWException( |
||
| 79 | 'Cannot override DeferredUpdates::addCallableUpdate callback in operation.' |
||
| 80 | ); |
||
| 81 | } |
||
| 82 | Assert::parameterType( 'callable', $callback, '$callback' ); |
||
| 83 | |||
| 84 | $previousValue = $this->deferredUpdatesAddCallableUpdateCallback; |
||
| 85 | $this->deferredUpdatesAddCallableUpdateCallback = $callback; |
||
| 86 | return new ScopedCallback( function() use ( $previousValue ) { |
||
| 87 | $this->deferredUpdatesAddCallableUpdateCallback = $previousValue; |
||
| 88 | } ); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Overrides the Revision::getTimestampFromId callback |
||
| 93 | * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined. |
||
| 94 | * |
||
| 95 | * @param callable $callback |
||
| 96 | * @see Revision::getTimestampFromId for callback signiture |
||
| 97 | * |
||
| 98 | * @return ScopedCallback to reset the overridden value |
||
| 99 | * @throws MWException |
||
| 100 | */ |
||
| 101 | View Code Duplication | public function overrideRevisionGetTimestampFromIdCallback( $callback ) { |
|
| 102 | if ( !defined( 'MW_PHPUNIT_TEST' ) ) { |
||
| 103 | throw new MWException( |
||
| 104 | 'Cannot override Revision::getTimestampFromId callback in operation.' |
||
| 105 | ); |
||
| 106 | } |
||
| 107 | Assert::parameterType( 'callable', $callback, '$callback' ); |
||
| 108 | |||
| 109 | $previousValue = $this->revisionGetTimestampFromIdCallback; |
||
| 110 | $this->revisionGetTimestampFromIdCallback = $callback; |
||
| 111 | return new ScopedCallback( function() use ( $previousValue ) { |
||
| 112 | $this->revisionGetTimestampFromIdCallback = $previousValue; |
||
| 113 | } ); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Overrides the default instance of this class |
||
| 118 | * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined. |
||
| 119 | * |
||
| 120 | * If this method is used it MUST also be called with null after a test to ensure a new |
||
| 121 | * default instance is created next time getDefaultInstance is called. |
||
| 122 | * |
||
| 123 | * @param WatchedItemStore|null $store |
||
| 124 | * |
||
| 125 | * @return ScopedCallback to reset the overridden value |
||
| 126 | * @throws MWException |
||
| 127 | */ |
||
| 128 | public static function overrideDefaultInstance( WatchedItemStore $store = null ) { |
||
| 129 | if ( !defined( 'MW_PHPUNIT_TEST' ) ) { |
||
| 130 | throw new MWException( |
||
| 131 | 'Cannot override ' . __CLASS__ . 'default instance in operation.' |
||
| 132 | ); |
||
| 133 | } |
||
| 134 | |||
| 135 | $previousValue = self::$instance; |
||
| 136 | self::$instance = $store; |
||
| 137 | return new ScopedCallback( function() use ( $previousValue ) { |
||
| 138 | self::$instance = $previousValue; |
||
| 139 | } ); |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @return self |
||
| 144 | */ |
||
| 145 | public static function getDefaultInstance() { |
||
| 154 | |||
| 155 | private function getCacheKey( User $user, LinkTarget $target ) { |
||
| 156 | return $this->cache->makeKey( |
||
| 157 | (string)$target->getNamespace(), |
||
| 158 | $target->getDBkey(), |
||
| 159 | (string)$user->getId() |
||
| 160 | ); |
||
| 161 | } |
||
| 162 | |||
| 163 | private function cache( WatchedItem $item ) { |
||
| 164 | $user = $item->getUser(); |
||
| 165 | $target = $item->getLinkTarget(); |
||
| 166 | $key = $this->getCacheKey( $user, $target ); |
||
| 167 | $this->cache->set( $key, $item ); |
||
| 168 | $this->cacheIndex[$target->getNamespace()][$target->getDBkey()][$user->getId()] = $key; |
||
| 169 | } |
||
| 170 | |||
| 171 | private function uncache( User $user, LinkTarget $target ) { |
||
| 172 | $this->cache->delete( $this->getCacheKey( $user, $target ) ); |
||
| 173 | unset( $this->cacheIndex[$target->getNamespace()][$target->getDBkey()][$user->getId()] ); |
||
| 174 | } |
||
| 175 | |||
| 176 | private function uncacheLinkTarget( LinkTarget $target ) { |
||
| 177 | if ( !isset( $this->cacheIndex[$target->getNamespace()][$target->getDBkey()] ) ) { |
||
| 178 | return; |
||
| 179 | } |
||
| 180 | foreach ( $this->cacheIndex[$target->getNamespace()][$target->getDBkey()] as $key ) { |
||
| 181 | $this->cache->delete( $key ); |
||
| 182 | } |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param User $user |
||
| 187 | * @param LinkTarget $target |
||
| 188 | * |
||
| 189 | * @return WatchedItem|null |
||
| 190 | */ |
||
| 191 | private function getCached( User $user, LinkTarget $target ) { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Return an array of conditions to select or update the appropriate database |
||
| 197 | * row. |
||
| 198 | * |
||
| 199 | * @param User $user |
||
| 200 | * @param LinkTarget $target |
||
| 201 | * |
||
| 202 | * @return array |
||
| 203 | */ |
||
| 204 | private function dbCond( User $user, LinkTarget $target ) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param int $slaveOrMaster DB_MASTER or DB_SLAVE |
||
| 214 | * |
||
| 215 | * @return DatabaseBase |
||
| 216 | * @throws MWException |
||
| 217 | */ |
||
| 218 | private function getConnection( $slaveOrMaster ) { |
||
| 219 | return $this->loadBalancer->getConnection( $slaveOrMaster, [ 'watchlist' ] ); |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * @param DatabaseBase $connection |
||
| 224 | * |
||
| 225 | * @throws MWException |
||
| 226 | */ |
||
| 227 | private function reuseConnection( $connection ) { |
||
| 228 | $this->loadBalancer->reuseConnection( $connection ); |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Count the number of individual items that are watched by the user. |
||
| 233 | * If a subject and corresponding talk page are watched this will return 2. |
||
| 234 | * |
||
| 235 | * @param User $user |
||
| 236 | * |
||
| 237 | * @return int |
||
| 238 | */ |
||
| 239 | public function countWatchedItems( User $user ) { |
||
| 240 | $dbr = $this->getConnection( DB_SLAVE ); |
||
| 241 | $return = (int)$dbr->selectField( |
||
| 242 | 'watchlist', |
||
| 243 | 'COUNT(*)', |
||
| 244 | [ |
||
| 245 | 'wl_user' => $user->getId() |
||
| 246 | ], |
||
| 247 | __METHOD__ |
||
| 248 | ); |
||
| 249 | $this->reuseConnection( $dbr ); |
||
| 250 | |||
| 251 | return $return; |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @param LinkTarget $target |
||
| 256 | * |
||
| 257 | * @return int |
||
| 258 | */ |
||
| 259 | public function countWatchers( LinkTarget $target ) { |
||
| 260 | $dbr = $this->getConnection( DB_SLAVE ); |
||
| 261 | $return = (int)$dbr->selectField( |
||
| 262 | 'watchlist', |
||
| 263 | 'COUNT(*)', |
||
| 264 | [ |
||
| 265 | 'wl_namespace' => $target->getNamespace(), |
||
| 266 | 'wl_title' => $target->getDBkey(), |
||
| 267 | ], |
||
| 268 | __METHOD__ |
||
| 269 | ); |
||
| 270 | $this->reuseConnection( $dbr ); |
||
| 271 | |||
| 272 | return $return; |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Number of page watchers who also visited a "recent" edit |
||
| 277 | * |
||
| 278 | * @param LinkTarget $target |
||
| 279 | * @param mixed $threshold timestamp accepted by wfTimestamp |
||
| 280 | * |
||
| 281 | * @return int |
||
| 282 | * @throws DBUnexpectedError |
||
| 283 | * @throws MWException |
||
| 284 | */ |
||
| 285 | public function countVisitingWatchers( LinkTarget $target, $threshold ) { |
||
| 286 | $dbr = $this->getConnection( DB_SLAVE ); |
||
| 287 | $visitingWatchers = (int)$dbr->selectField( |
||
| 288 | 'watchlist', |
||
| 289 | 'COUNT(*)', |
||
| 290 | [ |
||
| 291 | 'wl_namespace' => $target->getNamespace(), |
||
| 292 | 'wl_title' => $target->getDBkey(), |
||
| 293 | 'wl_notificationtimestamp >= ' . |
||
| 294 | $dbr->addQuotes( $dbr->timestamp( $threshold ) ) . |
||
|
|
|||
| 295 | ' OR wl_notificationtimestamp IS NULL' |
||
| 296 | ], |
||
| 297 | __METHOD__ |
||
| 298 | ); |
||
| 299 | $this->reuseConnection( $dbr ); |
||
| 300 | |||
| 301 | return $visitingWatchers; |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param LinkTarget[] $targets |
||
| 306 | * @param array $options Allowed keys: |
||
| 307 | * 'minimumWatchers' => int |
||
| 308 | * |
||
| 309 | * @return array multi dimensional like $return[$namespaceId][$titleString] = int $watchers |
||
| 310 | * All targets will be present in the result. 0 either means no watchers or the number |
||
| 311 | * of watchers was below the minimumWatchers option if passed. |
||
| 312 | */ |
||
| 313 | public function countWatchersMultiple( array $targets, array $options = [] ) { |
||
| 314 | $dbOptions = [ 'GROUP BY' => [ 'wl_namespace', 'wl_title' ] ]; |
||
| 315 | |||
| 316 | $dbr = $this->getConnection( DB_SLAVE ); |
||
| 317 | |||
| 318 | if ( array_key_exists( 'minimumWatchers', $options ) ) { |
||
| 319 | $dbOptions['HAVING'] = 'COUNT(*) >= ' . (int)$options['minimumWatchers']; |
||
| 320 | } |
||
| 321 | |||
| 322 | $lb = new LinkBatch( $targets ); |
||
| 323 | $res = $dbr->select( |
||
| 324 | 'watchlist', |
||
| 325 | [ 'wl_title', 'wl_namespace', 'watchers' => 'COUNT(*)' ], |
||
| 326 | [ $lb->constructSet( 'wl', $dbr ) ], |
||
| 327 | __METHOD__, |
||
| 328 | $dbOptions |
||
| 329 | ); |
||
| 330 | |||
| 331 | $this->reuseConnection( $dbr ); |
||
| 332 | |||
| 333 | $watchCounts = []; |
||
| 334 | foreach ( $targets as $linkTarget ) { |
||
| 335 | $watchCounts[$linkTarget->getNamespace()][$linkTarget->getDBkey()] = 0; |
||
| 336 | } |
||
| 337 | |||
| 338 | foreach ( $res as $row ) { |
||
| 339 | $watchCounts[$row->wl_namespace][$row->wl_title] = (int)$row->watchers; |
||
| 340 | } |
||
| 341 | |||
| 342 | return $watchCounts; |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Number of watchers of each page who have visited recent edits to that page |
||
| 347 | * |
||
| 348 | * @param array $targetsWithVisitThresholds array of pairs (LinkTarget $target, mixed $threshold), |
||
| 349 | * $threshold is: |
||
| 350 | * - a timestamp of the recent edit if $target exists (format accepted by wfTimestamp) |
||
| 351 | * - null if $target doesn't exist |
||
| 352 | * @param int|null $minimumWatchers |
||
| 353 | * @return array multi-dimensional like $return[$namespaceId][$titleString] = $watchers, |
||
| 354 | * where $watchers is an int: |
||
| 355 | * - if the page exists, number of users watching who have visited the page recently |
||
| 356 | * - if the page doesn't exist, number of users that have the page on their watchlist |
||
| 357 | * - 0 means there are no visiting watchers or their number is below the minimumWatchers |
||
| 358 | * option (if passed). |
||
| 359 | */ |
||
| 360 | public function countVisitingWatchersMultiple( |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Generates condition for the query used in a batch count visiting watchers. |
||
| 397 | * |
||
| 398 | * @param IDatabase $db |
||
| 399 | * @param array $targetsWithVisitThresholds array of pairs (LinkTarget, last visit threshold) |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | private function getVisitingWatchersCondition( |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Get an item (may be cached) |
||
| 441 | * |
||
| 442 | * @param User $user |
||
| 443 | * @param LinkTarget $target |
||
| 444 | * |
||
| 445 | * @return WatchedItem|false |
||
| 446 | */ |
||
| 447 | public function getWatchedItem( User $user, LinkTarget $target ) { |
||
| 448 | if ( $user->isAnon() ) { |
||
| 449 | return false; |
||
| 450 | } |
||
| 451 | |||
| 452 | $cached = $this->getCached( $user, $target ); |
||
| 453 | if ( $cached ) { |
||
| 454 | return $cached; |
||
| 455 | } |
||
| 456 | return $this->loadWatchedItem( $user, $target ); |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Loads an item from the db |
||
| 461 | * |
||
| 462 | * @param User $user |
||
| 463 | * @param LinkTarget $target |
||
| 464 | * |
||
| 465 | * @return WatchedItem|false |
||
| 466 | */ |
||
| 467 | public function loadWatchedItem( User $user, LinkTarget $target ) { |
||
| 468 | // Only loggedin user can have a watchlist |
||
| 469 | if ( $user->isAnon() ) { |
||
| 470 | return false; |
||
| 471 | } |
||
| 472 | |||
| 473 | $dbr = $this->getConnection( DB_SLAVE ); |
||
| 474 | $row = $dbr->selectRow( |
||
| 475 | 'watchlist', |
||
| 476 | 'wl_notificationtimestamp', |
||
| 477 | $this->dbCond( $user, $target ), |
||
| 478 | __METHOD__ |
||
| 479 | ); |
||
| 480 | $this->reuseConnection( $dbr ); |
||
| 481 | |||
| 482 | if ( !$row ) { |
||
| 483 | return false; |
||
| 484 | } |
||
| 485 | |||
| 486 | $item = new WatchedItem( |
||
| 487 | $user, |
||
| 488 | $target, |
||
| 489 | $row->wl_notificationtimestamp |
||
| 490 | ); |
||
| 491 | $this->cache( $item ); |
||
| 492 | |||
| 493 | return $item; |
||
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @param User $user |
||
| 498 | * @param array $options Allowed keys: |
||
| 499 | * 'forWrite' => bool defaults to false |
||
| 500 | * 'sort' => string optional sorting by namespace ID and title |
||
| 501 | * one of the self::SORT_* constants |
||
| 502 | * |
||
| 503 | * @return WatchedItem[] |
||
| 504 | */ |
||
| 505 | public function getWatchedItemsForUser( User $user, array $options = [] ) { |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Must be called separately for Subject & Talk namespaces |
||
| 546 | * |
||
| 547 | * @param User $user |
||
| 548 | * @param LinkTarget $target |
||
| 549 | * |
||
| 550 | * @return bool |
||
| 551 | */ |
||
| 552 | public function isWatched( User $user, LinkTarget $target ) { |
||
| 555 | |||
| 556 | /** |
||
| 557 | * @param User $user |
||
| 558 | * @param LinkTarget[] $targets |
||
| 559 | * |
||
| 560 | * @return array multi-dimensional like $return[$namespaceId][$titleString] = $timestamp, |
||
| 561 | * where $timestamp is: |
||
| 562 | * - string|null value of wl_notificationtimestamp, |
||
| 563 | * - false if $target is not watched by $user. |
||
| 564 | */ |
||
| 565 | public function getNotificationTimestampsBatch( User $user, array $targets ) { |
||
| 610 | |||
| 611 | /** |
||
| 612 | * Must be called separately for Subject & Talk namespaces |
||
| 613 | * |
||
| 614 | * @param User $user |
||
| 615 | * @param LinkTarget $target |
||
| 616 | */ |
||
| 617 | public function addWatch( User $user, LinkTarget $target ) { |
||
| 620 | |||
| 621 | /** |
||
| 622 | * @param User $user |
||
| 623 | * @param LinkTarget[] $targets |
||
| 624 | * |
||
| 625 | * @return bool success |
||
| 626 | */ |
||
| 627 | public function addWatchBatchForUser( User $user, array $targets ) { |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Removes the an entry for the User watching the LinkTarget |
||
| 664 | * Must be called separately for Subject & Talk namespaces |
||
| 665 | * |
||
| 666 | * @param User $user |
||
| 667 | * @param LinkTarget $target |
||
| 668 | * |
||
| 669 | * @return bool success |
||
| 670 | * @throws DBUnexpectedError |
||
| 671 | * @throws MWException |
||
| 672 | */ |
||
| 673 | public function removeWatch( User $user, LinkTarget $target ) { |
||
| 674 | // Only logged in user can have a watchlist |
||
| 675 | if ( $this->loadBalancer->getReadOnlyReason() !== false || $user->isAnon() ) { |
||
| 676 | return false; |
||
| 677 | } |
||
| 678 | |||
| 679 | $this->uncache( $user, $target ); |
||
| 680 | |||
| 681 | $dbw = $this->getConnection( DB_MASTER ); |
||
| 682 | $dbw->delete( 'watchlist', |
||
| 683 | [ |
||
| 684 | 'wl_user' => $user->getId(), |
||
| 685 | 'wl_namespace' => $target->getNamespace(), |
||
| 686 | 'wl_title' => $target->getDBkey(), |
||
| 687 | ], __METHOD__ |
||
| 688 | ); |
||
| 689 | $success = (bool)$dbw->affectedRows(); |
||
| 690 | $this->reuseConnection( $dbw ); |
||
| 691 | |||
| 692 | return $success; |
||
| 693 | } |
||
| 694 | |||
| 695 | /** |
||
| 696 | * @param User $editor The editor that triggered the update. Their notification |
||
| 697 | * timestamp will not be updated(they have already seen it) |
||
| 698 | * @param LinkTarget $target The target to update timestamps for |
||
| 699 | * @param string $timestamp Set the update timestamp to this value |
||
| 700 | * |
||
| 701 | * @return int[] Array of user IDs the timestamp has been updated for |
||
| 702 | */ |
||
| 703 | public function updateNotificationTimestamp( User $editor, LinkTarget $target, $timestamp ) { |
||
| 704 | $dbw = $this->getConnection( DB_MASTER ); |
||
| 705 | $res = $dbw->select( [ 'watchlist' ], |
||
| 706 | [ 'wl_user' ], |
||
| 707 | [ |
||
| 708 | 'wl_user != ' . intval( $editor->getId() ), |
||
| 709 | 'wl_namespace' => $target->getNamespace(), |
||
| 710 | 'wl_title' => $target->getDBkey(), |
||
| 711 | 'wl_notificationtimestamp IS NULL', |
||
| 712 | ], __METHOD__ |
||
| 713 | ); |
||
| 714 | |||
| 715 | $watchers = []; |
||
| 716 | foreach ( $res as $row ) { |
||
| 717 | $watchers[] = intval( $row->wl_user ); |
||
| 718 | } |
||
| 719 | |||
| 720 | if ( $watchers ) { |
||
| 721 | // Update wl_notificationtimestamp for all watching users except the editor |
||
| 722 | $fname = __METHOD__; |
||
| 723 | $dbw->onTransactionIdle( |
||
| 724 | function () use ( $dbw, $timestamp, $watchers, $target, $fname ) { |
||
| 725 | $dbw->update( 'watchlist', |
||
| 726 | [ /* SET */ |
||
| 727 | 'wl_notificationtimestamp' => $dbw->timestamp( $timestamp ) |
||
| 728 | ], [ /* WHERE */ |
||
| 729 | 'wl_user' => $watchers, |
||
| 730 | 'wl_namespace' => $target->getNamespace(), |
||
| 731 | 'wl_title' => $target->getDBkey(), |
||
| 732 | ], $fname |
||
| 733 | ); |
||
| 734 | $this->uncacheLinkTarget( $target ); |
||
| 735 | } |
||
| 736 | ); |
||
| 737 | } |
||
| 738 | |||
| 739 | $this->reuseConnection( $dbw ); |
||
| 740 | |||
| 741 | return $watchers; |
||
| 742 | } |
||
| 743 | |||
| 744 | /** |
||
| 745 | * Reset the notification timestamp of this entry |
||
| 746 | * |
||
| 747 | * @param User $user |
||
| 748 | * @param Title $title |
||
| 749 | * @param string $force Whether to force the write query to be executed even if the |
||
| 750 | * page is not watched or the notification timestamp is already NULL. |
||
| 751 | * 'force' in order to force |
||
| 752 | * @param int $oldid The revision id being viewed. If not given or 0, latest revision is assumed. |
||
| 753 | * |
||
| 754 | * @return bool success |
||
| 755 | */ |
||
| 756 | public function resetNotificationTimestamp( User $user, Title $title, $force = '', $oldid = 0 ) { |
||
| 794 | |||
| 795 | private function getNotificationTimestamp( User $user, Title $title, $item, $force, $oldid ) { |
||
| 796 | if ( !$oldid ) { |
||
| 797 | // No oldid given, assuming latest revision; clear the timestamp. |
||
| 798 | return null; |
||
| 799 | } |
||
| 800 | |||
| 801 | if ( !$title->getNextRevisionID( $oldid ) ) { |
||
| 802 | // Oldid given and is the latest revision for this title; clear the timestamp. |
||
| 803 | return null; |
||
| 804 | } |
||
| 805 | |||
| 806 | if ( $item === null ) { |
||
| 807 | $item = $this->loadWatchedItem( $user, $title ); |
||
| 841 | |||
| 842 | /** |
||
| 843 | * @param User $user |
||
| 844 | * @param int $unreadLimit |
||
| 845 | * |
||
| 846 | * @return int|bool The number of unread notifications |
||
| 847 | * true if greater than or equal to $unreadLimit |
||
| 848 | */ |
||
| 849 | public function countUnreadNotifications( User $user, $unreadLimit = null ) { |
||
| 879 | |||
| 880 | /** |
||
| 881 | * Check if the given title already is watched by the user, and if so |
||
| 882 | * add a watch for the new title. |
||
| 883 | * |
||
| 884 | * To be used for page renames and such. |
||
| 885 | * |
||
| 886 | * @param LinkTarget $oldTarget |
||
| 887 | * @param LinkTarget $newTarget |
||
| 888 | */ |
||
| 889 | public function duplicateAllAssociatedEntries( LinkTarget $oldTarget, LinkTarget $newTarget ) { |
||
| 900 | |||
| 901 | /** |
||
| 902 | * Check if the given title already is watched by the user, and if so |
||
| 903 | * add a watch for the new title. |
||
| 904 | * |
||
| 905 | * To be used for page renames and such. |
||
| 906 | * This must be called separately for Subject and Talk pages |
||
| 907 | * |
||
| 908 | * @param LinkTarget $oldTarget |
||
| 909 | * @param LinkTarget $newTarget |
||
| 910 | */ |
||
| 911 | public function duplicateEntry( LinkTarget $oldTarget, LinkTarget $newTarget ) { |
||
| 953 | |||
| 954 | } |
||
| 955 |