Index: branches/resourceloader/phase3/includes/OutputPage.php |
— | — | @@ -24,7 +24,7 @@ |
25 | 25 | var $mCategoryLinks = array(), $mCategories = array(), $mLanguageLinks = array(); |
26 | 26 | |
27 | 27 | var $mScripts = '', $mLinkColours, $mPageLinkTitle = '', $mHeadItems = array(); |
28 | | - var $mResources = array(); |
| 28 | + var $mModules = array(), $mModuleScripts = array(), $mModuleStyles = array(), $mModuleMessages = array(); |
29 | 29 | var $mInlineMsg = array(); |
30 | 30 | |
31 | 31 | var $mTemplateIds = array(); |
— | — | @@ -225,24 +225,78 @@ |
226 | 226 | } |
227 | 227 | |
228 | 228 | /** |
229 | | - * Get the list of resources to include on this page |
| 229 | + * Get the list of modules to include on this page |
230 | 230 | * @return array of module names |
231 | 231 | */ |
232 | | - public function getResources() { |
233 | | - return $this->mResources; |
| 232 | + public function getModules() { |
| 233 | + return $this->mModules; |
234 | 234 | } |
235 | 235 | |
236 | 236 | /** |
237 | | - * Add a module recognized by the resource loader. Modules added |
| 237 | + * Add one or more modules recognized by the resource loader. Modules added |
238 | 238 | * through this function will be loaded by the resource loader when the |
239 | 239 | * page loads. |
240 | 240 | * @param $module mixed Module name (string) or array of module names |
241 | 241 | */ |
242 | 242 | public function addModules( $modules ) { |
243 | | - $this->mResources = array_merge( $this->mResources, (array)$modules ); |
| 243 | + $this->mModules = array_merge( $this->mModules, (array)$modules ); |
244 | 244 | } |
245 | 245 | |
246 | 246 | /** |
| 247 | + * Get the list of module JS to include on this page |
| 248 | + * @return array of module names |
| 249 | + */ |
| 250 | + public function getModuleScripts() { |
| 251 | + return $this->mModuleScripts; |
| 252 | + } |
| 253 | + |
| 254 | + /** |
| 255 | + * Add only JS of one or more modules recognized by the resource loader. Module |
| 256 | + * scripts added through this function will be loaded by the resource loader when |
| 257 | + * the page loads. |
| 258 | + * @param $module mixed Module name (string) or array of module names |
| 259 | + */ |
| 260 | + public function addModuleScripts( $modules ) { |
| 261 | + $this->mModuleScripts = array_merge( $this->mModuleScripts, (array)$modules ); |
| 262 | + } |
| 263 | + |
| 264 | + /** |
| 265 | + * Get the list of module CSS to include on this page |
| 266 | + * @return array of module names |
| 267 | + */ |
| 268 | + public function getModuleStyles() { |
| 269 | + return $this->mModuleStyles; |
| 270 | + } |
| 271 | + |
| 272 | + /** |
| 273 | + * Add only CSS of one or more modules recognized by the resource loader. Module |
| 274 | + * styles added through this function will be loaded by the resource loader when |
| 275 | + * the page loads. |
| 276 | + * @param $module mixed Module name (string) or array of module names |
| 277 | + */ |
| 278 | + public function addModuleStyles( $modules ) { |
| 279 | + $this->mModuleStyles = array_merge( $this->mModuleStyles, (array)$modules ); |
| 280 | + } |
| 281 | + |
| 282 | + /** |
| 283 | + * Get the list of module messages to include on this page |
| 284 | + * @return array of module names |
| 285 | + */ |
| 286 | + public function getModuleMessages() { |
| 287 | + return $this->mModuleMessages; |
| 288 | + } |
| 289 | + |
| 290 | + /** |
| 291 | + * Add only messages of one or more modules recognized by the resource loader. |
| 292 | + * Module messages added through this function will be loaded by the resource |
| 293 | + * loader when the page loads. |
| 294 | + * @param $module mixed Module name (string) or array of module names |
| 295 | + */ |
| 296 | + public function addModuleMessages( $modules ) { |
| 297 | + $this->mModuleMessages = array_merge( $this->mModuleMessages, (array)$modules ); |
| 298 | + } |
| 299 | + |
| 300 | + /** |
247 | 301 | * Get all header items in a string |
248 | 302 | * |
249 | 303 | * @return String |
— | — | @@ -1113,7 +1167,7 @@ |
1114 | 1168 | } |
1115 | 1169 | $this->mNoGallery = $parserOutput->getNoGallery(); |
1116 | 1170 | $this->mHeadItems = array_merge( $this->mHeadItems, $parserOutput->getHeadItems() ); |
1117 | | - $this->addModules( $parserOutput->getResources() ); |
| 1171 | + $this->addModules( $parserOutput->getModules() ); |
1118 | 1172 | // Versioning... |
1119 | 1173 | foreach ( (array)$parserOutput->mTemplateIds as $ns => $dbks ) { |
1120 | 1174 | if ( isset( $this->mTemplateIds[$ns] ) ) { |
— | — | @@ -1549,7 +1603,8 @@ |
1550 | 1604 | $sk = $wgUser->getSkin(); |
1551 | 1605 | |
1552 | 1606 | // Add base resources |
1553 | | - $this->addModules( array( 'jquery', 'mediawiki', 'mediawiki.legacy.wikibits' ) ); |
| 1607 | + $this->addModuleScripts( array( 'jquery', 'mediawiki' ) ); |
| 1608 | + $this->addModules( array( 'mediawiki.legacy.wikibits' ) ); |
1554 | 1609 | |
1555 | 1610 | // Add site JS if enabled |
1556 | 1611 | global $wgUseSiteJs; |
— | — | @@ -2217,21 +2272,25 @@ |
2218 | 2273 | |
2219 | 2274 | return $ret; |
2220 | 2275 | } |
2221 | | - |
2222 | | - static function makeResourceLoaderLinkedScript( $skin, $modules ) { |
| 2276 | + |
| 2277 | + static function makeResourceLoaderLink( $skin, $modules, $only = null ) { |
2223 | 2278 | global $wgUser, $wgLang, $wgRequest, $wgScriptPath; |
2224 | 2279 | // TODO: Should this be a static function of ResourceLoader instead? |
2225 | 2280 | $query = array( |
2226 | 2281 | 'modules' => implode( '|', array_unique( (array) $modules ) ), |
2227 | | - 'user' => $wgUser->isLoggedIn(), |
2228 | 2282 | 'lang' => $wgLang->getCode(), |
2229 | | - 'debug' => ( |
2230 | | - $wgRequest->getVal( 'debug' ) === 'true' || |
2231 | | - ( $wgRequest->getVal( 'debug' ) !== 'false' && $wgRequest->getBool( 'debug' ) ) |
2232 | | - ), |
| 2283 | + 'debug' => $wgRequest->getBool( 'debug' ) && $wgRequest->getVal( 'debug' ) !== 'false', |
2233 | 2284 | 'skin' => $wgUser->getSkin()->getSkinName(), |
2234 | 2285 | ); |
2235 | | - return Html::linkedScript( wfAppendQuery( $wgScriptPath . '/load.php', $query ) ); |
| 2286 | + if ( isset( $only ) ) { |
| 2287 | + $query['only'] = $only; |
| 2288 | + } |
| 2289 | + // Automatically select style/script elements |
| 2290 | + if ( $only === 'styles' ) { |
| 2291 | + return Html::linkedStyle( wfAppendQuery( $wgScriptPath . '/load.php', $query ) ); |
| 2292 | + } else { |
| 2293 | + return Html::linkedScript( wfAppendQuery( $wgScriptPath . '/load.php', $query ) ); |
| 2294 | + } |
2236 | 2295 | } |
2237 | 2296 | |
2238 | 2297 | /** |
— | — | @@ -2247,22 +2306,33 @@ |
2248 | 2307 | global $wgStylePath, $wgStyleVersion; |
2249 | 2308 | |
2250 | 2309 | $scripts = ''; |
2251 | | - // Include base modules and wikibits legacy code |
2252 | | - if ( $wgRequest->getVal( 'debug' ) === 'true' || $wgRequest->getBool( 'debug' ) ) { |
2253 | | - $skipped = array(); |
2254 | | - foreach ( $this->getResources() as $name ) { |
2255 | | - $module = ResourceLoader::getModule( $name ); |
2256 | | - if ( $module->isRaw() ) { |
2257 | | - $scripts .= self::makeResourceLoaderLinkedScript( $sk, $name ); |
2258 | | - } else { |
2259 | | - $skipped[] = $name; |
2260 | | - } |
| 2310 | + // Support individual script requests in debug mode |
| 2311 | + if ( $wgRequest->getBool( 'debug' ) && $wgRequest->getVal( 'debug' ) !== 'false' ) { |
| 2312 | + foreach ( $this->getModuleStyles() as $name ) { |
| 2313 | + $scripts .= self::makeResourceLoaderLink( $sk, $name, 'styles' ); |
2261 | 2314 | } |
2262 | | - foreach ( $skipped as $name ) { |
2263 | | - $scripts .= self::makeResourceLoaderLinkedScript( $sk, $name ); |
| 2315 | + foreach ( $this->getModuleScripts() as $name ) { |
| 2316 | + $scripts .= self::makeResourceLoaderLink( $sk, $name, 'scripts' ); |
2264 | 2317 | } |
| 2318 | + foreach ( $this->getModuleMessages() as $name ) { |
| 2319 | + $scripts .= self::makeResourceLoaderLink( $sk, $name, 'messages' ); |
| 2320 | + } |
| 2321 | + foreach ( $this->getModules() as $name ) { |
| 2322 | + $scripts .= self::makeResourceLoaderLink( $sk, $name ); |
| 2323 | + } |
2265 | 2324 | } else { |
2266 | | - $scripts .= self::makeResourceLoaderLinkedScript( $sk, $this->getResources() ); |
| 2325 | + if ( count( $this->getModuleStyles() ) ) { |
| 2326 | + $scripts .= self::makeResourceLoaderLink( $sk, $this->getModuleStyles(), 'styles' ); |
| 2327 | + } |
| 2328 | + if ( count( $this->getModuleScripts() ) ) { |
| 2329 | + $scripts .= self::makeResourceLoaderLink( $sk, $this->getModuleScripts(), 'scripts' ); |
| 2330 | + } |
| 2331 | + if ( count( $this->getModuleMessages() ) ) { |
| 2332 | + $scripts .= self::makeResourceLoaderLink( $sk, $this->getModuleMessages(), 'messages' ); |
| 2333 | + } |
| 2334 | + if ( count( $this->getModules() ) ) { |
| 2335 | + $scripts .= self::makeResourceLoaderLink( $sk, $this->getModules() ); |
| 2336 | + } |
2267 | 2337 | } |
2268 | 2338 | // Configure page |
2269 | 2339 | $scripts .= Skin::makeGlobalVariablesScript( $sk->getSkinName() ) . "\n"; |
Index: branches/resourceloader/phase3/includes/ResourceLoader.php |
— | — | @@ -227,32 +227,8 @@ |
228 | 228 | } |
229 | 229 | // Use output buffering |
230 | 230 | ob_start(); |
231 | | - // Special meta-information for the 'mediawiki' module |
232 | | - if ( in_array( 'mediawiki', $modules ) ) { |
233 | | - $config = array( 'server' => $server, 'debug', 'debug' => $parameters['debug'] ); |
234 | | - echo "mediaWiki.config.set( " . FormatJson::encode( $config ) . " );\n"; |
235 | | - // Generate list of registrations and collect all loader scripts |
236 | | - $loaders = array(); |
237 | | - $registrations = array(); |
238 | | - foreach ( self::$modules as $name => $module ) { |
239 | | - $loader = $module->getLoaderScript(); |
240 | | - if ( $loader !== false ) { |
241 | | - echo $loader; |
242 | | - } else { |
243 | | - if ( !count( $module->getDependencies() ) && !in_array( $name, $missing ) ) { |
244 | | - $registrations[$name] = $name; |
245 | | - } else { |
246 | | - $registrations[$name] = array( $name, $module->getDependencies() ); |
247 | | - if ( in_array( $name, $missing ) ) { |
248 | | - $registrations[$name][] = 'missing'; |
249 | | - } |
250 | | - } |
251 | | - } |
252 | | - } |
253 | | - // Register modules without loaders |
254 | | - echo "mediaWiki.loader.register( " . FormatJson::encode( array_values( $registrations ) ) . " );\n"; |
255 | | - } |
256 | | - // Output non-raw modules |
| 231 | + // A list of registrations will be collected and appended to mediawiki script-only output |
| 232 | + $registrations = array(); |
257 | 233 | $blobs = MessageBlobStore::get( $modules, $parameters['lang'] ); |
258 | 234 | foreach ( $modules as $name ) { |
259 | 235 | $module = self::getModule( $name ); |
— | — | @@ -262,12 +238,31 @@ |
263 | 239 | if ( $includeScripts ) { |
264 | 240 | $scripts .= $module->getScript(); |
265 | 241 | if ( $parameters['debug'] ) { |
266 | | - $script .= $module->getDebugScript(); |
| 242 | + $scripts .= $module->getDebugScript(); |
267 | 243 | } |
268 | 244 | $scripts .= $module->getLanguageScript( $parameters['lang'] ); |
269 | 245 | $scripts .= $module->getSkinScript( $parameters['skin'] ); |
| 246 | + // Special meta-information for the 'mediawiki' module |
| 247 | + if ( $name === 'mediawiki' && $parameters['only'] === 'scripts' ) { |
| 248 | + $config = array( 'server' => $server, 'debug', 'debug' => $parameters['debug'] ); |
| 249 | + $scripts .= "mediaWiki.config.set( " . FormatJson::encode( $config ) . " );\n"; |
| 250 | + foreach ( self::$modules as $name => $module ) { |
| 251 | + $loader = $module->getLoaderScript(); |
| 252 | + if ( $loader !== false ) { |
| 253 | + $scripts .= $loader; |
| 254 | + } else { |
| 255 | + if ( !count( $module->getDependencies() ) && !in_array( $name, $missing ) ) { |
| 256 | + $registrations[$name] = $name; |
| 257 | + } else { |
| 258 | + $registrations[$name] = array( $name, $module->getDependencies() ); |
| 259 | + if ( in_array( $name, $missing ) ) { |
| 260 | + $registrations[$name][] = 'missing'; |
| 261 | + } |
| 262 | + } |
| 263 | + } |
| 264 | + } |
| 265 | + } |
270 | 266 | } |
271 | | - |
272 | 267 | // Styles |
273 | 268 | $styles = ''; |
274 | 269 | if ( $includeStyles ) { |
— | — | @@ -284,14 +279,20 @@ |
285 | 280 | // Messages |
286 | 281 | $messages = $includeMessages && isset( $blobs[$name] ) ? $blobs[$name] : '{}'; |
287 | 282 | // Output |
288 | | - if ( $parameters['only'] == 'styles' ) { |
| 283 | + if ( $parameters['only'] === 'styles' ) { |
289 | 284 | echo $styles; |
290 | | - } else if ( $parameters['only'] == 'messages' ) { |
| 285 | + } else if ( $parameters['only'] === 'scripts' ) { |
| 286 | + echo $scripts; |
| 287 | + } else if ( $parameters['only'] === 'messages' ) { |
291 | 288 | echo "mediaWiki.msg.set( $messages );\n"; |
292 | 289 | } else { |
293 | 290 | $styles = Xml::escapeJsString( $styles ); |
294 | 291 | echo "mediaWiki.loader.implement( '{$name}', function() {\n{$scripts}\n}, '{$styles}', {$messages} );\n"; |
295 | 292 | } |
| 293 | + if ( $includeScripts ) { |
| 294 | + // Register modules without loaders |
| 295 | + $scripts .= "mediaWiki.loader.register( " . FormatJson::encode( array_values( $registrations ) ) . " );\n"; |
| 296 | + } |
296 | 297 | } |
297 | 298 | |
298 | 299 | // Final processing |