Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 5
n/a
0 / 0
CRAP
n/a
0 / 0
UpdateDenyList
n/a
0 / 0
n/a
0 / 0
11
n/a
0 / 0
 __construct
n/a
0 / 0
n/a
0 / 0
1
 execute
n/a
0 / 0
n/a
0 / 0
10
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 2 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 * https://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\Extension\StopForumSpam\Maintenance;
22
23use Maintenance;
24use MediaWiki\Extension\StopForumSpam\DenyListManager;
25use Wikimedia\IPUtils;
26
27require_once getenv( 'MW_INSTALL_PATH' ) !== false
28    ? getenv( 'MW_INSTALL_PATH' ) . "/maintenance/Maintenance.php"
29    : __DIR__ . '/../../../maintenance/Maintenance.php';
30
31/**
32 * Reads the deny-list file and sticks it in the wancache
33 *
34 * @codeCoverageIgnore
35 */
36class UpdateDenyList extends Maintenance {
37
38    public function __construct() {
39        parent::__construct();
40        $this->addDescription( 'Load the list of StopForumSpam deny-listed ' .
41            'IPs when no additional arguments are passed.' );
42        $this->addOption(
43            'show',
44            'Print the current list of deny-listed IPs'
45        );
46        $this->addOption(
47            'purge',
48            'Delete the current list of deny-listed IPs from cache'
49        );
50        $this->addOption(
51            'check-ip',
52            'Check if a specific IP is within the cache',
53            false,
54            true
55        );
56        $this->requireExtension( 'StopForumSpam' );
57    }
58
59    public function execute() {
60        global $wgSFSIPListLocation;
61
62        if ( $wgSFSIPListLocation === false ) {
63            $this->fatalError( '$wgSFSIPListLocation has not been configured.' );
64        }
65
66        if ( $this->hasOption( 'show' ) ) {
67            $this->output( "List of SFS IPs...\n\n" );
68            $ipAddresses = DenyListManager::singleton()->getCachedIpDenyList();
69            $this->output(
70                is_array( $ipAddresses )
71                ? implode( "\n", $ipAddresses ) . "\n"
72                : "No deny-listed IPs found in cache.\n"
73            );
74            return;
75        }
76
77        if ( $this->hasOption( 'purge' ) ) {
78            $this->output( "Purge list of SFS IPs from cache...\n" );
79            $this->output(
80                DenyListManager::singleton()->purgeCachedIpDenyList() === true ?
81                "Purge successful\n" : "Purge failed.\n"
82            );
83            return;
84        }
85
86        if ( $this->hasOption( 'check-ip' ) ) {
87            $ipAddress = IPUtils::sanitizeIP( $this->getOption( 'check-ip' ) );
88            if ( $ipAddress !== null ) {
89                $this->output( "Checking cache for IP '{$ipAddress}'...\n\n" );
90                $result = DenyListManager::singleton()->isIpDenyListed( $ipAddress );
91                $outputMsg = $result == true ? "Found!" : "NOT Found!";
92                $this->output( "{$outputMsg}\n\n" );
93            } else {
94                $this->output( "Invalid IP address provided." );
95            }
96            return;
97        }
98
99        $this->output( "Starting update of SFS deny-list in cache...\n" );
100        $before = microtime( true );
101
102        // Where the magic happens!
103        $results = DenyListManager::singleton()->getIpDenyList( 'recache' );
104        if ( $results ) {
105            $diff = microtime( true ) - $before;
106
107            $numIPs = count( $results );
108            $this->output( "Done! Loaded {$numIPs} IPs.\n" );
109            $this->output( "Took {$diff} seconds\n" );
110        } else {
111            $this->fatalError( "Failed!\n" );
112        }
113    }
114
115}
116
117$maintClass = UpdateDenyList::class;
118require_once RUN_MAINTENANCE_IF_MAIN;