| 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 3 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 | */ |
| 20 | |
| 21 | use MediaWiki\MediaWikiServices; |
| 22 | use MediaWiki\User\User; |
| 23 | use Wikimedia\Rdbms\IDBAccessObject; |
| 24 | |
| 25 | /** |
| 26 | * Job that updates a user's preferences. |
| 27 | * |
| 28 | * The following job parameters are required: |
| 29 | * - userId: the user ID |
| 30 | * - options: a map of (option => value) |
| 31 | * |
| 32 | * @since 1.33 |
| 33 | * @ingroup JobQueue |
| 34 | */ |
| 35 | class UserOptionsUpdateJob extends Job implements GenericParameterJob { |
| 36 | public function __construct( array $params ) { |
| 37 | parent::__construct( 'userOptionsUpdate', $params ); |
| 38 | $this->removeDuplicates = true; |
| 39 | } |
| 40 | |
| 41 | public function run() { |
| 42 | if ( !$this->params['options'] ) { |
| 43 | return true; // nothing to do |
| 44 | } |
| 45 | |
| 46 | $user = User::newFromId( $this->params['userId'] ); |
| 47 | $user->load( IDBAccessObject::READ_EXCLUSIVE ); |
| 48 | if ( !$user->isNamed() ) { |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | $userOptionsManager = MediaWikiServices::getInstance() |
| 53 | ->getUserOptionsManager(); |
| 54 | foreach ( $this->params['options'] as $name => $value ) { |
| 55 | $userOptionsManager->setOption( $user, $name, $value ); |
| 56 | } |
| 57 | |
| 58 | $user->saveSettings(); |
| 59 | |
| 60 | return true; |
| 61 | } |
| 62 | } |