MediaWiki  master
UserOptionsUpdateJob.php
Go to the documentation of this file.
1 <?php
22 
33 class UserOptionsUpdateJob extends Job implements GenericParameterJob {
34  public function __construct( array $params ) {
35  parent::__construct( 'userOptionsUpdate', $params );
36  $this->removeDuplicates = true;
37  }
38 
39  public function run() {
40  if ( !$this->params['options'] ) {
41  return true; // nothing to do
42  }
43 
44  $user = User::newFromId( $this->params['userId'] );
45  $user->load( $user::READ_EXCLUSIVE );
46  if ( !$user->isRegistered() ) {
47  return true;
48  }
49 
50  $userOptionsManager = MediaWikiServices::getInstance()
51  ->getUserOptionsManager();
52  foreach ( $this->params['options'] as $name => $value ) {
53  $userOptionsManager->setOption( $user, $name, $value );
54  }
55 
56  $user->saveSettings();
57 
58  return true;
59  }
60 }
Class to both describe a background job and handle jobs.
Definition: Job.php:39
array $params
Array of job parameters.
Definition: Job.php:44
Service locator for MediaWiki core services.
Job that updates a user's preferences.
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:626
Interface for generic jobs only uses the parameters field and are JSON serializable.