Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 87
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
FlaggedRevsSetup
0.00% covered (danger)
0.00%
0 / 87
0.00% covered (danger)
0.00%
0 / 7
272
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 doSetup
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 setAutopromoteConfig
0.00% covered (danger)
0.00%
0 / 67
0.00% covered (danger)
0.00%
0 / 1
42
 setSpecialPageCacheUpdates
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 setAPIModules
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 setConditionalRights
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 setConditionalPreferences
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3use MediaWiki\Config\Config;
4
5/**
6 * Class containing basic setup functions.
7 * This class depends on config variables in LocalSettings.php.
8 * Note: avoid FlaggedRevs class calls here for performance (like load.php).
9 */
10class FlaggedRevsSetup {
11
12    private Config $config;
13
14    public function __construct( Config $config ) {
15        $this->config = $config;
16    }
17
18    /**
19     * Entry point for hook handler
20     *
21     * TODO make this the hook handler directly, and combine the methods here
22     */
23    public function doSetup() {
24        # Conditional autopromote groups
25        $this->setAutopromoteConfig();
26
27        # Register special pages (some are conditional)
28        $this->setSpecialPageCacheUpdates();
29        # Conditional API modules
30        $this->setAPIModules();
31        # Remove conditionally applicable rights
32        $this->setConditionalRights();
33        # Defaults for user preferences
34        $this->setConditionalPreferences();
35    }
36
37    /**
38     * Set $wgAutopromoteOnce
39     */
40    private function setAutopromoteConfig() {
41        global $wgAutopromoteOnce, $wgGroupPermissions;
42
43        # $wgFlaggedRevsAutoconfirm is now a wrapper around $wgAutopromoteOnce
44        $autoconfirm = $this->config->get( 'FlaggedRevsAutoconfirm' );
45        if ( is_array( $autoconfirm ) ) {
46            $criteria = [ '&', // AND
47                [ APCOND_AGE, $autoconfirm['days'] * 86400 ],
48                [ APCOND_EDITCOUNT, $autoconfirm['edits'] ],
49                [ APCOND_FR_EDITSUMMARYCOUNT, $autoconfirm['editComments'] ],
50                [ APCOND_FR_UNIQUEPAGECOUNT, $autoconfirm['uniqueContentPages'] ],
51                [
52                    APCOND_FR_EDITSPACING,
53                    $autoconfirm['spacing'],
54                    $autoconfirm['benchmarks']
55                ],
56                [ '|', // OR
57                    [
58                        APCOND_FR_CONTENTEDITCOUNT,
59                        $autoconfirm['totalContentEdits'],
60                        $autoconfirm['excludeLastDays'] * 86400
61                    ],
62                    [
63                        APCOND_FR_CHECKEDEDITCOUNT,
64                        $autoconfirm['totalCheckedEdits'],
65                        $autoconfirm['excludeLastDays'] * 86400
66                    ]
67                ],
68            ];
69            if ( $autoconfirm['email'] ) {
70                $criteria[] = [ APCOND_EMAILCONFIRMED ];
71            }
72            if ( $autoconfirm['neverBlocked'] ) {
73                $criteria[] = [ APCOND_FR_NEVERBLOCKED ];
74            }
75            $wgAutopromoteOnce['onEdit']['autoreview'] = $criteria;
76            $wgGroupPermissions['autoreview']['autoreview'] = true;
77        }
78
79        # $wgFlaggedRevsAutopromote is now a wrapper around $wgAutopromoteOnce
80        $autopromote = $this->config->get( 'FlaggedRevsAutopromote' );
81        if ( is_array( $autopromote ) ) {
82            $criteria = [ '&', // AND
83                [ APCOND_AGE, $autopromote['days'] * 86400 ],
84                [
85                    APCOND_FR_EDITCOUNT,
86                    $autopromote['edits'],
87                    $autopromote['excludeLastDays'] * 86400
88                ],
89                [ APCOND_FR_EDITSUMMARYCOUNT, $autopromote['editComments'] ],
90                [ APCOND_FR_UNIQUEPAGECOUNT, $autopromote['uniqueContentPages'] ],
91                [ APCOND_FR_USERPAGEBYTES, $autopromote['userpageBytes'] ],
92                [ APCOND_FR_NEVERDEMOTED ], // for b/c
93                [
94                    APCOND_FR_EDITSPACING,
95                    $autopromote['spacing'],
96                    $autopromote['benchmarks']
97                ],
98                [ '|', // OR
99                    [
100                        APCOND_FR_CONTENTEDITCOUNT,
101                        $autopromote['totalContentEdits'],
102                        $autopromote['excludeLastDays'] * 86400
103                    ],
104                    [
105                        APCOND_FR_CHECKEDEDITCOUNT,
106                        $autopromote['totalCheckedEdits'],
107                        $autopromote['excludeLastDays'] * 86400
108                    ]
109                ],
110                [ APCOND_FR_MAXREVERTEDEDITRATIO, $autopromote['maxRevertedEditRatio'] ],
111                [ '!', APCOND_ISBOT ]
112            ];
113            if ( $autopromote['neverBlocked'] ) {
114                $criteria[] = [ APCOND_FR_NEVERBLOCKED ];
115            }
116            $wgAutopromoteOnce['onEdit']['editor'] = $criteria;
117        }
118    }
119
120    private function setSpecialPageCacheUpdates() {
121        global $wgSpecialPageCacheUpdates;
122
123        // Show special pages only if FlaggedRevs is enabled on some namespaces
124        if ( $this->config->get( 'FlaggedRevsNamespaces' ) ) {
125            if ( !$this->config->get( 'FlaggedRevsProtection' ) ) {
126                $wgSpecialPageCacheUpdates['UnreviewedPages'] = [ UnreviewedPages::class, 'updateQueryCache' ];
127            }
128            $wgSpecialPageCacheUpdates['ValidationStatistics'] = [ FlaggedRevsStats::class, 'updateCache' ];
129        }
130    }
131
132    private function setAPIModules() {
133        global $wgAPIModules, $wgAPIListModules;
134
135        if ( $this->config->get( 'FlaggedRevsProtection' ) ) {
136            $wgAPIModules['stabilize'] = ApiStabilizeProtect::class;
137        } else {
138            $wgAPIModules['stabilize'] = ApiStabilizeGeneral::class;
139            $wgAPIListModules['unreviewedpages'] = ApiQueryUnreviewedpages::class;
140            $wgAPIListModules['configuredpages'] = ApiQueryConfiguredpages::class;
141        }
142    }
143
144    /**
145     * Remove irrelevant user rights
146     */
147    private function setConditionalRights() {
148        global $wgGroupPermissions;
149
150        if ( $this->config->get( 'FlaggedRevsProtection' ) ) {
151            // XXX: Removes sp:ListGroupRights cruft
152            unset( $wgGroupPermissions['editor']['unreviewedpages'] );
153            unset( $wgGroupPermissions['reviewer']['unreviewedpages'] );
154            unset( $wgGroupPermissions['reviewer']['validate'] );
155        }
156    }
157
158    /**
159     * Set $wgDefaultUserOptions
160     */
161    private function setConditionalPreferences() {
162        global $wgDefaultUserOptions;
163
164        $wgDefaultUserOptions['flaggedrevssimpleui'] = (int)$this->config->get( 'SimpleFlaggedRevsUI' );
165    }
166}