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