2012-09-16 17:31:47 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2017-02-16 16:25:21 +00:00
|
|
|
* Generate fancy captchas using a python script and copy them into storage.
|
2012-09-16 17:31:47 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* @author Aaron Schulz
|
|
|
|
* @ingroup Maintenance
|
|
|
|
*/
|
|
|
|
if ( getenv( 'MW_INSTALL_PATH' ) ) {
|
|
|
|
$IP = getenv( 'MW_INSTALL_PATH' );
|
|
|
|
} else {
|
2017-02-07 18:02:46 +00:00
|
|
|
$IP = __DIR__ . '/../../..';
|
2012-09-16 17:31:47 +00:00
|
|
|
}
|
|
|
|
|
2017-07-08 13:55:41 +00:00
|
|
|
require_once "$IP/maintenance/Maintenance.php";
|
2012-09-16 17:31:47 +00:00
|
|
|
|
2022-04-08 16:53:12 +00:00
|
|
|
use MediaWiki\Extension\ConfirmEdit\FancyCaptcha\FancyCaptcha;
|
2022-04-08 16:40:15 +00:00
|
|
|
use MediaWiki\Extension\ConfirmEdit\Hooks;
|
2024-01-15 23:12:43 +00:00
|
|
|
use MediaWiki\Shell\Shell;
|
2022-04-08 16:40:15 +00:00
|
|
|
|
2012-09-16 17:31:47 +00:00
|
|
|
/**
|
2017-02-16 16:25:21 +00:00
|
|
|
* Maintenance script to generate fancy captchas using a python script and copy them into storage.
|
2012-09-16 17:31:47 +00:00
|
|
|
*
|
|
|
|
* @ingroup Maintenance
|
|
|
|
*/
|
|
|
|
class GenerateFancyCaptchas extends Maintenance {
|
|
|
|
public function __construct() {
|
|
|
|
parent::__construct();
|
2017-02-07 18:02:46 +00:00
|
|
|
|
2012-09-16 17:31:47 +00:00
|
|
|
// See captcha.py for argument usage
|
|
|
|
$this->addOption( "wordlist", 'A list of words', true, true );
|
|
|
|
$this->addOption( "font", "The font to use", true, true );
|
|
|
|
$this->addOption( "font-size", "The font size ", false, true );
|
2022-03-29 17:42:29 +00:00
|
|
|
$this->addOption( "badwordlist", "A list of words that should not be used", false, true );
|
|
|
|
$this->addOption( "blacklist", "DEPRECATED: A list of words that should not be used", false, true );
|
2012-09-16 17:31:47 +00:00
|
|
|
$this->addOption( "fill", "Fill the captcha container to N files", true, true );
|
2019-10-16 21:14:58 +00:00
|
|
|
$this->addOption(
|
|
|
|
"verbose",
|
|
|
|
"Show debugging information when running the captcha python script"
|
|
|
|
);
|
2016-11-04 18:52:55 +00:00
|
|
|
$this->addOption(
|
|
|
|
"oldcaptcha",
|
|
|
|
"Whether to use captcha-old.py which doesn't have OCR fighting improvements"
|
|
|
|
);
|
2019-10-16 21:14:58 +00:00
|
|
|
$this->addOption( "delete", "Deletes all the old captchas" );
|
2017-02-10 18:04:12 +00:00
|
|
|
$this->addOption( "threads", "The number of threads to use to generate the images",
|
|
|
|
false, true );
|
2019-08-19 17:03:36 +00:00
|
|
|
$this->addDescription( "Generate new fancy captchas and move them into storage" );
|
2017-02-09 22:05:27 +00:00
|
|
|
|
2017-01-01 17:20:53 +00:00
|
|
|
$this->requireExtension( "FancyCaptcha" );
|
2012-09-16 17:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
global $wgCaptchaSecret, $wgCaptchaDirectoryLevels;
|
|
|
|
|
2017-02-09 22:05:27 +00:00
|
|
|
$totalTime = -microtime( true );
|
|
|
|
|
2022-04-08 16:40:15 +00:00
|
|
|
$instance = Hooks::getInstance();
|
2012-09-16 17:31:47 +00:00
|
|
|
if ( !( $instance instanceof FancyCaptcha ) ) {
|
2020-09-18 16:11:12 +00:00
|
|
|
$this->fatalError( "\$wgCaptchaClass is not FancyCaptcha.\n", 1 );
|
2012-09-16 17:31:47 +00:00
|
|
|
}
|
|
|
|
$backend = $instance->getBackend();
|
|
|
|
|
2016-11-21 20:57:57 +00:00
|
|
|
$deleteOldCaptchas = $this->getOption( 'delete' );
|
|
|
|
|
|
|
|
$countGen = (int)$this->getOption( 'fill' );
|
|
|
|
if ( !$deleteOldCaptchas ) {
|
2017-02-07 18:02:46 +00:00
|
|
|
$countAct = $instance->getCaptchaCount();
|
|
|
|
$this->output( "Current number of captchas is $countAct.\n" );
|
2016-11-21 20:57:57 +00:00
|
|
|
$countGen -= $countAct;
|
|
|
|
}
|
2012-09-16 17:31:47 +00:00
|
|
|
|
|
|
|
if ( $countGen <= 0 ) {
|
|
|
|
$this->output( "No need to generate anymore captchas.\n" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$tmpDir = wfTempDir() . '/mw-fancycaptcha-' . time() . '-' . wfRandomString( 6 );
|
|
|
|
if ( !wfMkdirParents( $tmpDir ) ) {
|
2020-09-18 16:11:12 +00:00
|
|
|
$this->fatalError( "Could not create temp directory.\n", 1 );
|
2012-09-16 17:31:47 +00:00
|
|
|
}
|
|
|
|
|
2017-03-09 18:43:41 +00:00
|
|
|
$captchaScript = 'captcha.py';
|
2016-11-04 18:52:55 +00:00
|
|
|
|
2017-03-09 18:43:41 +00:00
|
|
|
if ( $this->hasOption( 'oldcaptcha' ) ) {
|
|
|
|
$captchaScript = 'captcha-old.py';
|
|
|
|
}
|
|
|
|
|
2024-01-15 23:12:43 +00:00
|
|
|
$cmd = [
|
|
|
|
"python3",
|
|
|
|
dirname( __DIR__ ) . '/' . $captchaScript,
|
|
|
|
"--key",
|
|
|
|
$wgCaptchaSecret,
|
|
|
|
"--output",
|
|
|
|
$tmpDir,
|
|
|
|
"--count",
|
|
|
|
(string)$countGen,
|
|
|
|
"--dirs",
|
|
|
|
$wgCaptchaDirectoryLevels
|
|
|
|
];
|
2017-02-10 18:04:12 +00:00
|
|
|
foreach (
|
2022-03-29 17:42:29 +00:00
|
|
|
[ 'wordlist', 'font', 'font-size', 'blacklist', 'badwordlist', 'verbose', 'threads' ] as $par
|
2017-02-10 18:04:12 +00:00
|
|
|
) {
|
2017-03-09 18:43:41 +00:00
|
|
|
if ( $this->hasOption( $par ) ) {
|
2024-01-15 23:12:43 +00:00
|
|
|
$cmd[] = "--$par";
|
|
|
|
$cmd[] = $this->getOption( $par );
|
2016-11-04 18:52:55 +00:00
|
|
|
}
|
2017-03-09 18:43:41 +00:00
|
|
|
}
|
2016-11-04 18:52:55 +00:00
|
|
|
|
2017-03-09 18:43:41 +00:00
|
|
|
$this->output( "Generating $countGen new captchas.." );
|
|
|
|
$captchaTime = -microtime( true );
|
2024-01-15 23:12:43 +00:00
|
|
|
$result = Shell::command( [] )
|
2024-01-19 13:54:59 +00:00
|
|
|
->params( $cmd )
|
2024-01-15 23:12:43 +00:00
|
|
|
->limits( [ 'time' => 0 ] )
|
|
|
|
->execute();
|
|
|
|
if ( $result->getExitCode() != 0 ) {
|
2019-10-16 21:06:57 +00:00
|
|
|
$this->output( " Failed.\n" );
|
2017-03-09 18:43:41 +00:00
|
|
|
wfRecursiveRemoveDir( $tmpDir );
|
2024-01-15 23:12:43 +00:00
|
|
|
$this->fatalError( "An error occurred when running $captchaScript.\n", 1 );
|
2017-03-09 18:43:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$captchaTime += microtime( true );
|
|
|
|
$this->output( " Done.\n" );
|
|
|
|
|
|
|
|
$this->output(
|
|
|
|
sprintf(
|
|
|
|
"\nGenerated %d captchas in %.1f seconds\n",
|
|
|
|
$countGen,
|
|
|
|
$captchaTime
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$filesToDelete = [];
|
|
|
|
if ( $deleteOldCaptchas ) {
|
|
|
|
$this->output( "Getting a list of old captchas to delete..." );
|
|
|
|
$path = $backend->getRootStoragePath() . '/captcha-render';
|
|
|
|
foreach ( $backend->getFileList( [ 'dir' => $path ] ) as $file ) {
|
|
|
|
$filesToDelete[] = [
|
|
|
|
'op' => 'delete',
|
|
|
|
'src' => $path . '/' . $file,
|
|
|
|
];
|
2012-09-16 17:31:47 +00:00
|
|
|
}
|
2017-03-09 18:43:41 +00:00
|
|
|
$this->output( " Done.\n" );
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->output( "Copying the new captchas to storage..." );
|
2012-09-16 17:31:47 +00:00
|
|
|
|
2017-03-09 18:43:41 +00:00
|
|
|
$storeTime = -microtime( true );
|
|
|
|
$iter = new RecursiveIteratorIterator(
|
|
|
|
new RecursiveDirectoryIterator(
|
|
|
|
$tmpDir,
|
|
|
|
FilesystemIterator::SKIP_DOTS
|
|
|
|
),
|
|
|
|
RecursiveIteratorIterator::LEAVES_ONLY
|
|
|
|
);
|
|
|
|
|
|
|
|
$captchasGenerated = iterator_count( $iter );
|
|
|
|
$filesToStore = [];
|
|
|
|
/**
|
|
|
|
* @var $fileInfo SplFileInfo
|
|
|
|
*/
|
|
|
|
foreach ( $iter as $fileInfo ) {
|
|
|
|
if ( !$fileInfo->isFile() ) {
|
|
|
|
continue;
|
2012-09-16 17:31:47 +00:00
|
|
|
}
|
2024-01-15 23:12:43 +00:00
|
|
|
[ $salt, $hash ] = $instance->hashFromImageName( $fileInfo->getBasename() );
|
2017-03-09 18:43:41 +00:00
|
|
|
$dest = $instance->imagePath( $salt, $hash );
|
|
|
|
$backend->prepare( [ 'dir' => dirname( $dest ) ] );
|
|
|
|
$filesToStore[] = [
|
|
|
|
'op' => 'store',
|
|
|
|
'src' => $fileInfo->getPathname(),
|
|
|
|
'dst' => $dest,
|
|
|
|
];
|
|
|
|
}
|
2012-09-16 17:31:47 +00:00
|
|
|
|
2017-03-09 18:43:41 +00:00
|
|
|
$ret = $backend->doQuickOperations( $filesToStore );
|
2012-09-16 17:31:47 +00:00
|
|
|
|
2017-03-09 18:43:41 +00:00
|
|
|
$storeTime += microtime( true );
|
|
|
|
|
2018-08-25 15:22:48 +00:00
|
|
|
$storeSucceeded = true;
|
2017-03-09 18:43:41 +00:00
|
|
|
if ( $ret->isOK() ) {
|
|
|
|
$this->output( " Done.\n" );
|
2017-02-09 22:05:27 +00:00
|
|
|
$this->output(
|
|
|
|
sprintf(
|
2017-03-09 18:43:41 +00:00
|
|
|
"\nCopied %d captchas to storage in %.1f seconds\n",
|
|
|
|
$ret->successCount,
|
|
|
|
$storeTime
|
2017-02-09 22:05:27 +00:00
|
|
|
)
|
|
|
|
);
|
2017-03-09 18:43:41 +00:00
|
|
|
if ( !$ret->isGood() ) {
|
|
|
|
$this->output(
|
|
|
|
"Non fatal errors:\n" .
|
2019-12-12 18:59:33 +00:00
|
|
|
Status::wrap( $ret )->getWikiText( false, false, 'en' ) .
|
2017-03-09 18:43:41 +00:00
|
|
|
"\n"
|
|
|
|
);
|
2016-11-21 20:57:57 +00:00
|
|
|
}
|
2017-03-09 18:43:41 +00:00
|
|
|
if ( $ret->failCount ) {
|
2018-08-25 15:22:48 +00:00
|
|
|
$storeSucceeded = false;
|
2017-03-09 18:43:41 +00:00
|
|
|
$this->error( sprintf( "\nFailed to copy %d captchas\n", $ret->failCount ) );
|
2012-09-16 17:31:47 +00:00
|
|
|
}
|
2017-03-09 18:43:41 +00:00
|
|
|
if ( $ret->successCount + $ret->failCount !== $captchasGenerated ) {
|
2018-08-25 15:22:48 +00:00
|
|
|
$storeSucceeded = false;
|
2017-03-09 18:43:41 +00:00
|
|
|
$this->error(
|
|
|
|
sprintf( "Internal error: captchasGenerated: %d, successCount: %d, failCount: %d\n",
|
|
|
|
$captchasGenerated, $ret->successCount, $ret->failCount
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
2018-08-25 15:22:48 +00:00
|
|
|
$storeSucceeded = false;
|
2017-03-09 18:43:41 +00:00
|
|
|
$this->output( "Errored.\n" );
|
|
|
|
$this->error(
|
2019-12-12 18:59:33 +00:00
|
|
|
Status::wrap( $ret )->getWikiText( false, false, 'en' ) .
|
2017-03-09 18:43:41 +00:00
|
|
|
"\n"
|
|
|
|
);
|
|
|
|
}
|
2017-02-16 21:06:12 +00:00
|
|
|
|
2018-08-25 15:22:48 +00:00
|
|
|
if ( $storeSucceeded && $deleteOldCaptchas ) {
|
2017-03-09 18:43:41 +00:00
|
|
|
$numOriginalFiles = count( $filesToDelete );
|
|
|
|
$this->output( "Deleting {$numOriginalFiles} old captchas...\n" );
|
|
|
|
$deleteTime = -microtime( true );
|
|
|
|
$ret = $backend->doQuickOperations( $filesToDelete );
|
2017-02-09 22:05:27 +00:00
|
|
|
|
2017-03-09 18:43:41 +00:00
|
|
|
$deleteTime += microtime( true );
|
2017-02-16 21:06:12 +00:00
|
|
|
if ( $ret->isOK() ) {
|
2017-03-09 18:43:41 +00:00
|
|
|
$this->output( "Done.\n" );
|
2017-02-16 21:06:12 +00:00
|
|
|
$this->output(
|
|
|
|
sprintf(
|
2017-03-09 18:43:41 +00:00
|
|
|
"\nDeleted %d old captchas in %.1f seconds\n",
|
|
|
|
$numOriginalFiles,
|
|
|
|
$deleteTime
|
2017-02-16 21:06:12 +00:00
|
|
|
)
|
|
|
|
);
|
2017-03-09 18:43:41 +00:00
|
|
|
if ( !$ret->isGood() ) {
|
2017-03-04 00:37:21 +00:00
|
|
|
$this->output(
|
2017-03-09 18:43:41 +00:00
|
|
|
"Non fatal errors:\n" .
|
2019-12-12 18:59:33 +00:00
|
|
|
Status::wrap( $ret )->getWikiText( false, false, 'en' ) .
|
2017-03-09 18:43:41 +00:00
|
|
|
"\n"
|
2017-03-04 00:37:21 +00:00
|
|
|
);
|
|
|
|
}
|
2017-02-16 21:06:12 +00:00
|
|
|
} else {
|
|
|
|
$this->output( "Errored.\n" );
|
2017-03-09 18:43:41 +00:00
|
|
|
$this->error(
|
2019-12-12 18:59:33 +00:00
|
|
|
Status::wrap( $ret )->getWikiText( false, false, 'en' ) .
|
2017-03-09 18:43:41 +00:00
|
|
|
"\n"
|
|
|
|
);
|
2017-02-16 21:06:12 +00:00
|
|
|
}
|
2016-11-21 20:57:57 +00:00
|
|
|
|
2012-09-16 17:31:47 +00:00
|
|
|
}
|
2017-02-09 22:05:27 +00:00
|
|
|
$this->output( "Removing temporary files..." );
|
2012-09-16 17:31:47 +00:00
|
|
|
wfRecursiveRemoveDir( $tmpDir );
|
2017-02-09 22:05:27 +00:00
|
|
|
$this->output( " Done.\n" );
|
|
|
|
|
|
|
|
$totalTime += microtime( true );
|
|
|
|
$this->output(
|
|
|
|
sprintf(
|
|
|
|
"\nWhole captchas generation process took %.1f seconds\n",
|
|
|
|
$totalTime
|
|
|
|
)
|
|
|
|
);
|
2012-09-16 17:31:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-02 18:39:36 +00:00
|
|
|
$maintClass = GenerateFancyCaptchas::class;
|
2017-07-08 13:55:41 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|