summaryrefslogtreecommitdiff
path: root/extensions/Vector/switchExperimentPrefs.php
blob: 82ddd868a0bd3bc135585cf16e4981744f7f2dd9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php

$path = '../..';

if ( getenv( 'MW_INSTALL_PATH' ) !== false ) {
	$path = getenv( 'MW_INSTALL_PATH' );
}

require_once( $path . '/maintenance/Maintenance.php' );

class SwitchExperimentPrefs extends Maintenance {
	function __construct() {
		parent::__construct();
		$this->addOption( 'pref', 'Preference to set', true, true );
		$this->addOption( 'value', 'Value to set the preference to', true, true );
		$this->mDescription = 'Set a preference for all users that have the vector-noexperiments preference enabled.';
	}

	function execute() {
		$dbw = wfGetDB( DB_MASTER );

		$batchSize = 100;
		$total = 0;
		$lastUserID = 0;
		while ( true ) {
			$res = $dbw->select( 'user_properties', array( 'up_user' ),
				array( 'up_property' => 'vector-noexperiments', "up_user > $lastUserID" ),
				__METHOD__,
				array( 'LIMIT' => $batchSize ) );
			if ( !$res->numRows() ) {
				$dbw->commit();
				break;
			}
			$total += $res->numRows();

			$ids = array();
			foreach ( $res as $row ) {
				$ids[] = $row->up_user;
			}
			$lastUserID = max( $ids );
			
			
			foreach ( $ids as $id ) {
				$user = User::newFromId( $id );
				if ( !$user->isLoggedIn() )
					continue;
				$user->setOption( $this->getOption( 'pref' ), $this->getOption( 'value' ) );
				$user->saveSettings();
			}

			echo "$total\n";

			wfWaitForSlaves(); // Must be wfWaitForSlaves_masterPos(); on 1.17wmf1
		}
		echo "Done\n";

	}
}

$maintClass = 'SwitchExperimentPrefs';
require_once( RUN_MAINTENANCE_IF_MAIN );