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