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