2012-09-12 07:57:31 +00:00
|
|
|
<?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,
|
2019-10-04 18:49:07 +00:00
|
|
|
* mapping expected inputs to outputs, which is used then run by QUnit.
|
2012-09-12 07:57:31 +00:00
|
|
|
*/
|
|
|
|
|
|
2013-05-17 00:16:59 +00:00
|
|
|
require __DIR__ . '/../../../maintenance/Maintenance.php';
|
2012-09-12 07:57:31 +00:00
|
|
|
|
|
|
|
|
class GenerateJqueryMsgData extends Maintenance {
|
|
|
|
|
|
2019-10-04 18:49:07 +00:00
|
|
|
private static $keyToTestArgs = [
|
2016-02-17 09:09:32 +00:00
|
|
|
'undelete_short' => [
|
|
|
|
|
[ 0 ],
|
|
|
|
|
[ 1 ],
|
|
|
|
|
[ 2 ],
|
|
|
|
|
[ 5 ],
|
|
|
|
|
[ 21 ],
|
|
|
|
|
[ 101 ]
|
|
|
|
|
],
|
|
|
|
|
'category-subcat-count' => [
|
|
|
|
|
[ 0, 10 ],
|
|
|
|
|
[ 1, 1 ],
|
|
|
|
|
[ 1, 2 ],
|
|
|
|
|
[ 3, 30 ]
|
|
|
|
|
]
|
|
|
|
|
];
|
2012-09-12 07:57:31 +00:00
|
|
|
|
2019-10-04 18:49:07 +00:00
|
|
|
private static $testLangs = [ 'en', 'fr', 'ar', 'jp', 'zh', 'nl', 'ml', 'hi' ];
|
|
|
|
|
|
2012-09-12 07:57:31 +00:00
|
|
|
public function __construct() {
|
2013-02-15 10:35:55 +00:00
|
|
|
parent::__construct();
|
2019-05-18 13:13:46 +00:00
|
|
|
$this->addDescription( 'Create a specification for message parsing ini JSON format' );
|
2013-02-15 10:35:55 +00:00
|
|
|
// add any other options here
|
2012-09-12 07:57:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function execute() {
|
2019-10-04 18:49:07 +00:00
|
|
|
$data = $this->getData();
|
|
|
|
|
$this->writeJsonFile( $data, __DIR__ . '/mediawiki.jqueryMsg.data.json' );
|
2012-09-12 07:57:31 +00:00
|
|
|
}
|
|
|
|
|
|
2019-10-04 18:49:07 +00:00
|
|
|
private function getData() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$messages = [];
|
|
|
|
|
$tests = [];
|
2019-10-04 18:49:07 +00:00
|
|
|
$jsData = [];
|
|
|
|
|
foreach ( self::$testLangs as $languageCode ) {
|
|
|
|
|
$jsData[$languageCode] = ResourceLoaderLanguageDataModule::getData( $languageCode );
|
2012-09-12 07:57:31 +00:00
|
|
|
foreach ( self::$keyToTestArgs as $key => $testArgs ) {
|
2013-02-15 10:35:55 +00:00
|
|
|
foreach ( $testArgs as $args ) {
|
2012-09-12 07:57:31 +00:00
|
|
|
// Get the raw message, without any transformations.
|
2019-10-04 18:49:07 +00:00
|
|
|
$template = wfMessage( $key )->useDatabase( false )
|
|
|
|
|
->inLanguage( $languageCode )->plain();
|
2012-09-12 07:57:31 +00:00
|
|
|
|
|
|
|
|
// Get the magic-parsed version with args.
|
2019-10-04 18:49:07 +00:00
|
|
|
$result = wfMessage( $key, ...$args )->useDatabase( false )
|
|
|
|
|
->inLanguage( $languageCode )->text();
|
2012-09-12 07:57:31 +00:00
|
|
|
|
|
|
|
|
// Record the template, args, language, and expected result
|
|
|
|
|
// fake multiple languages by flattening them together.
|
|
|
|
|
$langKey = $languageCode . '_' . $key;
|
|
|
|
|
$messages[$langKey] = $template;
|
2016-02-17 09:09:32 +00:00
|
|
|
$tests[] = [
|
2016-03-08 08:13:12 +00:00
|
|
|
'name' => $languageCode . ' ' . $key . ' ' . implode( ',', $args ),
|
2012-09-12 07:57:31 +00:00
|
|
|
'key' => $langKey,
|
|
|
|
|
'args' => $args,
|
|
|
|
|
'result' => $result,
|
|
|
|
|
'lang' => $languageCode
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2012-09-12 07:57:31 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-04 18:49:07 +00:00
|
|
|
return [
|
2012-09-12 07:57:31 +00:00
|
|
|
'messages' => $messages,
|
|
|
|
|
'tests' => $tests,
|
2019-10-04 18:49:07 +00:00
|
|
|
'jsData' => $jsData,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2019-10-04 18:49:07 +00:00
|
|
|
}
|
2012-09-12 07:57:31 +00:00
|
|
|
|
2019-10-04 18:49:07 +00:00
|
|
|
private function writeJsonFile( array $data, $dataSpecFile ) {
|
|
|
|
|
$phpParserData = [
|
|
|
|
|
'@' => 'Last generated with ' . basename( __FILE__ ) . ' at ' . gmdate( 'r' ),
|
|
|
|
|
] + $data;
|
2012-09-12 07:57:31 +00:00
|
|
|
|
2019-10-04 18:49:07 +00:00
|
|
|
$output = FormatJson::encode( $phpParserData, true ) . "\n";
|
2012-09-12 07:57:31 +00:00
|
|
|
$fp = file_put_contents( $dataSpecFile, $output );
|
|
|
|
|
if ( $fp === false ) {
|
|
|
|
|
die( "Couldn't write to $dataSpecFile." );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 20:39:38 +00:00
|
|
|
$maintClass = GenerateJqueryMsgData::class;
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|