2017-02-16 16:37:04 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Deletes fancy captchas from storage
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup Maintenance
|
|
|
|
*/
|
|
|
|
if ( getenv( 'MW_INSTALL_PATH' ) ) {
|
|
|
|
$IP = getenv( 'MW_INSTALL_PATH' );
|
|
|
|
} else {
|
|
|
|
$IP = __DIR__ . '/../../..';
|
|
|
|
}
|
|
|
|
|
2017-07-08 13:55:41 +00:00
|
|
|
require_once "$IP/maintenance/Maintenance.php";
|
2017-02-16 16:37:04 +00:00
|
|
|
|
2022-04-08 16:40:15 +00:00
|
|
|
use MediaWiki\Extension\ConfirmEdit\Hooks;
|
|
|
|
|
2017-02-16 16:37:04 +00:00
|
|
|
/**
|
|
|
|
* Maintenance script that deletes old fancy captchas from storage
|
|
|
|
*
|
|
|
|
* @ingroup Maintenance
|
|
|
|
*/
|
|
|
|
class DeleteOldFancyCaptchas extends Maintenance {
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
2019-08-19 17:03:36 +00:00
|
|
|
$this->addDescription( "Deletes old fancy captchas from storage" );
|
2017-02-16 16:37:04 +00:00
|
|
|
$this->addOption(
|
|
|
|
"date",
|
|
|
|
'Delete fancy captchas that were created before this date (e.g. 20170101000000)',
|
|
|
|
true,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
$this->requireExtension( "FancyCaptcha" );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute() {
|
2022-04-08 16:40:15 +00:00
|
|
|
$instance = Hooks::getInstance();
|
2017-02-16 16:37:04 +00:00
|
|
|
if ( !( $instance instanceof FancyCaptcha ) ) {
|
2020-09-18 16:11:12 +00:00
|
|
|
$this->fatalError( "\$wgCaptchaClass is not FancyCaptcha.\n", 1 );
|
2017-02-16 16:37:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$countAct = $instance->getCaptchaCount();
|
|
|
|
$this->output( "Current number of captchas is $countAct.\n" );
|
|
|
|
|
|
|
|
$backend = $instance->getBackend();
|
|
|
|
$dir = $backend->getRootStoragePath() . '/captcha-render';
|
|
|
|
|
|
|
|
$filesToDelete = [];
|
|
|
|
$deleteDate = $this->getOption( 'date' );
|
|
|
|
foreach (
|
2019-10-15 23:06:22 +00:00
|
|
|
$backend->getFileList( [ 'dir' => $dir, 'adviseStat' => true ] ) as $file
|
2017-02-16 16:37:04 +00:00
|
|
|
) {
|
|
|
|
$fullPath = $dir . '/' . $file;
|
|
|
|
$timestamp = $backend->getFileTimestamp( [ 'src' => $fullPath ] );
|
|
|
|
if ( $timestamp < $deleteDate ) {
|
|
|
|
$filesToDelete[] = [ 'op' => 'delete', 'src' => $fullPath, ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$count = count( $filesToDelete );
|
|
|
|
|
|
|
|
if ( !$count ) {
|
|
|
|
$this->output( "No old fancy captchas to delete!\n" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-15 23:10:35 +00:00
|
|
|
$this->output( "$count old fancy captchas to be deleted.\n" );
|
|
|
|
|
2021-10-19 14:28:08 +00:00
|
|
|
$deletedCount = 0;
|
|
|
|
foreach ( array_chunk( $filesToDelete, 1000 ) as $chunk ) {
|
|
|
|
$ret = $backend->doQuickOperations( $chunk );
|
2017-02-16 16:37:04 +00:00
|
|
|
|
2021-10-19 14:28:08 +00:00
|
|
|
if ( $ret->isOK() ) {
|
|
|
|
$chunkCount = count( $chunk );
|
|
|
|
$this->output( "$chunkCount...\n" );
|
|
|
|
$deletedCount += $chunkCount;
|
|
|
|
} else {
|
|
|
|
$status = Status::wrap( $ret );
|
|
|
|
$this->output( "Deleting old captchas errored.\n" );
|
|
|
|
$this->output( $status->getWikiText( false, false, 'en' ) );
|
|
|
|
}
|
2017-02-16 16:37:04 +00:00
|
|
|
}
|
2021-10-19 14:28:08 +00:00
|
|
|
|
|
|
|
$this->output( "$deletedCount old fancy captchas deleted.\n" );
|
2017-02-16 16:37:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 18:39:36 +00:00
|
|
|
$maintClass = DeleteOldFancyCaptchas::class;
|
2017-07-08 13:55:41 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|