Go to the documentation of this file.
24 if ( getenv(
'MW_INSTALL_PATH' ) ) {
25 $IP = getenv(
'MW_INSTALL_PATH' );
27 $IP = __DIR__ .
'/../../..';
30 require_once
"$IP/maintenance/Maintenance.php";
39 parent::__construct();
42 $this->
addOption(
"wordlist",
'A list of words',
true,
true );
43 $this->
addOption(
"font",
"The font to use",
true,
true );
44 $this->
addOption(
"font-size",
"The font size ",
false,
true );
45 $this->
addOption(
"blacklist",
"A blacklist of words that should not be used",
false,
true );
46 $this->
addOption(
"fill",
"Fill the captcha container to N files",
true,
true );
47 $this->
addOption(
"verbose",
"Show debugging information" );
50 "Whether to use captcha-old.py which doesn't have OCR fighting improvements"
52 $this->
addOption(
"delete",
"Delete the old captches" );
53 $this->
addOption(
"threads",
"The number of threads to use to generate the images",
55 $this->mDescription =
"Generate new fancy captchas and move them into storage";
61 global $wgCaptchaSecret, $wgCaptchaDirectoryLevels;
63 $totalTime = -microtime(
true );
67 $this->
error(
"\$wgCaptchaClass is not FancyCaptcha.\n", 1 );
69 $backend = $instance->getBackend();
71 $deleteOldCaptchas = $this->
getOption(
'delete' );
73 $countGen = (int)$this->
getOption(
'fill' );
74 if ( !$deleteOldCaptchas ) {
75 $countAct = $instance->getCaptchaCount();
76 $this->
output(
"Current number of captchas is $countAct.\n" );
77 $countGen -= $countAct;
80 if ( $countGen <= 0 ) {
81 $this->
output(
"No need to generate anymore captchas.\n" );
87 $this->
error(
"Could not create temp directory.\n", 1 );
90 $captchaScript =
'captcha.py';
93 $captchaScript =
'captcha-old.py';
96 $cmd = sprintf(
"python %s --key %s --output %s --count %s --dirs %s",
104 [
'wordlist',
'font',
'font-size',
'blacklist',
'verbose',
'threads' ]
as $par
111 $this->
output(
"Generating $countGen new captchas.." );
113 $captchaTime = -microtime(
true );
115 if ( $retVal != 0 ) {
117 $this->
error(
"Could not run generation script.\n", 1 );
120 $captchaTime += microtime(
true );
121 $this->
output(
" Done.\n" );
125 "\nGenerated %d captchas in %.1f seconds\n",
132 if ( $deleteOldCaptchas ) {
133 $this->
output(
"Getting a list of old captchas to delete..." );
134 $path = $backend->getRootStoragePath() .
'/captcha-render';
135 foreach ( $backend->getFileList( [
'dir' =>
$path ] )
as $file ) {
141 $this->
output(
" Done.\n" );
144 $this->
output(
"Copying the new captchas to storage..." );
146 $storeTime = -microtime(
true );
147 $iter =
new RecursiveIteratorIterator(
148 new RecursiveDirectoryIterator(
150 FilesystemIterator::SKIP_DOTS
152 RecursiveIteratorIterator::LEAVES_ONLY
155 $captchasGenerated = iterator_count( $iter );
160 foreach ( $iter
as $fileInfo ) {
161 if ( !$fileInfo->isFile() ) {
164 list( $salt, $hash ) = $instance->hashFromImageName( $fileInfo->getBasename() );
165 $dest = $instance->imagePath( $salt, $hash );
166 $backend->prepare( [
'dir' => dirname( $dest ) ] );
169 'src' => $fileInfo->getPathname(),
174 $ret = $backend->doQuickOperations( $filesToStore );
176 $storeTime += microtime(
true );
178 $storeSucceeded =
true;
179 if (
$ret->isOK() ) {
180 $this->
output(
" Done.\n" );
183 "\nCopied %d captchas to storage in %.1f seconds\n",
188 if ( !
$ret->isGood() ) {
190 "Non fatal errors:\n" .
195 if (
$ret->failCount ) {
196 $storeSucceeded =
false;
197 $this->
error( sprintf(
"\nFailed to copy %d captchas\n",
$ret->failCount ) );
199 if (
$ret->successCount +
$ret->failCount !== $captchasGenerated ) {
200 $storeSucceeded =
false;
202 sprintf(
"Internal error: captchasGenerated: %d, successCount: %d, failCount: %d\n",
203 $captchasGenerated,
$ret->successCount,
$ret->failCount
208 $storeSucceeded =
false;
209 $this->
output(
"Errored.\n" );
216 if ( $storeSucceeded && $deleteOldCaptchas ) {
217 $numOriginalFiles =
count( $filesToDelete );
218 $this->
output(
"Deleting {$numOriginalFiles} old captchas...\n" );
219 $deleteTime = -microtime(
true );
220 $ret = $backend->doQuickOperations( $filesToDelete );
222 $deleteTime += microtime(
true );
223 if (
$ret->isOK() ) {
224 $this->
output(
"Done.\n" );
227 "\nDeleted %d old captchas in %.1f seconds\n",
232 if ( !
$ret->isGood() ) {
234 "Non fatal errors:\n" .
240 $this->
output(
"Errored.\n" );
248 $this->
output(
"Removing temporary files..." );
250 $this->
output(
" Done.\n" );
252 $totalTime += microtime(
true );
255 "\nWhole captchas generation process took %.1f seconds\n",
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
wfMkdirParents( $dir, $mode=null, $caller=null)
Make directory, and make all parent directories if they don't exist.
require_once RUN_MAINTENANCE_IF_MAIN
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
static getInstance()
Get the global Captcha instance.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
static wrap( $sv)
Succinct helper method to wrap a StatusValue.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
requireExtension( $name)
Indicate that the specified extension must be loaded before the script can run.
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
FancyCaptcha for displaying captchas precomputed by captcha.py.
execute()
Do the actual work.
Maintenance script to generate fancy captchas using a python script and copy them into storage.
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
__construct()
Default constructor.
wfTempDir()
Tries to get the system directory for temporary files.
getOption( $name, $default=null)
Get an option, or return the default.
wfRecursiveRemoveDir( $dir)
Remove a directory and all its content.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
wfEscapeShellArg(... $args)
Version of escapeshellarg() that works better on Windows.
error( $err, $die=0)
Throw an error to the user.
output( $out, $channel=null)
Throw some output to the user.
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
hasOption( $name)
Checks to see if a particular option exists.
wfShellExec( $cmd, &$retval=null, $environ=[], $limits=[], $options=[])
Execute a shell command, with time and memory limits mirrored from the PHP configuration if supported...
wfRandomString( $length=32)
Get a random string containing a number of pseudo-random hex characters.