r96947 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96946‎ | r96947 | r96948 >
Date:09:55, 13 September 2011
Author:catrope
Status:ok
Tags:
Comment:
RL2: Make LocalGadgetRepo a singleton
Modified paths:
  • /branches/RL2/extensions/Gadgets/GadgetHooks.php (modified) (history)
  • /branches/RL2/extensions/Gadgets/SpecialGadgetManager.php (modified) (history)
  • /branches/RL2/extensions/Gadgets/api/ApiQueryGadgetCategories.php (modified) (history)
  • /branches/RL2/extensions/Gadgets/api/ApiQueryGadgets.php (modified) (history)
  • /branches/RL2/extensions/Gadgets/backend/LocalGadgetRepo.php (modified) (history)

Diff [purge]

Index: branches/RL2/extensions/Gadgets/GadgetHooks.php
@@ -42,7 +42,7 @@
4343 return true;
4444 }
4545
46 - $repo = new LocalGadgetRepo;
 46+ $repo = LocalGadgetRepo::singleton();
4747 $repo->deleteGadget( $id );
4848 // deleteGadget() may return an error if the Gadget didn't exist, but we don't care here
4949 return true;
@@ -73,7 +73,7 @@
7474 $prevTs = $previousRev instanceof Revision ? $previousRev->getTimestamp() : wfTimestampNow();
7575
7676 // Update the database entry for this gadget
77 - $repo = new LocalGadgetRepo;
 77+ $repo = LocalGadgetRepo::singleton();
7878 // TODO: Timestamp in the constructor is ugly
7979 $gadget = new Gadget( $id, $repo, $text, $prevTs );
8080 $repo->modifyGadget( $gadget, $revision->getTimestamp() );
@@ -98,7 +98,7 @@
9999
100100 // Check whether this undeletion changed the latest revision of the page, by comparing
101101 // the timestamp of the latest revision with the timestamp in the DB
102 - $repo = new LocalGadgetRepo;
 102+ $repo = LocalGadgetRepo::singleton();
103103 $gadget = $repo->getGadget( $id );
104104 $gadgetTS = $gadget ? $gadget->getTimestamp() : 0;
105105
@@ -195,7 +195,7 @@
196196 */
197197 public static function getPreferences( $user, &$preferences ) {
198198 // TODO: Part of this is duplicated from registerModules(), factor out into the repo
199 - $repo = new LocalGadgetRepo;
 199+ $repo = LocalGadgetRepo::singleton();
200200
201201 $gadgets = $repo->getGadgetIds();
202202 $categories = array(); // array( category => array( desc => name ) )
@@ -262,6 +262,7 @@
263263 */
264264 public static function registerModules( &$resourceLoader ) {
265265 global $wgGadgetRepositories;
 266+ // TODO: Factor this loop out somewhere and include LocalGadgetRepo more magically so we can use ::singleton()
266267 foreach ( $wgGadgetRepositories as $params ) {
267268 $repoClass = $params['class'];
268269 unset( $params['class'] );
Index: branches/RL2/extensions/Gadgets/backend/LocalGadgetRepo.php
@@ -1,6 +1,4 @@
22 <?php
3 -// TODO: Make LocalGadgetRepo a singleton
4 -
53 /**
64 * Gadget repository that gets its gadgets from the local database.
75 */
@@ -8,9 +6,25 @@
97 protected $data = array();
108 protected $namesLoaded = false;
119
12 - /** Memcached key of the gadget names list. Subclasses may override this in their constructor */
 10+ /** Memcached key of the gadget names list. Subclasses may override this in their constructor.
 11+ * This could've been a static member if we had PHP 5.3's late static binding.
 12+ */
1313 protected $namesKey;
1414
 15+ /*** Public static methods ***/
 16+
 17+ /**
 18+ * Get the instance of this class
 19+ * @return LocalGadgetRepo
 20+ */
 21+ public static function singleton() {
 22+ static $instance = null;
 23+ if ( $instance === null ) {
 24+ $instance = new self;
 25+ }
 26+ return $instance;
 27+ }
 28+
1529 /*** Public methods inherited from GadgetRepo ***/
1630
1731 /**
Index: branches/RL2/extensions/Gadgets/api/ApiQueryGadgets.php
@@ -51,7 +51,7 @@
5252 }
5353
5454 private function getList() {
55 - $repo = new LocalGadgetRepo;
 55+ $repo = LocalGadgetRepo::singleton();
5656 $result = array();
5757
5858 if ( $this->neededIds !== false ) {
Index: branches/RL2/extensions/Gadgets/api/ApiQueryGadgetCategories.php
@@ -43,7 +43,7 @@
4444 private function getList() {
4545 $data = array();
4646 $result = $this->getResult();
47 - $repo = new LocalGadgetRepo;
 47+ $repo = LocalGadgetRepo::singleton();
4848
4949 $gadgetsByCategory = $repo->getGadgetsByCategory();
5050 foreach ( $gadgetsByCategory as $category => $gadgets ) {
Index: branches/RL2/extensions/Gadgets/SpecialGadgetManager.php
@@ -43,7 +43,7 @@
4444 private function generateOverview() {
4545 global $wgGadgetEnableSharing;
4646
47 - $repo = new LocalGadgetRepo;
 47+ $repo = LocalGadgetRepo::singleton();
4848 $gadgetsByCategory = $repo->getGadgetsByCategory();
4949
5050 // If there there are no gadgets at all, exit early.

Status & tagging log