MediaWiki master
ActivityUpdateJob.php
Go to the documentation of this file.
1<?php
8
9use InvalidArgumentException;
14
27class ActivityUpdateJob extends Job {
32 public function __construct( $title, array $params ) {
33 // If we know its a PageReference, we could just pass that to the parent
34 // constructor, but its simpler to just extract namespace and dbkey, and
35 // that works for both LinkTarget and PageReference
36 $params['namespace'] = $title->getNamespace();
37 $params['title'] = $title->getDBkey();
38
39 parent::__construct( 'activityUpdateJob', $params );
40
41 static $required = [ 'type', 'userid', 'notifTime', 'curTime' ];
42 $missing = implode( ', ', array_diff( $required, array_keys( $this->params ) ) );
43 if ( $missing != '' ) {
44 throw new InvalidArgumentException( "Missing parameter(s) $missing" );
45 }
46
47 $this->removeDuplicates = true;
48 }
49
51 public function run() {
52 if ( $this->params['type'] === 'updateWatchlistNotification' ) {
54 } else {
55 throw new InvalidArgumentException( "Invalid 'type' '{$this->params['type']}'." );
56 }
57
58 return true;
59 }
60
61 protected function updateWatchlistNotification() {
62 $casTimestamp = $this->params['notifTime'] ?? $this->params['curTime'];
63
64 // TODO: Inject
65 $dbw = MediaWikiServices::getInstance()->getConnectionProvider()->getPrimaryDatabase();
66 // Add a "check and set" style comparison to handle conflicts.
67 // The inequality always avoids updates when the current value
68 // is already NULL per ANSI SQL. This is desired since NULL means
69 // that the user is "caught up" on edits already. When the field
70 // is non-NULL, make sure not to set it back in time or set it to
71 // NULL when newer revisions were in fact added to the page.
72 $casTimeCond = $dbw->expr( 'wl_notificationtimestamp', '<', $dbw->timestamp( $casTimestamp ) );
73
74 // select primary key first instead of directly update to avoid deadlocks per T204561
75 $wlId = $dbw->newSelectQueryBuilder()
76 ->select( 'wl_id' )
77 ->from( 'watchlist' )
78 ->where( [
79 'wl_user' => $this->params['userid'],
80 'wl_namespace' => $this->title->getNamespace(),
81 'wl_title' => $this->title->getDBkey(),
82 $casTimeCond
83 ] )->caller( __METHOD__ )->fetchField();
84
85 if ( !$wlId ) {
86 return;
87 }
88 $dbw->newUpdateQueryBuilder()
89 ->update( 'watchlist' )
90 ->set( [ 'wl_notificationtimestamp' => $dbw->timestampOrNull( $this->params['notifTime'] ) ] )
91 ->where( [ 'wl_id' => (int)$wlId, $casTimeCond ] )
92 ->caller( __METHOD__ )->execute();
93 }
94}
96class_alias( ActivityUpdateJob::class, 'ActivityUpdateJob' );
Describe and execute a background job.
Definition Job.php:28
array $params
Array of job parameters.
Definition Job.php:33
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:1037
getDBkey()
Get the main part with underscores.
Definition Title.php:1028
Job for updating user activity like "last viewed" timestamps.
run()
Run the job.If this method returns false or completes exceptionally, the job runner will retry execut...
Represents the target of a wiki link.
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.