r71661 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r71660‎ | r71661 | r71662 >
Date:21:32, 25 August 2010
Author:tparscal
Status:ok (Comments)
Tags:
Comment:
Got things working again since the addition of the only parameter
Modified paths:
  • /branches/resourceloader/phase3/includes/OutputPage.php (modified) (history)
  • /branches/resourceloader/phase3/includes/ResourceLoader.php (modified) (history)

Diff [purge]

Index: branches/resourceloader/phase3/includes/OutputPage.php
@@ -24,7 +24,7 @@
2525 var $mCategoryLinks = array(), $mCategories = array(), $mLanguageLinks = array();
2626
2727 var $mScripts = '', $mLinkColours, $mPageLinkTitle = '', $mHeadItems = array();
28 - var $mResources = array();
 28+ var $mModules = array(), $mModuleScripts = array(), $mModuleStyles = array(), $mModuleMessages = array();
2929 var $mInlineMsg = array();
3030
3131 var $mTemplateIds = array();
@@ -225,24 +225,78 @@
226226 }
227227
228228 /**
229 - * Get the list of resources to include on this page
 229+ * Get the list of modules to include on this page
230230 * @return array of module names
231231 */
232 - public function getResources() {
233 - return $this->mResources;
 232+ public function getModules() {
 233+ return $this->mModules;
234234 }
235235
236236 /**
237 - * Add a module recognized by the resource loader. Modules added
 237+ * Add one or more modules recognized by the resource loader. Modules added
238238 * through this function will be loaded by the resource loader when the
239239 * page loads.
240240 * @param $module mixed Module name (string) or array of module names
241241 */
242242 public function addModules( $modules ) {
243 - $this->mResources = array_merge( $this->mResources, (array)$modules );
 243+ $this->mModules = array_merge( $this->mModules, (array)$modules );
244244 }
245245
246246 /**
 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+ /**
247301 * Get all header items in a string
248302 *
249303 * @return String
@@ -1113,7 +1167,7 @@
11141168 }
11151169 $this->mNoGallery = $parserOutput->getNoGallery();
11161170 $this->mHeadItems = array_merge( $this->mHeadItems, $parserOutput->getHeadItems() );
1117 - $this->addModules( $parserOutput->getResources() );
 1171+ $this->addModules( $parserOutput->getModules() );
11181172 // Versioning...
11191173 foreach ( (array)$parserOutput->mTemplateIds as $ns => $dbks ) {
11201174 if ( isset( $this->mTemplateIds[$ns] ) ) {
@@ -1549,7 +1603,8 @@
15501604 $sk = $wgUser->getSkin();
15511605
15521606 // 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' ) );
15541609
15551610 // Add site JS if enabled
15561611 global $wgUseSiteJs;
@@ -2217,21 +2272,25 @@
22182273
22192274 return $ret;
22202275 }
2221 -
2222 - static function makeResourceLoaderLinkedScript( $skin, $modules ) {
 2276+
 2277+ static function makeResourceLoaderLink( $skin, $modules, $only = null ) {
22232278 global $wgUser, $wgLang, $wgRequest, $wgScriptPath;
22242279 // TODO: Should this be a static function of ResourceLoader instead?
22252280 $query = array(
22262281 'modules' => implode( '|', array_unique( (array) $modules ) ),
2227 - 'user' => $wgUser->isLoggedIn(),
22282282 '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',
22332284 'skin' => $wgUser->getSkin()->getSkinName(),
22342285 );
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+ }
22362295 }
22372296
22382297 /**
@@ -2247,22 +2306,33 @@
22482307 global $wgStylePath, $wgStyleVersion;
22492308
22502309 $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' );
22612314 }
2262 - foreach ( $skipped as $name ) {
2263 - $scripts .= self::makeResourceLoaderLinkedScript( $sk, $name );
 2315+ foreach ( $this->getModuleScripts() as $name ) {
 2316+ $scripts .= self::makeResourceLoaderLink( $sk, $name, 'scripts' );
22642317 }
 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+ }
22652324 } 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+ }
22672337 }
22682338 // Configure page
22692339 $scripts .= Skin::makeGlobalVariablesScript( $sk->getSkinName() ) . "\n";
Index: branches/resourceloader/phase3/includes/ResourceLoader.php
@@ -227,32 +227,8 @@
228228 }
229229 // Use output buffering
230230 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();
257233 $blobs = MessageBlobStore::get( $modules, $parameters['lang'] );
258234 foreach ( $modules as $name ) {
259235 $module = self::getModule( $name );
@@ -262,12 +238,31 @@
263239 if ( $includeScripts ) {
264240 $scripts .= $module->getScript();
265241 if ( $parameters['debug'] ) {
266 - $script .= $module->getDebugScript();
 242+ $scripts .= $module->getDebugScript();
267243 }
268244 $scripts .= $module->getLanguageScript( $parameters['lang'] );
269245 $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+ }
270266 }
271 -
272267 // Styles
273268 $styles = '';
274269 if ( $includeStyles ) {
@@ -284,14 +279,20 @@
285280 // Messages
286281 $messages = $includeMessages && isset( $blobs[$name] ) ? $blobs[$name] : '{}';
287282 // Output
288 - if ( $parameters['only'] == 'styles' ) {
 283+ if ( $parameters['only'] === 'styles' ) {
289284 echo $styles;
290 - } else if ( $parameters['only'] == 'messages' ) {
 285+ } else if ( $parameters['only'] === 'scripts' ) {
 286+ echo $scripts;
 287+ } else if ( $parameters['only'] === 'messages' ) {
291288 echo "mediaWiki.msg.set( $messages );\n";
292289 } else {
293290 $styles = Xml::escapeJsString( $styles );
294291 echo "mediaWiki.loader.implement( '{$name}', function() {\n{$scripts}\n}, '{$styles}', {$messages} );\n";
295292 }
 293+ if ( $includeScripts ) {
 294+ // Register modules without loaders
 295+ $scripts .= "mediaWiki.loader.register( " . FormatJson::encode( array_values( $registrations ) ) . " );\n";
 296+ }
296297 }
297298
298299 // Final processing

Comments

#Comment by Platonides (talk | contribs)   11:57, 26 August 2010

So each module needs to register itself, its js and its css in three different calls to Outputpage ?

I think this should be added with a single call passing an array with messages, scripts, style... Similarly to how makeResourceLoaderLink has a $only parameter.

#Comment by Trevor Parscal (WMF) (talk | contribs)   17:20, 26 August 2010

You are misunderstanding the design. Essentially you can either request the whole thing, or just parts of it using the "only" parameter. When loading initial CSS (like the skin styles) or JavaScript (like jquery and the mediawiki javascript libraries) the information needs to come through directly, no wrapped in loading functions as it would if it were asked for as a whole.

Status & tagging log