Merge "Explicitly declare ImportReporter::reportPage() and override public"
[lhc/web/wiklou.git] / includes / WatchedItem.php
index 0ef2373..b597f99 100644 (file)
  */
 class WatchedItem {
        /** @var Title */
-       public $mTitle;
+       private $mTitle;
 
        /** @var User */
-       public $mUser;
+       private $mUser;
 
        /** @var int */
-       public $mCheckRights;
+       private $mCheckRights;
 
        /** @var bool */
        private $loaded = false;
@@ -59,17 +59,6 @@ class WatchedItem {
         */
        const CHECK_USER_RIGHTS = 1;
 
-       /**
-        * Do DB master updates right now
-        * @since 1.26
-        */
-       const IMMEDIATE = 0;
-       /**
-        * Do DB master updates via the job queue
-        * @since 1.26
-        */
-       const DEFERRED = 1;
-
        /**
         * Create a WatchedItem object with the given user and title
         * @since 1.22 $checkRights parameter added
@@ -129,11 +118,11 @@ class WatchedItem {
         * @return array
         */
        private function dbCond() {
-               return array(
+               return [
                        'wl_user' => $this->getUserId(),
                        'wl_namespace' => $this->getTitleNs(),
                        'wl_title' => $this->getTitleDBkey(),
-               );
+               ];
        }
 
        /**
@@ -219,10 +208,9 @@ class WatchedItem {
         * @param bool $force Whether to force the write query to be executed even if the
         *    page is not watched or the notification timestamp is already NULL.
         * @param int $oldid The revision id being viewed. If not given or 0, latest revision is assumed.
-        * @mode int $mode WatchedItem::DEFERRED/IMMEDIATE
         */
        public function resetNotificationTimestamp(
-               $force = '', $oldid = 0, $mode = self::IMMEDIATE
+               $force = '', $oldid = 0
        ) {
                // Only loggedin user can have a watchlist
                if ( wfReadOnly() || $this->mUser->isAnon() || !$this->isAllowed( 'editmywatchlist' ) ) {
@@ -273,28 +261,19 @@ class WatchedItem {
                }
 
                // If the page is watched by the user (or may be watched), update the timestamp
-               if ( $mode === self::DEFERRED ) {
-                       $job = new ActivityUpdateJob(
-                               $title,
-                               array(
-                                       'type'      => 'updateWatchlistNotification',
-                                       'userid'    => $this->getUserId(),
-                                       'notifTime' => $notificationTimestamp,
-                                       'curTime'   => time()
-                               )
-                       );
-                       // Try to run this post-send
-                       DeferredUpdates::addCallableUpdate( function() use ( $job ) {
-                               $job->run();
-                       } );
-               } else {
-                       $dbw = wfGetDB( DB_MASTER );
-                       $dbw->update( 'watchlist',
-                               array( 'wl_notificationtimestamp' => $dbw->timestampOrNull( $notificationTimestamp ) ),
-                               $this->dbCond(),
-                               __METHOD__
-                       );
-               }
+               $job = new ActivityUpdateJob(
+                       $title,
+                       [
+                               'type'      => 'updateWatchlistNotification',
+                               'userid'    => $this->getUserId(),
+                               'notifTime' => $notificationTimestamp,
+                               'curTime'   => time()
+                       ]
+               );
+               // Try to run this post-send
+               DeferredUpdates::addCallableUpdate( function() use ( $job ) {
+                       $job->run();
+               } );
 
                $this->timestamp = null;
        }
@@ -309,26 +288,26 @@ class WatchedItem {
                        return false;
                }
 
-               $rows = array();
+               $rows = [];
                foreach ( $items as $item ) {
                        // Only loggedin user can have a watchlist
                        if ( $item->mUser->isAnon() || !$item->isAllowed( 'editmywatchlist' ) ) {
                                continue;
                        }
-                       $rows[] = array(
+                       $rows[] = [
                                'wl_user' => $item->getUserId(),
                                'wl_namespace' => MWNamespace::getSubject( $item->getTitleNs() ),
                                'wl_title' => $item->getTitleDBkey(),
                                'wl_notificationtimestamp' => null,
-                       );
+                       ];
                        // Every single watched page needs now to be listed in watchlist;
                        // namespace:page and namespace_talk:page need separate entries:
-                       $rows[] = array(
+                       $rows[] = [
                                'wl_user' => $item->getUserId(),
                                'wl_namespace' => MWNamespace::getTalk( $item->getTitleNs() ),
                                'wl_title' => $item->getTitleDBkey(),
                                'wl_notificationtimestamp' => null
-                       );
+                       ];
                        $item->watched = true;
                }
 
@@ -351,7 +330,7 @@ class WatchedItem {
         * @return bool
         */
        public function addWatch() {
-               return self::batchAddWatch( array( $this ) );
+               return self::batchAddWatch( [ $this ] );
        }
 
        /**
@@ -368,11 +347,11 @@ class WatchedItem {
                $success = false;
                $dbw = wfGetDB( DB_MASTER );
                $dbw->delete( 'watchlist',
-                       array(
+                       [
                                'wl_user' => $this->getUserId(),
                                'wl_namespace' => MWNamespace::getSubject( $this->getTitleNs() ),
                                'wl_title' => $this->getTitleDBkey(),
-                       ), __METHOD__
+                       ], __METHOD__
                );
                if ( $dbw->affectedRows() ) {
                        $success = true;
@@ -383,11 +362,11 @@ class WatchedItem {
                # in watchlist namespace:page and namespace_talk:page had separate
                # entries: clear them
                $dbw->delete( 'watchlist',
-                       array(
+                       [
                                'wl_user' => $this->getUserId(),
                                'wl_namespace' => MWNamespace::getTalk( $this->getTitleNs() ),
                                'wl_title' => $this->getTitleDBkey(),
-                       ), __METHOD__
+                       ], __METHOD__
                );
 
                if ( $dbw->affectedRows() ) {
@@ -400,63 +379,15 @@ class WatchedItem {
        }
 
        /**
-        * Check if the given title already is watched by the user, and if so
-        * add watches on a new title. To be used for page renames and such.
+        * @deprecated since 1.27. See WatchedItemStore::duplicateEntry
         *
-        * @param Title $ot Page title to duplicate entries from, if present
-        * @param Title $nt Page title to add watches on
+        * @param Title $oldTitle
+        * @param Title $newTitle
         */
-       public static function duplicateEntries( $ot, $nt ) {
-               WatchedItem::doDuplicateEntries( $ot->getSubjectPage(), $nt->getSubjectPage() );
-               WatchedItem::doDuplicateEntries( $ot->getTalkPage(), $nt->getTalkPage() );
+       public static function duplicateEntries( Title $oldTitle, Title $newTitle ) {
+               $store = WatchedItemStore::getDefaultInstance();
+               $store->duplicateEntry( $oldTitle->getSubjectPage(), $newTitle->getSubjectPage() );
+               $store->duplicateEntry( $oldTitle->getTalkPage(), $newTitle->getTalkPage() );
        }
 
-       /**
-        * Handle duplicate entries. Backend for duplicateEntries().
-        *
-        * @param Title $ot
-        * @param Title $nt
-        *
-        * @return bool
-        */
-       private static function doDuplicateEntries( $ot, $nt ) {
-               $oldnamespace = $ot->getNamespace();
-               $newnamespace = $nt->getNamespace();
-               $oldtitle = $ot->getDBkey();
-               $newtitle = $nt->getDBkey();
-
-               $dbw = wfGetDB( DB_MASTER );
-               $res = $dbw->select( 'watchlist',
-                       array( 'wl_user', 'wl_notificationtimestamp' ),
-                       array( 'wl_namespace' => $oldnamespace, 'wl_title' => $oldtitle ),
-                       __METHOD__, 'FOR UPDATE'
-               );
-               # Construct array to replace into the watchlist
-               $values = array();
-               foreach ( $res as $s ) {
-                       $values[] = array(
-                               'wl_user' => $s->wl_user,
-                               'wl_namespace' => $newnamespace,
-                               'wl_title' => $newtitle,
-                               'wl_notificationtimestamp' => $s->wl_notificationtimestamp,
-                       );
-               }
-
-               if ( empty( $values ) ) {
-                       // Nothing to do
-                       return true;
-               }
-
-               # Perform replace
-               # Note that multi-row replace is very efficient for MySQL but may be inefficient for
-               # some other DBMSes, mostly due to poor simulation by us
-               $dbw->replace(
-                       'watchlist',
-                       array( array( 'wl_user', 'wl_namespace', 'wl_title' ) ),
-                       $values,
-                       __METHOD__
-               );
-
-               return true;
-       }
 }