| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
if ( getenv( 'MW_INSTALL_PATH' ) ) { |
| 25 |
$IP = getenv( 'MW_INSTALL_PATH' ); |
| 26 |
} else { |
| 27 |
$IP = __DIR__ . '/../../..'; |
| 28 |
} |
| 29 |
|
| 30 |
require_once "$IP/maintenance/Maintenance.php"; |
| 31 |
|
| 32 |
use MediaWiki\Extension\ConfirmEdit\FancyCaptcha\FancyCaptcha; |
| 33 |
use MediaWiki\Extension\ConfirmEdit\Hooks; |
| 34 |
use MediaWiki\Maintenance\Maintenance; |
| 35 |
use MediaWiki\Shell\Shell; |
| 36 |
use MediaWiki\Status\Status; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
class GenerateFancyCaptchas extends Maintenance { |
| 44 |
public function __construct() { |
| 45 |
parent::__construct(); |
| 46 |
|
| 47 |
|
| 48 |
$this->addOption( "wordlist", 'A list of words', true, true ); |
| 49 |
$this->addOption( "font", "The font to use", true, true ); |
| 50 |
$this->addOption( "font-size", "The font size ", false, true ); |
| 51 |
$this->addOption( "badwordlist", "A list of words that should not be used", false, true ); |
| 52 |
$this->addOption( "fill", "Fill the captcha container to N files", true, true ); |
| 53 |
$this->addOption( |
| 54 |
"verbose", |
| 55 |
"Show debugging information when running the captcha python script" |
| 56 |
); |
| 57 |
$this->addOption( |
| 58 |
"oldcaptcha", |
| 59 |
"DEPRECATED: Whether to use captcha-old.py which doesn't have OCR fighting improvements" |
| 60 |
); |
| 61 |
$this->addOption( "delete", "Deletes all the old captchas" ); |
| 62 |
$this->addOption( "threads", "The number of threads to use to generate the images", |
| 63 |
false, true ); |
| 64 |
$this->addOption( |
| 65 |
'captchastoragedir', |
| 66 |
'Overrides the value of $wgCaptchaStorageDirectory', |
| 67 |
false, |
| 68 |
true |
| 69 |
); |
| 70 |
$this->addDescription( "Generate new fancy captchas and move them into storage" ); |
| 71 |
|
| 72 |
$this->requireExtension( "FancyCaptcha" ); |
| 73 |
} |
| 74 |
|
| 75 |
public function execute() { |
| 76 |
global $wgCaptchaSecret, $wgCaptchaDirectoryLevels; |
| 77 |
|
| 78 |
$totalTime = -microtime( true ); |
| 79 |
|
| 80 |
$instance = Hooks::getInstance(); |
| 81 |
if ( !( $instance instanceof FancyCaptcha ) ) { |
| 82 |
$this->fatalError( "\$wgCaptchaClass is not FancyCaptcha.\n", 1 ); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
if ( $this->hasOption( 'captchastoragedir' ) ) { |
| 87 |
global $wgCaptchaStorageDirectory; |
| 88 |
$wgCaptchaStorageDirectory = $this->getOption( 'captchastoragedir' ); |
| 89 |
} |
| 90 |
|
| 91 |
$backend = $instance->getBackend(); |
| 92 |
|
| 93 |
$deleteOldCaptchas = $this->getOption( 'delete' ); |
| 94 |
|
| 95 |
$countGen = (int)$this->getOption( 'fill' ); |
| 96 |
if ( !$deleteOldCaptchas ) { |
| 97 |
$countAct = $instance->getCaptchaCount(); |
| 98 |
$this->output( "Current number of captchas is $countAct.\n" ); |
| 99 |
$countGen -= $countAct; |
| 100 |
} |
| 101 |
|
| 102 |
if ( $countGen <= 0 ) { |
| 103 |
$this->output( "No need to generate any extra captchas.\n" ); |
| 104 |
return; |
| 105 |
} |
| 106 |
|
| 107 |
$tmpDir = wfTempDir() . '/mw-fancycaptcha-' . time() . '-' . wfRandomString( 6 ); |
| 108 |
if ( !wfMkdirParents( $tmpDir ) ) { |
| 109 |
$this->fatalError( "Could not create temp directory.\n", 1 ); |
| 110 |
} |
| 111 |
|
| 112 |
$captchaScript = 'captcha.py'; |
| 113 |
|
| 114 |
if ( $this->hasOption( 'oldcaptcha' ) ) { |
| 115 |
$this->output( "Using --oldcaptcha is deprecated, and captcha-old.py will be removed in the future!" ); |
| 116 |
$captchaScript = 'captcha-old.py'; |
| 117 |
} |
| 118 |
|
| 119 |
$cmd = [ |
| 120 |
"python3", |
| 121 |
dirname( __DIR__ ) . '/' . $captchaScript, |
| 122 |
"--key", |
| 123 |
$wgCaptchaSecret, |
| 124 |
"--output", |
| 125 |
$tmpDir, |
| 126 |
"--count", |
| 127 |
(string)$countGen, |
| 128 |
"--dirs", |
| 129 |
$wgCaptchaDirectoryLevels |
| 130 |
]; |
| 131 |
foreach ( |
| 132 |
[ 'wordlist', 'font', 'font-size', 'badwordlist', 'verbose', 'threads' ] as $par |
| 133 |
) { |
| 134 |
if ( $this->hasOption( $par ) ) { |
| 135 |
$cmd[] = "--$par"; |
| 136 |
$cmd[] = $this->getOption( $par ); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
$this->output( "Generating $countGen new captchas.." ); |
| 141 |
$captchaTime = -microtime( true ); |
| 142 |
$result = Shell::command( [] ) |
| 143 |
->params( $cmd ) |
| 144 |
->limits( [ 'time' => 0, 'memory' => 0, 'walltime' => 0, 'filesize' => 0 ] ) |
| 145 |
->disableSandbox() |
| 146 |
->execute(); |
| 147 |
if ( $result->getExitCode() !== 0 ) { |
| 148 |
$this->output( " Failed.\n" ); |
| 149 |
wfRecursiveRemoveDir( $tmpDir ); |
| 150 |
|
| 151 |
$this->fatalError( |
| 152 |
"An error occurred when running $captchaScript:\n{$result->getStderr()}\n", |
| 153 |
1 |
| 154 |
); |
| 155 |
} |
| 156 |
|
| 157 |
$captchaTime += microtime( true ); |
| 158 |
$this->output( " Done.\n" ); |
| 159 |
|
| 160 |
$this->output( |
| 161 |
sprintf( |
| 162 |
"\nGeneration script for %d captchas ran in %.1f seconds\n", |
| 163 |
$countGen, |
| 164 |
$captchaTime |
| 165 |
) |
| 166 |
); |
| 167 |
|
| 168 |
$tmpCountTime = -microtime( true ); |
| 169 |
$iter = new RecursiveIteratorIterator( |
| 170 |
new RecursiveDirectoryIterator( |
| 171 |
$tmpDir, |
| 172 |
FilesystemIterator::SKIP_DOTS |
| 173 |
), |
| 174 |
RecursiveIteratorIterator::LEAVES_ONLY |
| 175 |
); |
| 176 |
|
| 177 |
$captchasGenerated = iterator_count( $iter ); |
| 178 |
$filesToStore = []; |
| 179 |
|
| 180 |
foreach ( $iter as $fileInfo ) { |
| 181 |
if ( !$fileInfo->isFile() ) { |
| 182 |
continue; |
| 183 |
} |
| 184 |
[ $salt, $hash ] = $instance->hashFromImageName( $fileInfo->getBasename() ); |
| 185 |
$dest = $instance->imagePath( $salt, $hash ); |
| 186 |
$backend->prepare( [ 'dir' => dirname( $dest ) ] ); |
| 187 |
$filesToStore[] = [ |
| 188 |
'op' => 'store', |
| 189 |
'src' => $fileInfo->getPathname(), |
| 190 |
'dst' => $dest, |
| 191 |
]; |
| 192 |
} |
| 193 |
$tmpCountTime += microtime( true ); |
| 194 |
$this->output( |
| 195 |
sprintf( |
| 196 |
"\nEnumerated %d temporary captchas in %.1f seconds\n", |
| 197 |
$captchasGenerated, |
| 198 |
$tmpCountTime |
| 199 |
) |
| 200 |
); |
| 201 |
|
| 202 |
if ( $captchasGenerated === 0 ) { |
| 203 |
wfRecursiveRemoveDir( $tmpDir ); |
| 204 |
$this->error( "No generated captchas found in temporary directory; did captcha.py actually succeed?" ); |
| 205 |
} elseif ( $captchasGenerated < $countGen ) { |
| 206 |
$this->output( "Expecting $countGen new captchas, only $captchasGenerated found on disk; continuing\n." ); |
| 207 |
} |
| 208 |
|
| 209 |
$filesToDelete = []; |
| 210 |
if ( $deleteOldCaptchas ) { |
| 211 |
$this->output( "Getting a list of old captchas to delete..." ); |
| 212 |
$path = $backend->getRootStoragePath() . '/' . $instance->getStorageDir(); |
| 213 |
foreach ( $backend->getFileList( [ 'dir' => $path ] ) as $file ) { |
| 214 |
$filesToDelete[] = [ |
| 215 |
'op' => 'delete', |
| 216 |
'src' => $path . '/' . $file, |
| 217 |
]; |
| 218 |
} |
| 219 |
$this->output( " Done.\n" ); |
| 220 |
} |
| 221 |
|
| 222 |
$this->output( "Copying the new captchas to storage..." ); |
| 223 |
$storeTime = -microtime( true ); |
| 224 |
$ret = $backend->doQuickOperations( $filesToStore ); |
| 225 |
|
| 226 |
$storeTime += microtime( true ); |
| 227 |
|
| 228 |
$storeSucceeded = true; |
| 229 |
if ( $ret->isOK() ) { |
| 230 |
$this->output( " Done.\n" ); |
| 231 |
$this->output( |
| 232 |
sprintf( |
| 233 |
"\nCopied %d captchas to storage in %.1f seconds\n", |
| 234 |
$ret->successCount, |
| 235 |
$storeTime |
| 236 |
) |
| 237 |
); |
| 238 |
if ( !$ret->isGood() ) { |
| 239 |
$this->output( |
| 240 |
"Non fatal errors:\n" . |
| 241 |
Status::wrap( $ret )->getWikiText( false, false, 'en' ) . |
| 242 |
"\n" |
| 243 |
); |
| 244 |
} |
| 245 |
if ( $ret->failCount ) { |
| 246 |
$storeSucceeded = false; |
| 247 |
$this->error( sprintf( "\nFailed to copy %d captchas\n", $ret->failCount ) ); |
| 248 |
} |
| 249 |
if ( $ret->successCount + $ret->failCount !== $captchasGenerated ) { |
| 250 |
$storeSucceeded = false; |
| 251 |
$this->error( |
| 252 |
sprintf( "Internal error: captchasGenerated: %d, successCount: %d, failCount: %d\n", |
| 253 |
$captchasGenerated, $ret->successCount, $ret->failCount |
| 254 |
) |
| 255 |
); |
| 256 |
} |
| 257 |
} else { |
| 258 |
$storeSucceeded = false; |
| 259 |
$this->output( "Errored.\n" ); |
| 260 |
$this->error( |
| 261 |
Status::wrap( $ret )->getWikiText( false, false, 'en' ) . |
| 262 |
"\n" |
| 263 |
); |
| 264 |
} |
| 265 |
|
| 266 |
if ( $storeSucceeded && $deleteOldCaptchas ) { |
| 267 |
$numOriginalFiles = count( $filesToDelete ); |
| 268 |
$this->output( "Deleting {$numOriginalFiles} old captchas...\n" ); |
| 269 |
$deleteTime = -microtime( true ); |
| 270 |
$ret = $backend->doQuickOperations( $filesToDelete ); |
| 271 |
|
| 272 |
$deleteTime += microtime( true ); |
| 273 |
if ( $ret->isOK() ) { |
| 274 |
$this->output( "Done.\n" ); |
| 275 |
$this->output( |
| 276 |
sprintf( |
| 277 |
"\nDeleted %d old captchas in %.1f seconds\n", |
| 278 |
$numOriginalFiles, |
| 279 |
$deleteTime |
| 280 |
) |
| 281 |
); |
| 282 |
if ( !$ret->isGood() ) { |
| 283 |
$this->output( |
| 284 |
"Non fatal errors:\n" . |
| 285 |
Status::wrap( $ret )->getWikiText( false, false, 'en' ) . |
| 286 |
"\n" |
| 287 |
); |
| 288 |
} |
| 289 |
} else { |
| 290 |
$this->output( "Errored.\n" ); |
| 291 |
$this->error( |
| 292 |
Status::wrap( $ret )->getWikiText( false, false, 'en' ) . |
| 293 |
"\n" |
| 294 |
); |
| 295 |
} |
| 296 |
|
| 297 |
} |
| 298 |
$this->output( "Removing temporary files..." ); |
| 299 |
wfRecursiveRemoveDir( $tmpDir ); |
| 300 |
$this->output( " Done.\n" ); |
| 301 |
|
| 302 |
$totalTime += microtime( true ); |
| 303 |
$this->output( |
| 304 |
sprintf( |
| 305 |
"\nWhole captchas generation process took %.1f seconds\n", |
| 306 |
$totalTime |
| 307 |
) |
| 308 |
); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
$maintClass = GenerateFancyCaptchas::class; |
| 313 |
require_once RUN_MAINTENANCE_IF_MAIN; |