Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / includes / jobqueue / jobs / ActivityUpdateJob.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup JobQueue
20 */
21
22 use MediaWiki\Linker\LinkTarget;
23
24 /**
25 * Job for updating user activity like "last viewed" timestamps
26 *
27 * Job parameters include:
28 * - type: one of (updateWatchlistNotification) [required]
29 * - userid: affected user ID [required]
30 * - notifTime: timestamp to set watchlist entries to [required]
31 * - curTime: UNIX timestamp of the event that triggered this job [required]
32 *
33 * @ingroup JobQueue
34 * @since 1.26
35 */
36 class ActivityUpdateJob extends Job {
37 function __construct( LinkTarget $title, array $params ) {
38 $title = Title::newFromLinkTarget( $title );
39
40 parent::__construct( 'activityUpdateJob', $title, $params );
41
42 static $required = [ 'type', 'userid', 'notifTime', 'curTime' ];
43 $missing = implode( ', ', array_diff( $required, array_keys( $this->params ) ) );
44 if ( $missing != '' ) {
45 throw new InvalidArgumentException( "Missing paramter(s) $missing" );
46 }
47
48 $this->removeDuplicates = true;
49 }
50
51 public function run() {
52 if ( $this->params['type'] === 'updateWatchlistNotification' ) {
53 $this->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 $dbw = wfGetDB( DB_MASTER );
65 $dbw->update( 'watchlist',
66 [
67 'wl_notificationtimestamp' => $dbw->timestampOrNull( $this->params['notifTime'] )
68 ],
69 [
70 'wl_user' => $this->params['userid'],
71 'wl_namespace' => $this->title->getNamespace(),
72 'wl_title' => $this->title->getDBkey(),
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 'wl_notificationtimestamp < ' . $dbw->addQuotes( $dbw->timestamp( $casTimestamp ) )
80 ],
81 __METHOD__
82 );
83 }
84 }