Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.49% covered (success)
91.49%
43 / 47
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
DeleteOldFancyCaptchas
91.49% covered (success)
91.49%
43 / 47
50.00% covered (danger)
50.00%
1 / 2
9.05
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 execute
87.50% covered (warning)
87.50%
28 / 32
0.00% covered (danger)
0.00%
0 / 1
8.12
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 */
23
24namespace MediaWiki\Extension\ConfirmEdit\Maintenance;
25
26// @codeCoverageIgnoreStart
27if ( getenv( 'MW_INSTALL_PATH' ) ) {
28    $IP = getenv( 'MW_INSTALL_PATH' );
29} else {
30    $IP = __DIR__ . '/../../..';
31}
32
33require_once "$IP/maintenance/Maintenance.php";
34// @codeCoverageIgnoreEnd
35
36use MediaWiki\Extension\ConfirmEdit\FancyCaptcha\FancyCaptcha;
37use MediaWiki\Extension\ConfirmEdit\Hooks;
38use MediaWiki\Maintenance\Maintenance;
39use MediaWiki\Status\Status;
40
41/**
42 * Maintenance script that deletes old fancy captchas from storage
43 *
44 * @ingroup Maintenance
45 */
46class DeleteOldFancyCaptchas extends Maintenance {
47    public function __construct() {
48        parent::__construct();
49        $this->addDescription( "Deletes old fancy captchas from storage" );
50        $this->addOption(
51            "date",
52            'Delete fancy captchas that were created before this date (e.g. 20170101000000)',
53            true,
54            true
55        );
56        $this->addOption(
57            'captchastoragedir',
58            'Overrides the value of $wgCaptchaStorageDirectory',
59            false,
60            true
61        );
62        $this->requireExtension( "FancyCaptcha" );
63    }
64
65    public function execute() {
66        $instance = Hooks::getInstance();
67        if ( !( $instance instanceof FancyCaptcha ) ) {
68            $this->fatalError( "\$wgCaptchaClass is not FancyCaptcha.\n", 1 );
69        }
70
71        // Overrides $wgCaptchaStorageDirectory for this script run
72        if ( $this->hasOption( 'captchastoragedir' ) ) {
73            global $wgCaptchaStorageDirectory;
74            $wgCaptchaStorageDirectory = $this->getOption( 'captchastoragedir' );
75        }
76
77        $countAct = $instance->getCaptchaCount();
78        $this->output( "Current number of captchas is $countAct.\n" );
79
80        $backend = $instance->getBackend();
81        $dir = $backend->getRootStoragePath() . '/' . $instance->getStorageDir();
82
83        $filesToDelete = [];
84        $deleteDate = $this->getOption( 'date' );
85        foreach (
86            $backend->getFileList( [ 'dir' => $dir, 'adviseStat' => true ] ) as $file
87        ) {
88            $fullPath = $dir . '/' . $file;
89            $timestamp = $backend->getFileTimestamp( [ 'src' => $fullPath ] );
90            if ( $timestamp < $deleteDate ) {
91                $filesToDelete[] = [ 'op' => 'delete', 'src' => $fullPath, ];
92            }
93        }
94
95        $count = count( $filesToDelete );
96
97        if ( !$count ) {
98            $this->output( "No old fancy captchas to delete!\n" );
99            return;
100        }
101
102        $this->output( "$count old fancy captchas to be deleted.\n" );
103
104        $deletedCount = 0;
105        foreach ( array_chunk( $filesToDelete, 1000 ) as $chunk ) {
106            $ret = $backend->doQuickOperations( $chunk );
107
108            if ( $ret->isOK() ) {
109                $chunkCount = count( $chunk );
110                $this->output( "$chunkCount...\n" );
111                $deletedCount += $chunkCount;
112            } else {
113                $status = Status::wrap( $ret );
114                $this->output( "Deleting old captchas errored.\n" );
115                $this->output( $status->getWikiText( false, false, 'en' ) );
116            }
117        }
118
119        $this->output( "$deletedCount old fancy captchas deleted.\n" );
120    }
121}
122
123// @codeCoverageIgnoreStart
124$maintClass = DeleteOldFancyCaptchas::class;
125require_once RUN_MAINTENANCE_IF_MAIN;
126// @codeCoverageIgnoreEnd