From 18849474b9cad7d2fdf911dad0a85884b331f654 Mon Sep 17 00:00:00 2001 From: Reedy Date: Mon, 21 Nov 2016 20:57:57 +0000 Subject: [PATCH] Add --delete to GenerateFancyCaptchas Bug: T151244 Change-Id: I722b507dc76da4d7f0970e40bb777137b1c0b327 --- maintenance/GenerateFancyCaptchas.php | 37 ++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/maintenance/GenerateFancyCaptchas.php b/maintenance/GenerateFancyCaptchas.php index f955a4a07..2ef07f2c6 100644 --- a/maintenance/GenerateFancyCaptchas.php +++ b/maintenance/GenerateFancyCaptchas.php @@ -48,6 +48,7 @@ class GenerateFancyCaptchas extends Maintenance { "oldcaptcha", "Whether to use captcha-old.py which doesn't have OCR fighting improvements" ); + $this->addOption( "delete", "Delete the old captches" ); $this->mDescription = "Generate new captchas and move them into storage"; } @@ -60,10 +61,15 @@ class GenerateFancyCaptchas extends Maintenance { } $backend = $instance->getBackend(); - $countAct = $instance->estimateCaptchaCount(); - $this->output( "Estimated number of captchas is $countAct.\n" ); + $deleteOldCaptchas = $this->getOption( 'delete' ); + + $countGen = (int)$this->getOption( 'fill' ); + if ( !$deleteOldCaptchas ) { + $countAct = $instance->estimateCaptchaCount(); + $this->output( "Estimated number of current captchas is $countAct.\n" ); + $countGen -= $countAct; + } - $countGen = (int)$this->getOption( 'fill' ) - $countAct; if ( $countGen <= 0 ) { $this->output( "No need to generate anymore captchas.\n" ); return; @@ -110,6 +116,21 @@ class GenerateFancyCaptchas extends Maintenance { RecursiveIteratorIterator::CHILD_FIRST // include dirs ); + $this->output( "Done.\n" ); + + $originalFiles = []; + if ( $deleteOldCaptchas ) { + $this->output( "Getting a list of old captchas...\n" ); + foreach ( + $backend->getFileList( + [ 'dir' => $backend->getRootStoragePath() . '/captcha-render' ] + ) as $file + ) { + $originalFiles[] = $file; + } + $this->output( "Done.\n" ); + } + $this->output( "Copying the new captchas to storage...\n" ); foreach ( $iter as $fileInfo ) { if ( !$fileInfo->isFile() ) { @@ -126,6 +147,16 @@ class GenerateFancyCaptchas extends Maintenance { $this->error( "Could not save file '{$fileInfo->getPathname()}'.\n" ); } } + $this->output( "Done.\n" ); + + if ( $deleteOldCaptchas ) { + $numOriginalFiles = count( $originalFiles ); + $this->output( "Deleting {$numOriginalFiles} old captchas...\n" ); + foreach ( $originalFiles as $file ) { + $backend->quickDelete( [ 'src' => $file ] ); + } + $this->output( "Done.\n" ); + } } catch ( Exception $e ) { wfRecursiveRemoveDir( $tmpDir ); throw $e;