MediaWiki  master
ActivityUpdateJob.php
Go to the documentation of this file.
1 <?php
23 
36 class 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 = 'wl_notificationtimestamp < ' . $dbw->addQuotes( $dbw->timestamp( $casTimestamp ) );
80 
81  // select primary key first instead of directly update to avoid deadlocks per T204561
82  $wlId = $dbw->selectField(
83  'watchlist',
84  'wl_id',
85  [
86  'wl_user' => $this->params['userid'],
87  'wl_namespace' => $this->title->getNamespace(),
88  'wl_title' => $this->title->getDBkey(),
89  $casTimeCond
90  ],
91  __METHOD__
92  );
93 
94  if ( !$wlId ) {
95  return;
96  }
97 
98  $dbw->update(
99  'watchlist',
100  [ 'wl_notificationtimestamp' => $dbw->timestampOrNull( $this->params['notifTime'] ) ],
101  [
102  'wl_id' => (int)$wlId,
103  // For paranoia, enforce the CAS time condition here too
104  $casTimeCond
105  ],
106  __METHOD__
107  );
108  }
109 }
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:39
Title $title
Definition: Job.php:50
array $params
Array of job parameters.
Definition: Job.php:44
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.
const DB_PRIMARY
Definition: defines.php:28