r96949 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96948‎ | r96949 | r96950 >
Date:10:17, 13 September 2011
Author:catrope
Status:resolved
Tags:
Comment:
RL2: Factor out $wgGadgetRepositories loop to GadgetRepo::getAllRepos() and include the LocalRepo singleton magically so it doesn't have to be in $wgGadgetRepositories anymore. Also factor out the code for getting all gadgets from all repos into GadgetRepo::getAllGadgets()
Modified paths:
  • /branches/RL2/extensions/Gadgets/GadgetHooks.php (modified) (history)
  • /branches/RL2/extensions/Gadgets/Gadgets.php (modified) (history)
  • /branches/RL2/extensions/Gadgets/backend/GadgetRepo.php (modified) (history)

Diff [purge]

Index: branches/RL2/extensions/Gadgets/Gadgets.php
@@ -56,12 +56,7 @@
5757 * // TODO
5858 * );
5959 */
60 -$wgGadgetRepositories = array(
61 - array(
62 - // Default local gadget repository. Doesn't need any parameters
63 - 'class' => 'LocalGadgetRepo',
64 - )
65 -);
 60+$wgGadgetRepositories = array();
6661
6762 /**
6863 * Whether or not to allow gadgets to be shared in the gadget manager.
Index: branches/RL2/extensions/Gadgets/GadgetHooks.php
@@ -261,18 +261,9 @@
262262 * @param $resourceLoader ResourceLoader
263263 */
264264 public static function registerModules( &$resourceLoader ) {
265 - global $wgGadgetRepositories;
266 - // TODO: Factor this loop out somewhere and include LocalGadgetRepo more magically so we can use ::singleton()
267 - foreach ( $wgGadgetRepositories as $params ) {
268 - $repoClass = $params['class'];
269 - unset( $params['class'] );
270 - $repo = new $repoClass( $params );
271 -
272 - $gadgets = $repo->getGadgetIds();
273 - foreach ( $gadgets as $id ) {
274 - $gadget = $repo->getGadget( $id );
275 - $resourceLoader->register( $gadget->getModuleName(), $gadget->getModule() );
276 - }
 265+ $gadgets = GadgetRepo::getAllGadgets();
 266+ foreach ( $gadgets as $gadget ) {
 267+ $resourceLoader->register( $gadget->getModuleName(), $gadget->getModule() );
277268 }
278269 return true;
279270 }
@@ -282,21 +273,14 @@
283274 * @param $out OutputPage
284275 */
285276 public static function beforePageDisplay( $out ) {
286 - global $wgUser, $wgGadgetRepositories;
 277+ global $wgUser;
287278
288279 wfProfileIn( __METHOD__ );
289280
290 - foreach ( $wgGadgetRepositories as $params ) {
291 - $repoClass = $params['class'];
292 - unset( $params['class'] );
293 - $repo = new $repoClass( $params );
294 -
295 - $gadgets = $repo->getGadgetIds();
296 - foreach ( $gadgets as $id ) {
297 - $gadget = $repo->getGadget( $id );
298 - if ( $gadget->isEnabledForUser( $wgUser ) && $gadget->isAllowed( $wgUser ) ) {
299 - $out->addModules( $gadget->getModuleName() );
300 - }
 281+ $gadgets = GadgetRepo::getAllGadgets();
 282+ foreach ( $gadgets as $gadget ) {
 283+ if ( $gadget->isEnabledForUser( $wgUser ) && $gadget->isAllowed( $wgUser ) ) {
 284+ $out->addModules( $gadget->getModuleName() );
301285 }
302286 }
303287
Index: branches/RL2/extensions/Gadgets/backend/GadgetRepo.php
@@ -111,4 +111,35 @@
112112 }
113113 return $msg->plain();
114114 }
 115+
 116+ /**
 117+ * Get all gadget repositories. Returns the LocalGadgetRepo singleton and any
 118+ * repositories configured in $wgGadgetRepositories
 119+ * @return array of GadgetRepo objects
 120+ */
 121+ public static function getAllRepos() {
 122+ global $wgGadgetRepositories;
 123+ $repos = array( LocalGadgetRepo::singleton() );
 124+ foreach ( $wgGadgetRepositories as $params ) {
 125+ $repoClass = $params['class'];
 126+ unset( $params['class'] ); // Safe because foreach operates on a copy of the array
 127+ $repos[] = new $repoClass( $params );
 128+ }
 129+ return $repos;
 130+ }
 131+
 132+ /**
 133+ * Get all gadgets from all repositories.
 134+ * @return array of Gadget objects
 135+ */
 136+ public static function getAllGadgets() {
 137+ $retval = array();
 138+ $repos = GadgetRepo::getAllRepos();
 139+ foreach ( $repos as $repo ) {
 140+ $gadgets = $repo->getGadgetIds();
 141+ foreach ( $gadgets as $id ) {
 142+ $retval[] = $repo->getGadget( $id );
 143+ }
 144+ }
 145+ }
115146 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r96957RL2: Fix stupidity in r96949catrope11:47, 13 September 2011
r97004[ResourceLoader 2]: Use context instead of global in beforePageDisplay hook...krinkle21:24, 13 September 2011
r97056RL2: Remove TODO comment that was addressed by r96949catrope13:27, 14 September 2011

Status & tagging log