4 * This PHP script defines the spec that the Javascript message parser should conform to.
6 * It does this by looking up the results of various string kinds of string parsing, with various languages,
7 * in the current installation of MediaWiki. It then outputs a static specification, mapping expected inputs to outputs,
8 * which can be used with the JasmineBDD framework. This specification can then be used by simply including it into
9 * the SpecRunner.html file.
11 * This is similar to Michael Dale (mdale@mediawiki.org)'s parser tests, except that it doesn't look up the
12 * API results while doing the test, so the Jasmine run is much faster(at the cost of being out of date in rare
13 * circumstances. But mostly the parsing that we are doing in Javascript doesn't change much.)
17 $maintenanceDir = dirname( dirname( dirname( __DIR__
) ) ) . '/maintenance';
19 require( "$maintenanceDir/Maintenance.php" );
21 class MakeLanguageSpec
extends Maintenance
{
23 static $keyToTestArgs = array(
24 'undelete_short' => array(
32 'category-subcat-count' => array(
40 public function __construct() {
41 parent
::__construct();
42 $this->mDescription
= "Create a JasmineBDD-compatible specification for message parsing";
43 // add any other options here
46 public function execute() {
47 list( $messages, $tests ) = $this->getMessagesAndTests();
48 $this->writeJavascriptFile( $messages, $tests, "spec/mediawiki.language.parser.spec.data.js" );
51 private function getMessagesAndTests() {
54 foreach ( array( 'en', 'fr', 'ar', 'jp', 'zh' ) as $languageCode ) {
55 foreach ( self
::$keyToTestArgs as $key => $testArgs ) {
56 foreach ($testArgs as $args) {
57 // get the raw template, without any transformations
58 $template = wfMessage( $key )->inLanguage( $languageCode )->plain();
60 $result = wfMessage( $key, $args )->inLanguage( $languageCode )->text();
62 // record the template, args, language, and expected result
63 // fake multiple languages by flattening them together
64 $langKey = $languageCode . '_' . $key;
65 $messages[ $langKey ] = $template;
67 'name' => $languageCode . " " . $key . " " . join( ",", $args ),
71 'lang' => $languageCode
76 return array( $messages, $tests );
79 private function writeJavascriptFile( $messages, $tests, $dataSpecFile ) {
81 $arguments = count($argv) ?
$argv : $_SERVER[ 'argv' ];
83 $json = new Services_JSON
;
85 $javascriptPrologue = "// This file stores the results from the PHP parser for certain messages and arguments,\n"
86 . "// so we can test the equivalent Javascript libraries.\n"
87 . '// Last generated with ' . join(' ', $arguments) . ' at ' . gmdate('c') . "\n\n";
88 $javascriptMessages = "mediaWiki.messages.set( " . $json->encode( $messages, true ) . " );\n";
89 $javascriptTests = 'var jasmineMsgSpec = ' . $json->encode( $tests, true ) . ";\n";
91 $fp = fopen( $dataSpecFile, 'w' );
93 die( "couldn't open $dataSpecFile for writing" );
95 $success = fwrite( $fp, $javascriptPrologue . $javascriptMessages . $javascriptTests );
97 die( "couldn't write to $dataSpecFile" );
99 $success = fclose( $fp );
101 die( "couldn't close $dataSpecFile" );
106 $maintClass = "MakeLanguageSpec";
107 require_once( "$maintenanceDir/doMaintenance.php" );