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