Debian Codegraph @

extensions/ConfirmEdit/maintenance/DeleteOldFancyCaptchas.php

raw symbols SCIP
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 use MediaWiki\Maintenance\Maintenance;
34 use MediaWiki\Status\Status;
35
36 /**
37 * Maintenance script that deletes old fancy captchas from storage
38 *
39 * @ingroup Maintenance
40 */
41 class DeleteOldFancyCaptchas extends Maintenance {
42 public function __construct() {
43 parent::__construct();
44 $this->addDescription( "Deletes old fancy captchas from storage" );
45 $this->addOption(
46 "date",
47 'Delete fancy captchas that were created before this date (e.g. 20170101000000)',
48 true,
49 true
50 );
51 $this->addOption(
52 'captchastoragedir',
53 'Overrides the value of $wgCaptchaStorageDirectory',
54 false,
55 true
56 );
57 $this->requireExtension( "FancyCaptcha" );
58 }
59
60 public function execute() {
61 $instance = Hooks::getInstance();
62 if ( !( $instance instanceof FancyCaptcha ) ) {
63 $this->fatalError( "\$wgCaptchaClass is not FancyCaptcha.\n", 1 );
64 }
65
66 // Overrides $wgCaptchaStorageDirectory for this script run
67 if ( $this->hasOption( 'captchastoragedir' ) ) {
68 global $wgCaptchaStorageDirectory;
69 $wgCaptchaStorageDirectory = $this->getOption( 'captchastoragedir' );
70 }
71
72 $countAct = $instance->getCaptchaCount();
73 $this->output( "Current number of captchas is $countAct.\n" );
74
75 $backend = $instance->getBackend();
76 $dir = $backend->getRootStoragePath() . '/' . $instance->getStorageDir();
77
78 $filesToDelete = [];
79 $deleteDate = $this->getOption( 'date' );
80 foreach (
81 $backend->getFileList( [ 'dir' => $dir, 'adviseStat' => true ] ) as $file
82 ) {
83 $fullPath = $dir . '/' . $file;
84 $timestamp = $backend->getFileTimestamp( [ 'src' => $fullPath ] );
85 if ( $timestamp < $deleteDate ) {
86 $filesToDelete[] = [ 'op' => 'delete', 'src' => $fullPath, ];
87 }
88 }
89
90 $count = count( $filesToDelete );
91
92 if ( !$count ) {
93 $this->output( "No old fancy captchas to delete!\n" );
94 return;
95 }
96
97 $this->output( "$count old fancy captchas to be deleted.\n" );
98
99 $deletedCount = 0;
100 foreach ( array_chunk( $filesToDelete, 1000 ) as $chunk ) {
101 $ret = $backend->doQuickOperations( $chunk );
102
103 if ( $ret->isOK() ) {
104 $chunkCount = count( $chunk );
105 $this->output( "$chunkCount...\n" );
106 $deletedCount += $chunkCount;
107 } else {
108 $status = Status::wrap( $ret );
109 $this->output( "Deleting old captchas errored.\n" );
110 $this->output( $status->getWikiText( false, false, 'en' ) );
111 }
112 }
113
114 $this->output( "$deletedCount old fancy captchas deleted.\n" );
115 }
116 }
117
118 $maintClass = DeleteOldFancyCaptchas::class;
119 require_once RUN_MAINTENANCE_IF_MAIN;