Add UserOptionsUpdateJob class
authorAaron Schulz <aschulz@wikimedia.org>
Fri, 29 Mar 2019 07:17:09 +0000 (00:17 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Fri, 3 May 2019 19:02:20 +0000 (12:02 -0700)
This can help avoid DB master queries on HTTP GET requests

Change-Id: I6fa2d53d96509d88f5d3e1ff18f3ca1de8fa4bb1

autoload.php
includes/DefaultSettings.php
includes/jobqueue/jobs/UserOptionsUpdateJob.php [new file with mode: 0644]

index f8e90b7..42ca6cc 100644 (file)
@@ -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',
index b40d33b..8a2828f 100644 (file)
@@ -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 (file)
index 0000000..0e8b19f
--- /dev/null
@@ -0,0 +1,58 @@
+<?php
+/**
+ * Job that updates a user's preferences.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup JobQueue
+ */
+
+/**
+ * Job that updates a user's preferences
+ *
+ * The following job parameters are required:
+ *   - userId: the user ID
+ *   - options: a map of (option => 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;
+       }
+}