r76076 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r76075‎ | r76076 | r76077 >
Date:10:57, 5 November 2010
Author:maxsem
Status:deferred
Tags:
Comment:
Refactoring and documentation
Modified paths:
  • /branches/Gadgets-work/Gadgets.php (modified) (history)
  • /branches/Gadgets-work/Gadgets_body.php (modified) (history)

Diff [purge]

Index: branches/Gadgets-work/Gadgets_body.php
@@ -12,9 +12,18 @@
1313 * @license GNU General Public Licence 2.0 or later
1414 */
1515
16 -class Gadgets {
 16+// @todo: Support specifying RL-awareness per gadget
1717
18 - public static function ArticleSaveComplete( $article, $user, $text ) {
 18+ class GadgetHooks {
 19+
 20+ /**
 21+ * ArticleSaveComplete hook handler.
 22+ *
 23+ * @param $article Article
 24+ * @param $user User
 25+ * @param $text String: New page text
 26+ */
 27+ public static function articleSaveComplete( $article, $user, $text ) {
1928 //update cache if MediaWiki:Gadgets-definition was edited
2029 $title = $article->mTitle;
2130 if( $title->getNamespace() == NS_MEDIAWIKI && $title->getText() == 'Gadgets-definition' ) {
@@ -23,6 +32,11 @@
2433 return true;
2534 }
2635
 36+ /**
 37+ * GetPreferences hook handler.
 38+ * @param $user User
 39+ * @param $preferences Array: Preference descriptions
 40+ */
2741 public static function getPreferences( $user, &$preferences ) {
2842 $gadgets = Gadget::loadStructuredList();
2943 if (!$gadgets) return true;
@@ -66,6 +80,10 @@
6781 return true;
6882 }
6983
 84+ /**
 85+ * ResourceLoaderRegisterModules hook handler.
 86+ * @param $resourceLoader ResourceLoader
 87+ */
7088 public static function registerModules( &$resourceLoader ) {
7189 $gadgets = Gadget::loadList();
7290 if ( !$gadgets ) {
@@ -80,6 +98,10 @@
8199 return true;
82100 }
83101
 102+ /**
 103+ * BeforePageDisplay hook handler.
 104+ * @param $out OutputPage
 105+ */
84106 public static function beforePageDisplay( $out ) {
85107 global $wgUser;
86108 if ( !$wgUser->isLoggedIn() ) return true;
@@ -122,16 +144,22 @@
123145 foreach ( $pages as $page ) {
124146 if ( isset( $done[$page] ) ) continue;
125147 $done[$page] = true;
126 - self::applyGadgetCode( $page, $out );
 148+ self::applyScript( $page, $out );
127149 }
128150
129151 return true;
130152 }
131153
132 - private static function applyGadgetCode( $page, $out ) {
 154+ /**
 155+ * Adds one legacy script to output.
 156+ *
 157+ * @param $page String: Unprefixed page title
 158+ * @param $out OutputPage
 159+ */
 160+ private static function applyScript( $page, $out ) {
133161 global $wgJsMimeType;
134162
135 - //FIXME: stuff added via $out->addScript appears below usercss and userjs in the head tag.
 163+ //FIXME: stuff added via $out->addScript appears below usercss and userjs
136164 // but we'd want it to appear above explicit user stuff, so it can be overwritten.
137165
138166 $t = Title::makeTitleSafe( NS_MEDIAWIKI, $page );
@@ -143,8 +171,14 @@
144172 }
145173 }
146174
 175+/**
 176+ * Wrapper for one gadget.
 177+ */
147178 class Gadget {
148 - const GADGET_CLASS_VERSION = 1; // Increment this when changing fields
 179+ /**
 180+ * Increment this when changing class structure
 181+ */
 182+ const GADGET_CLASS_VERSION = 1;
149183
150184 private $version = self::GADGET_CLASS_VERSION,
151185 $scripts = array(),
@@ -152,6 +186,11 @@
153187 $name,
154188 $resourceLoaded = false;
155189
 190+ /**
 191+ * Creates an instance of this class from definition in MediaWiki:Gadgets-definition
 192+ * @param $definition String: Gadget definition
 193+ * @return Mixed: Instance of Gadget class or false if $definition is invalid
 194+ */
156195 public static function newFromDefinition( $definition ) {
157196 if ( !preg_match( '/^\*+ *([a-zA-Z](?:[-_:.\w\d ]*[a-zA-Z0-9])?)\s*((\|[^|]*)+)\s*$/', $definition, $m ) ) {
158197 return false;
@@ -172,40 +211,70 @@
173212 return $gadget;
174213 }
175214
 215+ /**
 216+ * @return String: Gadget name
 217+ */
176218 public function getName() {
177219 return $this->name;
178220 }
179221
 222+ /**
 223+ * @return String: Name of ResourceLoader module for this gadget
 224+ */
180225 public function getModuleName() {
181226 return "ext.gadget.{$this->name}";
182227 }
183228
 229+ /**
 230+ * Checks whether this is an instance of an older version of this class deserialized from cache
 231+ * @return Boolean
 232+ */
184233 public function isOutdated() {
185234 return $this->version != GADGET_CLASS_VERSION;
186235 }
187236
 237+ /**
 238+ * @return Boolean: Whether all of this gadget's JS components support ResourceLoader
 239+ */
188240 public function supportsResourceLoader() {
189241 return $this->resourceLoaded;
190242 }
191243
 244+ /**
 245+ * @return Boolean: Whether this gadget has resources that can be loaded via ResourceLoader
 246+ */
192247 public function hasModule() {
193248 return count( $this->styles )
194249 + ( $this->supportsResourceLoader() ? count( $this->scripts ) : 0 )
195250 > 0;
196251 }
197252
 253+ /**
 254+ * @return Array: Array of pages with JS not prefixed with namespace
 255+ */
198256 public function getScripts() {
199257 return $this->scripts;
200258 }
201259
 260+ /**
 261+ * @return Array: Array of pages with CSS not prefixed with namespace
 262+ */
202263 public function getStyles() {
203264 return $this->styles;
204265 }
205266
 267+ /**
 268+ * @return Array: Array of all of this gadget's resources
 269+ */
206270 public function getScriptsAndStyles() {
207271 return array_merge( $this->scripts, $this->styles );
208272 }
209273
 274+ /**
 275+ * Returns module for ResourceLoader, see getModuleName() for its name.
 276+ * If our gadget has no scripts or styles suitable for RL, false will be returned.
 277+ * @return Mixed: GadgetResourceLoaderModule or false
 278+ */
210279 public function getModule() {
211280 $pages = array();
212281 foreach( $this->styles as $style ) {
@@ -222,6 +291,10 @@
223292 return new GadgetResourceLoaderModule( $pages );
224293 }
225294
 295+ /**
 296+ * Returns list of scripts that don't support ResourceLoader
 297+ * @return Array
 298+ */
226299 public function getLegacyScripts() {
227300 if ( $this->supportsResourceLoader() ) {
228301 return array();
@@ -229,6 +302,10 @@
230303 return $this->scripts;
231304 }
232305
 306+ /**
 307+ * Loads and returns a list of all gadgets
 308+ * @return Mixed: Array of gadgets or false
 309+ */
233310 public static function loadList() {
234311 static $gadgets = null;
235312
@@ -248,6 +325,34 @@
249326 return $gadgets;
250327 }
251328
 329+ /**
 330+ * Checks whether gadget list from cache can be used.
 331+ * @return Boolean
 332+ */
 333+ private static function isValidList( $gadgets ) {
 334+ if ( !is_array( $gadgets ) ) return false;
 335+ // Check if we have 1) array of gadgets 2) the gadgets are up to date
 336+ // One check is enough
 337+ foreach ( $gadgets as $section => $list ) {
 338+ foreach ( $list as $g ) {
 339+ if ( !( $g instanceof Gadget ) || $g->isOutdated() ) {
 340+ return false;
 341+ } else {
 342+ return true;
 343+ }
 344+ }
 345+ }
 346+ return true; // empty array
 347+ }
 348+
 349+ /**
 350+ * Loads list of gadgets and returns it as associative array of sections with gadgets
 351+ * e.g. array( 'sectionnname1' => array( $gadget1, $gadget2),
 352+ * 'sectionnname2' => array( $gadget3 ) );
 353+ * @param $forceNewText String: New text of MediaWiki:gadgets-sdefinition. If specified, will
 354+ * force a purge of cache and recreation of the gadget list.
 355+ * @return Mixed: Array or false
 356+ */
252357 public static function loadStructuredList( $forceNewText = null ) {
253358 global $wgMemc;
254359
@@ -259,8 +364,7 @@
260365 if ( $forceNewText === null ) {
261366 //cached?
262367 $gadgets = $wgMemc->get( $key );
263 - // TODO: isOutdated()
264 - if ( is_array($gadgets) && next( $gadgets ) instanceof Gadget ) return $gadgets;
 368+ if ( self::isValidList( $gadgets ) ) return $gadgets;
265369
266370 $g = wfMsgForContentNoTrans( "gadgets-definition" );
267371 if ( wfEmptyMsg( "gadgets-definition", $g ) ) {
@@ -282,7 +386,7 @@
283387 $section = $m[1];
284388 }
285389 else {
286 - $gadget = Gadget::newFromDefinition( $line );
 390+ $gadget = self::newFromDefinition( $line );
287391 if ( $gadget ) {
288392 $gadgets[$section][$gadget->getName()] = $gadget;
289393 }
@@ -298,13 +402,29 @@
299403 }
300404 }
301405
 406+/**
 407+ * Class representing a list of resources for one gadget
 408+ */
302409 class GadgetResourceLoaderModule extends ResourceLoaderWikiModule {
303410 private $pages;
304411
 412+ /**
 413+ * Creates an instance of this class
 414+ * @param $pages Array: Associative array of pages in ResourceLoaderWikiModule-compatible
 415+ * format, for example:
 416+ * array(
 417+ * 'Gadget-foo.js' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'script' ),
 418+ * 'Gadget-foo.css' => array( 'ns' => NS_MEDIAWIKI, 'type' => 'style' ),
 419+ * )
 420+ */
305421 public function __construct( $pages ) {
306422 $this->pages = $pages;
307423 }
308424
 425+ /**
 426+ * Overrides the abstract function from ResourceLoaderWikiModule class
 427+ * @return Array: $pages passed to __construct()
 428+ */
309429 protected function getPages( ResourceLoaderContext $context ) {
310430 return $this->pages;
311431 }
Index: branches/Gadgets-work/Gadgets.php
@@ -29,17 +29,17 @@
3030 'descriptionmsg' => 'gadgets-desc',
3131 );
3232
33 -$wgHooks['ArticleSaveComplete'][] = 'Gadgets::articleSaveComplete';
34 -$wgHooks['BeforePageDisplay'][] = 'Gadgets::beforePageDisplay';
35 -$wgHooks['GetPreferences'][] = 'Gadgets::getPreferences';
36 -$wgHooks['ResourceLoaderRegisterModules'][] = 'Gadgets::registerModules';
 33+$wgHooks['ArticleSaveComplete'][] = 'GadgetHooks::articleSaveComplete';
 34+$wgHooks['BeforePageDisplay'][] = 'GadgetHooks::beforePageDisplay';
 35+$wgHooks['GetPreferences'][] = 'GadgetHooks::getPreferences';
 36+$wgHooks['ResourceLoaderRegisterModules'][] = 'GadgetHooks::registerModules';
3737
3838 $dir = dirname(__FILE__) . '/';
3939 $wgExtensionMessagesFiles['Gadgets'] = $dir . 'Gadgets.i18n.php';
4040 $wgExtensionAliasesFiles['Gadgets'] = $dir . 'Gadgets.alias.php';
4141
4242 $wgAutoloadClasses['Gadget'] = $dir . 'Gadgets_body.php';
43 -$wgAutoloadClasses['Gadgets'] = $dir . 'Gadgets_body.php';
 43+$wgAutoloadClasses['GadgetHooks'] = $dir . 'Gadgets_body.php';
4444 $wgAutoloadClasses['GadgetsResourceLoaderModule'] = $dir . 'Gadgets_body.php';
4545 $wgAutoloadClasses['SpecialGadgets'] = $dir . 'SpecialGadgets.php';
4646

Status & tagging log