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.
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
= [
27 'category-subcat-count' => [
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() {
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;
74 'name' => $languageCode . ' ' . $key . ' ' . implode( ',', $args ),
78 'lang' => $languageCode
84 'messages' => $messages,
90 private function writeJsonFile( array $data, $dataSpecFile ) {
92 '@' => 'Last generated with ' . basename( __FILE__
) . ' at ' . gmdate( 'r' ),
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
;