From 4d8bfbdca469f49f8f03e8daf5f20de8c5f876a6 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Fri, 29 Mar 2019 00:17:09 -0700 Subject: [PATCH] Add UserOptionsUpdateJob class This can help avoid DB master queries on HTTP GET requests Change-Id: I6fa2d53d96509d88f5d3e1ff18f3ca1de8fa4bb1 --- autoload.php | 1 + includes/DefaultSettings.php | 2 + .../jobqueue/jobs/UserOptionsUpdateJob.php | 58 +++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 includes/jobqueue/jobs/UserOptionsUpdateJob.php diff --git a/autoload.php b/autoload.php index f8e90b73c1..42ca6cc0b7 100644 --- a/autoload.php +++ b/autoload.php @@ -1564,6 +1564,7 @@ $wgAutoloadLocalClasses = [ 'UserNamePrefixSearch' => __DIR__ . '/includes/user/UserNamePrefixSearch.php', 'UserNotLoggedIn' => __DIR__ . '/includes/exception/UserNotLoggedIn.php', 'UserOptionsMaintenance' => __DIR__ . '/maintenance/userOptions.php', + 'UserOptionsUpdateJob' => __DIR__ . '/includes/jobqueue/jobs/UserOptionsUpdateJob.php', 'UserPasswordPolicy' => __DIR__ . '/includes/password/UserPasswordPolicy.php', 'UserRightsProxy' => __DIR__ . '/includes/user/UserRightsProxy.php', 'UserrightsPage' => __DIR__ . '/includes/specials/SpecialUserrights.php', diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index b40d33b17e..8a2828f9a4 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -7506,6 +7506,7 @@ $wgServiceWiringFiles = [ * can add to this to provide custom jobs. * A job handler should either be a class name to be instantiated, * or (since 1.30) a callback to use for creating the job object. + * The callback takes (Title, array map of parameters) as arguments. */ $wgJobClasses = [ 'deletePage' => DeletePageJob::class, @@ -7530,6 +7531,7 @@ $wgJobClasses = [ 'cdnPurge' => CdnPurgeJob::class, 'userGroupExpiry' => UserGroupExpiryJob::class, 'clearWatchlistNotifications' => ClearWatchlistNotificationsJob::class, + 'userOptionsUpdate' => UserOptionsUpdateJob::class, 'enqueue' => EnqueueJob::class, // local queue for multi-DC setups 'null' => NullJob::class, ]; diff --git a/includes/jobqueue/jobs/UserOptionsUpdateJob.php b/includes/jobqueue/jobs/UserOptionsUpdateJob.php new file mode 100644 index 0000000000..0e8b19f2da --- /dev/null +++ b/includes/jobqueue/jobs/UserOptionsUpdateJob.php @@ -0,0 +1,58 @@ + value) + * + * @since 1.33 + */ +class UserOptionsUpdateJob extends Job implements GenericParameterJob { + public function __construct( array $params ) { + parent::__construct( 'userOptionsUpdate', $params ); + $this->removeDuplicates = true; + } + + public function run() { + if ( !$this->params['options'] ) { + return true; // nothing to do + } + + $user = User::newFromId( $this->params['userId'] ); + $user->load( $user::READ_EXCLUSIVE ); + if ( !$user->getId() ) { + return true; + } + + foreach ( $this->params['options'] as $name => $value ) { + $user->setOption( $name, $value ); + } + + $user->saveSettings(); + + return true; + } +} -- 2.20.1