MediaWiki  REL1_19
makeJqueryMsgSpec.php
Go to the documentation of this file.
00001 <?php
00002 
00017 $maintenanceDir = dirname( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) ) . '/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                 $wfMsgExtOptions = array( 'parsemag' );
00055                 foreach ( array( 'en', 'fr', 'ar', 'jp', 'zh' ) as $languageCode ) {
00056                         $wfMsgExtOptions['language'] = $languageCode;
00057                         foreach ( self::$keyToTestArgs as $key => $testArgs ) {
00058                                 foreach ($testArgs as $args) {
00059                                         // get the raw template, without any transformations
00060                                         $template = wfMsgGetKey( $key, /* useDb */ true, $languageCode, /* transform */ false );
00061 
00062                                         // get the magic-parsed version with args
00063                                         $wfMsgExtArgs = array_merge( array( $key, $wfMsgExtOptions ), $args );
00064                                         $result = call_user_func_array( 'wfMsgExt', $wfMsgExtArgs ); 
00065 
00066                                         // record the template, args, language, and expected result
00067                                         // fake multiple languages by flattening them together  
00068                                         $langKey = $languageCode . '_' . $key;
00069                                         $messages[ $langKey ] = $template;
00070                                         $tests[] = array( 
00071                                                 'name' => $languageCode . " " . $key . " " . join( ",", $args ),
00072                                                 'key' => $langKey,
00073                                                 'args' => $args, 
00074                                                 'result' => $result,
00075                                                 'lang' => $languageCode
00076                                         );
00077                                 }
00078                         }
00079                 }
00080                 return array( $messages, $tests );
00081         }
00082 
00083         private function writeJavascriptFile( $messages, $tests, $dataSpecFile ) {
00084                 global $argv;
00085                 $arguments = count($argv) ? $argv : $_SERVER[ 'argv' ];
00086 
00087                 $json = new Services_JSON;
00088                 $json->pretty = true;
00089                 $javascriptPrologue = "// This file stores the results from the PHP parser for certain messages and arguments,\n"
00090                                       . "// so we can test the equivalent Javascript libraries.\n"
00091                                       . '// Last generated with ' . join(' ', $arguments) . ' at ' . gmdate('c') . "\n\n";
00092                 $javascriptMessages = "mediaWiki.messages.set( " . $json->encode( $messages, true ) . " );\n";
00093                 $javascriptTests = 'var jasmineMsgSpec = ' . $json->encode( $tests, true ) . ";\n";
00094 
00095                 $fp = fopen( $dataSpecFile, 'w' );
00096                 if ( !$fp ) {
00097                         die( "couldn't open $dataSpecFile for writing" );
00098                 }
00099                 $success = fwrite( $fp, $javascriptPrologue . $javascriptMessages . $javascriptTests );
00100                 if ( !$success ) { 
00101                         die( "couldn't write to $dataSpecFile" );
00102                 }
00103                 $success = fclose( $fp );
00104                 if ( !$success ) {
00105                         die( "couldn't close $dataSpecFile" );
00106                 }
00107         }
00108 }
00109 
00110 $maintClass = "MakeLanguageSpec";
00111 require_once( "$maintenanceDir/doMaintenance.php" );
00112 
00113 
00114