Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
DeleteOldFancyCaptchas
0.00% covered (danger)
0.00%
0 / 48
0.00% covered (danger)
0.00%
0 / 2
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
72
1<?php
2/**
3 * Deletes fancy captchas from storage
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Maintenance
22 */
23if ( getenv( 'MW_INSTALL_PATH' ) ) {
24    $IP = getenv( 'MW_INSTALL_PATH' );
25} else {
26    $IP = __DIR__ . '/../../..';
27}
28
29require_once "$IP/maintenance/Maintenance.php";
30
31use MediaWiki\Extension\ConfirmEdit\FancyCaptcha\FancyCaptcha;
32use MediaWiki\Extension\ConfirmEdit\Hooks;
33use MediaWiki\Status\Status;
34
35/**
36 * Maintenance script that deletes old fancy captchas from storage
37 *
38 * @ingroup Maintenance
39 */
40class DeleteOldFancyCaptchas extends Maintenance {
41    public function __construct() {
42        parent::__construct();
43        $this->addDescription( "Deletes old fancy captchas from storage" );
44        $this->addOption(
45            "date",
46            'Delete fancy captchas that were created before this date (e.g. 20170101000000)',
47            true,
48            true
49        );
50        $this->addOption(
51            'captchastoragedir',
52            'Overrides the value of $wgCaptchaStorageDirectory',
53            false,
54            true
55        );
56        $this->requireExtension( "FancyCaptcha" );
57    }
58
59    public function execute() {
60        $instance = Hooks::getInstance();
61        if ( !( $instance instanceof FancyCaptcha ) ) {
62            $this->fatalError( "\$wgCaptchaClass is not FancyCaptcha.\n", 1 );
63        }
64
65        // Overrides $wgCaptchaStorageDirectory for this script run
66        if ( $this->hasOption( 'captchastoragedir' ) ) {
67            global $wgCaptchaStorageDirectory;
68            $wgCaptchaStorageDirectory = $this->getOption( 'captchastoragedir' );
69        }
70
71        $countAct = $instance->getCaptchaCount();
72        $this->output( "Current number of captchas is $countAct.\n" );
73
74        $backend = $instance->getBackend();
75        $dir = $backend->getRootStoragePath() . '/' . $instance->getStorageDir();
76
77        $filesToDelete = [];
78        $deleteDate = $this->getOption( 'date' );
79        foreach (
80            $backend->getFileList( [ 'dir' => $dir, 'adviseStat' => true ] ) as $file
81        ) {
82            $fullPath = $dir . '/' . $file;
83            $timestamp = $backend->getFileTimestamp( [ 'src' => $fullPath ] );
84            if ( $timestamp < $deleteDate ) {
85                $filesToDelete[] = [ 'op' => 'delete', 'src' => $fullPath, ];
86            }
87        }
88
89        $count = count( $filesToDelete );
90
91        if ( !$count ) {
92            $this->output( "No old fancy captchas to delete!\n" );
93            return;
94        }
95
96        $this->output( "$count old fancy captchas to be deleted.\n" );
97
98        $deletedCount = 0;
99        foreach ( array_chunk( $filesToDelete, 1000 ) as $chunk ) {
100            $ret = $backend->doQuickOperations( $chunk );
101
102            if ( $ret->isOK() ) {
103                $chunkCount = count( $chunk );
104                $this->output( "$chunkCount...\n" );
105                $deletedCount += $chunkCount;
106            } else {
107                $status = Status::wrap( $ret );
108                $this->output( "Deleting old captchas errored.\n" );
109                $this->output( $status->getWikiText( false, false, 'en' ) );
110            }
111        }
112
113        $this->output( "$deletedCount old fancy captchas deleted.\n" );
114    }
115}
116
117$maintClass = DeleteOldFancyCaptchas::class;
118require_once RUN_MAINTENANCE_IF_MAIN;