Index: branches/RL2/extensions/Gadgets/GadgetHooks.php |
— | — | @@ -42,7 +42,7 @@ |
43 | 43 | return true; |
44 | 44 | } |
45 | 45 | |
46 | | - $repo = new LocalGadgetRepo; |
| 46 | + $repo = LocalGadgetRepo::singleton(); |
47 | 47 | $repo->deleteGadget( $id ); |
48 | 48 | // deleteGadget() may return an error if the Gadget didn't exist, but we don't care here |
49 | 49 | return true; |
— | — | @@ -73,7 +73,7 @@ |
74 | 74 | $prevTs = $previousRev instanceof Revision ? $previousRev->getTimestamp() : wfTimestampNow(); |
75 | 75 | |
76 | 76 | // Update the database entry for this gadget |
77 | | - $repo = new LocalGadgetRepo; |
| 77 | + $repo = LocalGadgetRepo::singleton(); |
78 | 78 | // TODO: Timestamp in the constructor is ugly |
79 | 79 | $gadget = new Gadget( $id, $repo, $text, $prevTs ); |
80 | 80 | $repo->modifyGadget( $gadget, $revision->getTimestamp() ); |
— | — | @@ -98,7 +98,7 @@ |
99 | 99 | |
100 | 100 | // Check whether this undeletion changed the latest revision of the page, by comparing |
101 | 101 | // the timestamp of the latest revision with the timestamp in the DB |
102 | | - $repo = new LocalGadgetRepo; |
| 102 | + $repo = LocalGadgetRepo::singleton(); |
103 | 103 | $gadget = $repo->getGadget( $id ); |
104 | 104 | $gadgetTS = $gadget ? $gadget->getTimestamp() : 0; |
105 | 105 | |
— | — | @@ -195,7 +195,7 @@ |
196 | 196 | */ |
197 | 197 | public static function getPreferences( $user, &$preferences ) { |
198 | 198 | // TODO: Part of this is duplicated from registerModules(), factor out into the repo |
199 | | - $repo = new LocalGadgetRepo; |
| 199 | + $repo = LocalGadgetRepo::singleton(); |
200 | 200 | |
201 | 201 | $gadgets = $repo->getGadgetIds(); |
202 | 202 | $categories = array(); // array( category => array( desc => name ) ) |
— | — | @@ -262,6 +262,7 @@ |
263 | 263 | */ |
264 | 264 | public static function registerModules( &$resourceLoader ) { |
265 | 265 | global $wgGadgetRepositories; |
| 266 | + // TODO: Factor this loop out somewhere and include LocalGadgetRepo more magically so we can use ::singleton() |
266 | 267 | foreach ( $wgGadgetRepositories as $params ) { |
267 | 268 | $repoClass = $params['class']; |
268 | 269 | unset( $params['class'] ); |
Index: branches/RL2/extensions/Gadgets/backend/LocalGadgetRepo.php |
— | — | @@ -1,6 +1,4 @@ |
2 | 2 | <?php |
3 | | -// TODO: Make LocalGadgetRepo a singleton |
4 | | - |
5 | 3 | /** |
6 | 4 | * Gadget repository that gets its gadgets from the local database. |
7 | 5 | */ |
— | — | @@ -8,9 +6,25 @@ |
9 | 7 | protected $data = array(); |
10 | 8 | protected $namesLoaded = false; |
11 | 9 | |
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 | + */ |
13 | 13 | protected $namesKey; |
14 | 14 | |
| 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 | + |
15 | 29 | /*** Public methods inherited from GadgetRepo ***/ |
16 | 30 | |
17 | 31 | /** |
Index: branches/RL2/extensions/Gadgets/api/ApiQueryGadgets.php |
— | — | @@ -51,7 +51,7 @@ |
52 | 52 | } |
53 | 53 | |
54 | 54 | private function getList() { |
55 | | - $repo = new LocalGadgetRepo; |
| 55 | + $repo = LocalGadgetRepo::singleton(); |
56 | 56 | $result = array(); |
57 | 57 | |
58 | 58 | if ( $this->neededIds !== false ) { |
Index: branches/RL2/extensions/Gadgets/api/ApiQueryGadgetCategories.php |
— | — | @@ -43,7 +43,7 @@ |
44 | 44 | private function getList() { |
45 | 45 | $data = array(); |
46 | 46 | $result = $this->getResult(); |
47 | | - $repo = new LocalGadgetRepo; |
| 47 | + $repo = LocalGadgetRepo::singleton(); |
48 | 48 | |
49 | 49 | $gadgetsByCategory = $repo->getGadgetsByCategory(); |
50 | 50 | foreach ( $gadgetsByCategory as $category => $gadgets ) { |
Index: branches/RL2/extensions/Gadgets/SpecialGadgetManager.php |
— | — | @@ -43,7 +43,7 @@ |
44 | 44 | private function generateOverview() { |
45 | 45 | global $wgGadgetEnableSharing; |
46 | 46 | |
47 | | - $repo = new LocalGadgetRepo; |
| 47 | + $repo = LocalGadgetRepo::singleton(); |
48 | 48 | $gadgetsByCategory = $repo->getGadgetsByCategory(); |
49 | 49 | |
50 | 50 | // If there there are no gadgets at all, exit early. |