MediaWiki  REL1_22
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 require __DIR__ . '/../../../maintenance/Maintenance.php';
00065 
00066 class GenerateJqueryMsgData extends Maintenance {
00067 
00068     static $keyToTestArgs = array(
00069         'undelete_short' => array(
00070             array( 0 ),
00071             array( 1 ),
00072             array( 2 ),
00073             array( 5 ),
00074             array( 21 ),
00075             array( 101 )
00076         ),
00077         'category-subcat-count' => array(
00078             array( 0, 10 ),
00079             array( 1, 1 ),
00080             array( 1, 2 ),
00081             array( 3, 30 )
00082         )
00083     );
00084 
00085     public function __construct() {
00086         parent::__construct();
00087         $this->mDescription = 'Create a specification for message parsing ini JSON format';
00088         // add any other options here
00089     }
00090 
00091     public function execute() {
00092         list( $messages, $tests ) = $this->getMessagesAndTests();
00093         $this->writeJavascriptFile( $messages, $tests, __DIR__ . '/mediawiki.jqueryMsg.data.js' );
00094     }
00095 
00096     private function getMessagesAndTests() {
00097         $messages = array();
00098         $tests = array();
00099         foreach ( array( 'en', 'fr', 'ar', 'jp', 'zh' ) as $languageCode ) {
00100             foreach ( self::$keyToTestArgs as $key => $testArgs ) {
00101                 foreach ( $testArgs as $args ) {
00102                     // Get the raw message, without any transformations.
00103                     $template = wfMessage( $key )->inLanguage( $languageCode )->plain();
00104 
00105                     // Get the magic-parsed version with args.
00106                     $result = wfMessage( $key, $args )->inLanguage( $languageCode )->text();
00107 
00108                     // Record the template, args, language, and expected result
00109                     // fake multiple languages by flattening them together.
00110                     $langKey = $languageCode . '_' . $key;
00111                     $messages[$langKey] = $template;
00112                     $tests[] = array(
00113                         'name' => $languageCode . ' ' . $key . ' ' . join( ',', $args ),
00114                         'key' => $langKey,
00115                         'args' => $args,
00116                         'result' => $result,
00117                         'lang' => $languageCode
00118                     );
00119                 }
00120             }
00121         }
00122         return array( $messages, $tests );
00123     }
00124 
00125     private function writeJavascriptFile( $messages, $tests, $dataSpecFile ) {
00126         $phpParserData = array(
00127             'messages' => $messages,
00128             'tests' => $tests,
00129         );
00130 
00131         $output =
00132             "// This file stores the output from the PHP parser for various messages, arguments,\n"
00133                 . "// languages, and parser modes. Intended for use by a unit test framework by looping\n"
00134                 . "// through the object and comparing its parser return value with the 'result' property.\n"
00135                 . '// Last generated with ' . basename( __FILE__ ) . ' at ' . gmdate( 'r' ) . "\n"
00136                 // This file will contain unquoted JSON strings as javascript native object literals,
00137                 // flip the quotemark convention for this file.
00138                 . "/*jshint quotmark: double */\n"
00139                 . "\n"
00140                 . 'mediaWiki.libs.phpParserData = ' . FormatJson::encode( $phpParserData, true ) . ";\n";
00141 
00142         $fp = file_put_contents( $dataSpecFile, $output );
00143         if ( $fp === false ) {
00144             die( "Couldn't write to $dataSpecFile." );
00145         }
00146     }
00147 }
00148 
00149 $maintClass = "GenerateJqueryMsgData";
00150 require_once RUN_MAINTENANCE_IF_MAIN;