r73971 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r73970‎ | r73971 | r73972 >
Date:19:04, 29 September 2010
Author:tparscal
Status:resolved (Comments)
Tags:
Comment:
Made ResourceLoader an instantiable object, rather than a static one, making it more modular and testable.
Modified paths:
  • /trunk/extensions/ArticleEmblems/ArticleEmblems.hooks.php (modified) (history)
  • /trunk/extensions/ClickTracking/ClickTracking.hooks.php (modified) (history)
  • /trunk/extensions/PrefSwitch/PrefSwitch.hooks.php (modified) (history)
  • /trunk/extensions/ProofreadPage/ProofreadPage_body.php (modified) (history)
  • /trunk/extensions/Translate/Translate.php (modified) (history)
  • /trunk/extensions/Vector/Vector.hooks.php (modified) (history)
  • /trunk/extensions/WikiEditor/WikiEditor.hooks.php (modified) (history)
  • /trunk/phase3/includes/MessageBlobStore.php (modified) (history)
  • /trunk/phase3/includes/OutputPage.php (modified) (history)
  • /trunk/phase3/includes/ResourceLoader.php (modified) (history)
  • /trunk/phase3/includes/ResourceLoaderContext.php (modified) (history)
  • /trunk/phase3/includes/ResourceLoaderModule.php (modified) (history)
  • /trunk/phase3/load.php (modified) (history)
  • /trunk/phase3/maintenance/tests/phpunit/includes/ResourceLoaderTest.php (modified) (history)

Diff [purge]

Index: trunk/phase3/maintenance/tests/phpunit/includes/ResourceLoaderTest.php
@@ -1,6 +1,7 @@
22 <?php
33
44 class ResourceLoaderTest extends PHPUnit_Framework_TestCase {
 5+
56 /* Provider Methods */
67
78 public function provide() {
Index: trunk/phase3/includes/ResourceLoaderContext.php
@@ -30,6 +30,7 @@
3131
3232 /* Protected Members */
3333
 34+ protected $resourceLoader;
3435 protected $request;
3536 protected $modules;
3637 protected $language;
@@ -43,9 +44,10 @@
4445
4546 /* Methods */
4647
47 - public function __construct( WebRequest $request ) {
 48+ public function __construct( ResourceLoader $resourceLoader, WebRequest $request ) {
4849 global $wgLang, $wgDefaultSkin, $wgResourceLoaderDebug;
4950
 51+ $this->resourceLoader = $resourceLoader;
5052 $this->request = $request;
5153 // Interperet request
5254 $this->modules = explode( '|', $request->getVal( 'modules' ) );
@@ -71,6 +73,10 @@
7274 }
7375 }
7476
 77+ public function getResourceLoader() {
 78+ return $this->resourceLoader;
 79+ }
 80+
7581 public function getRequest() {
7682 return $this->request;
7783 }
Index: trunk/phase3/includes/OutputPage.php
@@ -25,6 +25,7 @@
2626
2727 var $mScripts = '', $mLinkColours, $mPageLinkTitle = '', $mHeadItems = array();
2828 var $mModules = array(), $mModuleScripts = array(), $mModuleStyles = array(), $mModuleMessages = array();
 29+ var $mResourceLoader;
2930 var $mInlineMsg = array();
3031
3132 var $mTemplateIds = array();
@@ -2281,9 +2282,13 @@
22822283 }
22832284
22842285 // TODO: Document
2285 - static function makeResourceLoaderLink( $skin, $modules, $only, $useESI = false ) {
 2286+ protected function makeResourceLoaderLink( $skin, $modules, $only, $useESI = false ) {
22862287 global $wgUser, $wgLang, $wgRequest, $wgLoadScript, $wgResourceLoaderDebug, $wgResourceLoaderUseESI,
22872288 $wgResourceLoaderInlinePrivateModules;
 2289+ // Lazy-load ResourceLoader
 2290+ if ( is_null( $this->mResourceLoader ) ) {
 2291+ $this->mResourceLoader = new ResourceLoader();
 2292+ }
22882293 // TODO: Should this be a static function of ResourceLoader instead?
22892294 // TODO: Divide off modules starting with "user", and add the user parameter to them
22902295 $query = array(
@@ -2299,7 +2304,7 @@
23002305 // Create keyed-by-group list of module objects from modules list
23012306 $groups = array();
23022307 foreach ( (array) $modules as $name ) {
2303 - $module = ResourceLoader::getModule( $name );
 2308+ $module = $this->mResourceLoader->getModule( $name );
23042309 $group = $module->getGroup();
23052310 if ( !isset( $groups[$group] ) ) {
23062311 $groups[$group] = array();
@@ -2315,17 +2320,17 @@
23162321 }
23172322 // Support inlining of private modules if configured as such
23182323 if ( $group === 'private' && $wgResourceLoaderInlinePrivateModules ) {
2319 - $context = new ResourceLoaderContext( new FauxRequest( $query ) );
 2324+ $context = new ResourceLoaderContext( $this->mResourceLoader, new FauxRequest( $query ) );
23202325 if ( $only == 'styles' ) {
23212326 $links .= Html::inlineStyle(
23222327 ResourceLoader::makeLoaderConditionalScript(
2323 - ResourceLoader::makeModuleResponse( $context, $modules )
 2328+ $this->mResourceLoader->makeModuleResponse( $context, $modules )
23242329 )
23252330 );
23262331 } else {
23272332 $links .= Html::inlineScript(
23282333 ResourceLoader::makeLoaderConditionalScript(
2329 - ResourceLoader::makeModuleResponse( $context, $modules )
 2334+ $this->mResourceLoader->makeModuleResponse( $context, $modules )
23302335 )
23312336 );
23322337 }
@@ -2336,7 +2341,7 @@
23372342 // we can ensure cache misses on change
23382343 if ( $group === 'user' || $group === 'site' ) {
23392344 // Create a fake request based on the one we are about to make so modules return correct times
2340 - $context = new ResourceLoaderContext( new FauxRequest( $query ) );
 2345+ $context = new ResourceLoaderContext( $this->mResourceLoader, new FauxRequest( $query ) );
23412346 // Get the maximum timestamp
23422347 $timestamp = 0;
23432348 foreach ( $modules as $module ) {
@@ -2380,7 +2385,7 @@
23812386 global $wgUser, $wgRequest, $wgUseSiteJs, $wgResourceLoaderDebug;
23822387
23832388 // Startup - this will immediately load jquery and mediawiki modules
2384 - $scripts = self::makeResourceLoaderLink( $sk, 'startup', 'scripts', true );
 2389+ $scripts = $this->makeResourceLoaderLink( $sk, 'startup', 'scripts', true );
23852390
23862391 // Configuration -- This could be merged together with the load and go, but makeGlobalVariablesScript returns a
23872392 // whole script tag -- grumble grumble...
@@ -2390,20 +2395,20 @@
23912396 if ( $wgRequest->getFuzzyBool( 'debug', $wgResourceLoaderDebug ) ) {
23922397 // Scripts
23932398 foreach ( $this->getModuleScripts() as $name ) {
2394 - $scripts .= self::makeResourceLoaderLink( $sk, $name, 'scripts' );
 2399+ $scripts .= $this->makeResourceLoaderLink( $sk, $name, 'scripts' );
23952400 }
23962401 // Messages
23972402 foreach ( $this->getModuleMessages() as $name ) {
2398 - $scripts .= self::makeResourceLoaderLink( $sk, $name, 'messages' );
 2403+ $scripts .= $this->makeResourceLoaderLink( $sk, $name, 'messages' );
23992404 }
24002405 } else {
24012406 // Scripts
24022407 if ( count( $this->getModuleScripts() ) ) {
2403 - $scripts .= self::makeResourceLoaderLink( $sk, $this->getModuleScripts(), 'scripts' );
 2408+ $scripts .= $this->makeResourceLoaderLink( $sk, $this->getModuleScripts(), 'scripts' );
24042409 }
24052410 // Messages
24062411 if ( count( $this->getModuleMessages() ) ) {
2407 - $scripts .= self::makeResourceLoaderLink( $sk, $this->getModuleMessages(), 'messages' );
 2412+ $scripts .= $this->makeResourceLoaderLink( $sk, $this->getModuleMessages(), 'messages' );
24082413 }
24092414 }
24102415
@@ -2423,18 +2428,18 @@
24242429 # XXX: additional security check/prompt?
24252430 $this->addInlineScript( $wgRequest->getText( 'wpTextbox1' ) );
24262431 } else {
2427 - $scripts .= self::makeResourceLoaderLink( $sk, array( 'user', 'user.options' ), 'scripts' );
 2432+ $scripts .= $this->makeResourceLoaderLink( $sk, array( 'user', 'user.options' ), 'scripts' );
24282433 $userOptionsAdded = true;
24292434 }
24302435 }
24312436 if ( !$userOptionsAdded ) {
2432 - $scripts .= self::makeResourceLoaderLink( $sk, 'user.options', 'scripts' );
 2437+ $scripts .= $this->makeResourceLoaderLink( $sk, 'user.options', 'scripts' );
24332438 }
24342439 $scripts .= "\n" . $this->mScripts;
24352440
24362441 // Add site JS if enabled
24372442 if ( $wgUseSiteJs ) {
2438 - $scripts .= self::makeResourceLoaderLink( $sk, 'site', 'scripts' );
 2443+ $scripts .= $this->makeResourceLoaderLink( $sk, 'site', 'scripts' );
24392444 }
24402445
24412446 return $scripts;
@@ -2558,11 +2563,11 @@
25592564 // Support individual script requests in debug mode
25602565 if ( $wgRequest->getFuzzyBool( 'debug', $wgResourceLoaderDebug ) ) {
25612566 foreach ( $this->getModuleStyles() as $name ) {
2562 - $tags[] = self::makeResourceLoaderLink( $sk, $name, 'styles' );
 2567+ $tags[] = $this->makeResourceLoaderLink( $sk, $name, 'styles' );
25632568 }
25642569 } else {
25652570 if ( count( $this->getModuleStyles() ) ) {
2566 - $tags[] = self::makeResourceLoaderLink( $sk, $this->getModuleStyles(), 'styles' );
 2571+ $tags[] = $this->makeResourceLoaderLink( $sk, $this->getModuleStyles(), 'styles' );
25672572 }
25682573 }
25692574
Index: trunk/phase3/includes/ResourceLoader.php
@@ -30,30 +30,11 @@
3131 /* Protected Static Members */
3232
3333 // @var array list of module name/ResourceLoaderModule object pairs
34 - protected static $modules = array();
35 - protected static $initialized = false;
 34+ protected $modules = array();
3635
37 - /* Protected Static Methods */
38 -
39 - /**
40 - * Registers core modules and runs registration hooks
41 - */
42 - protected static function initialize() {
43 - global $IP;
44 -
45 - // Safety check - this should never be called more than once
46 - if ( !self::$initialized ) {
47 - wfProfileIn( __METHOD__ );
48 - // This needs to be first, because hooks might call ResourceLoader
49 - // public interfaces which will call this
50 - self::$initialized = true;
51 - self::register( include( "$IP/resources/Resources.php" ) );
52 - wfRunHooks( 'ResourceLoaderRegisterModules' );
53 - wfProfileOut( __METHOD__ );
54 - }
55 - }
 36+ /* Protected Methods */
5637
57 - /*
 38+ /**
5839 * Loads information stored in the database about modules
5940 *
6041 * This is not inside the module code because it's so much more performant to request all of the information at once
@@ -62,7 +43,7 @@
6344 * @param $modules array list of module names to preload information for
6445 * @param $context ResourceLoaderContext context to load the information within
6546 */
66 - protected static function preloadModuleInfo( array $modules, ResourceLoaderContext $context ) {
 47+ protected function preloadModuleInfo( array $modules, ResourceLoaderContext $context ) {
6748 $dbr = wfGetDb( DB_SLAVE );
6849 $skin = $context->getSkin();
6950 $lang = $context->getLanguage();
@@ -76,21 +57,21 @@
7758
7859 $modulesWithDeps = array();
7960 foreach ( $res as $row ) {
80 - self::$modules[$row->md_module]->setFileDependencies( $skin,
 61+ $this->modules[$row->md_module]->setFileDependencies( $skin,
8162 FormatJson::decode( $row->md_deps, true )
8263 );
8364 $modulesWithDeps[] = $row->md_module;
8465 }
8566 // Register the absence of a dependencies row too
8667 foreach ( array_diff( $modules, $modulesWithDeps ) as $name ) {
87 - self::$modules[$name]->setFileDependencies( $skin, array() );
 68+ $this->modules[$name]->setFileDependencies( $skin, array() );
8869 }
8970
9071 // Get message blob mtimes. Only do this for modules with messages
9172 $modulesWithMessages = array();
9273 $modulesWithoutMessages = array();
9374 foreach ( $modules as $name ) {
94 - if ( count( self::$modules[$name]->getMessages() ) ) {
 75+ if ( count( $this->modules[$name]->getMessages() ) ) {
9576 $modulesWithMessages[] = $name;
9677 } else {
9778 $modulesWithoutMessages[] = $name;
@@ -103,11 +84,11 @@
10485 ), __METHOD__
10586 );
10687 foreach ( $res as $row ) {
107 - self::$modules[$row->mr_resource]->setMsgBlobMtime( $lang, $row->mr_timestamp );
 88+ $this->modules[$row->mr_resource]->setMsgBlobMtime( $lang, $row->mr_timestamp );
10889 }
10990 }
11091 foreach ( $modulesWithoutMessages as $name ) {
111 - self::$modules[$name]->setMsgBlobMtime( $lang, 0 );
 92+ $this->modules[$name]->setMsgBlobMtime( $lang, 0 );
11293 }
11394 }
11495
@@ -119,7 +100,7 @@
120101 * @param $file String: path to file being filtered, (optional: only required for CSS to resolve paths)
121102 * @return String: filtered data
122103 */
123 - protected static function filter( $filter, $data ) {
 104+ protected function filter( $filter, $data ) {
124105 global $wgMemc;
125106 wfProfileIn( __METHOD__ );
126107
@@ -166,9 +147,25 @@
167148 return $result;
168149 }
169150
170 - /* Static Methods */
 151+ /* Methods */
171152
172153 /**
 154+ * Registers core modules and runs registration hooks
 155+ */
 156+ public function __construct() {
 157+ global $IP;
 158+
 159+ wfProfileIn( __METHOD__ );
 160+
 161+ // Register core modules
 162+ $this->register( include( "$IP/resources/Resources.php" ) );
 163+ // Register extension modules
 164+ wfRunHooks( 'ResourceLoaderRegisterModules', array( &$this ) );
 165+
 166+ wfProfileOut( __METHOD__ );
 167+ }
 168+
 169+ /**
173170 * Registers a module with the ResourceLoader system.
174171 *
175172 * Note that registering the same object under multiple names is not supported
@@ -184,14 +181,13 @@
185182 * happened, but in bringing errors to the client in a way that they can
186183 * easily see them if they want to, such as by using FireBug
187184 */
188 - public static function register( $name, ResourceLoaderModule $object = null ) {
 185+ public function register( $name, ResourceLoaderModule $object = null ) {
189186 wfProfileIn( __METHOD__ );
190 - self::initialize();
191187
192188 // Allow multiple modules to be registered in one call
193189 if ( is_array( $name ) && !isset( $object ) ) {
194190 foreach ( $name as $key => $value ) {
195 - self::register( $key, $value );
 191+ $this->register( $key, $value );
196192 }
197193
198194 wfProfileOut( __METHOD__ );
@@ -199,13 +195,14 @@
200196 }
201197
202198 // Disallow duplicate registrations
203 - if ( isset( self::$modules[$name] ) ) {
 199+ if ( isset( $this->modules[$name] ) ) {
204200 // A module has already been registered by this name
205201 throw new MWException( 'Another module has already been registered as ' . $name );
206202 }
207203 // Attach module
208 - self::$modules[$name] = $object;
 204+ $this->modules[$name] = $object;
209205 $object->setName( $name );
 206+
210207 wfProfileOut( __METHOD__ );
211208 }
212209
@@ -214,11 +211,8 @@
215212 *
216213 * @return Array: array( modulename => ResourceLoaderModule )
217214 */
218 - public static function getModules() {
219 -
220 - self::initialize();
221 -
222 - return self::$modules;
 215+ public function getModules() {
 216+ return $this->modules;
223217 }
224218
225219 /**
@@ -227,51 +221,27 @@
228222 * @param $name String: module name
229223 * @return mixed ResourceLoaderModule or null if not registered
230224 */
231 - public static function getModule( $name ) {
232 -
233 - self::initialize();
234 -
235 - return isset( self::$modules[$name] ) ? self::$modules[$name] : null;
 225+ public function getModule( $name ) {
 226+ return isset( $this->modules[$name] ) ? $this->modules[$name] : null;
236227 }
237228
238229 /**
239 - * Get the highest modification time of all modules, based on a given
240 - * combination of language code, skin name and debug mode flag.
241 - *
242 - * @param $context ResourceLoaderContext object
243 - * @return Integer: UNIX timestamp
244 - */
245 - public static function getHighestModifiedTime( ResourceLoaderContext $context ) {
246 -
247 - self::initialize();
248 -
249 - $time = 1; // wfTimestamp() treats 0 as 'now', so that's not a suitable choice
250 -
251 - foreach ( self::$modules as $module ) {
252 - $time = max( $time, $module->getModifiedTime( $context ) );
253 - }
254 -
255 - return $time;
256 - }
257 -
258 - /**
259230 * Outputs a response to a resource load-request, including a content-type header
260231 *
261232 * @param $context ResourceLoaderContext object
262233 */
263 - public static function respond( ResourceLoaderContext $context ) {
 234+ public function respond( ResourceLoaderContext $context ) {
264235 global $wgResourceLoaderMaxage;
265236
266237 wfProfileIn( __METHOD__ );
267 - self::initialize();
268238
269239 // Split requested modules into two groups, modules and missing
270240 $modules = array();
271241 $missing = array();
272242
273243 foreach ( $context->getModules() as $name ) {
274 - if ( isset( self::$modules[$name] ) ) {
275 - $modules[$name] = self::$modules[$name];
 244+ if ( isset( $this->modules[$name] ) ) {
 245+ $modules[$name] = $this->modules[$name];
276246 } else {
277247 $missing[] = $name;
278248 }
@@ -291,7 +261,7 @@
292262 }
293263
294264 // Preload information needed to the mtime calculation below
295 - self::preloadModuleInfo( array_keys( $modules ), $context );
 265+ $this->preloadModuleInfo( array_keys( $modules ), $context );
296266
297267 // To send Last-Modified and support If-Modified-Since, we need to detect
298268 // the last modified time
@@ -326,10 +296,10 @@
327297 wfProfileOut( __METHOD__ );
328298 }
329299
330 - public static function makeModuleResponse( ResourceLoaderContext $context, array $modules, $missing = null ) {
 300+ public function makeModuleResponse( ResourceLoaderContext $context, array $modules, $missing = null ) {
331301 // Pre-fetch blobs
332302 $blobs = $context->shouldIncludeMessages() ?
333 - MessageBlobStore::get( array_keys( $modules ), $context->getLanguage() ) : array();
 303+ MessageBlobStore::get( $modules, $context->getLanguage() ) : array();
334304
335305 // Generate output
336306 $out = '';
@@ -349,9 +319,9 @@
350320 ( count( $styles = $module->getStyles( $context ) ) )
351321 ) {
352322 // Flip CSS on a per-module basis
353 - if ( self::$modules[$name]->getFlip( $context ) ) {
 323+ if ( $this->modules[$name]->getFlip( $context ) ) {
354324 foreach ( $styles as $media => $style ) {
355 - $styles[$media] = self::filter( 'flip-css', $style );
 325+ $styles[$media] = $this->filter( 'flip-css', $style );
356326 }
357327 }
358328 }
@@ -374,7 +344,7 @@
375345 // Minify CSS before embedding in mediaWiki.loader.implement call (unless in debug mode)
376346 if ( !$context->getDebug() ) {
377347 foreach ( $styles as $media => $style ) {
378 - $styles[$media] = self::filter( 'minify-css', $style );
 348+ $styles[$media] = $this->filter( 'minify-css', $style );
379349 }
380350 }
381351 $out .= self::makeLoaderImplementScript( $name, $scripts, $styles, $messages );
@@ -400,14 +370,14 @@
401371 return $out;
402372 } else {
403373 if ( $context->getOnly() === 'styles' ) {
404 - return self::filter( 'minify-css', $out );
 374+ return $this->filter( 'minify-css', $out );
405375 } else {
406 - return self::filter( 'minify-js', $out );
 376+ return $this->filter( 'minify-js', $out );
407377 }
408378 }
409379 }
410380
411 - // Client code generation methods
 381+ /* Static Methods */
412382
413383 public static function makeLoaderImplementScript( $name, $scripts, $styles, $messages ) {
414384 if ( is_array( $scripts ) ) {
Index: trunk/phase3/includes/MessageBlobStore.php
@@ -31,7 +31,7 @@
3232 class MessageBlobStore {
3333 /**
3434 * Get the message blobs for a set of modules
35 - * @param $modules array Array of module names
 35+ * @param $modules array Array of module objects keyed by module name
3636 * @param $lang string Language code
3737 * @return array An array mapping module names to message blobs
3838 */
@@ -43,14 +43,14 @@
4444 return array();
4545 }
4646 // Try getting from the DB first
47 - $blobs = self::getFromDB( $modules, $lang );
 47+ $blobs = self::getFromDB( array_keys( $modules ), $lang );
4848
4949 // Generate blobs for any missing modules and store them in the DB
50 - $missing = array_diff( $modules, array_keys( $blobs ) );
51 - foreach ( $missing as $module ) {
52 - $blob = self::insertMessageBlob( $module, $lang );
 50+ $missing = array_diff( array_keys( $modules ), array_keys( $blobs ) );
 51+ foreach ( $missing as $name ) {
 52+ $blob = self::insertMessageBlob( $name, $modules[$name], $lang );
5353 if ( $blob ) {
54 - $blobs[$module] = $blob;
 54+ $blobs[$name] = $blob;
5555 }
5656 }
5757
@@ -66,8 +66,8 @@
6767 * @param $lang string Language code
6868 * @return mixed Message blob or false if the module has no messages
6969 */
70 - public static function insertMessageBlob( $module, $lang ) {
71 - $blob = self::generateMessageBlob( $module, $lang );
 70+ public static function insertMessageBlob( $name, ResourceLoaderModule $module, $lang ) {
 71+ $blob = self::generateMessageBlob( $name, $module, $lang );
7272
7373 if ( !$blob ) {
7474 return false;
@@ -76,7 +76,7 @@
7777 $dbw = wfGetDB( DB_MASTER );
7878 $success = $dbw->insert( 'msg_resource', array(
7979 'mr_lang' => $lang,
80 - 'mr_resource' => $module,
 80+ 'mr_resource' => $name,
8181 'mr_blob' => $blob,
8282 'mr_timestamp' => $dbw->timestamp()
8383 ),
@@ -88,7 +88,7 @@
8989 if ( $dbw->affectedRows() == 0 ) {
9090 // Blob was already present, fetch it
9191 $blob = $dbw->selectField( 'msg_resource', 'mr_blob', array(
92 - 'mr_resource' => $module,
 92+ 'mr_resource' => $name,
9393 'mr_lang' => $lang,
9494 ),
9595 __METHOD__
@@ -96,11 +96,10 @@
9797 } else {
9898 // Update msg_resource_links
9999 $rows = array();
100 - $mod = ResourceLoader::getModule( $module );
101100
102 - foreach ( $mod->getMessages() as $key ) {
 101+ foreach ( $module->getMessages() as $key ) {
103102 $rows[] = array(
104 - 'mrl_resource' => $module,
 103+ 'mrl_resource' => $name,
105104 'mrl_message' => $key
106105 );
107106 }
@@ -120,14 +119,14 @@
121120 * @return mixed If $lang is set, the new message blob for that language is
122121 * returned if present. Otherwise, null is returned.
123122 */
124 - public static function updateModule( $module, $lang = null ) {
 123+ public static function updateModule( $name, ResourceLoaderModule $module, $lang = null ) {
125124 $retval = null;
126125
127126 // Find all existing blobs for this module
128127 $dbw = wfGetDB( DB_MASTER );
129128 $res = $dbw->select( 'msg_resource',
130129 array( 'mr_lang', 'mr_blob' ),
131 - array( 'mr_resource' => $module ),
 130+ array( 'mr_resource' => $name ),
132131 __METHOD__
133132 );
134133
@@ -139,13 +138,13 @@
140139
141140 foreach ( $res as $row ) {
142141 $oldBlob = $row->mr_blob;
143 - $newBlob = self::generateMessageBlob( $module, $row->mr_lang );
 142+ $newBlob = self::generateMessageBlob( $name, $module, $row->mr_lang );
144143
145144 if ( $row->mr_lang === $lang ) {
146145 $retval = $newBlob;
147146 }
148147 $newRows[] = array(
149 - 'mr_resource' => $module,
 148+ 'mr_resource' => $name,
150149 'mr_lang' => $row->mr_lang,
151150 'mr_blob' => $newBlob,
152151 'mr_timestamp' => $now
@@ -166,7 +165,7 @@
167166 // Delete removed messages, insert added ones
168167 if ( $removed ) {
169168 $dbw->delete( 'msg_resource_links', array(
170 - 'mrl_resource' => $module,
 169+ 'mrl_resource' => $name,
171170 'mrl_message' => $removed
172171 ), __METHOD__
173172 );
@@ -176,7 +175,7 @@
177176
178177 foreach ( $added as $message ) {
179178 $newLinksRows[] = array(
180 - 'mrl_resource' => $module,
 179+ 'mrl_resource' => $name,
181180 'mrl_message' => $message
182181 );
183182 }
@@ -343,11 +342,10 @@
344343 * @param $lang string Language code
345344 * @return string JSON object
346345 */
347 - private static function generateMessageBlob( $module, $lang ) {
348 - $mod = ResourceLoader::getModule( $module );
 346+ private static function generateMessageBlob( $name, ResourceLoaderModule $module, $lang ) {
349347 $messages = array();
350348
351 - foreach ( $mod->getMessages() as $key ) {
 349+ foreach ( $module->getMessages() as $key ) {
352350 $messages[$key] = wfMsgExt( $key, array( 'language' => $lang ) );
353351 }
354352
Index: trunk/phase3/includes/ResourceLoaderModule.php
@@ -1054,7 +1054,7 @@
10551055
10561056 $out = '';
10571057 $registrations = array();
1058 - foreach ( ResourceLoader::getModules() as $name => $module ) {
 1058+ foreach ( $context->getResourceLoader()->getModules() as $name => $module ) {
10591059 // Support module loader scripts
10601060 if ( ( $loader = $module->getLoaderScript() ) !== false ) {
10611061 $deps = FormatJson::encode( $module->getDependencies() );
@@ -1104,8 +1104,8 @@
11051105 'skin' => $context->getSkin(),
11061106 'debug' => $context->getDebug() ? 'true' : 'false',
11071107 'version' => wfTimestamp( TS_ISO_8601, round( max(
1108 - ResourceLoader::getModule( 'jquery' )->getModifiedTime( $context ),
1109 - ResourceLoader::getModule( 'mediawiki' )->getModifiedTime( $context )
 1108+ $context->getResourceLoader()->getModule( 'jquery' )->getModifiedTime( $context ),
 1109+ $context->getResourceLoader()->getModule( 'mediawiki' )->getModifiedTime( $context )
11101110 ), -2 ) )
11111111 );
11121112 // Ensure uniform query order
@@ -1132,10 +1132,14 @@
11331133 return $this->modifiedTime[$hash];
11341134 }
11351135 $this->modifiedTime[$hash] = filemtime( "$IP/resources/startup.js" );
1136 -
 1136+
11371137 // ATTENTION!: Because of the line above, this is not going to cause infinite recursion - think carefully
11381138 // before making changes to this code!
1139 - return $this->modifiedTime[$hash] = ResourceLoader::getHighestModifiedTime( $context );
 1139+ $time = 1; // wfTimestamp() treats 0 as 'now', so that's not a suitable choice
 1140+ foreach ( $context->getResourceLoader()->getModules() as $module ) {
 1141+ $time = max( $time, $module->getModifiedTime( $context ) );
 1142+ }
 1143+ return $this->modifiedTime[$hash] = $time;
11401144 }
11411145
11421146 public function getFlip( $context ) {
Index: trunk/phase3/load.php
@@ -45,7 +45,8 @@
4646 }
4747
4848 // Respond to resource loading request
49 -ResourceLoader::respond( new ResourceLoaderContext( $wgRequest ) );
 49+$resourceLoader = new ResourceLoader();
 50+$resourceLoader->respond( new ResourceLoaderContext( $resourceLoader, $wgRequest ) );
5051
5152 wfProfileOut( 'load.php' );
5253 wfLogProfilingData();
Index: trunk/extensions/WikiEditor/WikiEditor.hooks.php
@@ -687,9 +687,9 @@
688688 *
689689 * Adds modules to ResourceLoader
690690 */
691 - public static function resourceLoaderRegisterModules() {
 691+ public static function resourceLoaderRegisterModules( &$resourceLoader ) {
692692 foreach ( self::$modules as $name => $resources ) {
693 - ResourceLoader::register( $name, new ResourceLoaderFileModule( $resources ) );
 693+ $resourceLoader->register( $name, new ResourceLoaderFileModule( $resources ) );
694694 }
695695 return true;
696696 }
Index: trunk/extensions/Vector/Vector.hooks.php
@@ -212,9 +212,9 @@
213213 *
214214 * Adds modules to ResourceLoader
215215 */
216 - public static function resourceLoaderRegisterModules() {
 216+ public static function resourceLoaderRegisterModules( &$resourceLoader ) {
217217 foreach ( self::$modules as $name => $resources ) {
218 - ResourceLoader::register( $name, new ResourceLoaderFileModule( $resources ) );
 218+ $resourceLoader->register( $name, new ResourceLoaderFileModule( $resources ) );
219219 }
220220 return true;
221221 }
Index: trunk/extensions/ProofreadPage/ProofreadPage_body.php
@@ -71,8 +71,8 @@
7272
7373 }
7474
75 - public static function resourceLoaderRegisterModules() {
76 - ResourceLoader::register(
 75+ public static function resourceLoaderRegisterModules( &$resourceLoader ) {
 76+ $resourceLoader->register(
7777 'ext.proofreadpage.page',
7878 new ResourceLoaderFileModule(
7979 array(
@@ -93,7 +93,7 @@
9494 )
9595 );
9696
97 - ResourceLoader::register(
 97+ $resourceLoader->register(
9898 'ext.proofreadpage.article',
9999 new ResourceLoaderFileModule(
100100 array(
@@ -102,7 +102,7 @@
103103 )
104104 );
105105
106 - ResourceLoader::register(
 106+ $resourceLoader->register(
107107 'ext.proofreadpage.index',
108108 new ResourceLoaderFileModule( array( 'scripts' => 'extensions/ProofreadPage/proofread_index.js' ) )
109109 );
Index: trunk/extensions/ArticleEmblems/ArticleEmblems.hooks.php
@@ -84,8 +84,8 @@
8585 /*
8686 * ResourceLoaderRegisterModules hook
8787 */
88 - public static function resourceLoaderRegisterModules() {
89 - ResourceLoader::register(
 88+ public static function resourceLoaderRegisterModules( &$resourceLoader ) {
 89+ $resourceLoader->register(
9090 'ext.articleEmblems',
9191 new ResourceLoaderFileModule( array(
9292 'styles' => 'extensions/ArticleEmblems/modules/ext.articleEmblems.css',
Index: trunk/extensions/ClickTracking/ClickTracking.hooks.php
@@ -71,8 +71,8 @@
7272 *
7373 * Adds modules to ResourceLoader
7474 */
75 - public static function resourceLoaderRegisterModules() {
76 - ResourceLoader::register( array(
 75+ public static function resourceLoaderRegisterModules( &$resourceLoader ) {
 76+ $resourceLoader->register( array(
7777 'jquery.clickTracking' => new ResourceLoaderFileModule( array(
7878 'scripts' => 'extensions/ClickTracking/modules/jquery.clickTracking.js',
7979 'dependencies' => 'jquery.cookie',
Index: trunk/extensions/PrefSwitch/PrefSwitch.hooks.php
@@ -83,9 +83,9 @@
8484 /*
8585 * ResourceLoaderRegisterModules hook
8686 */
87 - public static function resourceLoaderRegisterModules() {
 87+ public static function resourceLoaderRegisterModules( &$resourceLoader ) {
8888 foreach ( self::$modules as $name => $resources ) {
89 - ResourceLoader::register( $name, new ResourceLoaderFileModule( $resources ) );
 89+ $resourceLoader->register( $name, new ResourceLoaderFileModule( $resources ) );
9090 }
9191 return true;
9292 }
Index: trunk/extensions/Translate/Translate.php
@@ -411,8 +411,8 @@
412412
413413
414414 # Startup code
415 -function efTranslateResources() {
416 - ResourceLoader::register( array( 'translate-css' =>
 415+function efTranslateResources( &$resourceLoader ) {
 416+ $resourceLoader->register( array( 'translate-css' =>
417417 new ResourceLoaderFileModule( array( 'styles' => 'extensions/Translate/Translate.css' ) )
418418 ) );
419419 return true;

Follow-up revisions

RevisionCommit summaryAuthorDate
r73972Updated hooks documentation as per changes in r73971.tparscal19:06, 29 September 2010
r73984Fixed a use of ResourceLoader::getModule as a static method when converting t...tparscal21:13, 29 September 2010
r74525* Improved on r73971 by removing the $name parameter from MessageBlobStore::g...tparscal18:42, 8 October 2010

Comments

#Comment by Reedy (talk | contribs)   20:32, 29 September 2010


Fatal error: Using $this when not in object context in /home/reedy/mediawiki/trunk/phase3/includes/ResourceLoader.php on line 225

Called from 324 of MessageBlobStore.php

#Comment by Trevor Parscal (WMF) (talk | contribs)   21:15, 29 September 2010

Good catch - r73984 resolves this issue.

#Comment by Catrope (talk | contribs)   15:08, 3 October 2010
+	private static function generateMessageBlob( $name, ResourceLoaderModule $module, $lang ) {

generateMessageBlob() doesn't need $name, so you should've just replaced it with $module like everywhere else rather than adding $module.

#Comment by Trevor Parscal (WMF) (talk | contribs)   18:42, 8 October 2010

See r74525, it removed the unused parameter.

#Comment by Nikerabbit (talk | contribs)   11:00, 8 October 2010

PHP Catchable fatal error: Argument 2 passed to MessageBlobStore::updateModule() must be an instance of ResourceLoaderModule, string given, called in /www/w/includes/MessageBlobStore.php on line 333 and defined in /www/w/includes/MessageBlobStore.php on line 121

#Comment by Trevor Parscal (WMF) (talk | contribs)   18:43, 8 October 2010

See r74525, there was an obvious case of passing the module name rather than the module object.

Status & tagging log