Merge "Database transaction flushing cleanups"
[lhc/web/wiklou.git] / includes / WatchedItemStore.php
index f0619d6..cf18fab 100644 (file)
@@ -1,7 +1,6 @@
 <?php
 
 use Liuggio\StatsdClient\Factory\StatsdDataFactoryInterface;
-use MediaWiki\MediaWikiServices;
 use MediaWiki\Linker\LinkTarget;
 use Wikimedia\Assert\Assert;
 
@@ -81,14 +80,12 @@ class WatchedItemStore implements StatsdAwareInterface {
         * @return ScopedCallback to reset the overridden value
         * @throws MWException
         */
-       public function overrideDeferredUpdatesAddCallableUpdateCallback( $callback ) {
+       public function overrideDeferredUpdatesAddCallableUpdateCallback( callable $callback ) {
                if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
                        throw new MWException(
                                'Cannot override DeferredUpdates::addCallableUpdate callback in operation.'
                        );
                }
-               Assert::parameterType( 'callable', $callback, '$callback' );
-
                $previousValue = $this->deferredUpdatesAddCallableUpdateCallback;
                $this->deferredUpdatesAddCallableUpdateCallback = $callback;
                return new ScopedCallback( function() use ( $previousValue ) {
@@ -106,14 +103,12 @@ class WatchedItemStore implements StatsdAwareInterface {
         * @return ScopedCallback to reset the overridden value
         * @throws MWException
         */
-       public function overrideRevisionGetTimestampFromIdCallback( $callback ) {
+       public function overrideRevisionGetTimestampFromIdCallback( callable $callback ) {
                if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
                        throw new MWException(
                                'Cannot override Revision::getTimestampFromId callback in operation.'
                        );
                }
-               Assert::parameterType( 'callable', $callback, '$callback' );
-
                $previousValue = $this->revisionGetTimestampFromIdCallback;
                $this->revisionGetTimestampFromIdCallback = $callback;
                return new ScopedCallback( function() use ( $previousValue ) {
@@ -724,42 +719,55 @@ class WatchedItemStore implements StatsdAwareInterface {
         */
        public function updateNotificationTimestamp( User $editor, LinkTarget $target, $timestamp ) {
                $dbw = $this->getConnection( DB_MASTER );
-               $res = $dbw->select( [ 'watchlist' ],
-                       [ 'wl_user' ],
+               $uids = $dbw->selectFieldValues(
+                       'watchlist',
+                       'wl_user',
                        [
                                'wl_user != ' . intval( $editor->getId() ),
                                'wl_namespace' => $target->getNamespace(),
                                'wl_title' => $target->getDBkey(),
                                'wl_notificationtimestamp IS NULL',
-                       ], __METHOD__
+                       ],
+                       __METHOD__
                );
+               $this->reuseConnection( $dbw );
 
-               $watchers = [];
-               foreach ( $res as $row ) {
-                       $watchers[] = intval( $row->wl_user );
-               }
-
+               $watchers = array_map( 'intval', $uids );
                if ( $watchers ) {
                        // Update wl_notificationtimestamp for all watching users except the editor
                        $fname = __METHOD__;
-                       $dbw->onTransactionIdle(
-                               function () use ( $dbw, $timestamp, $watchers, $target, $fname ) {
-                                       $dbw->update( 'watchlist',
-                                               [ /* SET */
-                                                       'wl_notificationtimestamp' => $dbw->timestamp( $timestamp )
-                                               ], [ /* WHERE */
-                                                       'wl_user' => $watchers,
-                                                       'wl_namespace' => $target->getNamespace(),
-                                                       'wl_title' => $target->getDBkey(),
-                                               ], $fname
-                                       );
+                       DeferredUpdates::addCallableUpdate(
+                               function () use ( $timestamp, $watchers, $target, $fname ) {
+                                       global $wgUpdateRowsPerQuery;
+
+                                       $dbw = $this->getConnection( DB_MASTER );
+                                       $factory = wfGetLBFactory();
+
+                                       $watchersChunks = array_chunk( $watchers, $wgUpdateRowsPerQuery );
+                                       foreach ( $watchersChunks as $watchersChunk ) {
+                                               $dbw->update( 'watchlist',
+                                                       [ /* SET */
+                                                               'wl_notificationtimestamp' => $dbw->timestamp( $timestamp )
+                                                       ], [ /* WHERE - TODO Use wl_id T130067 */
+                                                               'wl_user' => $watchersChunk,
+                                                               'wl_namespace' => $target->getNamespace(),
+                                                               'wl_title' => $target->getDBkey(),
+                                                       ], $fname
+                                               );
+                                               if ( count( $watchersChunks ) > 1 ) {
+                                                       $factory->commitMasterChanges( __METHOD__ );
+                                                       $factory->waitForReplication( [ 'wiki' => $dbw->getWikiID() ] );
+                                               }
+                                       }
                                        $this->uncacheLinkTarget( $target );
-                               }
+
+                                       $this->reuseConnection( $dbw );
+                               },
+                               DeferredUpdates::POSTSEND,
+                               $dbw
                        );
                }
 
-               $this->reuseConnection( $dbw );
-
                return $watchers;
        }