Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 322 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
MockApiHelper | |
0.00% |
0 / 322 |
|
0.00% |
0 / 10 |
13572 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
setApiPrefix | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
addArticle | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
makeRequest | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
56 | |||
transformHelper | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
42 | |||
imageInfo | |
0.00% |
0 / 130 |
|
0.00% |
0 / 1 |
2256 | |||
processQuery | |
0.00% |
0 / 116 |
|
0.00% |
0 / 1 |
1722 | |||
parse | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
56 | |||
preProcess | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
fetchTemplateData | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | // phpcs:disable Generic.Files.LineLength.TooLong |
5 | |
6 | namespace Wikimedia\Parsoid\ParserTests; |
7 | |
8 | use Error; |
9 | use Wikimedia\Parsoid\Config\Api\ApiHelper; |
10 | |
11 | /** |
12 | * This class supports the implementation of Parser Tests in a standalone mode |
13 | * and without network access. |
14 | * |
15 | * In standalone mode, the config and data transformations needed by Parsoid |
16 | * cannot come from MediaWiki's database or its core classes. |
17 | * |
18 | * Without network access, we cannot fetch site configs or do data transformations |
19 | * on a remote wiki. This class supports this by intercepting network requests |
20 | * and returning mock responses based on cached site configs, hardcoded network |
21 | * responses and config, |
22 | * |
23 | * So, this API helper should be used with the Parsoid\Config\Api* set of config classes |
24 | * (and any subclasses derived from them). |
25 | * |
26 | * A lot of the responses here are tuned to what ParserTests needed. But, presumably |
27 | * this can be used by PHP Unit tests as long as the specific set of mocked responses |
28 | * satisfies the needs of those tests. Ideally, this class should NOT be updated for |
29 | * anything but the needs of running parser tests. |
30 | * |
31 | * Alternatively, PHP Unit tests could bypass the Api* classes altogether and use |
32 | * a (sub)set of mocked classes (Env, SiteConfig, PageConfig, DataAccess) if those |
33 | * classes and the data they provide satisfies the needs of those tests. |
34 | */ |
35 | class MockApiHelper extends ApiHelper { |
36 | // configuration to match PHP parserTests |
37 | private const IMAGE_BASE_URL = 'http://example.com/images'; |
38 | private const IMAGE_DESC_URL = self::IMAGE_BASE_URL; |
39 | private const FILE_PROPS = [ |
40 | 'Foobar.jpg' => [ |
41 | 'size' => 7881, |
42 | 'width' => 1941, |
43 | 'height' => 220, |
44 | 'bits' => 8, |
45 | 'mime' => 'image/jpeg' |
46 | ], |
47 | 'Thumb.png' => [ |
48 | 'size' => 22589, |
49 | 'width' => 135, |
50 | 'height' => 135, |
51 | 'bits' => 8, |
52 | 'mime' => 'image/png' |
53 | ], |
54 | 'Foobar.svg' => [ |
55 | 'size' => 12345, |
56 | 'width' => 240, |
57 | 'height' => 180, |
58 | 'bits' => 24, |
59 | 'mime' => 'image/svg+xml' |
60 | ], |
61 | 'Bad.jpg' => [ |
62 | 'size' => 12345, |
63 | 'width' => 320, |
64 | 'height' => 240, |
65 | 'bits' => 24, |
66 | 'mime' => 'image/jpeg', |
67 | ], |
68 | 'LoremIpsum.djvu' => [ |
69 | 'size' => 3249, |
70 | 'width' => 2480, |
71 | 'height' => 3508, |
72 | 'bits' => 8, |
73 | 'mime' => 'image/vnd.djvu', |
74 | 'mediatype' => 'OFFICE', |
75 | 'pagecount' => 5, |
76 | ], |
77 | 'Video.ogv' => [ |
78 | 'size' => 12345, |
79 | 'width' => 320, |
80 | 'height' => 240, |
81 | 'bits' => 0, |
82 | # duration comes from |
83 | # TimedMediaHandler/tests/phpunit/mocks/MockOggHandler::getLength() |
84 | 'duration' => 4.3666666666667, |
85 | 'mime' => 'video/ogg; codecs="theora"', |
86 | 'mediatype' => 'VIDEO', |
87 | # hacky way to get seek parameters to return the correct info |
88 | 'extraParams' => [ |
89 | 'seek=1.2' => 'seek%3D1.2', |
90 | 'seek=85' => 'seek%3D3.3666666666667', # hard limited by duration |
91 | ], |
92 | ], |
93 | 'Transcode.webm' => [ |
94 | 'size' => 12345, |
95 | 'width' => 492, |
96 | 'height' => 360, |
97 | 'bits' => 0, |
98 | 'duration' => 4, |
99 | 'mime' => 'video/webm; codecs="vp8, vorbis"', |
100 | 'mediatype' => 'VIDEO', |
101 | 'derivatives' => [ |
102 | [ |
103 | 'type' => 'video/webm; codecs="vp9, opus"', |
104 | 'transcodekey' => '240p.vp9.webm', |
105 | 'width' => 328, |
106 | 'height' => 240, |
107 | ], |
108 | ], |
109 | ], |
110 | 'Audio.oga' => [ |
111 | 'size' => 12345, |
112 | 'width' => 0, |
113 | 'height' => 0, |
114 | 'bits' => 0, |
115 | # duration comes from |
116 | # TimedMediaHandler/tests/phpunit/mocks/MockOggHandler::getLength() |
117 | 'duration' => 0.99875, |
118 | 'mime' => 'audio/ogg; codecs="vorbis"', |
119 | 'mediatype' => 'AUDIO', |
120 | ], |
121 | 'Hi-ho.jpg' => [ |
122 | 'size' => 7881, |
123 | 'width' => 1941, |
124 | 'height' => 220, |
125 | 'bits' => 8, |
126 | 'mime' => 'image/jpeg' |
127 | ], |
128 | ]; |
129 | |
130 | private array $articleCache = []; |
131 | private array $cachedConfigs = []; |
132 | |
133 | private const MAIN_PAGE = [ |
134 | 'query' => [ |
135 | 'pages' => [ |
136 | [ |
137 | 'pageid' => 1, |
138 | 'ns' => 0, |
139 | 'title' => 'Main Page', |
140 | 'revisions' => [ |
141 | [ |
142 | 'revid' => 1, |
143 | 'parentid' => 0, |
144 | 'slots' => [ |
145 | 'main' => [ |
146 | 'contentmodel' => 'wikitext', |
147 | 'contentformat' => 'text/x-wiki', |
148 | 'content' => "<strong>MediaWiki has been successfully installed.</strong>\n\nConsult the [//meta.wikimedia.org/wiki/Help:Contents User's Guide] for information on using the wiki software.\n\n== Getting started ==\n* [//www.mediawiki.org/wiki/Special:MyLanguage/Manual:Configuration_settings Configuration settings list]\n* [//www.mediawiki.org/wiki/Special:MyLanguage/Manual:FAQ MediaWiki FAQ]\n* [https://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list]\n* [//www.mediawiki.org/wiki/Special:MyLanguage/Localisation#Translation_resources Localise MediaWiki for your language]" |
149 | ] |
150 | ] |
151 | ] |
152 | ] |
153 | ] |
154 | ] |
155 | ] |
156 | ]; |
157 | |
158 | // Old response structure, pre-mcr |
159 | private const OLD_RESPONSE = [ |
160 | 'query' => [ |
161 | 'pages' => [ |
162 | [ |
163 | 'pageid' => 999, |
164 | 'ns' => 0, |
165 | 'title' => 'Old Response', |
166 | 'revisions' => [ |
167 | [ |
168 | 'revid' => 999, |
169 | 'parentid' => 0, |
170 | 'contentmodel' => 'wikitext', |
171 | 'contentformat' => 'text/x-wiki', |
172 | '*' => "<strong>MediaWiki was successfully installed.</strong>\n\nConsult the [//meta.wikimedia.org/wiki/Help:Contents User's Guide] for information on using the wiki software.\n\n== Getting started ==\n* [//www.mediawiki.org/wiki/Special:MyLanguage/Manual:Configuration_settings Configuration settings list]\n* [//www.mediawiki.org/wiki/Special:MyLanguage/Manual:FAQ MediaWiki FAQ]\n* [https://lists.wikimedia.org/mailman/listinfo/mediawiki-announce MediaWiki release mailing list]\n* [//www.mediawiki.org/wiki/Special:MyLanguage/Localisation#Translation_resources Localise MediaWiki for your language]" |
173 | ] |
174 | ] |
175 | ] |
176 | ] |
177 | ] |
178 | ]; |
179 | |
180 | private const JUNK_PAGE = [ |
181 | 'query' => [ |
182 | 'pages' => [ |
183 | [ |
184 | 'pageid' => 2, |
185 | 'ns' => 0, |
186 | 'title' => 'Junk Page', |
187 | 'revisions' => [ |
188 | [ |
189 | 'revid' => 2, |
190 | 'parentid' => 0, |
191 | 'slots' => [ |
192 | 'main' => [ |
193 | 'contentmodel' => 'wikitext', |
194 | 'contentformat' => 'text/x-wiki', |
195 | 'content' => '2. This is just some junk. See the comment above.' |
196 | ] |
197 | ] |
198 | ] |
199 | ] |
200 | ] |
201 | ] |
202 | ] |
203 | ]; |
204 | |
205 | private const LARGE_PAGE = [ |
206 | 'query' => [ |
207 | 'pages' => [ |
208 | [ |
209 | 'pageid' => 3, |
210 | 'ns' => 0, |
211 | 'title' => 'Large_Page', |
212 | 'revisions' => [ |
213 | [ |
214 | 'revid' => 3, |
215 | 'parentid' => 0, |
216 | 'slots' => [ |
217 | 'main' => [ |
218 | 'contentmodel' => 'wikitext', |
219 | 'contentformat' => 'text/x-wiki', |
220 | /* content will be set separately */ |
221 | ] |
222 | ] |
223 | ] |
224 | ] |
225 | ] |
226 | ] |
227 | ] |
228 | ]; |
229 | |
230 | private const REUSE_PAGE = [ |
231 | 'query' => [ |
232 | 'pages' => [ |
233 | [ |
234 | 'pageid' => 100, |
235 | 'ns' => 0, |
236 | 'title' => 'Reuse_Page', |
237 | 'revisions' => [ |
238 | [ |
239 | 'revid' => 100, |
240 | 'parentid' => 0, |
241 | 'slots' => [ |
242 | 'main' => [ |
243 | 'contentmodel' => 'wikitext', |
244 | 'contentformat' => 'text/x-wiki', |
245 | 'content' => '{{colours of the rainbow}}' |
246 | ] |
247 | ] |
248 | ] |
249 | ] |
250 | ] |
251 | ] |
252 | ] |
253 | ]; |
254 | |
255 | private const JSON_PAGE = [ |
256 | 'query' => [ |
257 | 'pages' => [ |
258 | [ |
259 | 'pageid' => 101, |
260 | 'ns' => 0, |
261 | 'title' => 'JSON_Page', |
262 | 'revisions' => [ |
263 | [ |
264 | 'revid' => 101, |
265 | 'parentid' => 0, |
266 | 'slots' => [ |
267 | 'main' => [ |
268 | 'contentmodel' => 'json', |
269 | 'contentformat' => 'text/json', |
270 | 'content' => '[1]' |
271 | ] |
272 | ] |
273 | ] |
274 | ] |
275 | ] |
276 | ] |
277 | ] |
278 | ]; |
279 | |
280 | private const LINT_PAGE = [ |
281 | 'query' => [ |
282 | 'pages' => [ |
283 | [ |
284 | 'pageid' => 102, |
285 | 'ns' => 0, |
286 | 'title' => 'Lint Page', |
287 | 'revisions' => [ |
288 | [ |
289 | 'revid' => 102, |
290 | 'parentid' => 0, |
291 | 'slots' => [ |
292 | 'main' => [ |
293 | 'contentmodel' => 'wikitext', |
294 | 'contentformat' => 'text/x-wiki', |
295 | 'content' => "{|\nhi\n|ho\n|}" |
296 | ] |
297 | ] |
298 | ] |
299 | ] |
300 | ] |
301 | ] |
302 | ] |
303 | ]; |
304 | |
305 | private const REDLINKS_PAGE = [ |
306 | 'query' => [ |
307 | 'pages' => [ |
308 | [ |
309 | 'pageid' => 103, |
310 | 'ns' => 0, |
311 | 'title' => 'Redlinks Page', |
312 | 'revisions' => [ |
313 | [ |
314 | 'revid' => 103, |
315 | 'parentid' => 0, |
316 | 'slots' => [ |
317 | 'main' => [ |
318 | 'contentmodel' => 'wikitext', |
319 | 'contentformat' => 'text/x-wiki', |
320 | 'content' => '[[Special:Version]] [[Doesnotexist]] [[Redirected]]' |
321 | ] |
322 | ] |
323 | ] |
324 | ] |
325 | ] |
326 | ] |
327 | ] |
328 | ]; |
329 | |
330 | private const VARIANT_PAGE = [ |
331 | 'query' => [ |
332 | 'pages' => [ |
333 | [ |
334 | 'pageid' => 104, |
335 | 'ns' => 0, |
336 | 'pagelanguage' => 'sr', |
337 | 'pagelanguagedir' => 'ltr', |
338 | 'title' => 'Variant Page', |
339 | 'revisions' => [ |
340 | [ |
341 | 'revid' => 104, |
342 | 'parentid' => 0, |
343 | 'slots' => [ |
344 | 'main' => [ |
345 | 'contentmodel' => 'wikitext', |
346 | 'contentformat' => 'text/x-wiki', |
347 | 'content' => "абвг abcd" |
348 | ] |
349 | ] |
350 | ] |
351 | ] |
352 | ] |
353 | ] |
354 | ] |
355 | ]; |
356 | |
357 | private const NOVARIANT_PAGE = [ |
358 | 'query' => [ |
359 | 'pages' => [ |
360 | [ |
361 | 'pageid' => 105, |
362 | 'ns' => 0, |
363 | 'pagelanguage' => 'sr', |
364 | 'pagelanguagedir' => 'ltr', |
365 | 'title' => 'No Variant Page', |
366 | 'revisions' => [ |
367 | [ |
368 | 'revid' => 105, |
369 | 'parentid' => 0, |
370 | 'slots' => [ |
371 | 'main' => [ |
372 | 'contentmodel' => 'wikitext', |
373 | 'contentformat' => 'text/x-wiki', |
374 | 'content' => "абвг abcd\n__NOCONTENTCONVERT__" |
375 | ] |
376 | ] |
377 | ] |
378 | ] |
379 | ] |
380 | ] |
381 | ] |
382 | ]; |
383 | |
384 | private const REVISION_PAGE = [ |
385 | 'query' => [ |
386 | 'pages' => [ |
387 | [ |
388 | 'pageid' => 63, |
389 | 'ns' => 0, |
390 | 'title' => 'Revision ID', |
391 | 'revisions' => [ |
392 | [ |
393 | 'revid' => 63, |
394 | 'parentid' => 0, |
395 | 'slots' => [ |
396 | 'main' => [ |
397 | 'contentmodel' => 'wikitext', |
398 | 'contentformat' => 'text/x-wiki', |
399 | 'content' => '{{REVISIONID}}' |
400 | ] |
401 | ] |
402 | ] |
403 | ] |
404 | ] |
405 | ] |
406 | ] |
407 | ]; |
408 | |
409 | private const MISSING_TITLES = [ 'Doesnotexist' ]; |
410 | private const SPECIAL_TITLES = [ |
411 | 'Special:Version', |
412 | 'Special:BookSources', |
413 | 'Special:BookSources/isbn=4-00-026157-6', |
414 | 'Special:BookSources/0978739256', |
415 | ]; |
416 | private const REDIRECT_TITLES = [ 'Redirected' ]; |
417 | private const DISAMBIG_TITLES = [ 'Disambiguation' ]; |
418 | |
419 | private const FNAMES = [ |
420 | 'Image:Foobar.jpg' => 'Foobar.jpg', |
421 | 'Datei:Foobar.jpg' => 'Foobar.jpg', |
422 | 'File:Foobar.jpg' => 'Foobar.jpg', |
423 | 'Archivo:Foobar.jpg' => 'Foobar.jpg', |
424 | 'Mynd:Foobar.jpg' => 'Foobar.jpg', |
425 | "Датотека:Foobar.jpg" => 'Foobar.jpg', |
426 | 'Dosiero:Foobar.jpg' => 'Foobar.jpg', |
427 | 'Image:Foobar.svg' => 'Foobar.svg', |
428 | 'File:Foobar.svg' => 'Foobar.svg', |
429 | 'Файл:Foobar.svg' => 'Foobar.svg', |
430 | 'Datei:Foobar.svg' => 'Foobar.svg', |
431 | 'Image:Thumb.png' => 'Thumb.png', |
432 | 'File:Thumb.png' => 'Thumb.png', |
433 | 'File:LoremIpsum.djvu' => 'LoremIpsum.djvu', |
434 | 'File:Video.ogv' => 'Video.ogv', |
435 | 'File:Transcode.webm' => 'Transcode.webm', |
436 | 'File:Audio.oga' => 'Audio.oga', |
437 | 'File:Bad.jpg' => 'Bad.jpg', |
438 | 'File:Hi-ho.jpg' => 'Hi-ho.jpg', |
439 | ]; |
440 | |
441 | private const PNAMES = [ |
442 | 'Image:Foobar.jpg' => 'File:Foobar.jpg', |
443 | 'Image:Foobar.svg' => 'File:Foobar.svg', |
444 | 'Image:Thumb.png' => 'File:Thumb.png' |
445 | ]; |
446 | |
447 | // FIXME: Get this info from pagelanguage of a revision for these pages |
448 | private const PAGELANGS = [ |
449 | 'Rupage' => 'ru', |
450 | 'Depage' => 'de', |
451 | ]; |
452 | |
453 | // File is present in these langs |
454 | private const FILELANGS = [ |
455 | 'Foobar.svg' => [ 'en', 'ru' ], |
456 | ]; |
457 | |
458 | // This templatedata description only provides a subset of fields |
459 | // that mediawiki API returns. Parsoid only uses the format and |
460 | // paramOrder fields at this point, so keeping these lean. |
461 | private const TEMPLATE_DATA = [ |
462 | 'Template:NoFormatWithParamOrder' => [ |
463 | 'paramOrder' => [ 'f0', 'f1', 'unused2', 'f2', 'unused3' ] |
464 | ], |
465 | 'Template:InlineTplNoParamOrder' => [ |
466 | 'format' => 'inline' |
467 | ], |
468 | 'Template:BlockTplNoParamOrder' => [ |
469 | 'format' => 'block' |
470 | ], |
471 | 'Template:InlineTplWithParamOrder' => [ |
472 | 'format' => 'inline', |
473 | 'paramOrder' => [ 'f1', 'f2' ] |
474 | ], |
475 | 'Template:BlockTplWithParamOrder' => [ |
476 | 'format' => 'block', |
477 | 'paramOrder' => [ 'f1', 'f2' ] |
478 | ], |
479 | 'Template:WithParamOrderAndAliases' => [ |
480 | 'params' => [ |
481 | 'f1' => [ 'aliases' => [ 'f4', 'f3' ] ] |
482 | ], |
483 | 'paramOrder' => [ 'f1', 'f2' ] |
484 | ], |
485 | 'Template:InlineFormattedTpl_1' => [ |
486 | 'format' => '{{_|_=_}}' |
487 | ], |
488 | 'Template:InlineFormattedTpl_2' => [ |
489 | 'format' => "\n{{_ | _ = _}}" |
490 | ], |
491 | 'Template:InlineFormattedTpl_3' => [ |
492 | 'format' => '{{_| _____ = _}}' |
493 | ], |
494 | 'Template:BlockFormattedTpl_1' => [ |
495 | 'format' => "{{_\n| _ = _\n}}" |
496 | ], |
497 | 'Template:BlockFormattedTpl_2' => [ |
498 | 'format' => "\n{{_\n| _ = _\n}}\n" |
499 | ], |
500 | 'Template:BlockFormattedTpl_3' => [ |
501 | 'format' => "{{_|\n _____ = _}}" |
502 | ] |
503 | ]; |
504 | |
505 | /** @var string wiki prefix for which we are mocking the api access */ |
506 | private $prefix = 'enwiki'; |
507 | |
508 | /** @var callable(string):string A helper to normalize titles. */ |
509 | private $normalizeTitle = null; |
510 | |
511 | public function __construct( ?string $prefix = null, ?callable $normalizeTitleFunc = null ) { |
512 | $this->prefix = $prefix ?? $this->prefix; |
513 | $this->normalizeTitle = $normalizeTitleFunc ?? |
514 | // poor man's normalization |
515 | ( static fn ( $t ) => str_replace( ' ', '_', $t ) ); |
516 | } |
517 | |
518 | /** |
519 | * Update prefix |
520 | * @param string $prefix |
521 | */ |
522 | public function setApiPrefix( string $prefix ): void { |
523 | $this->prefix = $prefix; |
524 | } |
525 | |
526 | /** |
527 | * Register an article defined in parsertests so that we can return |
528 | * the proper known/missing information about that title. |
529 | * @param string $key The normalized title of the article |
530 | * @param Article $article The contents of the article |
531 | * @return callable |
532 | */ |
533 | public function addArticle( string $key, Article $article ): callable { |
534 | $oldVal = $this->articleCache[$key] ?? null; |
535 | $this->articleCache[$key] = $article; |
536 | return function () use ( $key, $oldVal ) { |
537 | $this->articleCache[$key] = $oldVal; |
538 | }; |
539 | } |
540 | |
541 | public function makeRequest( array $params ): array { |
542 | switch ( $params['action'] ?? null ) { |
543 | case 'query': |
544 | return $this->processQuery( $params ); |
545 | |
546 | case 'parse': |
547 | return $this->parse( $params['text'], !empty( $params['onlypst'] ) ); |
548 | |
549 | case 'templatedata': |
550 | return $this->fetchTemplateData( $params ); |
551 | |
552 | case 'expandtemplates': |
553 | $ret = $this->preProcess( $params['titles'] ?? $params['title'], $params['text'], $params['revid'] ?? null ); |
554 | if ( $ret ) { |
555 | $ret += [ |
556 | 'categories' => [], |
557 | 'modules' => [], |
558 | 'modulestyles' => [] |
559 | ]; |
560 | } |
561 | return $ret; |
562 | |
563 | default: |
564 | return []; // FIXME: Maybe some error |
565 | } |
566 | } |
567 | |
568 | /** |
569 | * Image scaling computation helper. |
570 | * |
571 | * Linker.php in core calls File::transform(...) for each dimension (1x, |
572 | * 1.5x, 2x) which then scales the image dimensions, using round/ceil/floor |
573 | * as appropriate to yield integer dimensions. Note that the results |
574 | * may be unintuitive due to the conversion to integer: eg, a 442px width |
575 | * image may become 883px in 2x mode. Resist the temptation to "optimize" |
576 | * this by computing the transformed size once and then scaling that; |
577 | * always scale the input dimensions instead. |
578 | * @see ImageHandler::normaliseParams, MediaHandler::fitBoxWidth, |
579 | * File::scaleHeight, etc, in core. |
580 | * |
581 | * Either $twidth or $theight or both will be set when called; both |
582 | * will be set when this function returns. |
583 | * |
584 | * @param int $width Original image width |
585 | * @param int $height Original image height |
586 | * @param int|float|null &$twidth Thumbnail width (inout parameter) |
587 | * @param int|float|null &$theight Thumbnail height (inout parameter) |
588 | */ |
589 | public static function transformHelper( $width, $height, &$twidth, &$theight ) { |
590 | if ( $theight === null ) { |
591 | // File::scaleHeight in PHP |
592 | $theight = round( $height * $twidth / $width ); |
593 | } elseif ( |
594 | $twidth === null || |
595 | // Match checks in ImageHandler.php::normaliseParams in core |
596 | ( $twidth * $height > $theight * $width ) |
597 | ) { |
598 | // MediaHandler::fitBoxWidth in PHP |
599 | // This is crazy! |
600 | $idealWidth = $width * $theight / $height; |
601 | $roundedUp = ceil( $idealWidth ); |
602 | if ( round( $roundedUp * $height / $width ) > $theight ) { |
603 | $twidth = floor( $idealWidth ); |
604 | } else { |
605 | $twidth = $roundedUp; |
606 | } |
607 | } else { |
608 | if ( round( $height * $twidth / $width ) > $theight ) { |
609 | $twidth = ceil( $width * $theight / $height ); |
610 | } else { |
611 | $theight = round( $height * $twidth / $width ); |
612 | } |
613 | } |
614 | } |
615 | |
616 | /** |
617 | * @param string $filename |
618 | * @param ?int $twidth |
619 | * @param ?int $theight |
620 | * @param ?string $extraParam optional iiurlparam, used for video/pdf/etc |
621 | * @param ?string $contexttitle optional iibadfilecontexttitle |
622 | * @return ?array |
623 | */ |
624 | private function imageInfo( |
625 | string $filename, ?int $twidth, ?int $theight, ?string $extraParam, |
626 | ?string $contexttitle |
627 | ): ?array { |
628 | $normPageName = self::PNAMES[$filename] ?? $filename; |
629 | $normFileName = self::FNAMES[$filename] ?? $filename; |
630 | $props = self::FILE_PROPS[$normFileName] ?? null; |
631 | if ( $props === null ) { |
632 | // We don't have info for this file |
633 | return null; |
634 | } |
635 | |
636 | $md5 = md5( $normFileName ); |
637 | $md5prefix = $md5[0] . '/' . $md5[0] . $md5[1] . '/'; |
638 | $baseurl = self::IMAGE_BASE_URL . '/' . $md5prefix . $normFileName; |
639 | $height = $props['height']; |
640 | $width = $props['width']; |
641 | $turl = self::IMAGE_BASE_URL . '/thumb/' . $md5prefix . $normFileName; |
642 | $durl = self::IMAGE_DESC_URL . '/' . $normFileName; |
643 | $mediatype = $props['mediatype'] ?? |
644 | ( $props['mime'] === 'image/svg+xml' ? 'DRAWING' : 'BITMAP' ); |
645 | |
646 | $info = [ |
647 | 'size' => $props['size'], |
648 | 'height' => $height, |
649 | 'width' => $width, |
650 | 'url' => $baseurl, |
651 | 'descriptionurl' => $durl, |
652 | 'mediatype' => $mediatype, |
653 | 'mime' => $props['mime'] |
654 | ]; |
655 | |
656 | if ( isset( $props['duration'] ) ) { |
657 | $info['duration'] = $props['duration']; |
658 | } |
659 | if ( isset( $props['pagecount'] ) ) { |
660 | $info['pagecount'] = $props['pagecount']; |
661 | } |
662 | |
663 | if ( ( $mediatype === 'VIDEO' || $mediatype === 'DRAWING' ) && !$twidth && !$theight ) { |
664 | $twidth = $width; |
665 | $theight = $height; |
666 | } |
667 | |
668 | preg_match( '/^lang([a-z]+(?:-[a-z]+)*)-(\d+)px$/i', $extraParam ?? '', $matches ); |
669 | $lang = $matches[1] ?? null; |
670 | $pagelang = self::PAGELANGS[$contexttitle] ?? 'en'; |
671 | $filelangs = self::FILELANGS[$normFileName] ?? [ 'en' ]; |
672 | |
673 | // Set $lang based on the targetlang, if the file is present in that lang |
674 | if ( |
675 | $lang === null && |
676 | $mediatype === 'DRAWING' && |
677 | $pagelang !== 'en' && |
678 | in_array( $pagelang, $filelangs, true ) |
679 | ) { |
680 | $lang = $pagelang; |
681 | $extraParam = "lang{$lang}-{$twidth}px"; |
682 | } |
683 | |
684 | if ( $theight || $twidth ) { |
685 | |
686 | // Save $twidth and $theight |
687 | $origThumbHeight = $theight; |
688 | $origThumbWidth = $twidth; |
689 | |
690 | // Set $twidth and $theight |
691 | self::transformHelper( $width, $height, $twidth, $theight ); |
692 | |
693 | $urlWidth = $twidth; |
694 | if ( $twidth > $width ) { |
695 | // The PHP api won't enlarge a bitmap ... but the batch api will. |
696 | // But, to match the PHP sections, don't scale. |
697 | if ( $mediatype !== 'DRAWING' ) { |
698 | $urlWidth = $width; |
699 | } |
700 | } |
701 | $thumbBaseUrl = $turl; |
702 | $page = null; |
703 | if ( $urlWidth !== $width || $mediatype === 'AUDIO' || $mediatype === 'VIDEO' || $mediatype === 'OFFICE' || $mediatype === 'DRAWING' ) { |
704 | $turl .= '/'; |
705 | if ( preg_match( '/^page(\d+)-(\d+)px$/', $extraParam ?? '', $matches ) ) { |
706 | $turl .= $extraParam; |
707 | $page = (int)$matches[1]; |
708 | } elseif ( $mediatype === 'OFFICE' ) { |
709 | $turl .= 'page1-' . $urlWidth . 'px'; |
710 | $page = 1; |
711 | } elseif ( $lang !== null ) { |
712 | // Explicit English just gets the default path |
713 | if ( $lang === 'en' ) { |
714 | $turl .= $urlWidth . 'px'; |
715 | $lang = null; |
716 | } else { |
717 | $turl .= $extraParam; |
718 | } |
719 | } else { |
720 | $turl .= $urlWidth . 'px'; |
721 | } |
722 | $turl .= '-'; |
723 | if ( $mediatype === 'VIDEO' ) { |
724 | // Hack in a 'seek' option, if provided (T258767) |
725 | if ( str_starts_with( $extraParam ?? '', 'seek' ) ) { |
726 | $turl .= $props['extraParams'][$extraParam] ?? ''; |
727 | } |
728 | $turl .= '-'; |
729 | } |
730 | $turl .= $normFileName; |
731 | switch ( $mediatype ) { |
732 | case 'AUDIO': |
733 | // No thumbs are generated for audio |
734 | $turl = self::IMAGE_BASE_URL . '/w/resources/assets/file-type-icons/fileicon-ogg.png'; |
735 | break; |
736 | case 'VIDEO': |
737 | case 'OFFICE': |
738 | $turl .= '.jpg'; |
739 | break; |
740 | case 'DRAWING': |
741 | $turl .= '.png'; |
742 | break; |
743 | } |
744 | } else { |
745 | $turl = $baseurl; |
746 | } |
747 | $info['thumbwidth'] = $twidth; |
748 | $info['thumbheight'] = $theight; |
749 | $info['thumburl'] = $turl; |
750 | // src set info; added to core API result as part of T226683 |
751 | // See Linker.php::processResponsiveImages() in core |
752 | foreach ( [ 1.5, 2 ] as $scale ) { |
753 | $stwidth = $stheight = null; |
754 | if ( $origThumbWidth !== null ) { |
755 | $stwidth = round( $origThumbWidth * $scale ); |
756 | } |
757 | if ( $origThumbHeight !== null ) { |
758 | $stheight = round( $origThumbHeight * $scale ); |
759 | } |
760 | self::transformHelper( $width, $height, $stwidth, $stheight ); |
761 | $turl = $baseurl; |
762 | if ( |
763 | $stwidth < $width || |
764 | $mediatype === 'DRAWING' || |
765 | $mediatype === 'OFFICE' |
766 | ) { |
767 | $turl = $thumbBaseUrl . '/'; |
768 | if ( $page !== null ) { |
769 | $turl .= "page{$page}-"; |
770 | } |
771 | if ( $lang !== null ) { |
772 | $turl .= "lang{$lang}-"; |
773 | } |
774 | $turl .= $stwidth . 'px-' . $normFileName; |
775 | if ( $mediatype === 'VIDEO' || $mediatype === 'OFFICE' ) { |
776 | $turl .= '.jpg'; |
777 | } elseif ( $mediatype === 'DRAWING' ) { |
778 | $turl .= '.png'; |
779 | } |
780 | } |
781 | if ( $info['thumburl'] !== $turl && $mediatype !== 'AUDIO' ) { |
782 | $info['responsiveUrls']["$scale"] = $turl; |
783 | } |
784 | } |
785 | } |
786 | |
787 | if ( isset( $props['derivatives'] ) ) { |
788 | $info['derivatives'] = [ |
789 | [ |
790 | 'src' => $info['url'], |
791 | 'type' => $info['mime'], |
792 | 'width' => strval( $info['width'] ), |
793 | 'height' => strval( $info['height'] ), |
794 | ], |
795 | ]; |
796 | foreach ( $props['derivatives'] as $derivative ) { |
797 | $info['derivatives'][] = [ |
798 | 'src' => self::IMAGE_BASE_URL . '/transcoded/' . |
799 | $md5prefix . $normFileName . '/' . |
800 | $normFileName . '.' . $derivative['transcodekey'], |
801 | 'type' => $derivative['type'], |
802 | 'transcodekey' => $derivative['transcodekey'], |
803 | 'width' => strval( $derivative['width'] ), |
804 | 'height' => strval( $derivative['height'] ), |
805 | ]; |
806 | } |
807 | } |
808 | |
809 | return [ |
810 | 'result' => $info, |
811 | 'normPageName' => $normPageName |
812 | ]; |
813 | } |
814 | |
815 | private const TRACKING_CATEGORIES = [ |
816 | 'broken-file-category' => 'Pages with broken file links', |
817 | 'magiclink-tracking-rfc' => 'Pages using RFC magic links', |
818 | 'magiclink-tracking-isbn' => 'Pages using ISBN magic links', |
819 | 'magiclink-tracking-pmid' => 'Pages using PMID magic links', |
820 | 'hidden-category-category' => 'Hidden categories', |
821 | ]; |
822 | |
823 | private function processQuery( array $params ): array { |
824 | if ( ( $params['meta'] ?? null ) === 'siteinfo' ) { |
825 | if ( !isset( $this->cachedConfigs[$this->prefix] ) ) { |
826 | $this->cachedConfigs[$this->prefix] = json_decode( |
827 | file_get_contents( __DIR__ . "/../../baseconfig/$this->prefix.json" ), true ); |
828 | } |
829 | return $this->cachedConfigs[$this->prefix]; |
830 | } |
831 | |
832 | if ( ( $params['meta'] ?? null ) === 'allmessages' ) { |
833 | $allmessages = []; |
834 | if ( isset( self::TRACKING_CATEGORIES[$params['ammessages']] ) ) { |
835 | $allmessages[] = [ |
836 | 'content' => self::TRACKING_CATEGORIES[$params['ammessages']] |
837 | ]; |
838 | } else { |
839 | $allmessages[] = [ 'missing' => true ]; |
840 | } |
841 | return [ 'query' => [ 'allmessages' => $allmessages ] ]; |
842 | } |
843 | |
844 | $revid = $params['revids'] ?? null; |
845 | |
846 | if ( ( $params['prop'] ?? null ) === 'revisions' ) { |
847 | if ( $revid === '1' || $params['titles'] === 'Main_Page' ) { |
848 | return self::MAIN_PAGE; |
849 | } elseif ( $revid === '2' || $params['titles'] === 'Junk_Page' ) { |
850 | return self::JUNK_PAGE; |
851 | } elseif ( $revid === '3' || $params['titles'] === 'Large_Page' ) { |
852 | $largePage = self::LARGE_PAGE; |
853 | // PORT-FIXME: Need to get this value |
854 | // $wtSizeLimit = $parsoidOptions->limits->wt2html->maxWikitextSize; |
855 | $wtSizeLimit = 1000000; |
856 | $largePage['query']['pages'][0]['revisions'][0]['slots']['main']['content'] |
857 | = str_repeat( 'a', $wtSizeLimit + 1 ); |
858 | return $largePage; |
859 | } elseif ( $revid === '63' || $params['titles'] === 'Revision_ID' ) { |
860 | return self::REVISION_PAGE; |
861 | } elseif ( $revid === '100' || $params['titles'] === 'Reuse_Page' ) { |
862 | return self::REUSE_PAGE; |
863 | } elseif ( $revid === '101' || $params['titles'] === 'JSON_Page' ) { |
864 | return self::JSON_PAGE; |
865 | } elseif ( $revid === '102' || $params['titles'] === 'Lint_Page' ) { |
866 | return self::LINT_PAGE; |
867 | } elseif ( $revid === '103' || $params['titles'] === 'Redlinks_Page' ) { |
868 | return self::REDLINKS_PAGE; |
869 | } elseif ( $revid === '104' || $params['titles'] === 'Variant_Page' ) { |
870 | return self::VARIANT_PAGE; |
871 | } elseif ( $revid === '105' || $params['titles'] === 'No_Variant_Page' ) { |
872 | return self::NOVARIANT_PAGE; |
873 | } elseif ( $revid === '999' || $params['titles'] === 'Old_Response' ) { |
874 | return self::OLD_RESPONSE; |
875 | } else { |
876 | return [ 'query' => [ 'pages' => [ |
877 | [ |
878 | 'ns' => 6, |
879 | 'title' => json_encode( $params['titles'] ), |
880 | 'missing' => true, |
881 | 'imagerepository' => true |
882 | ] |
883 | ] |
884 | ] |
885 | ]; |
886 | } |
887 | } |
888 | |
889 | if ( ( $params['prop'] ?? null ) === 'info' ) { |
890 | $ret = []; |
891 | $titles = preg_split( '/\|/', $params['titles'] ); |
892 | foreach ( $titles as $t ) { |
893 | $props = [ 'title' => $t ]; |
894 | $normalizeTitle = $this->normalizeTitle; |
895 | $key = $normalizeTitle( $t ); |
896 | $definedInPt = isset( $this->articleCache[$key] ); |
897 | if ( in_array( $t, self::MISSING_TITLES, true ) || |
898 | !$definedInPt ) { |
899 | $props['missing'] = true; |
900 | } |
901 | if ( in_array( $t, self::SPECIAL_TITLES, true ) ) { |
902 | $props['special'] = true; |
903 | $props['missing'] = false; |
904 | } |
905 | if ( in_array( $t, self::REDIRECT_TITLES, true ) ) { |
906 | $props['redirect'] = true; |
907 | $props['missing'] = false; |
908 | } |
909 | if ( in_array( $t, self::DISAMBIG_TITLES, true ) ) { |
910 | $props['linkclasses'] = [ 'mw-disambig' ]; |
911 | $props['missing'] = false; |
912 | } |
913 | $ret[] = $props; |
914 | } |
915 | return [ 'query' => [ 'pages' => $ret ] ]; |
916 | } |
917 | |
918 | if ( ( $params['prop'] ?? null ) === 'imageinfo' ) { |
919 | $response = [ 'query' => [] ]; |
920 | $filename = $params['titles']; // assumes this is a single file |
921 | $tonum = static function ( $x ) { |
922 | return $x ? (int)$x : null; |
923 | }; |
924 | $ii = self::imageInfo( |
925 | $filename, |
926 | isset( $params['iiurlwidth'] ) ? $tonum( $params['iiurlwidth'] ) : null, |
927 | isset( $params['iiurlheight'] ) ? $tonum( $params['iiurlheight'] ) : null, |
928 | $params['iiurlparam'] ?? null, |
929 | $params['iibadfilecontexttitle'] ?? null |
930 | ); |
931 | if ( $ii === null ) { |
932 | $p = [ |
933 | 'ns' => 6, |
934 | 'title' => $filename, |
935 | 'imagerepository' => true, |
936 | 'imageinfo' => [ [ |
937 | 'size' => 0, |
938 | 'width' => 0, |
939 | 'height' => 0, |
940 | 'filemissing' => true, |
941 | 'mime' => null, |
942 | 'mediatype' => null |
943 | ] ] |
944 | ]; |
945 | $p['missing'] = $p['imageinfo']['filemissing'] = true; |
946 | $p['badfile'] = false; |
947 | } else { |
948 | if ( $filename !== $ii['normPageName'] ) { |
949 | $response['query']['normalized'] = [ |
950 | [ 'from' => $filename, 'to' => $ii['normPageName'] ] |
951 | ]; |
952 | } |
953 | $p = [ |
954 | 'pageid' => 1, |
955 | 'ns' => 6, |
956 | 'title' => $ii['normPageName'], |
957 | 'imageinfo' => [ $ii['result'] ] |
958 | ]; |
959 | $p['badfile'] = ( $filename === 'File:Bad.jpg' ); |
960 | } |
961 | $response['query']['pages'] = [ $p ]; |
962 | |
963 | return $response; |
964 | } |
965 | |
966 | return [ "error" => new Error( 'Uh oh!' ) ]; |
967 | } |
968 | |
969 | private function parse( string $text, bool $onlypst ): array { |
970 | // We're performing a subst |
971 | if ( $onlypst ) { |
972 | return [ 'text' => preg_replace( '/\{\{subst:1x\|([^}]+)\}\}/', '$1', $text, 1 ) ]; |
973 | } |
974 | |
975 | $res = null; |
976 | // Render to html the contents of known extension tags |
977 | // These are the only known extensions (besides native extensions) |
978 | // used in parser tests currently. This would need to be updated |
979 | // as more templates are added OR we need to rely on true parsing. |
980 | preg_match( '#<([A-Za-z][^\t\n\v />\0]*)#', $text, $match ); |
981 | switch ( $match[1] ?? '' ) { |
982 | // FIXME: this isn't really used by the mocha tests |
983 | // since some mocha tests hit the production db, but |
984 | // when we fix that, they should go through this. |
985 | case 'templatestyles': |
986 | $res = "<style data-mw-deduplicate='TemplateStyles:r123456'>small { font-size: 120% } big { font-size: 80% }</style>"; // Silliness |
987 | break; |
988 | |
989 | case 'translate': |
990 | $res = $text; |
991 | break; |
992 | |
993 | case 'indicator': |
994 | case 'section': |
995 | $res = ""; |
996 | break; |
997 | |
998 | default: |
999 | throw new Error( 'Unhandled extension type encountered in: ' . $text ); |
1000 | } |
1001 | |
1002 | $parse = [ |
1003 | 'text' => $res, |
1004 | 'categories' => [], |
1005 | 'modules' => [], |
1006 | 'modulestyles' => [] |
1007 | ]; |
1008 | return [ 'parse' => $parse ]; |
1009 | } |
1010 | |
1011 | private function preProcess( |
1012 | string $title, string $text, ?int $revid |
1013 | ): ?array { |
1014 | // These are the only known templates in current parser tests. |
1015 | // This would need to be updated as more templates are added OR we need |
1016 | // to rely on true (instead of mock) preprocessing. |
1017 | preg_match( '/{{1x\|(.*?)}}/', $text, $match ); |
1018 | if ( $match ) { |
1019 | return [ 'wikitext' => $match[1] ]; |
1020 | } elseif ( $text === '{{colours of the rainbow}}' ) { |
1021 | return [ 'wikitext' => 'purple' ]; |
1022 | } elseif ( $text === '{{REVISIONID}}' ) { |
1023 | return [ 'wikitext' => (string)$revid ]; |
1024 | } else { |
1025 | error_log( "UNKNOWN TEMPLATE: $text for $title\n" ); |
1026 | return null; |
1027 | } |
1028 | } |
1029 | |
1030 | private function fetchTemplateData( array $params ): array { |
1031 | return [ |
1032 | // Assumes that titles is a single title |
1033 | // (which is how Parsoid uses this) |
1034 | 'pages' => [ |
1035 | '1' => self::TEMPLATE_DATA[$params['titles'] ?? ''] ?? [] |
1036 | ] |
1037 | ]; |
1038 | } |
1039 | } |