MediaWiki 1.41.2
ActivityUpdateJob.php
Go to the documentation of this file.
1<?php
23
36class ActivityUpdateJob extends Job {
41 public function __construct( $title, array $params ) {
42 // If we know its a PageReference, we could just pass that to the parent
43 // constructor, but its simpler to just extract namespace and dbkey, and
44 // that works for both LinkTarget and PageReference
45 $params['namespace'] = $title->getNamespace();
46 $params['title'] = $title->getDBkey();
47
48 parent::__construct( 'activityUpdateJob', $params );
49
50 static $required = [ 'type', 'userid', 'notifTime', 'curTime' ];
51 $missing = implode( ', ', array_diff( $required, array_keys( $this->params ) ) );
52 if ( $missing != '' ) {
53 throw new InvalidArgumentException( "Missing parameter(s) $missing" );
54 }
55
56 $this->removeDuplicates = true;
57 }
58
59 public function run() {
60 if ( $this->params['type'] === 'updateWatchlistNotification' ) {
62 } else {
63 throw new InvalidArgumentException( "Invalid 'type' '{$this->params['type']}'." );
64 }
65
66 return true;
67 }
68
69 protected function updateWatchlistNotification() {
70 $casTimestamp = $this->params['notifTime'] ?? $this->params['curTime'];
71
72 $dbw = wfGetDB( DB_PRIMARY );
73 // Add a "check and set" style comparison to handle conflicts.
74 // The inequality always avoids updates when the current value
75 // is already NULL per ANSI SQL. This is desired since NULL means
76 // that the user is "caught up" on edits already. When the field
77 // is non-NULL, make sure not to set it back in time or set it to
78 // NULL when newer revisions were in fact added to the page.
79 $casTimeCond = $dbw->buildComparison(
80 '<',
81 [ 'wl_notificationtimestamp' => $dbw->timestamp( $casTimestamp ) ]
82 );
83
84 // select primary key first instead of directly update to avoid deadlocks per T204561
85 $wlId = $dbw->newSelectQueryBuilder()
86 ->select( 'wl_id' )
87 ->from( 'watchlist' )
88 ->where( [
89 'wl_user' => $this->params['userid'],
90 'wl_namespace' => $this->title->getNamespace(),
91 'wl_title' => $this->title->getDBkey(),
92 $casTimeCond
93 ] )->caller( __METHOD__ )->fetchField();
94
95 if ( !$wlId ) {
96 return;
97 }
98 $dbw->newUpdateQueryBuilder()
99 ->update( 'watchlist' )
100 ->set( [ 'wl_notificationtimestamp' => $dbw->timestampOrNull( $this->params['notifTime'] ) ] )
101 ->where( [ 'wl_id' => (int)$wlId, $casTimeCond ] )
102 ->caller( __METHOD__ )->execute();
103 }
104}
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Job for updating user activity like "last viewed" timestamps.
__construct( $title, array $params)
Class to both describe a background job and handle jobs.
Definition Job.php:40
Title $title
Definition Job.php:51
getNamespace()
Get the namespace index, i.e.
Definition Title.php:1058
getDBkey()
Get the main part with underscores.
Definition Title.php:1049
Represents the target of a wiki link.
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.
const DB_PRIMARY
Definition defines.php:28