r59991 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r59990‎ | r59991 | r59992 >
Date:01:29, 12 December 2009
Author:dale
Status:deferred
Tags:
Comment:
* some comment updates
Modified paths:
  • /branches/js2-work/phase3/js2/mwEmbed/jsScriptLoader.php (modified) (history)
  • /branches/js2-work/phase3/js2/mwEmbed/mwEmbed.js (modified) (history)

Diff [purge]

Index: branches/js2-work/phase3/js2/mwEmbed/mwEmbed.js
@@ -1381,6 +1381,7 @@
13821382 mwSetupFlag = true;
13831383
13841384 mw.log( 'mw:setupMwEmbed' );
 1385+
13851386 // Make sure jQuery is loaded:
13861387 $.load( 'window.jQuery', function(){
13871388 if ( !window['$j'] ) {
@@ -1421,7 +1422,7 @@
14221423 mw.log('ran loader' );
14231424 }
14241425
1425 - //Flag to register the domReady has been called
 1426+ // Flag to register the domReady has been called
14261427 var mwDomReadyFlag = false;
14271428
14281429 /**
@@ -1743,9 +1744,10 @@
17441745 ||
17451746 (
17461747 ( src.indexOf( 'mwScriptLoader.php' ) !== -1 || src.indexOf( 'jsScriptLoader.php' ) !== -1 )
1747 - && src.indexOf( 'mwEmbed' ) !== -1 )
 1748+ &&
 1749+ src.indexOf( 'mwEmbed' ) !== -1
17481750 )
1749 - {
 1751+ ){
17501752 mwEmbedSrc = src;
17511753 return mwEmbedSrc;
17521754 }
Index: branches/js2-work/phase3/js2/mwEmbed/jsScriptLoader.php
@@ -31,6 +31,11 @@
3232 var $jsvarurl = false;
3333 var $doProcReqFlag = true;
3434
 35+ /**
 36+ * Output the javascript from cache
 37+ *
 38+ * @return {Boolean} true on success, false on failure
 39+ */
3540 function outputFromCache(){
3641 // Process the request
3742 $this->requestKey = $this->preProcRequestVars();
@@ -45,6 +50,15 @@
4651 return false;
4752 }
4853
 54+ /**
 55+ * Core scriptLoader driver:
 56+ * get request key
 57+ * builds javascript string
 58+ * optionally gzips the output
 59+ * checks for errors
 60+ * sends the headers
 61+ * outputs js
 62+ */
4963 function doScriptLoader() {
5064 global $wgJSAutoloadClasses, $wgJSAutoloadLocalClasses, $IP,
5165 $wgEnableScriptMinify, $wgUseFileCache, $wgExtensionMessagesFiles;
@@ -95,7 +109,14 @@
96110 $this->outputJsWithHeaders();
97111 }
98112 }
99 - function getScriptText($classKey, $file_name=''){
 113+ /**
 114+ * Gets Script Text
 115+ *
 116+ * @param {String} $classKey Class Key to grab text for
 117+ * @param {String} [$file_name] Optional file path to get js text
 118+ * @return unknown
 119+ */
 120+ function getScriptText( $classKey, $file_name = '' ){
100121 $jsout = '';
101122 // Special case: title classes
102123 if ( substr( $classKey, 0, 3 ) == 'WT:' ) {
@@ -170,6 +191,9 @@
171192 $this->error_msg .= "\nUnknown error\n";
172193 return false;
173194 }
 195+ /**
 196+ * Outputs the script headers
 197+ */
174198 function outputJsHeaders() {
175199 // Output JS MIME type:
176200 header( 'Content-Type: text/javascript' );
@@ -184,6 +208,9 @@
185209 }
186210 }
187211
 212+ /**
 213+ * Outputs the javascript text with script headers
 214+ */
188215 function outputJsWithHeaders() {
189216 global $wgUseGzip;
190217 $this->outputJsHeaders();
@@ -374,6 +401,13 @@
375402
376403 return $rKey;
377404 }
 405+ /**
 406+ * Check for the commons language hack.
 407+ * ( someone had the bright idea to use language keys as message
 408+ * name-spaces for separate upload forms )
 409+ *
 410+ * @param {String} $langKey The lang key for the form
 411+ */
378412 public static function checkForCommonsLanguageFormHack( $langKey){
379413 $formNames = array( 'ownwork', 'fromflickr', 'fromwikimedia', 'fromgov');
380414 foreach($formNames as $formName){
@@ -389,6 +423,12 @@
390424 //else just return the key unchanged:
391425 return $langKey;
392426 }
 427+ /**
 428+ * Get a file path for a given class
 429+ *
 430+ * @param {String} $reqClass Class key to get the path for
 431+ * @return path of the class or "false"
 432+ */
393433 public static function getJsPathFromClass( $reqClass ){
394434 global $wgJSAutoloadLocalClasses, $wgJSAutoloadClasses;
395435 if ( isset( $wgJSAutoloadLocalClasses[$reqClass] ) ) {
@@ -399,8 +439,16 @@
400440 return false;
401441 }
402442 }
 443+
 444+ /**
 445+ * Retrieve the js file into a string, updates error_msg if not retrivable.
 446+ *
 447+ * @param {String} $file_path File to get
 448+ * @return {String} of the file contents
 449+ */
403450 function doGetJsFile( $file_path ) {
404451 global $IP;
 452+
405453 // Load the file
406454 $str = @file_get_contents( "{$IP}/{$file_path}" );
407455
@@ -411,6 +459,16 @@
412460 }
413461 return $str;
414462 }
 463+
 464+ /**
 465+ * Process the javascript string
 466+ *
 467+ * Strips debug statements: mw.log( 'msg' );
 468+ * Localizes the javascript calling the languageMsgReplace function
 469+ *
 470+ * @param {String} $str Javascript string to be processed.
 471+ * @return processed javascript string
 472+ */
415473 function doProcessJs( $str ){
416474 global $wgEnableScriptLocalization;
417475 // Strip out js_log debug lines (if not in debug mode)
@@ -419,7 +477,7 @@
420478
421479 // Do language swap by index:
422480 if ( $wgEnableScriptLocalization ){
423 - $inx = self::getLoadGmIndex( $str );
 481+ $inx = self::getAddMessagesIndex( $str );
424482 if($inx){
425483 $translated = $this->languageMsgReplace( substr($str, $inx['s'], ($inx['e']-$inx['s']) ));
426484 //return the final string (without double {})
@@ -429,7 +487,13 @@
430488 //return the js str unmodified if we did not transform with the localisation.
431489 return $str;
432490 }
433 - static public function getLoadGmIndex( $str ){
 491+ /**
 492+ * Get the addMesseges index ( for replacing msg text with localized json )
 493+ *
 494+ * @param {String} $str Javascript string to grab msg text from
 495+ * @return {Array} Array with start and end points character indexes
 496+ */
 497+ static public function getAddMessagesIndex( $str ){
434498 $returnIndex = array();
435499 preg_match('/mw.addMessages\s*\(\s*\{/', $str, $matches, PREG_OFFSET_CAPTURE );
436500 if( count($matches) == 0){
@@ -449,7 +513,7 @@
450514 if ( $ignorenext ) {
451515 $ignorenext = false;
452516 } else {
453 - //search for a close } that is not in quotes or escaped
 517+ // Search for a close } that is not in quotes or escaped
454518 switch( $char ) {
455519 case '"':
456520 $inquote = !$inquote;
@@ -467,28 +531,46 @@
468532 }
469533 }
470534 }
471 -
 535+ /**
 536+ * Generates an in-line addMessege call for page output.
 537+ * For use with OutputPage when the script-loader is disabled.
 538+ *
 539+ * @param {String} $class Name of class to get inin-lineline messages for.
 540+ * @return in-line msg javascript text or empty string if no msgs need to be localised.
 541+ */
472542 function getInlineMsgFromClass( $class ){
473543 $jsmsg = $this->getMsgKeysFromClass( $class );
474544 if( $jsmsg ){
475 - self::getMsgKeys ( $jsmsg );
 545+ self::updateMsgKeys ( $jsmsg );
476546 return 'mw.addMessages(' . FormatJson::encode( $jsmsg ) . ');';
477547 }else{
478548 //if could not parse return empty string:
479549 return '';
480550 }
481551 }
 552+ /**
 553+ * Get the set of message associated with a given javascript class
 554+ *
 555+ * @param {String} $class Class to restive msgs from
 556+ * @return {Array} decoded json array of message key value pairs
 557+ */
482558 function getMsgKeysFromClass( $class ){
483559 $file_path = self::getJsPathFromClass( $class );
484560 $str = $this->getScriptText($class, $file_path);
485561
486 - $inx = self::getLoadGmIndex( $str );
 562+ $inx = self::getAddMessagesIndex( $str );
487563 if(!$inx)
488 - return '';
 564+ return false;
 565+
489566 return FormatJson::decode( '{' . substr($str, $inx['s'], ($inx['e']-$inx['s'])) . '}', true);
490567 }
491 -
492 - static public function getMsgKeys(& $jmsg, $langCode = false){
 568+ /**
 569+ * Updates an array of messages with the wfMsgGetKey value
 570+ *
 571+ * @param {Array} $jmsg Associative array of message key -> message value pairs
 572+ * @param {String} $langCode Language code override
 573+ */
 574+ static public function updateMsgKeys(& $jmsg, $langCode = false){
493575 global $wgContLanguageCode;
494576 // Check the langCode
495577 if(!$langCode)
@@ -499,6 +581,12 @@
500582 $jmsg[ $msgKey ] = wfMsgGetKey( $msgKey, true, $langCode, false );
501583 }
502584 }
 585+ /**
 586+ * Replace a string of json msgs with the translated json msgs.
 587+ *
 588+ * @param {String} $json_str Json string to be replaced
 589+ * @return {String} of msgs updated with the given language code
 590+ */
503591 function languageMsgReplace( $json_str ) {
504592 $jmsg = FormatJson::decode( '{' . $json_str . '}', true );
505593 // Do the language lookup
@@ -506,7 +594,7 @@
507595
508596 // See if any msgKey has the PLURAL template tag
509597 //package in PLURAL mapping
510 - self::getMsgKeys( $jmsg, $this->langCode );
 598+ self::updateMsgKeys( $jmsg, $this->langCode );
511599
512600 // Return the updated loadGM JSON with updated msgs:
513601 return FormatJson::encode( $jmsg );
@@ -523,17 +611,29 @@
524612 }
525613 }
526614
527 -// A simple version of HTMLFileCache (@@todo abstract shared pieces)
 615+/*
 616+ * A simple version of HTMLFileCache so that the scriptLoader can operate stand alone
 617+ */
528618 class simpleFileCache {
529619 var $mFileCache;
530620 var $filename = null;
531 - var $rKey = null;
 621+ var $requestKey = null;
532622
533 - public function __construct( &$rKey ) {
534 - $this->requestKey = $rKey;
 623+ /**
 624+ * Constructor
 625+ *
 626+ * @param {String} $requestKey Request key for unique identifying this cache file
 627+ */
 628+ public function __construct( $requestKey ) {
 629+ $this->requestKey = $requestKey;
535630 $this->getCacheFileName();
536631 }
537632
 633+ /**
 634+ * get Cache file file Name based on $requestKey and if gzip is enabled or not
 635+ *
 636+ * @return unknown
 637+ */
538638 public function getCacheFileName() {
539639 global $wgUseGzip, $wgScriptCacheDirectory;
540640
@@ -547,15 +647,16 @@
548648
549649 // Check for defined files::
550650 if( is_file( $this->filename ) )
551 - return $this->filename;
 651+ return $this->filename;
552652
553 - if( is_file( $this->filename .'.gz') ){
554 - $this->filename.='.gz';
 653+ if( is_file( $this->filename . '.gz') ){
 654+ $this->filename .= '.gz';
555655 return $this->filename;
556656 }
557657 //check the update the name based on the $wgUseGzip config var
558658 if ( isset($wgUseGzip) && $wgUseGzip )
559 - $this->filename.='.gz';
 659+ $this->filename.='.gz';
 660+
560661 }
561662
562663 public function isFileCached() {

Status & tagging log