MediaWiki  master
generateJqueryMsgData.php
Go to the documentation of this file.
00001 <?php
00016 /*
00017  * @example QUnit
00018  * <code>
00019         QUnit.test( 'Output matches PHP parser', mw.libs.phpParserData.tests.length, function ( assert ) {
00020                 mw.messages.set( mw.libs.phpParserData.messages );
00021                 $.each( mw.libs.phpParserData.tests, function ( i, test ) {
00022                         QUnit.stop();
00023                         getMwLanguage( test.lang, function ( langClass ) {
00024                                 var parser = new mw.jqueryMsg.parser( { language: langClass } );
00025                                 assert.equal(
00026                                         parser.parse( test.key, test.args ).html(),
00027                                         test.result,
00028                                         test.name
00029                                 );
00030                                 QUnit.start();
00031                         } );
00032                 } );
00033         });
00034  * </code>
00035  *
00036  * @example Jasmine
00037  * <code>
00038         describe( 'match output to output from PHP parser', function () {
00039                 mw.messages.set( mw.libs.phpParserData.messages );
00040                 $.each( mw.libs.phpParserData.tests, function ( i, test ) {
00041                         it( 'should parse ' + test.name, function () {
00042                                 var langClass;
00043                                 runs( function () {
00044                                         getMwLanguage( test.lang, function ( gotIt ) {
00045                                                 langClass = gotIt;
00046                                         });
00047                                 });
00048                                 waitsFor( function () {
00049                                         return langClass !== undefined;
00050                                 }, 'Language class should be loaded', 1000 );
00051                                 runs( function () {
00052                                         console.log( test.lang, 'running tests' );
00053                                         var parser = new mw.jqueryMsg.parser( { language: langClass } );
00054                                         expect(
00055                                                 parser.parse( test.key, test.args ).html()
00056                                         ).toEqual( test.result );
00057                                 } );
00058                         } );
00059                 } );
00060         } );
00061  * </code>
00062  */
00063 
00064 $maintenanceDir = dirname( dirname( dirname( __DIR__ ) ) ) . '/maintenance';
00065 
00066 require( "$maintenanceDir/Maintenance.php" );
00067 
00068 class GenerateJqueryMsgData extends Maintenance {
00069 
00070         static $keyToTestArgs = array(
00071                 'undelete_short' => array(
00072                         array( 0 ),
00073                         array( 1 ),
00074                         array( 2 ),
00075                         array( 5 ),
00076                         array( 21 ),
00077                         array( 101 )
00078                 ),
00079                 'category-subcat-count' => array(
00080                         array( 0, 10 ),
00081                         array( 1, 1 ),
00082                         array( 1, 2 ),
00083                         array( 3, 30 )
00084                 )
00085         );
00086 
00087         public function __construct() {
00088                         parent::__construct();
00089                         $this->mDescription = 'Create a specification for message parsing ini JSON format';
00090                         // add any other options here
00091         }
00092 
00093         public function execute() {
00094                 list( $messages, $tests ) = $this->getMessagesAndTests();
00095                 $this->writeJavascriptFile( $messages, $tests, __DIR__ . '/mediawiki.jqueryMsg.data.js' );
00096         }
00097 
00098         private function getMessagesAndTests() {
00099                 $messages = array();
00100                 $tests = array();
00101                 foreach ( array( 'en', 'fr', 'ar', 'jp', 'zh' ) as $languageCode ) {
00102                         foreach ( self::$keyToTestArgs as $key => $testArgs ) {
00103                                 foreach ($testArgs as $args) {
00104                                         // Get the raw message, without any transformations.
00105                                         $template = wfMessage( $key )->inLanguage( $languageCode )->plain();
00106 
00107                                         // Get the magic-parsed version with args.
00108                                         $result = wfMessage( $key, $args )->inLanguage( $languageCode )->text();
00109 
00110                                         // Record the template, args, language, and expected result
00111                                         // fake multiple languages by flattening them together.
00112                                         $langKey = $languageCode . '_' . $key;
00113                                         $messages[$langKey] = $template;
00114                                         $tests[] = array(
00115                                                 'name' => $languageCode . ' ' . $key . ' ' . join( ',', $args ),
00116                                                 'key' => $langKey,
00117                                                 'args' => $args,
00118                                                 'result' => $result,
00119                                                 'lang' => $languageCode
00120                                         );
00121                                 }
00122                         }
00123                 }
00124                 return array( $messages, $tests );
00125         }
00126 
00127         private function writeJavascriptFile( $messages, $tests, $dataSpecFile ) {
00128                 $phpParserData = array(
00129                         'messages' => $messages,
00130                         'tests' => $tests,
00131                 );
00132 
00133                 $output =
00134                         "// This file stores the output from the PHP parser for various messages, arguments,\n"
00135                         . "// languages, and parser modes. Intended for use by a unit test framework by looping\n"
00136                         . "// through the object and comparing its parser return value with the 'result' property.\n"
00137                         . '// Last generated with ' . basename( __FILE__ ) . ' at ' . gmdate( 'r' ) . "\n"
00138                         // This file will contain unquoted JSON strings as javascript native object literals,
00139                         // flip the quotemark convention for this file.
00140                         . "/*jshint quotmark: double */\n"
00141                         . "\n"
00142                         . 'mediaWiki.libs.phpParserData = ' . FormatJson::encode( $phpParserData, true ) . ";\n";
00143 
00144                 $fp = file_put_contents( $dataSpecFile, $output );
00145                 if ( $fp === false ) {
00146                         die( "Couldn't write to $dataSpecFile." );
00147                 }
00148         }
00149 }
00150 
00151 $maintClass = "GenerateJqueryMsgData";
00152 require_once( "$maintenanceDir/doMaintenance.php" );