r100884 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100883‎ | r100884 | r100885 >
Date:21:48, 26 October 2011
Author:catrope
Status:deferred (Comments)
Tags:
Comment:
[RL2] Turn getAllGadgets() into a versatile helper function that supports getting all gadgets with and without local gadgets, as either ID strings or Gadget objects. Also add isLocal() to the GadgetRepo class, because instanceof LocalGadgetRepo is problematic (since ForeignDBGadgetRepo inherits from LocalGadgetRepo).
Modified paths:
  • /branches/RL2/extensions/Gadgets/backend/ForeignDBGadgetRepo.php (modified) (history)
  • /branches/RL2/extensions/Gadgets/backend/GadgetRepo.php (modified) (history)
  • /branches/RL2/extensions/Gadgets/backend/LocalGadgetRepo.php (modified) (history)

Diff [purge]

Index: branches/RL2/extensions/Gadgets/backend/LocalGadgetRepo.php
@@ -179,6 +179,10 @@
180180 return wfGetDB( DB_SLAVE );
181181 }
182182
 183+ public function isLocal() {
 184+ return true;
 185+ }
 186+
183187 /*** Public methods ***/
184188
185189 /**
Index: branches/RL2/extensions/Gadgets/backend/ForeignDBGadgetRepo.php
@@ -46,6 +46,10 @@
4747 return $this->getMasterDB();
4848 }
4949
 50+ public function isLocal() {
 51+ return false;
 52+ }
 53+
5054 /*** Overridden protected methods from LocalGadgetRepo ***/
5155 protected function getMasterDB() {
5256 if ( $this->db === null ) {
Index: branches/RL2/extensions/Gadgets/backend/GadgetRepo.php
@@ -75,6 +75,12 @@
7676 */
7777 abstract public function deleteGadget( $id );
7878
 79+ /**
 80+ * Whether this repository is the local repository
 81+ * @return boolean
 82+ */
 83+ abstract public function isLocal();
 84+
7985 /**** Public methods ****/
8086
8187 public function getGadgetsByCategory() {
@@ -106,18 +112,60 @@
107113 }
108114
109115 /**
110 - * Get all gadgets from all repositories.
111 - * @return array of Gadget objects
 116+ * Helper function for getAllGadgets(), getAllGadgetIDs(), getAllRemoteGadgets() and getAllRemoteGadgetIDs()
 117+ * @param $includeLocal boolean Whether gadgets from the local repo should be included
 118+ * @param $getObjects boolean Whether Gadget objects should be constructed. If false, IDs (strings) will be returned
 119+ * @return array of Gadget objects or strings
112120 */
113 - public static function getAllGadgets() {
 121+ private static function getAllGadgets_internal( $includeLocal, $getObjects ) {
114122 $retval = array();
115123 $repos = GadgetRepo::getAllRepos();
116124 foreach ( $repos as $repo ) {
 125+ if ( !$includeLocal && $repo->isLocal() ) {
 126+ continue;
 127+ }
 128+
117129 $gadgets = $repo->getGadgetIds();
118 - foreach ( $gadgets as $id ) {
119 - $retval[] = $repo->getGadget( $id );
 130+ if ( $getObjects ) {
 131+ foreach ( $gadgets as $id ) {
 132+ $retval[] = $repo->getGadget( $id );
 133+ }
 134+ } else {
 135+ $retval = array_merge( $retval, $gadgets );
120136 }
121137 }
122138 return $retval;
123139 }
 140+
 141+ /**
 142+ * Get all gadgets from all repositories
 143+ * @return array of Gadget objects
 144+ */
 145+ public static function getAllGadgets() {
 146+ return self::getAllGadgets_internal( true, true );
 147+ }
 148+
 149+ /**
 150+ * Get all gadget IDs from all repositories
 151+ * @return array of gadget IDs (strings)
 152+ */
 153+ public static function getAllGadgetIDs() {
 154+ return self::getAllGadgets_internal( true, false );
 155+ }
 156+
 157+ /**
 158+ * Get all gadgets from all remote repositories (i.e. all repositories except the local repository)
 159+ * @return array of Gadget objects
 160+ */
 161+ public static function getAllRemoteGadgets() {
 162+ return self::getAllGadgets_internal( false, true );
 163+ }
 164+
 165+ /**
 166+ * Get all gadget IDs from all remote repositories (i.e. all repositories except the local repository)
 167+ * @return array of gadget IDs (strings)
 168+ */
 169+ public static function getAllRemoteGadgetIDs() {
 170+ return self::getAllGadgets_internal( false, false );
 171+ }
124172 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r101305[RL2] Use Strangs instead of booleans for GadgetRepo::getAllGadgets_internal...krinkle16:42, 30 October 2011

Comments

#Comment by Nikerabbit (talk | contribs)   06:43, 27 October 2011

Wouldn't it be cleaner to have one function which returns ids and another one which returns objects when given ids?

#Comment by Catrope (talk | contribs)   10:53, 27 October 2011

That's hard to do in practice, because in order to get the object you have to know which repository the ID came from.

Status & tagging log