| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 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 |
|
| 38 |
|
| 39 |
|
| 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 |
|
| 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; |