Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
FlowUpdateBetaFeaturePreference
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 3
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 doDBUpdates
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 1
20
 getUpdateKey
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace Flow\Maintenance;
4
5use LoggedUpdateMaintenance;
6use MediaWiki\User\UserArray;
7
8$IP = getenv( 'MW_INSTALL_PATH' );
9if ( $IP === false ) {
10    $IP = __DIR__ . '/../../..';
11}
12
13require_once "$IP/maintenance/Maintenance.php";
14
15/**
16 * Sets Flow beta feature preference to true
17 * for users who are already using flow on
18 * their user talk page.
19 *
20 * @ingroup Maintenance
21 */
22class FlowUpdateBetaFeaturePreference extends LoggedUpdateMaintenance {
23
24    public function __construct() {
25        parent::__construct();
26        $this->setBatchSize( 300 );
27        $this->requireExtension( 'Flow' );
28    }
29
30    /**
31     * When the Flow beta feature is enable, it finds users
32     * who already have Flow enabled on their user talk page
33     * and opt them in the beta feature so their preferences
34     * and user talk page state are in sync.
35     *
36     * @return bool
37     */
38    protected function doDBUpdates() {
39        global $wgFlowEnableOptInBetaFeature;
40        if ( !$wgFlowEnableOptInBetaFeature ) {
41            return true;
42        }
43
44        $db = $this->getDB( DB_PRIMARY );
45
46        $innerQuery = $db->newSelectQueryBuilder()
47            ->select( 'up_user' )
48            ->from( 'user_properties' )
49            ->where( [
50                // It works because this beta feature is exempted from auto-enroll.
51                'up_property' => BETA_FEATURE_FLOW_USER_TALK_PAGE,
52                'up_value' => '1',
53            ] )
54            ->caller( __METHOD__ );
55
56        $result = $db->newSelectQueryBuilder()
57            ->select( 'user_id' )
58            ->from( 'page' )
59            ->join( 'user', null, [
60                'page_namespace' => NS_USER_TALK,
61                "page_title = REPLACE(user_name, ' ', '_')"
62            ] )
63            ->where( [
64                'page_content_model' => CONTENT_MODEL_FLOW_BOARD,
65                'user_id NOT IN(' . $innerQuery->getSQL() . ')',
66            ] )
67            ->caller( __METHOD__ )
68            ->fetchResultSet();
69
70        $services = $this->getServiceContainer();
71        $userOptionsManager = $services->getUserOptionsManager();
72
73        $i = 0;
74        $batchSize = $this->getBatchSize();
75        $users = UserArray::newFromResult( $result );
76        foreach ( $users as $user ) {
77            // It works because this beta feature is exempted from auto-enroll.
78            $userOptionsManager->setOption( $user, BETA_FEATURE_FLOW_USER_TALK_PAGE, 1 );
79            $userOptionsManager->saveOptions( $user );
80
81            if ( ++$i % $batchSize === 0 ) {
82                $this->waitForReplication();
83            }
84        }
85
86        return true;
87    }
88
89    /**
90     * Get the update key name to go in the update log table
91     *
92     * Returns a different key when the beta feature is enabled or disable
93     * so that enabling it would trigger this script
94     * to execute so it can correctly update users preferences.
95     *
96     * @return string
97     */
98    protected function getUpdateKey() {
99        global $wgFlowEnableOptInBetaFeature;
100        return $wgFlowEnableOptInBetaFeature ? 'FlowBetaFeatureEnable' : 'FlowBetaFeatureDisable';
101    }
102}
103
104$maintClass = FlowUpdateBetaFeaturePreference::class;
105require_once RUN_MAINTENANCE_IF_MAIN;