wiki.techinc.nl/tests/qunit/data/generateJqueryMsgData.php
Tim Starling a59c446154 ResourceLoader: Allow FileModule scripts to be generated with a callback
To implement source maps, we want FileModule::getScript() to return
an array with path information, but that would break subclasses of
FileModule which concatenate to the return value of parent::getScript().
So allow scripts to be generated by a callback, eliminating the need for
concatenation in subclasses.

* Factor out most of the loop body of expandPackageFiles() into
  expandFileInfo(). Add 'name' to the return value. Ensure 'filePath'
  is always a FilePath object since we need that to safely return it
  to ResourceLoader later. Document the return value.
* Make sure the base path is always set in FilePath objects returned by
  expandFileInfo().
* Factor out the loop body of the final stage of file info expansion
  into readFileInfo(). Retain filePath, do not unset it.
* Assert that $fileInfo['content'] is definitely set.
* Convert array_map() in getDefinitionSummary() to a loop.
* Migrate LanguageDataModule.

Bug: T47514
Change-Id: I97d61b5793159cea365740e0563f7b733e0f16de
2023-05-09 11:47:40 +10:00

103 lines
2.9 KiB
PHP

<?php
/**
* This PHP script defines the spec that the mediawiki.jqueryMsg module should conform to.
*
* It does this by looking up the results of various kinds of string parsing, with various
* languages, in the current installation of MediaWiki. It then outputs a static specification,
* mapping expected inputs to outputs, which is used then run by QUnit.
*/
use MediaWiki\Languages\LanguageFactory;
use MediaWiki\MediaWikiServices;
require __DIR__ . '/../../../maintenance/Maintenance.php';
class GenerateJqueryMsgData extends Maintenance {
private static $keyToTestArgs = [
'undelete_short' => [
[ 0 ],
[ 1 ],
[ 2 ],
[ 5 ],
[ 21 ],
[ 101 ]
],
'category-subcat-count' => [
[ 0, 10 ],
[ 1, 1 ],
[ 1, 2 ],
[ 3, 30 ]
]
];
private static $testLangs = [ 'en', 'fr', 'ar', 'jp', 'zh', 'nl', 'ml', 'hi' ];
/** @var LanguageFactory */
private $languageFactory;
public function __construct() {
parent::__construct();
$this->addDescription( 'Create a specification for message parsing ini JSON format' );
// add any other options here
}
public function execute() {
$this->languageFactory = MediaWikiServices::getInstance()->getLanguageFactory();
$data = $this->getData();
$this->writeJsonFile( $data, __DIR__ . '/mediawiki.jqueryMsg.data.json' );
}
private function getData() {
$messages = [];
$tests = [];
$jsData = [];
foreach ( self::$testLangs as $languageCode ) {
$language = $this->languageFactory->getLanguage( $languageCode );
$jsData[$languageCode] = $language->getJsData();
foreach ( self::$keyToTestArgs as $key => $testArgs ) {
foreach ( $testArgs as $args ) {
// Get the raw message, without any transformations.
$template = wfMessage( $key )->useDatabase( false )
->inLanguage( $languageCode )->plain();
// Get the magic-parsed version with args.
$result = wfMessage( $key, ...$args )->useDatabase( false )
->inLanguage( $languageCode )->text();
// Record the template, args, language, and expected result
// fake multiple languages by flattening them together.
$langKey = $languageCode . '_' . $key;
$messages[$langKey] = $template;
$tests[] = [
'name' => $languageCode . ' ' . $key . ' ' . implode( ',', $args ),
'key' => $langKey,
'args' => $args,
'result' => $result,
'lang' => $languageCode
];
}
}
}
return [
'messages' => $messages,
'tests' => $tests,
'jsData' => $jsData,
];
}
private function writeJsonFile( array $data, $dataSpecFile ) {
$phpParserData = [
'@' => 'Last generated with ' . basename( __FILE__ ) . ' at ' . gmdate( 'r' ),
] + $data;
$output = FormatJson::encode( $phpParserData, true ) . "\n";
$fp = file_put_contents( $dataSpecFile, $output );
if ( $fp === false ) {
die( "Couldn't write to $dataSpecFile." );
}
}
}
$maintClass = GenerateJqueryMsgData::class;
require_once RUN_MAINTENANCE_IF_MAIN;