*/
$wgEnotifUseJobQ = false;
+/**
+ * Use the job queue for user activity updates like updating "last visited"
+ * fields for email notifications of page changes. This should only be enabled
+ * if the jobs have a dedicated runner to avoid update lag.
+ *
+ * @since 1.26
+ */
+$wgActivityUpdatesUseJobQueue = false;
+
/**
* Use real name instead of username in e-mail "from" field.
*/
'ThumbnailRender' => 'ThumbnailRenderJob',
'recentChangesUpdate' => 'RecentChangesUpdateJob',
'refreshLinksPrioritized' => 'RefreshLinksJob', // for cascading protection
+ 'activityUpdateJob' => 'ActivityUpdateJob',
'enqueue' => 'EnqueueJob', // local queue for multi-DC setups
'null' => 'NullJob'
);
$force = 'force';
}
- $this->getWatchedItem( $title )->resetNotificationTimestamp( $force, $oldid );
+ $this->getWatchedItem( $title )->resetNotificationTimestamp(
+ $force, $oldid, WatchedItem::DEFERRED
+ );
}
/**
* @ingroup Watchlist
*/
class WatchedItem {
- /**
- * Constant to specify that user rights 'editmywatchlist' and
- * 'viewmywatchlist' should not be checked.
- * @since 1.22
- */
- const IGNORE_USER_RIGHTS = 0;
-
- /**
- * Constant to specify that user rights 'editmywatchlist' and
- * 'viewmywatchlist' should be checked.
- * @since 1.22
- */
- const CHECK_USER_RIGHTS = 1;
-
/** @var Title */
public $mTitle;
/** @var string */
private $timestamp;
+ /**
+ * Constant to specify that user rights 'editmywatchlist' and
+ * 'viewmywatchlist' should not be checked.
+ * @since 1.22
+ */
+ const IGNORE_USER_RIGHTS = 0;
+
+ /**
+ * Constant to specify that user rights 'editmywatchlist' and
+ * 'viewmywatchlist' should be checked.
+ * @since 1.22
+ */
+ 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
* @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 ) {
+ public function resetNotificationTimestamp(
+ $force = '', $oldid = 0, $mode = self::IMMEDIATE
+ ) {
+ global $wgActivityUpdatesUseJobQueue;
+
// Only loggedin user can have a watchlist
if ( wfReadOnly() || $this->mUser->isAnon() || !$this->isAllowed( 'editmywatchlist' ) ) {
return;
}
}
- // If the page is watched by the user (or may be watched), update the timestamp on any
- // any matching rows
- $dbw = wfGetDB( DB_MASTER );
- $dbw->update( 'watchlist', array( 'wl_notificationtimestamp' => $notificationTimestamp ),
- $this->dbCond(), __METHOD__ );
+ // If the page is watched by the user (or may be watched), update the timestamp
+ if ( $mode === self::DEFERRED && $wgActivityUpdatesUseJobQueue ) {
+ JobQueueGroup::singleton()->push(
+ EnqueueJob::newFromLocalJobs( new JobSpecification(
+ 'activityUpdateJob',
+ array(
+ 'type' => 'updateWatchlistNotification',
+ 'userid' => $this->getUserId(),
+ 'notifTime' => $notificationTimestamp,
+ 'curTime' => time()
+ ),
+ array( 'removeDuplicates' => true ),
+ $title
+ ) )
+ );
+ } else {
+ $dbw = wfGetDB( DB_MASTER );
+ $dbw->update( 'watchlist',
+ array( 'wl_notificationtimestamp' => $notificationTimestamp ),
+ $this->dbCond(),
+ __METHOD__
+ );
+ }
+
$this->timestamp = null;
}
--- /dev/null
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @author Aaron Schulz
+ * @ingroup JobQueue
+ */
+
+/**
+ * Job for updating user activity like "last viewed" timestamps
+ *
+ * @ingroup JobQueue
+ * @since 1.26
+ */
+class ActivityUpdateJob extends Job {
+ function __construct( $title, $params ) {
+ parent::__construct( 'activityUpdateJob', $title, $params );
+
+ if ( !isset( $params['type'] ) ) {
+ throw new InvalidArgumentException( "Missing 'type' parameter." );
+ }
+
+ $this->removeDuplicates = true;
+ }
+
+ public function run() {
+ if ( $this->params['type'] === 'updateWatchlistNotification' ) {
+ $this->updateWatchlistNotification();
+ } else {
+ throw new Exception( "Invalid 'type' parameter '{$this->params['type']}'." );
+ }
+
+ return true;
+ }
+
+ protected function updateWatchlistNotification() {
+ $casTimestamp = ( $this->params['notifTime'] !== null )
+ ? $this->params['notifTime']
+ : $this->params['curTime'];
+
+ $dbw = wfGetDB( DB_MASTER );
+ $dbw->update( 'watchlist',
+ array(
+ 'wl_notificationtimestamp' => $dbw->timestampOrNull( $this->params['notifTime'] )
+ ),
+ array(
+ 'wl_user' => $this->params['userid'],
+ 'wl_namespace' => $this->title->getNamespace(),
+ 'wl_title' => $this->title->getDBkey(),
+ // Add a "check and set" style comparison to handle conflicts.
+ // The inequality always avoids updates when the current value
+ // is already NULL per ANSI SQL. This is desired since NULL means
+ // that the user is "caught up" on edits already. When the field
+ // is non-NULL, make sure not to set it back in time or set it to
+ // NULL when newer revisions were in fact added to the page.
+ 'wl_notificationtimestamp < ' . $dbw->addQuotes( $dbw->timestamp( $casTimestamp ) )
+ ),
+ __METHOD__
+ );
+ }
+}