Merge "Fix Postgres support"
[lhc/web/wiklou.git] / includes / WatchedItemStore.php
index cc4779e..06f93c6 100644 (file)
@@ -1,16 +1,23 @@
 <?php
 
+use Wikimedia\Rdbms\IDatabase;
 use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
 use MediaWiki\Linker\LinkTarget;
+use MediaWiki\MediaWikiServices;
 use Wikimedia\Assert\Assert;
 use Wikimedia\ScopedCallback;
+use Wikimedia\Rdbms\LoadBalancer;
+use Wikimedia\Rdbms\DBUnexpectedError;
 
 /**
  * Storage layer class for WatchedItems.
  * Database interaction.
  *
- * @author Addshore
+ * Uses database because this uses User::isAnon
+ *
+ * @group Database
  *
+ * @author Addshore
  * @since 1.27
  */
 class WatchedItemStore implements StatsdAwareInterface {
@@ -23,6 +30,11 @@ class WatchedItemStore implements StatsdAwareInterface {
         */
        private $loadBalancer;
 
+       /**
+        * @var ReadOnlyMode
+        */
+       private $readOnlyMode;
+
        /**
         * @var HashBagOStuff
         */
@@ -54,13 +66,16 @@ class WatchedItemStore implements StatsdAwareInterface {
        /**
         * @param LoadBalancer $loadBalancer
         * @param HashBagOStuff $cache
+        * @param ReadOnlyMode $readOnlyMode
         */
        public function __construct(
                LoadBalancer $loadBalancer,
-               HashBagOStuff $cache
+               HashBagOStuff $cache,
+               ReadOnlyMode $readOnlyMode
        ) {
                $this->loadBalancer = $loadBalancer;
                $this->cache = $cache;
+               $this->readOnlyMode = $readOnlyMode;
                $this->stats = new NullStatsdDataFactory();
                $this->deferredUpdatesAddCallableUpdateCallback = [ 'DeferredUpdates', 'addCallableUpdate' ];
                $this->revisionGetTimestampFromIdCallback = [ 'Revision', 'getTimestampFromId' ];
@@ -452,7 +467,7 @@ class WatchedItemStore implements StatsdAwareInterface {
                $item = new WatchedItem(
                        $user,
                        $target,
-                       $row->wl_notificationtimestamp
+                       wfTimestampOrNull( TS_MW, $row->wl_notificationtimestamp )
                );
                $this->cache( $item );
 
@@ -566,7 +581,8 @@ class WatchedItemStore implements StatsdAwareInterface {
                );
 
                foreach ( $res as $row ) {
-                       $timestamps[$row->wl_namespace][$row->wl_title] = $row->wl_notificationtimestamp;
+                       $timestamps[$row->wl_namespace][$row->wl_title] =
+                               wfTimestampOrNull( TS_MW, $row->wl_notificationtimestamp );
                }
 
                return $timestamps;
@@ -589,7 +605,7 @@ class WatchedItemStore implements StatsdAwareInterface {
         * @return bool success
         */
        public function addWatchBatchForUser( User $user, array $targets ) {
-               if ( $this->loadBalancer->getReadOnlyReason() !== false ) {
+               if ( $this->readOnlyMode->isReadOnly() ) {
                        return false;
                }
                // Only loggedin user can have a watchlist
@@ -647,7 +663,7 @@ class WatchedItemStore implements StatsdAwareInterface {
         */
        public function removeWatch( User $user, LinkTarget $target ) {
                // Only logged in user can have a watchlist
-               if ( $this->loadBalancer->getReadOnlyReason() !== false || $user->isAnon() ) {
+               if ( $this->readOnlyMode->isReadOnly() || $user->isAnon() ) {
                        return false;
                }
 
@@ -668,7 +684,7 @@ class WatchedItemStore implements StatsdAwareInterface {
 
        /**
         * @param User $user The user to set the timestamp for
-        * @param string $timestamp Set the update timestamp to this value
+        * @param string|null $timestamp Set the update timestamp to this value
         * @param LinkTarget[] $targets List of targets to update. Default to all targets
         *
         * @return bool success
@@ -687,9 +703,13 @@ class WatchedItemStore implements StatsdAwareInterface {
                        $conds[] = $batch->constructSet( 'wl', $dbw );
                }
 
+               if ( $timestamp !== null ) {
+                       $timestamp = $dbw->timestamp( $timestamp );
+               }
+
                $success = $dbw->update(
                        'watchlist',
-                       [ 'wl_notificationtimestamp' => $dbw->timestamp( $timestamp ) ],
+                       [ 'wl_notificationtimestamp' => $timestamp ],
                        $conds,
                        __METHOD__
                );
@@ -730,7 +750,7 @@ class WatchedItemStore implements StatsdAwareInterface {
                                        global $wgUpdateRowsPerQuery;
 
                                        $dbw = $this->getConnectionRef( DB_MASTER );
-                                       $factory = wfGetLBFactory();
+                                       $factory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
                                        $ticket = $factory->getEmptyTransactionTicket( __METHOD__ );
 
                                        $watchersChunks = array_chunk( $watchers, $wgUpdateRowsPerQuery );
@@ -774,7 +794,7 @@ class WatchedItemStore implements StatsdAwareInterface {
         */
        public function resetNotificationTimestamp( User $user, Title $title, $force = '', $oldid = 0 ) {
                // Only loggedin user can have a watchlist
-               if ( $this->loadBalancer->getReadOnlyReason() !== false || $user->isAnon() ) {
+               if ( $this->readOnlyMode->isReadOnly() || $user->isAnon() ) {
                        return false;
                }