Index: trunk/extensions/JS2Support/JS2Support.i18n.php |
— | — | @@ -10,6 +10,8 @@ |
11 | 11 | |
12 | 12 | $messages['en'] = array( |
13 | 13 | 'js2support-desc' => 'JS2 support as an extension', |
| 14 | + 'js2support-debug-preference' => 'Disable script grouping and minification', |
| 15 | + 'prefs-script-debug' => 'Script Debugging' |
14 | 16 | ); |
15 | 17 | |
16 | 18 | /** Belarusian (Taraškievica orthography) (Беларуская (тарашкевіца)) |
Index: trunk/extensions/JS2Support/ScriptLoaderOutputPage.php |
— | — | @@ -34,8 +34,8 @@ |
35 | 35 | * this way you would not break the cache for other per page scripts |
36 | 36 | */ |
37 | 37 | var $mScriptLoaderClassList = array( |
38 | | - 'js'=> array(), |
39 | | - 'css'=>array() |
| 38 | + 'js' => array(), |
| 39 | + 'css' => array() |
40 | 40 | ); |
41 | 41 | |
42 | 42 | // Stores the options for a given bucket ( like css media type ) |
— | — | @@ -44,6 +44,9 @@ |
45 | 45 | // Local scriptLoader instance object |
46 | 46 | var $mScriptLoader = null; |
47 | 47 | |
| 48 | + // If script grouping is enabled or not. (lazy initialised use () ) |
| 49 | + var $mEnabledScriptGrouping = null; // lazy initializsed |
| 50 | + |
48 | 51 | /** |
49 | 52 | * Add a JavaScript file out of skins/common, or a given relative path. |
50 | 53 | * |
— | — | @@ -51,7 +54,7 @@ |
52 | 55 | * (/foo/bar.js) |
53 | 56 | */ |
54 | 57 | public function addScriptFile( $file , $scriptRequestBucket = 'page' ) { |
55 | | - global $wgStylePath, $wgStyleVersion, $wgEnableScriptLoader, $wgScript; |
| 58 | + global $wgStylePath, $wgStyleVersion, $wgScript; |
56 | 59 | if( substr( $file, 0, 1 ) == '/' || preg_match( '#^[a-z]*://#i', $file ) ) { |
57 | 60 | $path = $file; |
58 | 61 | } else { |
— | — | @@ -59,7 +62,7 @@ |
60 | 63 | } |
61 | 64 | |
62 | 65 | // If script-loader enabled check if we can add the script via script-loader |
63 | | - if( $wgEnableScriptLoader ) { |
| 66 | + if( $this->isScriptGroupingEnabled() ) { |
64 | 67 | $jsClass = $this->getClassFromPath( $path ); |
65 | 68 | if( $jsClass ) { |
66 | 69 | $this->addScriptClass( $jsClass, $scriptRequestBucket ); |
— | — | @@ -71,6 +74,22 @@ |
72 | 75 | } |
73 | 76 | |
74 | 77 | /** |
| 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 | + /** |
75 | 94 | * Gets the class name From an internal wiki title link |
76 | 95 | * @param $path String: script include path |
77 | 96 | */ |
— | — | @@ -118,7 +137,7 @@ |
119 | 138 | */ |
120 | 139 | function getHeadScripts( Skin $sk ) { |
121 | 140 | global $wgUser, $wgRequest, $wgJsMimeType, $wgUseSiteJs; |
122 | | - global $wgStylePath, $wgStyleVersion, $wgEnableScriptLoader, $wgDebugJavaScript; |
| 141 | + global $wgStylePath, $wgStyleVersion, $wgDebugJavaScript; |
123 | 142 | |
124 | 143 | $scripts = Skin::makeGlobalVariablesScript( $sk->getSkinName() ); |
125 | 144 | |
— | — | @@ -155,7 +174,7 @@ |
156 | 175 | |
157 | 176 | |
158 | 177 | // If the script-loader is enabled get script-loader classes per buckets |
159 | | - if( $wgEnableScriptLoader ) { |
| 178 | + if( $this->isScriptGroupingEnabled() ) { |
160 | 179 | $this->mScripts .= $this->getScriptLoaderJS(); |
161 | 180 | } |
162 | 181 | |
— | — | @@ -200,7 +219,6 @@ |
201 | 220 | * @param $bucket String: Script-loader grouping string |
202 | 221 | */ |
203 | 222 | public function addStyle( $style, $media='', $condition='', $dir='', $bucket='' ) { |
204 | | - global $wgEnableScriptLoader; |
205 | 223 | $options = array(); |
206 | 224 | |
207 | 225 | if( $media ) { |
— | — | @@ -226,7 +244,7 @@ |
227 | 245 | } |
228 | 246 | |
229 | 247 | // If script-loader enabled check if we can add the script via script-loader |
230 | | - if( $wgEnableScriptLoader ) { |
| 248 | + if( $this->isScriptGroupingEnabled() ) { |
231 | 249 | global $wgStylePath, $wgScript; |
232 | 250 | // Append style path if we are not a wikititle self reference: |
233 | 251 | if( strpos( $style, $wgScript ) === false ) { |
— | — | @@ -255,7 +273,7 @@ |
256 | 274 | * 'bucket' String: Script-loader grouping string |
257 | 275 | */ |
258 | 276 | public function addStyleClass( $cssClass , $options = array()) { |
259 | | - global $wgEnableScriptLoader, $wgExtensionAssetsPath; |
| 277 | + global $wgExtensionAssetsPath; |
260 | 278 | // Build the bucket key with supplied options |
261 | 279 | |
262 | 280 | |
— | — | @@ -271,18 +289,18 @@ |
272 | 290 | // Update the css bucket options: |
273 | 291 | $this->setClassBucketOptions( $bucketKey, $options ); |
274 | 292 | |
275 | | - if( $wgEnableScriptLoader ) { |
| 293 | + if( $this->isScriptGroupingEnabled() ) { |
276 | 294 | $this->addScriptClass( $cssClass, $bucketKey, 'css'); |
277 | 295 | } else { |
278 | 296 | $stylePath = jsScriptLoader::getPathFromClass( $cssClass ); |
279 | 297 | // Else use normal styles output |
280 | 298 | //( unfortunately $this->styles appends /skins ) so use addLink |
281 | 299 | $this->addLink( |
282 | | - array( |
| 300 | + array( |
283 | 301 | 'rel' => 'stylesheet', |
284 | 302 | 'type' => 'text/css', |
285 | 303 | 'href' => $stylePath . '?' . $this->getURIDparam( $cssClass ), |
286 | | - ) |
| 304 | + ) |
287 | 305 | ); |
288 | 306 | } |
289 | 307 | |
— | — | @@ -318,9 +336,8 @@ |
319 | 337 | * These will be applied to various media & IE conditionals. |
320 | 338 | */ |
321 | 339 | public function buildCssLinks() { |
322 | | - global $wgEnableScriptLoader; |
323 | 340 | $scriptLoaderCss = ''; |
324 | | - if( $wgEnableScriptLoader ){ |
| 341 | + if( $this->isScriptGroupingEnabled() ){ |
325 | 342 | $scriptLoaderCss = $this->getScriptLoaderCss(); |
326 | 343 | } |
327 | 344 | return $scriptLoaderCss . "\n" . parent::buildCssLinks(); |
— | — | @@ -337,7 +354,7 @@ |
338 | 355 | * @since 1.17 |
339 | 356 | */ |
340 | 357 | public function includeAllPageJS ( ) { |
341 | | - global $wgExtensionJavascriptModules, $wgEnableScriptLoader, |
| 358 | + global $wgExtensionJavascriptModules, |
342 | 359 | $wgScriptLoaderNamedPaths, $wgScriptPath; |
343 | 360 | |
344 | 361 | // Set core Classes |
— | — | @@ -358,7 +375,7 @@ |
359 | 376 | $postScripts = $this->mScripts; |
360 | 377 | $this->mScripts = ''; |
361 | 378 | |
362 | | - if( $wgEnableScriptLoader ) { |
| 379 | + if( $this->isScriptGroupingEnabled() ) { |
363 | 380 | $this->mScripts = "\n<!-- Script bucket: allpage --> \n"; |
364 | 381 | $this->mScripts = $this->getLinkedScriptLoaderJs( $coreClasses ); |
365 | 382 | } else { |
— | — | @@ -381,10 +398,10 @@ |
382 | 399 | $loaderDir = $modulePath . "/"; |
383 | 400 | $this->addScript( Html::inlineScript( |
384 | 401 | "mw.setConfig( 'loaderContext', '" . xml::escapeJsString( $wgScriptPath .'/'. $loaderDir ) . "');" |
385 | | - ) ); |
386 | | - $this->addScriptFile( |
| 402 | + ) ); |
| 403 | + $this->addScriptFile( |
387 | 404 | "$wgScriptPath/$modulePath/loader.js" |
388 | | - ); |
| 405 | + ); |
389 | 406 | } |
390 | 407 | } |
391 | 408 | //Now re-append any scripts that got added prior to the includeJQuery call |
— | — | @@ -484,8 +501,7 @@ |
485 | 502 | * @return boolean False if the class wasn't found, True on success |
486 | 503 | */ |
487 | 504 | 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, |
490 | 506 | $wgUser; |
491 | 507 | |
492 | 508 | if( !$this->mScriptLoader ){ |
— | — | @@ -494,7 +510,7 @@ |
495 | 511 | |
496 | 512 | // Check for wikiTitle: |
497 | 513 | if ( substr( $className, 0, 3 ) == 'WT:' ) { |
498 | | - if( $wgEnableScriptLoader ) { |
| 514 | + if( $this->isScriptGroupingEnabled() ) { |
499 | 515 | // Add to the mScriptLoaderClassList list |
500 | 516 | $this->addClassToOutputBucket( $className, $scriptRequestBucket, $type); |
501 | 517 | return true; |
— | — | @@ -512,9 +528,9 @@ |
513 | 529 | } |
514 | 530 | // Add the script |
515 | 531 | $this->addScriptFile( |
516 | | - Skin::makeUrl( $titlePage, |
| 532 | + Skin::makeUrl( $titlePage, |
517 | 533 | "action=raw$jsCache&gen=js$titleArg" |
518 | | - ) |
| 534 | + ) |
519 | 535 | ); |
520 | 536 | } |
521 | 537 | |
— | — | @@ -529,7 +545,7 @@ |
530 | 546 | return false; // could not find the class |
531 | 547 | } |
532 | 548 | // Valid path add it to script-loader or "link" directly |
533 | | - if( $wgEnableScriptLoader ) { |
| 549 | + if( $this->isScriptGroupingEnabled() ) { |
534 | 550 | // Register it with the script loader |
535 | 551 | $this->addClassToOutputBucket( $className, $scriptRequestBucket, $type ); |
536 | 552 | return true; |
Index: trunk/extensions/JS2Support/mwEmbed/jsScriptLoader.php |
— | — | @@ -21,7 +21,7 @@ |
22 | 22 | $myScriptLoader->doScriptLoader(); |
23 | 23 | } |
24 | 24 | |
25 | | -class jsScriptLoader { |
| 25 | +class jsScriptLoader { |
26 | 26 | |
27 | 27 | // The list of named javascript & css files |
28 | 28 | var $namedFileList = array(); |
— | — | @@ -584,7 +584,7 @@ |
585 | 585 | |
586 | 586 | // Add the unique rid |
587 | 587 | $this->requestKey .= $this->urid; |
588 | | - |
| 588 | + |
589 | 589 | } |
590 | 590 | /** |
591 | 591 | * Pre-process request variables ~without configuration~ or any utility functions. |
— | — | @@ -948,12 +948,12 @@ |
949 | 949 | * @return {Array} decoded json array of message key value pairs |
950 | 950 | */ |
951 | 951 | function getMsgKeysFromClass( $className ){ |
| 952 | + global $wgExtensionJavascriptModules, $wgExtensionMessagesFiles; |
952 | 953 | |
953 | 954 | $scriptString = $this->getScriptText( $className ); |
954 | 955 | |
955 | 956 | // Try for includeAllModuleMsgs function call |
956 | 957 | if ( preg_match ( self::$includeAllMsgsRegEx, $scriptString ) !== false ) { |
957 | | - global $wgExtensionJavascriptModules, $wgExtensionMessagesFiles; |
958 | 958 | // Get the module $messages keys |
959 | 959 | |
960 | 960 | $moduleName = jsClassLoader::getClassModuleName( $className ); |
— | — | @@ -968,7 +968,7 @@ |
969 | 969 | // Get the i18n file from $wgExtensionMessagesFiles path |
970 | 970 | require( $wgExtensionMessagesFiles[ $moduleName ] ); |
971 | 971 | |
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 ) |
973 | 973 | return $messages['en']; |
974 | 974 | } else { |
975 | 975 | // No $moduleName found. |
Index: trunk/extensions/JS2Support/JS2Support.php |
— | — | @@ -14,52 +14,30 @@ |
15 | 15 | ); |
16 | 16 | |
17 | 17 | $js2Dir = dirname( __FILE__ ) . '/'; |
18 | | -$wgExtensionMessagesFiles[ 'JS2Support' ] = $js2Dir . 'JS2Support.i18n.php'; |
19 | 18 | |
| 19 | +// Localizations |
| 20 | +$wgExtensionMessagesFiles[ 'JS2Support' ] = |
| 21 | + dirname( __FILE__ ) . '/JS2Support.i18n.php'; |
20 | 22 | |
| 23 | +// Hooks |
| 24 | +$wgAutoloadClasses['JS2SupportHooks'] = |
| 25 | + dirname( __FILE__ ) . "/JS2Support.hooks.php"; |
| 26 | + |
21 | 27 | /** |
22 | | - * Setup the js2 extension: |
| 28 | + * Add Setup the js2 extension hook: |
23 | 29 | */ |
24 | | -$wgExtensionFunctions[] = 'wfSetupJS2'; |
25 | | -function wfSetupJS2(){ |
26 | | - global $wgOut, $js2Dir, $wgAutoloadClasses, $wgScriptLoaderNamedPaths, |
27 | | - $wgExtensionJavascriptModules, $wgEnableTestJavascriptModules; |
| 30 | +$wgExtensionFunctions[] = 'JS2SupportHooks::setup'; |
28 | 31 | |
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 | | - |
50 | 32 | /** |
51 | 33 | * MakeGlobalVariablesScript hook ( add the wgScriptLoaderPath var ) |
52 | 34 | */ |
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'; |
63 | 36 | |
| 37 | +/** |
| 38 | +* Add a preference hook |
| 39 | +*/ |
| 40 | +$wgHooks['GetPreferences'][] = 'JS2SupportHooks::addPreferences'; |
| 41 | + |
64 | 42 | /**************************** |
65 | 43 | * Configuration |
66 | 44 | * 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 |
1 | 54 | + native |
Index: trunk/extensions/JS2Support/mwScriptLoader.php |
— | — | @@ -36,8 +36,8 @@ |
37 | 37 | |
38 | 38 | // No-cache hit load up mediaWiki stuff and continue scriptloader processing: |
39 | 39 | |
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' ) ){ |
42 | 42 | // Use '../../' because WebStart.php uses realpath( '.' ); to define $IP |
43 | 43 | chdir( '../../' ); |
44 | 44 | } |
— | — | @@ -56,7 +56,7 @@ |
57 | 57 | return; |
58 | 58 | } |
59 | 59 | // Verify the script loader is on: |
60 | | -if ( !$wgEnableScriptLoader && $myScriptLoader->outputFormat != 'messages') { |
| 60 | +if ( !$wgEnableScriptLoader && $myScriptLoader->outputFormat != 'messages' ) { |
61 | 61 | echo '/*ScriptLoader is not enabled for this site. To enable add the following line to your LocalSettings.php'; |
62 | 62 | echo '<pre><b>$wgEnableScriptLoader=true;</b></pre>*/'; |
63 | 63 | echo 'alert(\'Script loader is disabled\');'; |