r72315 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r72314‎ | r72315 | r72316 >
Date:21:33, 3 September 2010
Author:tparscal
Status:ok
Tags:
Comment:
Added getHash to ResourceLoaderContext. Moved special casing of startup module to ResourceLoaderStartupModule
Modified paths:
  • /branches/resourceloader/phase3/includes/ResourceLoader.php (modified) (history)
  • /branches/resourceloader/phase3/includes/ResourceLoaderContext.php (modified) (history)
  • /branches/resourceloader/phase3/includes/ResourceLoaderModule.php (modified) (history)

Diff [purge]

Index: branches/resourceloader/phase3/includes/ResourceLoaderContext.php
@@ -24,6 +24,8 @@
2525 */
2626 class ResourceLoaderContext {
2727
 28+ /* Protected Members */
 29+
2830 protected $request;
2931 protected $server;
3032 protected $modules;
@@ -32,7 +34,10 @@
3335 protected $skin;
3436 protected $debug;
3537 protected $only;
 38+ protected $hash;
3639
 40+ /* Methods */
 41+
3742 public function __construct( WebRequest $request, $server ) {
3843 global $wgUser, $wgLang, $wgDefaultSkin;
3944
@@ -56,37 +61,53 @@
5762 $this->skin = $wgDefaultSkin;
5863 }
5964 }
 65+
6066 public function getRequest() {
6167 return $this->request;
6268 }
 69+
6370 public function getServer() {
6471 return $this->server;
6572 }
 73+
6674 public function getModules() {
6775 return $this->modules;
6876 }
 77+
6978 public function getLanguage() {
7079 return $this->language;
7180 }
 81+
7282 public function getDirection() {
7383 return $this->direction;
7484 }
 85+
7586 public function getSkin() {
7687 return $this->skin;
7788 }
 89+
7890 public function getDebug() {
7991 return $this->debug;
8092 }
 93+
8194 public function getOnly() {
8295 return $this->only;
8396 }
 97+
8498 public function shouldIncludeScripts() {
8599 return is_null( $this->only ) || $this->only === 'scripts';
86100 }
 101+
87102 public function shouldIncludeStyles() {
88103 return is_null( $this->only ) || $this->only === 'styles';
89104 }
 105+
90106 public function shouldIncludeMessages() {
91107 return is_null( $this->only ) || $this->only === 'messages';
92108 }
 109+
 110+ public function getHash() {
 111+ return isset( $this->hash ) ?
 112+ $this->hash : $this->hash = implode( '|', array( $this->language, $this->skin, $this->debug ) );
 113+ }
93114 }
\ No newline at end of file
Index: branches/resourceloader/phase3/includes/ResourceLoader.php
@@ -218,6 +218,8 @@
219219 return $time;
220220 }
221221
 222+ /* Methods */
 223+
222224 /*
223225 * Outputs a response to a resource load-request, including a content-type header
224226 *
@@ -280,38 +282,6 @@
281283 $scripts = '';
282284 if ( $context->shouldIncludeScripts() ) {
283285 $scripts .= self::$modules[$name]->getScript( $context );
284 - // Special meta-information for the 'startup' module
285 - if ( $name === 'startup' && $context->getOnly() === 'scripts' ) {
286 - // Get all module registrations
287 - $registration = self::getModuleRegistrations( $context );
288 - // Build configuration
289 - $config = FormatJson::encode(
290 - array( 'server' => $context->getServer(), 'debug' => $context->getDebug() )
291 - );
292 - // Add a well-known start-up function
293 - $scripts .= "window.startUp = function() { $registration mediaWiki.config.set( $config ); };";
294 - // Build load query for jquery and mediawiki modules
295 - $query = wfArrayToCGI(
296 - array(
297 - 'modules' => implode( '|', array( 'jquery', 'mediawiki' ) ),
298 - 'only' => 'scripts',
299 - 'lang' => $context->getLanguage(),
300 - 'dir' => $context->getDirection(),
301 - 'skin' => $context->getSkin(),
302 - 'debug' => $context->getDebug(),
303 - 'version' => wfTimestamp( TS_ISO_8601, round( max(
304 - self::$modules['jquery']->getModifiedTime( $context ),
305 - self::$modules['mediawiki']->getModifiedTime( $context )
306 - ), -2 ) )
307 - )
308 - );
309 - // Build HTML code for loading jquery and mediawiki modules
310 - $loadScript = Html::linkedScript( $context->getServer() . "?$query" );
311 - // Add code to add jquery and mediawiki loading code; only if the current client is compatible
312 - $scripts .= "if ( isCompatible() ) { document.write( '$loadScript' ); }";
313 - // Delete the compatible function - it's not needed anymore
314 - $scripts .= "delete window['isCompatible'];";
315 - }
316286 }
317287 // Styles
318288 $styles = '';
@@ -335,7 +305,7 @@
336306 echo "mediaWiki.msg.set( $messages );\n";
337307 } else {
338308 $styles = Xml::escapeJsString( $styles );
339 - echo "mediaWiki.loader.implement( '{$name}', function() {{$scripts}},\n'{$styles}',\n{$messages} );\n";
 309+ echo "mediaWiki.loader.implement( '$name', function() {{$scripts}},\n'$styles',\n$messages );\n";
340310 }
341311 }
342312 // Update the status of script-only modules
@@ -345,12 +315,12 @@
346316 $statuses[$name] = 'ready';
347317 }
348318 $statuses = FormatJson::encode( $statuses );
349 - echo "mediaWiki.loader.state( {$statuses} );";
 319+ echo "mediaWiki.loader.state( $statuses );";
350320 }
351321 // Register missing modules
352322 if ( $context->shouldIncludeScripts() ) {
353323 foreach ( $missing as $name ) {
354 - echo "mediaWiki.loader.register( '{$name}', null, 'missing' );\n";
 324+ echo "mediaWiki.loader.register( '$name', null, 'missing' );\n";
355325 }
356326 }
357327 // Output the appropriate header
Index: branches/resourceloader/phase3/includes/ResourceLoaderModule.php
@@ -22,8 +22,13 @@
2323 * Interface for resource loader modules, with name registration and maxage functionality.
2424 */
2525 abstract class ResourceLoaderModule {
26 - private $name = null;
2726
 27+ /* Protected Members */
 28+
 29+ protected $name = null;
 30+
 31+ /* Methods */
 32+
2833 /**
2934 * Get this module's name. This is set when the module is registered
3035 * with ResourceLoader::register()
@@ -130,23 +135,26 @@
131136 * Module based on local JS/CSS files. This is the most common type of module.
132137 */
133138 class ResourceLoaderFileModule extends ResourceLoaderModule {
134 - private $scripts = array();
135 - private $styles = array();
136 - private $messages = array();
137 - private $dependencies = array();
138 - private $debugScripts = array();
139 - private $languageScripts = array();
140 - private $skinScripts = array();
141 - private $skinStyles = array();
142 - private $loaders = array();
143 - private $parameters = array();
144139
 140+ /* Protected Members */
 141+
 142+ protected $scripts = array();
 143+ protected $styles = array();
 144+ protected $messages = array();
 145+ protected $dependencies = array();
 146+ protected $debugScripts = array();
 147+ protected $languageScripts = array();
 148+ protected $skinScripts = array();
 149+ protected $skinStyles = array();
 150+ protected $loaders = array();
 151+ protected $parameters = array();
 152+
145153 // In-object cache for file dependencies
146 - private $fileDeps = array();
 154+ protected $fileDeps = array();
147155 // In-object cache for mtime
148 - private $modifiedTime = array();
 156+ protected $modifiedTime = array();
149157
150 - /* Public methods */
 158+ /* Methods */
151159
152160 /**
153161 * Construct a new module from an options array.
@@ -384,9 +392,8 @@
385393 * @return int UNIX timestamp
386394 */
387395 public function getModifiedTime( ResourceLoaderContext $context ) {
388 - $hash = implode( '|', array( $context->getLanguage(), $context->getSkin(), $context->getDebug() ) );
389 - if ( isset( $this->modifiedTime[$hash] ) ) {
390 - return $this->modifiedTime[$hash];
 396+ if ( isset( $this->modifiedTime[$context->getHash()] ) ) {
 397+ return $this->modifiedTime[$context->getHash()];
391398 }
392399
393400 $files = array_merge(
@@ -400,12 +407,14 @@
401408 $this->loaders,
402409 $this->getFileDependencies( $context->getSkin() )
403410 );
404 - $this->modifiedTime[$hash] = max(
 411+ $this->modifiedTime[$context->getHash()] = max(
405412 array_map( 'filemtime', array_map( array( __CLASS__, 'remapFilename' ), $files ) )
406413 );
407 - return $this->modifiedTime[$hash];
 414+ return $this->modifiedTime[$context->getHash()];
408415 }
409416
 417+ /* Protected Members */
 418+
410419 /**
411420 * Get the primary JS for this module. This is pulled from the
412421 * script files added through addScripts()
@@ -558,17 +567,21 @@
559568 * TODO: Add Site CSS functionality too
560569 */
561570 class ResourceLoaderSiteModule extends ResourceLoaderModule {
 571+
 572+ /* Protected Members */
 573+
562574 // In-object cache for modified time
563 - private $modifiedTime = null;
 575+ protected $modifiedTime = null;
564576
 577+ /* Methods */
 578+
565579 public function getScript( ResourceLoaderContext $context ) {
566580 return Skin::newFromKey( $context->getSkin() )->generateUserJs();
567581 }
568582
569583 public function getModifiedTime( ResourceLoaderContext $context ) {
570 - $hash = implode( '|', array( $context->getLanguage(), $context->getSkin(), $context->getDebug() ) );
571 - if ( isset( $this->modifiedTime[$hash] ) ) {
572 - return $this->modifiedTime[$hash];
 584+ if ( isset( $this->modifiedTime[$context->getHash()] ) ) {
 585+ return $this->modifiedTime[$context->getHash()];
573586 }
574587
575588 // HACK: We duplicate the message names from generateUserJs()
@@ -600,11 +613,49 @@
601614
602615
603616 class ResourceLoaderStartUpModule extends ResourceLoaderModule {
604 - private $modifiedTime = null;
605617
 618+ /* Protected Members */
 619+
 620+ protected $modifiedTime = null;
 621+
 622+ /* Methods */
 623+
606624 public function getScript( ResourceLoaderContext $context ) {
607625 global $IP;
608 - return file_get_contents( "$IP/resources/startup.js" );
 626+
 627+ $scripts = file_get_contents( "$IP/resources/startup.js" );
 628+ if ( $context->getOnly() === 'scripts' ) {
 629+ // Get all module registrations
 630+ $registration = ResourceLoader::getModuleRegistrations( $context );
 631+ // Build configuration
 632+ $config = FormatJson::encode(
 633+ array( 'server' => $context->getServer(), 'debug' => $context->getDebug() )
 634+ );
 635+ // Add a well-known start-up function
 636+ $scripts .= "window.startUp = function() { $registration mediaWiki.config.set( $config ); };";
 637+ // Build load query for jquery and mediawiki modules
 638+ $query = wfArrayToCGI(
 639+ array(
 640+ 'modules' => implode( '|', array( 'jquery', 'mediawiki' ) ),
 641+ 'only' => 'scripts',
 642+ 'lang' => $context->getLanguage(),
 643+ 'dir' => $context->getDirection(),
 644+ 'skin' => $context->getSkin(),
 645+ 'debug' => $context->getDebug(),
 646+ 'version' => wfTimestamp( TS_ISO_8601, round( max(
 647+ ResourceLoader::getModule( 'jquery' )->getModifiedTime( $context ),
 648+ ResourceLoader::getModule( 'mediawiki' )->getModifiedTime( $context )
 649+ ), -2 ) )
 650+ )
 651+ );
 652+ // Build HTML code for loading jquery and mediawiki modules
 653+ $loadScript = Html::linkedScript( $context->getServer() . "?$query" );
 654+ // Add code to add jquery and mediawiki loading code; only if the current client is compatible
 655+ $scripts .= "if ( isCompatible() ) { document.write( '$loadScript' ); }";
 656+ // Delete the compatible function - it's not needed anymore
 657+ $scripts .= "delete window['isCompatible'];";
 658+ }
 659+ return $scripts;
609660 }
610661
611662 public function getModifiedTime( ResourceLoaderContext $context ) {

Status & tagging log