r66703 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r66702‎ | r66703 | r66704 >
Date:19:20, 20 May 2010
Author:dale
Status:deferred
Tags:
Comment:
added user preference for enabling disabling script-grouping
Modified paths:
  • /trunk/extensions/JS2Support/JS2Support.hooks.php (added) (history)
  • /trunk/extensions/JS2Support/JS2Support.i18n.php (modified) (history)
  • /trunk/extensions/JS2Support/JS2Support.php (modified) (history)
  • /trunk/extensions/JS2Support/ScriptLoaderOutputPage.php (modified) (history)
  • /trunk/extensions/JS2Support/mwEmbed/jsScriptLoader.php (modified) (history)
  • /trunk/extensions/JS2Support/mwScriptLoader.php (modified) (history)

Diff [purge]

Index: trunk/extensions/JS2Support/JS2Support.i18n.php
@@ -10,6 +10,8 @@
1111
1212 $messages['en'] = array(
1313 'js2support-desc' => 'JS2 support as an extension',
 14+ 'js2support-debug-preference' => 'Disable script grouping and minification',
 15+ 'prefs-script-debug' => 'Script Debugging'
1416 );
1517
1618 /** Belarusian (Taraškievica orthography) (Беларуская (тарашкевіца))
Index: trunk/extensions/JS2Support/ScriptLoaderOutputPage.php
@@ -34,8 +34,8 @@
3535 * this way you would not break the cache for other per page scripts
3636 */
3737 var $mScriptLoaderClassList = array(
38 - 'js'=> array(),
39 - 'css'=>array()
 38+ 'js' => array(),
 39+ 'css' => array()
4040 );
4141
4242 // Stores the options for a given bucket ( like css media type )
@@ -44,6 +44,9 @@
4545 // Local scriptLoader instance object
4646 var $mScriptLoader = null;
4747
 48+ // If script grouping is enabled or not. (lazy initialised use () )
 49+ var $mEnabledScriptGrouping = null; // lazy initializsed
 50+
4851 /**
4952 * Add a JavaScript file out of skins/common, or a given relative path.
5053 *
@@ -51,7 +54,7 @@
5255 * (/foo/bar.js)
5356 */
5457 public function addScriptFile( $file , $scriptRequestBucket = 'page' ) {
55 - global $wgStylePath, $wgStyleVersion, $wgEnableScriptLoader, $wgScript;
 58+ global $wgStylePath, $wgStyleVersion, $wgScript;
5659 if( substr( $file, 0, 1 ) == '/' || preg_match( '#^[a-z]*://#i', $file ) ) {
5760 $path = $file;
5861 } else {
@@ -59,7 +62,7 @@
6063 }
6164
6265 // If script-loader enabled check if we can add the script via script-loader
63 - if( $wgEnableScriptLoader ) {
 66+ if( $this->isScriptGroupingEnabled() ) {
6467 $jsClass = $this->getClassFromPath( $path );
6568 if( $jsClass ) {
6669 $this->addScriptClass( $jsClass, $scriptRequestBucket );
@@ -71,6 +74,22 @@
7275 }
7376
7477 /**
 78+ * Check the user preference for script grouping for
 79+ * by default script grouping uses the wgEnableScriptLoader global
 80+ */
 81+ function isScriptGroupingEnabled() {
 82+ global $wgEnableScriptLoader, $wgUser;
 83+ // Get the preferences for the current user
 84+ $preferences = Preferences::getPreferences( $wgUser );
 85+ // For some reaons prefrence "value" is stored in "default"
 86+ if( $preferences[ 'scriptdebug' ]['default'] == 1 ) {
 87+ return false;
 88+ } else {
 89+ return $wgEnableScriptLoader;
 90+ }
 91+ }
 92+
 93+ /**
7594 * Gets the class name From an internal wiki title link
7695 * @param $path String: script include path
7796 */
@@ -118,7 +137,7 @@
119138 */
120139 function getHeadScripts( Skin $sk ) {
121140 global $wgUser, $wgRequest, $wgJsMimeType, $wgUseSiteJs;
122 - global $wgStylePath, $wgStyleVersion, $wgEnableScriptLoader, $wgDebugJavaScript;
 141+ global $wgStylePath, $wgStyleVersion, $wgDebugJavaScript;
123142
124143 $scripts = Skin::makeGlobalVariablesScript( $sk->getSkinName() );
125144
@@ -155,7 +174,7 @@
156175
157176
158177 // If the script-loader is enabled get script-loader classes per buckets
159 - if( $wgEnableScriptLoader ) {
 178+ if( $this->isScriptGroupingEnabled() ) {
160179 $this->mScripts .= $this->getScriptLoaderJS();
161180 }
162181
@@ -200,7 +219,6 @@
201220 * @param $bucket String: Script-loader grouping string
202221 */
203222 public function addStyle( $style, $media='', $condition='', $dir='', $bucket='' ) {
204 - global $wgEnableScriptLoader;
205223 $options = array();
206224
207225 if( $media ) {
@@ -226,7 +244,7 @@
227245 }
228246
229247 // If script-loader enabled check if we can add the script via script-loader
230 - if( $wgEnableScriptLoader ) {
 248+ if( $this->isScriptGroupingEnabled() ) {
231249 global $wgStylePath, $wgScript;
232250 // Append style path if we are not a wikititle self reference:
233251 if( strpos( $style, $wgScript ) === false ) {
@@ -255,7 +273,7 @@
256274 * 'bucket' String: Script-loader grouping string
257275 */
258276 public function addStyleClass( $cssClass , $options = array()) {
259 - global $wgEnableScriptLoader, $wgExtensionAssetsPath;
 277+ global $wgExtensionAssetsPath;
260278 // Build the bucket key with supplied options
261279
262280
@@ -271,18 +289,18 @@
272290 // Update the css bucket options:
273291 $this->setClassBucketOptions( $bucketKey, $options );
274292
275 - if( $wgEnableScriptLoader ) {
 293+ if( $this->isScriptGroupingEnabled() ) {
276294 $this->addScriptClass( $cssClass, $bucketKey, 'css');
277295 } else {
278296 $stylePath = jsScriptLoader::getPathFromClass( $cssClass );
279297 // Else use normal styles output
280298 //( unfortunately $this->styles appends /skins ) so use addLink
281299 $this->addLink(
282 - array(
 300+ array(
283301 'rel' => 'stylesheet',
284302 'type' => 'text/css',
285303 'href' => $stylePath . '?' . $this->getURIDparam( $cssClass ),
286 - )
 304+ )
287305 );
288306 }
289307
@@ -318,9 +336,8 @@
319337 * These will be applied to various media & IE conditionals.
320338 */
321339 public function buildCssLinks() {
322 - global $wgEnableScriptLoader;
323340 $scriptLoaderCss = '';
324 - if( $wgEnableScriptLoader ){
 341+ if( $this->isScriptGroupingEnabled() ){
325342 $scriptLoaderCss = $this->getScriptLoaderCss();
326343 }
327344 return $scriptLoaderCss . "\n" . parent::buildCssLinks();
@@ -337,7 +354,7 @@
338355 * @since 1.17
339356 */
340357 public function includeAllPageJS ( ) {
341 - global $wgExtensionJavascriptModules, $wgEnableScriptLoader,
 358+ global $wgExtensionJavascriptModules,
342359 $wgScriptLoaderNamedPaths, $wgScriptPath;
343360
344361 // Set core Classes
@@ -358,7 +375,7 @@
359376 $postScripts = $this->mScripts;
360377 $this->mScripts = '';
361378
362 - if( $wgEnableScriptLoader ) {
 379+ if( $this->isScriptGroupingEnabled() ) {
363380 $this->mScripts = "\n<!-- Script bucket: allpage --> \n";
364381 $this->mScripts = $this->getLinkedScriptLoaderJs( $coreClasses );
365382 } else {
@@ -381,10 +398,10 @@
382399 $loaderDir = $modulePath . "/";
383400 $this->addScript( Html::inlineScript(
384401 "mw.setConfig( 'loaderContext', '" . xml::escapeJsString( $wgScriptPath .'/'. $loaderDir ) . "');"
385 - ) );
386 - $this->addScriptFile(
 402+ ) );
 403+ $this->addScriptFile(
387404 "$wgScriptPath/$modulePath/loader.js"
388 - );
 405+ );
389406 }
390407 }
391408 //Now re-append any scripts that got added prior to the includeJQuery call
@@ -484,8 +501,7 @@
485502 * @return boolean False if the class wasn't found, True on success
486503 */
487504 function addScriptClass( $className, $scriptRequestBucket = 'page' , $type='js') {
488 - global $wgDebugJavaScript, $wgScriptLoaderNamedPaths, $IP,
489 - $wgEnableScriptLoader, $wgStyleVersion, $wgScriptPath, $wgStylePath,
 505+ global $wgDebugJavaScript, $wgScriptLoaderNamedPaths, $IP, $wgStyleVersion, $wgScriptPath, $wgStylePath,
490506 $wgUser;
491507
492508 if( !$this->mScriptLoader ){
@@ -494,7 +510,7 @@
495511
496512 // Check for wikiTitle:
497513 if ( substr( $className, 0, 3 ) == 'WT:' ) {
498 - if( $wgEnableScriptLoader ) {
 514+ if( $this->isScriptGroupingEnabled() ) {
499515 // Add to the mScriptLoaderClassList list
500516 $this->addClassToOutputBucket( $className, $scriptRequestBucket, $type);
501517 return true;
@@ -512,9 +528,9 @@
513529 }
514530 // Add the script
515531 $this->addScriptFile(
516 - Skin::makeUrl( $titlePage,
 532+ Skin::makeUrl( $titlePage,
517533 "action=raw$jsCache&gen=js$titleArg"
518 - )
 534+ )
519535 );
520536 }
521537
@@ -529,7 +545,7 @@
530546 return false; // could not find the class
531547 }
532548 // Valid path add it to script-loader or "link" directly
533 - if( $wgEnableScriptLoader ) {
 549+ if( $this->isScriptGroupingEnabled() ) {
534550 // Register it with the script loader
535551 $this->addClassToOutputBucket( $className, $scriptRequestBucket, $type );
536552 return true;
Index: trunk/extensions/JS2Support/mwEmbed/jsScriptLoader.php
@@ -21,7 +21,7 @@
2222 $myScriptLoader->doScriptLoader();
2323 }
2424
25 -class jsScriptLoader {
 25+class jsScriptLoader {
2626
2727 // The list of named javascript & css files
2828 var $namedFileList = array();
@@ -584,7 +584,7 @@
585585
586586 // Add the unique rid
587587 $this->requestKey .= $this->urid;
588 -
 588+
589589 }
590590 /**
591591 * Pre-process request variables ~without configuration~ or any utility functions.
@@ -948,12 +948,12 @@
949949 * @return {Array} decoded json array of message key value pairs
950950 */
951951 function getMsgKeysFromClass( $className ){
 952+ global $wgExtensionJavascriptModules, $wgExtensionMessagesFiles;
952953
953954 $scriptString = $this->getScriptText( $className );
954955
955956 // Try for includeAllModuleMsgs function call
956957 if ( preg_match ( self::$includeAllMsgsRegEx, $scriptString ) !== false ) {
957 - global $wgExtensionJavascriptModules, $wgExtensionMessagesFiles;
958958 // Get the module $messages keys
959959
960960 $moduleName = jsClassLoader::getClassModuleName( $className );
@@ -968,7 +968,7 @@
969969 // Get the i18n file from $wgExtensionMessagesFiles path
970970 require( $wgExtensionMessagesFiles[ $moduleName ] );
971971
972 - // Return the English key set ( since base message text is in english )
 972+ // Return the English key set ( since base message text is in English )
973973 return $messages['en'];
974974 } else {
975975 // No $moduleName found.
Index: trunk/extensions/JS2Support/JS2Support.php
@@ -14,52 +14,30 @@
1515 );
1616
1717 $js2Dir = dirname( __FILE__ ) . '/';
18 -$wgExtensionMessagesFiles[ 'JS2Support' ] = $js2Dir . 'JS2Support.i18n.php';
1918
 19+// Localizations
 20+$wgExtensionMessagesFiles[ 'JS2Support' ] =
 21+ dirname( __FILE__ ) . '/JS2Support.i18n.php';
2022
 23+// Hooks
 24+$wgAutoloadClasses['JS2SupportHooks'] =
 25+ dirname( __FILE__ ) . "/JS2Support.hooks.php";
 26+
2127 /**
22 - * Setup the js2 extension:
 28+ * Add Setup the js2 extension hook:
2329 */
24 -$wgExtensionFunctions[] = 'wfSetupJS2';
25 -function wfSetupJS2(){
26 - global $wgOut, $js2Dir, $wgAutoloadClasses, $wgScriptLoaderNamedPaths,
27 - $wgExtensionJavascriptModules, $wgEnableTestJavascriptModules;
 30+$wgExtensionFunctions[] = 'JS2SupportHooks::setup';
2831
29 - // Remap output page as part of the extension setup
30 - $wgOut = new StubObject( 'wgOut', 'ScriptLoaderOutputPage' );
31 - $wgAutoloadClasses[ 'ScriptLoaderOutputPage' ] = $js2Dir . 'ScriptLoaderOutputPage.php';
32 -
33 - // Include all the mediaWiki autoload classes:
34 - require( $js2Dir . 'JS2AutoLoader.php');
35 -
36 - // Add the core test module loaders (extensions can add their own test modules referencing this global )
37 - if( $wgEnableTestJavascriptModules ) {
38 - $wgExtensionJavascriptModules['JS2Tests'] = 'extensions/JS2Support/tests';
39 - }
40 -
41 - // Update all the javascript modules classNames and localization by reading respective loader.js files
42 - // @dependent on all extensions defining $wgExtensionJavascriptModules paths in config file ( not in setup )
43 - //
44 - // @NOTE runtime for loadClassPaths with 8 or so loaders with 100 or so named paths is
45 - // is around .002 seconds on my laptop. Could probably be further optimized and of course it only runs
46 - // on non-cached pages.
47 - jsClassLoader::loadClassPaths();
48 -}
49 -
5032 /**
5133 * MakeGlobalVariablesScript hook ( add the wgScriptLoaderPath var )
5234 */
53 -$wgHooks['MakeGlobalVariablesScript'][] = 'js2SupportAddJSVars';
54 -function js2SupportAddJSVars( &$vars ) {
55 - global $wgExtensionAssetsPath;
56 - $vars = array_merge( $vars,
57 - array(
58 - 'wgScriptLoaderLocation' => $wgExtensionAssetsPath . '/JS2Support/mwScriptLoader.php'
59 - )
60 - );
61 - return true;
62 -}
 35+$wgHooks['MakeGlobalVariablesScript'][] = 'JS2SupportHooks::addJSVars';
6336
 37+/**
 38+* Add a preference hook
 39+*/
 40+$wgHooks['GetPreferences'][] = 'JS2SupportHooks::addPreferences';
 41+
6442 /****************************
6543 * Configuration
6644 * Could eventually go into DefaultSettings.php
Index: trunk/extensions/JS2Support/JS2Support.hooks.php
@@ -0,0 +1,52 @@
 2+<?php
 3+
 4+/**
 5+* Setup JS2 hooks
 6+*/
 7+class JS2SupportHooks {
 8+
 9+ public static function setup(){
 10+ global $wgOut, $js2Dir, $wgAutoloadClasses, $wgScriptLoaderNamedPaths,
 11+ $wgExtensionJavascriptModules, $wgEnableTestJavascriptModules;
 12+
 13+ // Remap output page as part of the extension setup
 14+ $wgOut = new StubObject( 'wgOut', 'ScriptLoaderOutputPage' );
 15+ $wgAutoloadClasses[ 'ScriptLoaderOutputPage' ] = $js2Dir . 'ScriptLoaderOutputPage.php';
 16+
 17+ // Include all the mediaWiki autoload classes:
 18+ require( $js2Dir . 'JS2AutoLoader.php');
 19+
 20+ // Add the core test module loaders (extensions can add their own test modules referencing this global )
 21+ if( $wgEnableTestJavascriptModules ) {
 22+ $wgExtensionJavascriptModules['JS2Tests'] = 'extensions/JS2Support/tests';
 23+ }
 24+
 25+ // Update all the javascript modules classNames and localization by reading respective loader.js files
 26+ // @dependent on all extensions defining $wgExtensionJavascriptModules paths in config file ( not in setup )
 27+ //
 28+ // @NOTE runtime for loadClassPaths with 8 or so loaders with 100 or so named paths is
 29+ // is around .002 seconds on my laptop. Could probably be further optimized and of course it only runs
 30+ // on non-cached pages.
 31+ jsClassLoader::loadClassPaths();
 32+ }
 33+
 34+ public static function addJSVars( &$vars ) {
 35+ global $wgExtensionAssetsPath;
 36+ $vars = array_merge( $vars,
 37+ array(
 38+ 'wgScriptLoaderLocation' => $wgExtensionAssetsPath . '/JS2Support/mwScriptLoader.php'
 39+ )
 40+ );
 41+ return true;
 42+ }
 43+
 44+ public static function addPreferences( $user, &$preferences ){
 45+ // Add the preference to enable / disable debugging:
 46+ $preferences['scriptdebug'] = array(
 47+ 'type' => 'toggle',
 48+ 'label-message' => 'js2support-debug-preference',
 49+ 'section' => 'misc/script-debug'
 50+ );
 51+ return true;
 52+ }
 53+}
\ No newline at end of file
Property changes on: trunk/extensions/JS2Support/JS2Support.hooks.php
___________________________________________________________________
Name: svn:eol-style
154 + native
Index: trunk/extensions/JS2Support/mwScriptLoader.php
@@ -36,8 +36,8 @@
3737
3838 // No-cache hit load up mediaWiki stuff and continue scriptloader processing:
3939
40 -// Check if we need to use directory traversal:
41 -if( !getenv( 'MW_INSTALL_PATH' ) ){
 40+// Check if we need to use directory traversal:
 41+if( !getenv( 'MW_INSTALL_PATH' ) ){
4242 // Use '../../' because WebStart.php uses realpath( '.' ); to define $IP
4343 chdir( '../../' );
4444 }
@@ -56,7 +56,7 @@
5757 return;
5858 }
5959 // Verify the script loader is on:
60 -if ( !$wgEnableScriptLoader && $myScriptLoader->outputFormat != 'messages') {
 60+if ( !$wgEnableScriptLoader && $myScriptLoader->outputFormat != 'messages' ) {
6161 echo '/*ScriptLoader is not enabled for this site. To enable add the following line to your LocalSettings.php';
6262 echo '<pre><b>$wgEnableScriptLoader=true;</b></pre>*/';
6363 echo 'alert(\'Script loader is disabled\');';

Status & tagging log