MediaWiki  REL1_20
makeJqueryMsgSpec.php
Go to the documentation of this file.
00001 <?php
00002 
00017 $maintenanceDir = dirname( dirname( dirname( __DIR__ ) ) ) . '/maintenance';
00018 
00019 require( "$maintenanceDir/Maintenance.php" );
00020 
00021 class MakeLanguageSpec extends Maintenance {
00022 
00023         static $keyToTestArgs = array(
00024                 'undelete_short' => array( 
00025                         array( 0 ), 
00026                         array( 1 ), 
00027                         array( 2 ), 
00028                         array( 5 ), 
00029                         array( 21 ), 
00030                         array( 101 ) 
00031                 ),
00032                 'category-subcat-count' => array(  
00033                         array( 0, 10 ), 
00034                         array( 1, 1 ), 
00035                         array( 1, 2 ), 
00036                         array( 3, 30 ) 
00037                 )
00038         );
00039 
00040         public function __construct() {
00041                 parent::__construct();
00042                 $this->mDescription = "Create a JasmineBDD-compatible specification for message parsing";
00043                 // add any other options here
00044         }
00045 
00046         public function execute() {
00047                 list( $messages, $tests ) = $this->getMessagesAndTests();
00048                 $this->writeJavascriptFile( $messages, $tests, "spec/mediawiki.language.parser.spec.data.js" );
00049         }
00050 
00051         private function getMessagesAndTests() {
00052                 $messages = array();
00053                 $tests = array();
00054                 foreach ( array( 'en', 'fr', 'ar', 'jp', 'zh' ) as $languageCode ) {
00055                         foreach ( self::$keyToTestArgs as $key => $testArgs ) {
00056                                 foreach ($testArgs as $args) {
00057                                         // get the raw template, without any transformations
00058                                         $template = wfMessage( $key )->inLanguage( $languageCode )->plain();
00059 
00060                                         $result = wfMessage( $key, $args )->inLanguage( $languageCode )->text();
00061 
00062                                         // record the template, args, language, and expected result
00063                                         // fake multiple languages by flattening them together
00064                                         $langKey = $languageCode . '_' . $key;
00065                                         $messages[ $langKey ] = $template;
00066                                         $tests[] = array(
00067                                                 'name' => $languageCode . " " . $key . " " . join( ",", $args ),
00068                                                 'key' => $langKey,
00069                                                 'args' => $args,
00070                                                 'result' => $result,
00071                                                 'lang' => $languageCode
00072                                         );
00073                                 }
00074                         }
00075                 }
00076                 return array( $messages, $tests );
00077         }
00078 
00079         private function writeJavascriptFile( $messages, $tests, $dataSpecFile ) {
00080                 global $argv;
00081                 $arguments = count($argv) ? $argv : $_SERVER[ 'argv' ];
00082 
00083                 $json = new Services_JSON;
00084                 $json->pretty = true;
00085                 $javascriptPrologue = "// This file stores the results from the PHP parser for certain messages and arguments,\n"
00086                                       . "// so we can test the equivalent Javascript libraries.\n"
00087                                       . '// Last generated with ' . join(' ', $arguments) . ' at ' . gmdate('c') . "\n\n";
00088                 $javascriptMessages = "mediaWiki.messages.set( " . $json->encode( $messages, true ) . " );\n";
00089                 $javascriptTests = 'var jasmineMsgSpec = ' . $json->encode( $tests, true ) . ";\n";
00090 
00091                 $fp = fopen( $dataSpecFile, 'w' );
00092                 if ( !$fp ) {
00093                         die( "couldn't open $dataSpecFile for writing" );
00094                 }
00095                 $success = fwrite( $fp, $javascriptPrologue . $javascriptMessages . $javascriptTests );
00096                 if ( !$success ) { 
00097                         die( "couldn't write to $dataSpecFile" );
00098                 }
00099                 $success = fclose( $fp );
00100                 if ( !$success ) {
00101                         die( "couldn't close $dataSpecFile" );
00102                 }
00103         }
00104 }
00105 
00106 $maintClass = "MakeLanguageSpec";
00107 require_once( "$maintenanceDir/doMaintenance.php" );
00108 
00109 
00110