Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 189
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
GenerateFancyCaptchas
0.00% covered (danger)
0.00%
0 / 183
0.00% covered (danger)
0.00%
0 / 2
506
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 162
0.00% covered (danger)
0.00%
0 / 1
462
1<?php
2/**
3 * Generate fancy captchas using a python script and copy them into storage.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @author Aaron Schulz
22 * @ingroup Maintenance
23 */
24if ( getenv( 'MW_INSTALL_PATH' ) ) {
25    $IP = getenv( 'MW_INSTALL_PATH' );
26} else {
27    $IP = __DIR__ . '/../../..';
28}
29
30require_once "$IP/maintenance/Maintenance.php";
31
32use MediaWiki\Extension\ConfirmEdit\FancyCaptcha\FancyCaptcha;
33use MediaWiki\Extension\ConfirmEdit\Hooks;
34use MediaWiki\Maintenance\Maintenance;
35use MediaWiki\Shell\Shell;
36use MediaWiki\Status\Status;
37
38/**
39 * Maintenance script to generate fancy captchas using a python script and copy them into storage.
40 *
41 * @ingroup Maintenance
42 */
43class GenerateFancyCaptchas extends Maintenance {
44    public function __construct() {
45        parent::__construct();
46
47        // See captcha.py for argument usage
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( "delete", "Deletes all the old captchas" );
58        $this->addOption( "threads", "The number of threads to use to generate the images",
59            false, true );
60        $this->addOption(
61            'captchastoragedir',
62            'Overrides the value of $wgCaptchaStorageDirectory',
63            false,
64            true
65        );
66        $this->addDescription( "Generate new fancy captchas and move them into storage" );
67
68        $this->requireExtension( "FancyCaptcha" );
69    }
70
71    public function execute() {
72        global $wgCaptchaSecret, $wgCaptchaDirectoryLevels;
73
74        $totalTime = -microtime( true );
75
76        $instance = Hooks::getInstance();
77        if ( !( $instance instanceof FancyCaptcha ) ) {
78            $this->fatalError( "\$wgCaptchaClass is not FancyCaptcha.\n", 1 );
79        }
80
81        // Overrides $wgCaptchaStorageDirectory for this script run
82        if ( $this->hasOption( 'captchastoragedir' ) ) {
83            global $wgCaptchaStorageDirectory;
84            $wgCaptchaStorageDirectory = $this->getOption( 'captchastoragedir' );
85        }
86
87        $backend = $instance->getBackend();
88
89        $deleteOldCaptchas = $this->getOption( 'delete' );
90
91        $countGen = (int)$this->getOption( 'fill' );
92        if ( !$deleteOldCaptchas ) {
93            $countAct = $instance->getCaptchaCount();
94            $this->output( "Current number of captchas is $countAct.\n" );
95            $countGen -= $countAct;
96        }
97
98        if ( $countGen <= 0 ) {
99            $this->output( "No need to generate any extra captchas.\n" );
100            return;
101        }
102
103        $tmpDir = wfTempDir() . '/mw-fancycaptcha-' . time() . '-' . wfRandomString( 6 );
104        if ( !wfMkdirParents( $tmpDir ) ) {
105            $this->fatalError( "Could not create temp directory.\n", 1 );
106        }
107
108        $cmd = [
109            "python3",
110            dirname( __DIR__ ) . '/captcha.py',
111            "--key",
112            $wgCaptchaSecret,
113            "--output",
114            $tmpDir,
115            "--count",
116            (string)$countGen,
117            "--dirs",
118            $wgCaptchaDirectoryLevels
119        ];
120        foreach (
121            [ 'wordlist', 'font', 'font-size', 'badwordlist', 'verbose', 'threads' ] as $par
122        ) {
123            if ( $this->hasOption( $par ) ) {
124                $cmd[] = "--$par";
125                $cmd[] = $this->getOption( $par );
126            }
127        }
128
129        $this->output( "Generating $countGen new captchas.." );
130        $captchaTime = -microtime( true );
131        $result = Shell::command( [] )
132            ->params( $cmd )
133            ->limits( [ 'time' => 0 ] )
134            ->disableSandbox()
135            ->execute();
136        if ( $result->getExitCode() !== 0 ) {
137            $this->output( " Failed.\n" );
138            wfRecursiveRemoveDir( $tmpDir );
139
140            $this->fatalError(
141                "An error occurred when running captcha.py:\n{$result->getStderr()}\n",
142                1
143            );
144        }
145
146        $captchaTime += microtime( true );
147        $this->output( " Done.\n" );
148
149        $this->output(
150            sprintf(
151                "\nGenerated %d captchas in %.1f seconds\n",
152                $countGen,
153                $captchaTime
154            )
155        );
156
157        $filesToDelete = [];
158        if ( $deleteOldCaptchas ) {
159            $this->output( "Getting a list of old captchas to delete..." );
160            $path = $backend->getRootStoragePath() . '/' . $instance->getStorageDir();
161            foreach ( $backend->getFileList( [ 'dir' => $path ] ) as $file ) {
162                $filesToDelete[] = [
163                    'op' => 'delete',
164                    'src' => $path . '/' . $file,
165                ];
166            }
167            $this->output( " Done.\n" );
168        }
169
170        $this->output( "Copying the new captchas to storage..." );
171
172        $storeTime = -microtime( true );
173        $iter = new RecursiveIteratorIterator(
174            new RecursiveDirectoryIterator(
175                $tmpDir,
176                FilesystemIterator::SKIP_DOTS
177            ),
178            RecursiveIteratorIterator::LEAVES_ONLY
179        );
180
181        $captchasGenerated = iterator_count( $iter );
182        $filesToStore = [];
183        /** @var SplFileInfo $fileInfo */
184        foreach ( $iter as $fileInfo ) {
185            if ( !$fileInfo->isFile() ) {
186                continue;
187            }
188            [ $salt, $hash ] = $instance->hashFromImageName( $fileInfo->getBasename() );
189            $dest = $instance->imagePath( $salt, $hash );
190            $backend->prepare( [ 'dir' => dirname( $dest ) ] );
191            $filesToStore[] = [
192                'op' => 'store',
193                'src' => $fileInfo->getPathname(),
194                'dst' => $dest,
195            ];
196        }
197
198        $ret = $backend->doQuickOperations( $filesToStore );
199
200        $storeTime += microtime( true );
201
202        $storeSucceeded = true;
203        if ( $ret->isOK() ) {
204            $this->output( " Done.\n" );
205            $this->output(
206                sprintf(
207                    "\nCopied %d captchas to storage in %.1f seconds\n",
208                    $ret->successCount,
209                    $storeTime
210                )
211            );
212            if ( !$ret->isGood() ) {
213                $this->output(
214                    "Non fatal errors:\n" .
215                    Status::wrap( $ret )->getWikiText( false, false, 'en' ) .
216                    "\n"
217                );
218            }
219            if ( $ret->failCount ) {
220                $storeSucceeded = false;
221                $this->error( sprintf( "\nFailed to copy %d captchas\n", $ret->failCount ) );
222            }
223            if ( $ret->successCount + $ret->failCount !== $captchasGenerated ) {
224                $storeSucceeded = false;
225                $this->error(
226                    sprintf( "Internal error: captchasGenerated: %d, successCount: %d, failCount: %d\n",
227                        $captchasGenerated, $ret->successCount, $ret->failCount
228                    )
229                );
230            }
231        } else {
232            $storeSucceeded = false;
233            $this->output( "Errored.\n" );
234            $this->error(
235                Status::wrap( $ret )->getWikiText( false, false, 'en' ) .
236                "\n"
237            );
238        }
239
240        if ( $storeSucceeded && $deleteOldCaptchas ) {
241            $numOriginalFiles = count( $filesToDelete );
242            $this->output( "Deleting {$numOriginalFiles} old captchas...\n" );
243            $deleteTime = -microtime( true );
244            $ret = $backend->doQuickOperations( $filesToDelete );
245
246            $deleteTime += microtime( true );
247            if ( $ret->isOK() ) {
248                $this->output( "Done.\n" );
249                $this->output(
250                    sprintf(
251                        "\nDeleted %d old captchas in %.1f seconds\n",
252                        $numOriginalFiles,
253                        $deleteTime
254                    )
255                );
256                if ( !$ret->isGood() ) {
257                    $this->output(
258                        "Non fatal errors:\n" .
259                        Status::wrap( $ret )->getWikiText( false, false, 'en' ) .
260                        "\n"
261                    );
262                }
263            } else {
264                $this->output( "Errored.\n" );
265                $this->error(
266                    Status::wrap( $ret )->getWikiText( false, false, 'en' ) .
267                    "\n"
268                );
269            }
270
271        }
272        $this->output( "Removing temporary files..." );
273        wfRecursiveRemoveDir( $tmpDir );
274        $this->output( " Done.\n" );
275
276        $totalTime += microtime( true );
277        $this->output(
278            sprintf(
279                "\nWhole captchas generation process took %.1f seconds\n",
280                $totalTime
281            )
282        );
283    }
284}
285
286$maintClass = GenerateFancyCaptchas::class;
287require_once RUN_MAINTENANCE_IF_MAIN;