Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 176 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
GenerateFancyCaptchas | |
0.00% |
0 / 170 |
|
0.00% |
0 / 2 |
506 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 150 |
|
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 | */ |
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 | |
35 | /** |
36 | * Maintenance script to generate fancy captchas using a python script and copy them into storage. |
37 | * |
38 | * @ingroup Maintenance |
39 | */ |
40 | class GenerateFancyCaptchas extends Maintenance { |
41 | public function __construct() { |
42 | parent::__construct(); |
43 | |
44 | // See captcha.py for argument usage |
45 | $this->addOption( "wordlist", 'A list of words', true, true ); |
46 | $this->addOption( "font", "The font to use", true, true ); |
47 | $this->addOption( "font-size", "The font size ", false, true ); |
48 | $this->addOption( "badwordlist", "A list of words that should not be used", false, true ); |
49 | $this->addOption( "blacklist", "DEPRECATED: A list of words that should not be used", false, true ); |
50 | $this->addOption( "fill", "Fill the captcha container to N files", true, true ); |
51 | $this->addOption( |
52 | "verbose", |
53 | "Show debugging information when running the captcha python script" |
54 | ); |
55 | $this->addOption( |
56 | "oldcaptcha", |
57 | "Whether to use captcha-old.py which doesn't have OCR fighting improvements" |
58 | ); |
59 | $this->addOption( "delete", "Deletes all the old captchas" ); |
60 | $this->addOption( "threads", "The number of threads to use to generate the images", |
61 | false, true ); |
62 | $this->addDescription( "Generate new fancy captchas and move them into storage" ); |
63 | |
64 | $this->requireExtension( "FancyCaptcha" ); |
65 | } |
66 | |
67 | public function execute() { |
68 | global $wgCaptchaSecret, $wgCaptchaDirectoryLevels; |
69 | |
70 | $totalTime = -microtime( true ); |
71 | |
72 | $instance = Hooks::getInstance(); |
73 | if ( !( $instance instanceof FancyCaptcha ) ) { |
74 | $this->fatalError( "\$wgCaptchaClass is not FancyCaptcha.\n", 1 ); |
75 | } |
76 | $backend = $instance->getBackend(); |
77 | |
78 | $deleteOldCaptchas = $this->getOption( 'delete' ); |
79 | |
80 | $countGen = (int)$this->getOption( 'fill' ); |
81 | if ( !$deleteOldCaptchas ) { |
82 | $countAct = $instance->getCaptchaCount(); |
83 | $this->output( "Current number of captchas is $countAct.\n" ); |
84 | $countGen -= $countAct; |
85 | } |
86 | |
87 | if ( $countGen <= 0 ) { |
88 | $this->output( "No need to generate anymore captchas.\n" ); |
89 | return; |
90 | } |
91 | |
92 | $tmpDir = wfTempDir() . '/mw-fancycaptcha-' . time() . '-' . wfRandomString( 6 ); |
93 | if ( !wfMkdirParents( $tmpDir ) ) { |
94 | $this->fatalError( "Could not create temp directory.\n", 1 ); |
95 | } |
96 | |
97 | $captchaScript = 'captcha.py'; |
98 | |
99 | if ( $this->hasOption( 'oldcaptcha' ) ) { |
100 | $captchaScript = 'captcha-old.py'; |
101 | } |
102 | |
103 | $cmd = sprintf( "python3 %s --key %s --output %s --count %s --dirs %s", |
104 | wfEscapeShellArg( dirname( __DIR__ ) . '/' . $captchaScript ), |
105 | wfEscapeShellArg( $wgCaptchaSecret ), |
106 | wfEscapeShellArg( $tmpDir ), |
107 | wfEscapeShellArg( (string)$countGen ), |
108 | wfEscapeShellArg( $wgCaptchaDirectoryLevels ) |
109 | ); |
110 | foreach ( |
111 | [ 'wordlist', 'font', 'font-size', 'blacklist', 'badwordlist', 'verbose', 'threads' ] as $par |
112 | ) { |
113 | if ( $this->hasOption( $par ) ) { |
114 | $cmd .= " --$par " . wfEscapeShellArg( $this->getOption( $par ) ); |
115 | } |
116 | } |
117 | |
118 | $this->output( "Generating $countGen new captchas.." ); |
119 | $retVal = 1; |
120 | $captchaTime = -microtime( true ); |
121 | wfShellExec( $cmd, $retVal, [], [ 'time' => 0 ] ); |
122 | if ( $retVal != 0 ) { |
123 | $this->output( " Failed.\n" ); |
124 | wfRecursiveRemoveDir( $tmpDir ); |
125 | $this->fatalError( "An error occured when running $captchaScript.\n", 1 ); |
126 | } |
127 | |
128 | $captchaTime += microtime( true ); |
129 | $this->output( " Done.\n" ); |
130 | |
131 | $this->output( |
132 | sprintf( |
133 | "\nGenerated %d captchas in %.1f seconds\n", |
134 | $countGen, |
135 | $captchaTime |
136 | ) |
137 | ); |
138 | |
139 | $filesToDelete = []; |
140 | if ( $deleteOldCaptchas ) { |
141 | $this->output( "Getting a list of old captchas to delete..." ); |
142 | $path = $backend->getRootStoragePath() . '/captcha-render'; |
143 | foreach ( $backend->getFileList( [ 'dir' => $path ] ) as $file ) { |
144 | $filesToDelete[] = [ |
145 | 'op' => 'delete', |
146 | 'src' => $path . '/' . $file, |
147 | ]; |
148 | } |
149 | $this->output( " Done.\n" ); |
150 | } |
151 | |
152 | $this->output( "Copying the new captchas to storage..." ); |
153 | |
154 | $storeTime = -microtime( true ); |
155 | $iter = new RecursiveIteratorIterator( |
156 | new RecursiveDirectoryIterator( |
157 | $tmpDir, |
158 | FilesystemIterator::SKIP_DOTS |
159 | ), |
160 | RecursiveIteratorIterator::LEAVES_ONLY |
161 | ); |
162 | |
163 | $captchasGenerated = iterator_count( $iter ); |
164 | $filesToStore = []; |
165 | /** |
166 | * @var $fileInfo SplFileInfo |
167 | */ |
168 | foreach ( $iter as $fileInfo ) { |
169 | if ( !$fileInfo->isFile() ) { |
170 | continue; |
171 | } |
172 | list( $salt, $hash ) = $instance->hashFromImageName( $fileInfo->getBasename() ); |
173 | $dest = $instance->imagePath( $salt, $hash ); |
174 | $backend->prepare( [ 'dir' => dirname( $dest ) ] ); |
175 | $filesToStore[] = [ |
176 | 'op' => 'store', |
177 | 'src' => $fileInfo->getPathname(), |
178 | 'dst' => $dest, |
179 | ]; |
180 | } |
181 | |
182 | $ret = $backend->doQuickOperations( $filesToStore ); |
183 | |
184 | $storeTime += microtime( true ); |
185 | |
186 | $storeSucceeded = true; |
187 | if ( $ret->isOK() ) { |
188 | $this->output( " Done.\n" ); |
189 | $this->output( |
190 | sprintf( |
191 | "\nCopied %d captchas to storage in %.1f seconds\n", |
192 | $ret->successCount, |
193 | $storeTime |
194 | ) |
195 | ); |
196 | if ( !$ret->isGood() ) { |
197 | $this->output( |
198 | "Non fatal errors:\n" . |
199 | Status::wrap( $ret )->getWikiText( false, false, 'en' ) . |
200 | "\n" |
201 | ); |
202 | } |
203 | if ( $ret->failCount ) { |
204 | $storeSucceeded = false; |
205 | $this->error( sprintf( "\nFailed to copy %d captchas\n", $ret->failCount ) ); |
206 | } |
207 | if ( $ret->successCount + $ret->failCount !== $captchasGenerated ) { |
208 | $storeSucceeded = false; |
209 | $this->error( |
210 | sprintf( "Internal error: captchasGenerated: %d, successCount: %d, failCount: %d\n", |
211 | $captchasGenerated, $ret->successCount, $ret->failCount |
212 | ) |
213 | ); |
214 | } |
215 | } else { |
216 | $storeSucceeded = false; |
217 | $this->output( "Errored.\n" ); |
218 | $this->error( |
219 | Status::wrap( $ret )->getWikiText( false, false, 'en' ) . |
220 | "\n" |
221 | ); |
222 | } |
223 | |
224 | if ( $storeSucceeded && $deleteOldCaptchas ) { |
225 | $numOriginalFiles = count( $filesToDelete ); |
226 | $this->output( "Deleting {$numOriginalFiles} old captchas...\n" ); |
227 | $deleteTime = -microtime( true ); |
228 | $ret = $backend->doQuickOperations( $filesToDelete ); |
229 | |
230 | $deleteTime += microtime( true ); |
231 | if ( $ret->isOK() ) { |
232 | $this->output( "Done.\n" ); |
233 | $this->output( |
234 | sprintf( |
235 | "\nDeleted %d old captchas in %.1f seconds\n", |
236 | $numOriginalFiles, |
237 | $deleteTime |
238 | ) |
239 | ); |
240 | if ( !$ret->isGood() ) { |
241 | $this->output( |
242 | "Non fatal errors:\n" . |
243 | Status::wrap( $ret )->getWikiText( false, false, 'en' ) . |
244 | "\n" |
245 | ); |
246 | } |
247 | } else { |
248 | $this->output( "Errored.\n" ); |
249 | $this->error( |
250 | Status::wrap( $ret )->getWikiText( false, false, 'en' ) . |
251 | "\n" |
252 | ); |
253 | } |
254 | |
255 | } |
256 | $this->output( "Removing temporary files..." ); |
257 | wfRecursiveRemoveDir( $tmpDir ); |
258 | $this->output( " Done.\n" ); |
259 | |
260 | $totalTime += microtime( true ); |
261 | $this->output( |
262 | sprintf( |
263 | "\nWhole captchas generation process took %.1f seconds\n", |
264 | $totalTime |
265 | ) |
266 | ); |
267 | } |
268 | } |
269 | |
270 | $maintClass = GenerateFancyCaptchas::class; |
271 | require_once RUN_MAINTENANCE_IF_MAIN; |