gallery: Fix phan annotation for ImageGalleryBase::getImages
[mediawiki.git] / tests / qunit / data / generateJqueryMsgData.php
blob2f935c686414c0a25865ea54d43a98be7da3e35d
1 <?php
2 /**
3 * This PHP script defines the spec that the mediawiki.jqueryMsg module should conform to.
5 * It does this by looking up the results of various kinds of string parsing, with various
6 * languages, in the current installation of MediaWiki. It then outputs a static specification,
7 * mapping expected inputs to outputs, which is used then run by QUnit.
8 */
10 use MediaWiki\Json\FormatJson;
11 use MediaWiki\Languages\LanguageFactory;
12 use MediaWiki\Maintenance\Maintenance;
14 require __DIR__ . '/../../../maintenance/Maintenance.php';
16 class GenerateJqueryMsgData extends Maintenance {
18 private const KEY_TO_TEST_ARGS = [
19 'undelete_short' => [
20 [ 0 ],
21 [ 1 ],
22 [ 2 ],
23 [ 5 ],
24 [ 21 ],
25 [ 101 ]
27 'category-subcat-count' => [
28 [ 0, 10 ],
29 [ 1, 1 ],
30 [ 1, 2 ],
31 [ 3, 30 ]
35 private const TEST_LANGS = [ 'en', 'fr', 'ar', 'jp', 'zh', 'nl', 'ml', 'hi' ];
37 /** @var LanguageFactory */
38 private $languageFactory;
40 public function __construct() {
41 parent::__construct();
42 $this->addDescription( 'Create a specification for message parsing ini JSON format' );
43 // add any other options here
46 public function execute() {
47 $this->languageFactory = $this->getServiceContainer()->getLanguageFactory();
48 $data = $this->getData();
49 $this->writeJsonFile( $data, __DIR__ . '/mediawiki.jqueryMsg.data.json' );
52 private function getData() {
53 $messages = [];
54 $tests = [];
55 $jsData = [];
56 foreach ( self::TEST_LANGS as $languageCode ) {
57 $language = $this->languageFactory->getLanguage( $languageCode );
58 $jsData[$languageCode] = $language->getJsData();
59 foreach ( self::KEY_TO_TEST_ARGS as $key => $testArgs ) {
60 foreach ( $testArgs as $args ) {
61 // Get the raw message, without any transformations.
62 $template = wfMessage( $key )->useDatabase( false )
63 ->inLanguage( $languageCode )->plain();
65 // Get the magic-parsed version with args.
66 $result = wfMessage( $key, ...$args )->useDatabase( false )
67 ->inLanguage( $languageCode )->text();
69 // Record the template, args, language, and expected result
70 // fake multiple languages by flattening them together.
71 $langKey = $languageCode . '_' . $key;
72 $messages[$langKey] = $template;
73 $tests[] = [
74 'name' => $languageCode . ' ' . $key . ' ' . implode( ',', $args ),
75 'key' => $langKey,
76 'args' => $args,
77 'result' => $result,
78 'lang' => $languageCode
83 return [
84 'messages' => $messages,
85 'tests' => $tests,
86 'jsData' => $jsData,
90 private function writeJsonFile( array $data, $dataSpecFile ) {
91 $phpParserData = [
92 '@' => 'Last generated with ' . basename( __FILE__ ) . ' at ' . gmdate( 'r' ),
93 ] + $data;
95 $output = FormatJson::encode( $phpParserData, true ) . "\n";
96 $fp = file_put_contents( $dataSpecFile, $output );
97 if ( $fp === false ) {
98 die( "Couldn't write to $dataSpecFile." );
103 $maintClass = GenerateJqueryMsgData::class;
104 require_once RUN_MAINTENANCE_IF_MAIN;