Index: branches/js2-work/phase3/js2/mwEmbed/mwEmbed.js |
— | — | @@ -1381,6 +1381,7 @@ |
1382 | 1382 | mwSetupFlag = true; |
1383 | 1383 | |
1384 | 1384 | mw.log( 'mw:setupMwEmbed' ); |
| 1385 | + |
1385 | 1386 | // Make sure jQuery is loaded: |
1386 | 1387 | $.load( 'window.jQuery', function(){ |
1387 | 1388 | if ( !window['$j'] ) { |
— | — | @@ -1421,7 +1422,7 @@ |
1422 | 1423 | mw.log('ran loader' ); |
1423 | 1424 | } |
1424 | 1425 | |
1425 | | - //Flag to register the domReady has been called |
| 1426 | + // Flag to register the domReady has been called |
1426 | 1427 | var mwDomReadyFlag = false; |
1427 | 1428 | |
1428 | 1429 | /** |
— | — | @@ -1743,9 +1744,10 @@ |
1744 | 1745 | || |
1745 | 1746 | ( |
1746 | 1747 | ( src.indexOf( 'mwScriptLoader.php' ) !== -1 || src.indexOf( 'jsScriptLoader.php' ) !== -1 ) |
1747 | | - && src.indexOf( 'mwEmbed' ) !== -1 ) |
| 1748 | + && |
| 1749 | + src.indexOf( 'mwEmbed' ) !== -1 |
1748 | 1750 | ) |
1749 | | - { |
| 1751 | + ){ |
1750 | 1752 | mwEmbedSrc = src; |
1751 | 1753 | return mwEmbedSrc; |
1752 | 1754 | } |
Index: branches/js2-work/phase3/js2/mwEmbed/jsScriptLoader.php |
— | — | @@ -31,6 +31,11 @@ |
32 | 32 | var $jsvarurl = false; |
33 | 33 | var $doProcReqFlag = true; |
34 | 34 | |
| 35 | + /** |
| 36 | + * Output the javascript from cache |
| 37 | + * |
| 38 | + * @return {Boolean} true on success, false on failure |
| 39 | + */ |
35 | 40 | function outputFromCache(){ |
36 | 41 | // Process the request |
37 | 42 | $this->requestKey = $this->preProcRequestVars(); |
— | — | @@ -45,6 +50,15 @@ |
46 | 51 | return false; |
47 | 52 | } |
48 | 53 | |
| 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 | + */ |
49 | 63 | function doScriptLoader() { |
50 | 64 | global $wgJSAutoloadClasses, $wgJSAutoloadLocalClasses, $IP, |
51 | 65 | $wgEnableScriptMinify, $wgUseFileCache, $wgExtensionMessagesFiles; |
— | — | @@ -95,7 +109,14 @@ |
96 | 110 | $this->outputJsWithHeaders(); |
97 | 111 | } |
98 | 112 | } |
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 = '' ){ |
100 | 121 | $jsout = ''; |
101 | 122 | // Special case: title classes |
102 | 123 | if ( substr( $classKey, 0, 3 ) == 'WT:' ) { |
— | — | @@ -170,6 +191,9 @@ |
171 | 192 | $this->error_msg .= "\nUnknown error\n"; |
172 | 193 | return false; |
173 | 194 | } |
| 195 | + /** |
| 196 | + * Outputs the script headers |
| 197 | + */ |
174 | 198 | function outputJsHeaders() { |
175 | 199 | // Output JS MIME type: |
176 | 200 | header( 'Content-Type: text/javascript' ); |
— | — | @@ -184,6 +208,9 @@ |
185 | 209 | } |
186 | 210 | } |
187 | 211 | |
| 212 | + /** |
| 213 | + * Outputs the javascript text with script headers |
| 214 | + */ |
188 | 215 | function outputJsWithHeaders() { |
189 | 216 | global $wgUseGzip; |
190 | 217 | $this->outputJsHeaders(); |
— | — | @@ -374,6 +401,13 @@ |
375 | 402 | |
376 | 403 | return $rKey; |
377 | 404 | } |
| 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 | + */ |
378 | 412 | public static function checkForCommonsLanguageFormHack( $langKey){ |
379 | 413 | $formNames = array( 'ownwork', 'fromflickr', 'fromwikimedia', 'fromgov'); |
380 | 414 | foreach($formNames as $formName){ |
— | — | @@ -389,6 +423,12 @@ |
390 | 424 | //else just return the key unchanged: |
391 | 425 | return $langKey; |
392 | 426 | } |
| 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 | + */ |
393 | 433 | public static function getJsPathFromClass( $reqClass ){ |
394 | 434 | global $wgJSAutoloadLocalClasses, $wgJSAutoloadClasses; |
395 | 435 | if ( isset( $wgJSAutoloadLocalClasses[$reqClass] ) ) { |
— | — | @@ -399,8 +439,16 @@ |
400 | 440 | return false; |
401 | 441 | } |
402 | 442 | } |
| 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 | + */ |
403 | 450 | function doGetJsFile( $file_path ) { |
404 | 451 | global $IP; |
| 452 | + |
405 | 453 | // Load the file |
406 | 454 | $str = @file_get_contents( "{$IP}/{$file_path}" ); |
407 | 455 | |
— | — | @@ -411,6 +459,16 @@ |
412 | 460 | } |
413 | 461 | return $str; |
414 | 462 | } |
| 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 | + */ |
415 | 473 | function doProcessJs( $str ){ |
416 | 474 | global $wgEnableScriptLocalization; |
417 | 475 | // Strip out js_log debug lines (if not in debug mode) |
— | — | @@ -419,7 +477,7 @@ |
420 | 478 | |
421 | 479 | // Do language swap by index: |
422 | 480 | if ( $wgEnableScriptLocalization ){ |
423 | | - $inx = self::getLoadGmIndex( $str ); |
| 481 | + $inx = self::getAddMessagesIndex( $str ); |
424 | 482 | if($inx){ |
425 | 483 | $translated = $this->languageMsgReplace( substr($str, $inx['s'], ($inx['e']-$inx['s']) )); |
426 | 484 | //return the final string (without double {}) |
— | — | @@ -429,7 +487,13 @@ |
430 | 488 | //return the js str unmodified if we did not transform with the localisation. |
431 | 489 | return $str; |
432 | 490 | } |
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 ){ |
434 | 498 | $returnIndex = array(); |
435 | 499 | preg_match('/mw.addMessages\s*\(\s*\{/', $str, $matches, PREG_OFFSET_CAPTURE ); |
436 | 500 | if( count($matches) == 0){ |
— | — | @@ -449,7 +513,7 @@ |
450 | 514 | if ( $ignorenext ) { |
451 | 515 | $ignorenext = false; |
452 | 516 | } 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 |
454 | 518 | switch( $char ) { |
455 | 519 | case '"': |
456 | 520 | $inquote = !$inquote; |
— | — | @@ -467,28 +531,46 @@ |
468 | 532 | } |
469 | 533 | } |
470 | 534 | } |
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 | + */ |
472 | 542 | function getInlineMsgFromClass( $class ){ |
473 | 543 | $jsmsg = $this->getMsgKeysFromClass( $class ); |
474 | 544 | if( $jsmsg ){ |
475 | | - self::getMsgKeys ( $jsmsg ); |
| 545 | + self::updateMsgKeys ( $jsmsg ); |
476 | 546 | return 'mw.addMessages(' . FormatJson::encode( $jsmsg ) . ');'; |
477 | 547 | }else{ |
478 | 548 | //if could not parse return empty string: |
479 | 549 | return ''; |
480 | 550 | } |
481 | 551 | } |
| 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 | + */ |
482 | 558 | function getMsgKeysFromClass( $class ){ |
483 | 559 | $file_path = self::getJsPathFromClass( $class ); |
484 | 560 | $str = $this->getScriptText($class, $file_path); |
485 | 561 | |
486 | | - $inx = self::getLoadGmIndex( $str ); |
| 562 | + $inx = self::getAddMessagesIndex( $str ); |
487 | 563 | if(!$inx) |
488 | | - return ''; |
| 564 | + return false; |
| 565 | + |
489 | 566 | return FormatJson::decode( '{' . substr($str, $inx['s'], ($inx['e']-$inx['s'])) . '}', true); |
490 | 567 | } |
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){ |
493 | 575 | global $wgContLanguageCode; |
494 | 576 | // Check the langCode |
495 | 577 | if(!$langCode) |
— | — | @@ -499,6 +581,12 @@ |
500 | 582 | $jmsg[ $msgKey ] = wfMsgGetKey( $msgKey, true, $langCode, false ); |
501 | 583 | } |
502 | 584 | } |
| 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 | + */ |
503 | 591 | function languageMsgReplace( $json_str ) { |
504 | 592 | $jmsg = FormatJson::decode( '{' . $json_str . '}', true ); |
505 | 593 | // Do the language lookup |
— | — | @@ -506,7 +594,7 @@ |
507 | 595 | |
508 | 596 | // See if any msgKey has the PLURAL template tag |
509 | 597 | //package in PLURAL mapping |
510 | | - self::getMsgKeys( $jmsg, $this->langCode ); |
| 598 | + self::updateMsgKeys( $jmsg, $this->langCode ); |
511 | 599 | |
512 | 600 | // Return the updated loadGM JSON with updated msgs: |
513 | 601 | return FormatJson::encode( $jmsg ); |
— | — | @@ -523,17 +611,29 @@ |
524 | 612 | } |
525 | 613 | } |
526 | 614 | |
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 | + */ |
528 | 618 | class simpleFileCache { |
529 | 619 | var $mFileCache; |
530 | 620 | var $filename = null; |
531 | | - var $rKey = null; |
| 621 | + var $requestKey = null; |
532 | 622 | |
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; |
535 | 630 | $this->getCacheFileName(); |
536 | 631 | } |
537 | 632 | |
| 633 | + /** |
| 634 | + * get Cache file file Name based on $requestKey and if gzip is enabled or not |
| 635 | + * |
| 636 | + * @return unknown |
| 637 | + */ |
538 | 638 | public function getCacheFileName() { |
539 | 639 | global $wgUseGzip, $wgScriptCacheDirectory; |
540 | 640 | |
— | — | @@ -547,15 +647,16 @@ |
548 | 648 | |
549 | 649 | // Check for defined files:: |
550 | 650 | if( is_file( $this->filename ) ) |
551 | | - return $this->filename; |
| 651 | + return $this->filename; |
552 | 652 | |
553 | | - if( is_file( $this->filename .'.gz') ){ |
554 | | - $this->filename.='.gz'; |
| 653 | + if( is_file( $this->filename . '.gz') ){ |
| 654 | + $this->filename .= '.gz'; |
555 | 655 | return $this->filename; |
556 | 656 | } |
557 | 657 | //check the update the name based on the $wgUseGzip config var |
558 | 658 | if ( isset($wgUseGzip) && $wgUseGzip ) |
559 | | - $this->filename.='.gz'; |
| 659 | + $this->filename.='.gz'; |
| 660 | + |
560 | 661 | } |
561 | 662 | |
562 | 663 | public function isFileCached() { |