r90525 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r90524‎ | r90525 | r90526 >
Date:14:03, 21 June 2011
Author:salvatoreingala
Status:deferred
Tags:
Comment:
- Moved preferences description into Gadget class
- Changed GadgetHooks::articleSaveComplete so that it rebuilds gadgets list if some MediaWiki:Gadget-*.preferences is changed
- Added profiling to GadgetHooks::userLoadOptions
- Switched to wfMessage instead of wfMsg*
Modified paths:
  • /branches/salvatoreingala/Gadgets/backend/Gadget.php (modified) (history)
  • /branches/salvatoreingala/Gadgets/backend/GadgetHooks.php (modified) (history)

Diff [purge]

Index: branches/salvatoreingala/Gadgets/backend/Gadget.php
@@ -32,6 +32,7 @@
3333 $requiredRights = array(),
3434 $onByDefault = false,
3535 $category,
 36+ $prefsDescription = null,
3637 $preferences = null;
3738
3839
@@ -227,6 +228,8 @@
228229 $gadget = new Gadget();
229230 $gadget->name = trim( str_replace(' ', '_', $m[1] ) );
230231 $gadget->definition = $definition;
 232+
 233+ //Parse gadget options
231234 $options = trim( $m[2], ' []' );
232235 foreach ( preg_split( '/\s*\|\s*/', $options, -1, PREG_SPLIT_NO_EMPTY ) as $option ) {
233236 $arr = preg_split( '/\s*=\s*/', $option, 2 );
@@ -260,6 +263,17 @@
261264 $gadget->styles[] = $page;
262265 }
263266 }
 267+
 268+ if ( $gadget->resourceLoaded ) {
 269+ //Retrieve preference descriptions
 270+ $prefsDescriptionMsg = "Gadget-{$gadget->name}.preferences";
 271+ $msg = wfMessage( $prefsDescriptionMsg );
 272+ if ( $msg->exists() ) {
 273+ $prefsDescription = FormatJson::decode( $msg->plain(), true );
 274+ $gadget->setPrefsDescription( $prefsDescription );
 275+ }
 276+ }
 277+
264278 return $gadget;
265279 }
266280
@@ -516,12 +530,14 @@
517531 return $gadgets;
518532 }
519533
520 - $g = wfMsgForContentNoTrans( "gadgets-definition" );
521 - if ( wfEmptyMsg( "gadgets-definition", $g ) ) {
 534+ $msg = wfMessage( "gadgets-definition" );
 535+ if ( !$msg->exists() ) {
522536 $gadgets = false;
523537 wfProfileOut( __METHOD__ );
524538 return $gadgets;
525539 }
 540+
 541+ $g = $msg->plain();
526542 } else {
527543 $g = $forceNewText;
528544 }
@@ -558,8 +574,7 @@
559575 //TODO: put the following static methods somewhere else
560576
561577 //Checks if the given description of the preferences is valid
562 - public static function isGadgetPrefsDescriptionValid( &$prefsDescriptionJson ) {
563 - $prefsDescription = FormatJson::decode( $prefsDescriptionJson, true );
 578+ public static function isPrefsDescriptionValid( $prefsDescription ) {
564579
565580 if ( $prefsDescription === null || !isset( $prefsDescription['fields'] ) ) {
566581 return false;
@@ -636,22 +651,25 @@
637652 /**
638653 * Gets description of preferences for this gadget.
639654 *
640 - * @return Mixed null if the gadget exists but doesn't have any preferences or if provided ones are not valid,
641 - * an array with the description of preferences otherwise.
 655+ * @return Mixed null or an array with preferences
642656 */
643657 public function getPrefsDescription() {
644 - $prefsDescriptionMsg = "Gadget-{$this->name}.preferences";
645 -
646 - //TODO: use cache
647 -
648 - $prefsDescriptionJson = wfMsgForContentNoTrans( $prefsDescriptionMsg );
649 - if ( wfEmptyMsg( $prefsDescriptionMsg, $prefsDescriptionJson ) ||
650 - !self::isGadgetPrefsDescriptionValid( $prefsDescriptionJson ) )
651 - {
652 - return null;
 658+ return $this->prefsDescription;
 659+ }
 660+
 661+ /**
 662+ * Sets the description of preferences for this gadget. If the given array is not valid,
 663+ * then internal preference description is set to null.
 664+ *
 665+ * @param $prefsDescription mixed an array with new preferences description, or null.
 666+ *
 667+ */
 668+ public function setPrefsDescription( $prefsDescription ) {
 669+ if ( self::isPrefsDescriptionValid( $prefsDescription ) ) {
 670+ $this->prefsDescription = $prefsDescription;
 671+ } else {
 672+ $this->prefsDescription = null;
653673 }
654 -
655 - return FormatJson::decode( $prefsDescriptionJson, true );
656674 }
657675
658676 //Check if a preference is valid, according to description
Index: branches/salvatoreingala/Gadgets/backend/GadgetHooks.php
@@ -24,9 +24,14 @@
2525 */
2626 public static function articleSaveComplete( $article, $user, $text ) {
2727 //update cache if MediaWiki:Gadgets-definition was edited
 28+ //or if a Mediawiki:Gadget-foo.preferences was edited
2829 $title = $article->mTitle;
29 - if( $title->getNamespace() == NS_MEDIAWIKI && $title->getText() == 'Gadgets-definition' ) {
30 - Gadget::loadStructuredList( $text );
 30+ if( $title->getNamespace() == NS_MEDIAWIKI ) {
 31+ if ( $title->getText() == 'Gadgets-definition'
 32+ || preg_match( '/Gadget-([a-zA-Z](?:[-_:.\w\d ]*[a-zA-Z0-9])?)-config/', $title->getText() ) )
 33+ {
 34+ Gadget::loadStructuredList( $text );
 35+ }
3136 }
3237 return true;
3338 }
@@ -54,7 +59,7 @@
5560 }
5661 }
5762 if ( $section !== '' ) {
58 - $section = wfMsgExt( "gadget-section-$section", 'parseinline' );
 63+ $section = wfMessage( "gadget-section-$section" )->parse();
5964 if ( count ( $available ) ) {
6065 $options[$section] = $available;
6166 }
@@ -69,7 +74,7 @@
7075 'label' => ' ',
7176 'default' => Xml::tags( 'tr', array(),
7277 Xml::tags( 'td', array( 'colspan' => 2 ),
73 - wfMsgExt( 'gadgets-prefstext', 'parse' ) ) ),
 78+ wfMessage( 'gadgets-prefstext' )->parse() ) ),
7479 'section' => 'gadgets',
7580 'raw' => 1,
7681 'rawrow' => 1,
@@ -161,12 +166,15 @@
162167 * @param &$options
163168 */
164169 public static function userLoadOptions( $user, &$options ) {
 170+
165171 //Only if it's current user
166172 $curUser = RequestContext::getMain()->getUser();
167173 if ( $curUser->getID() !== $user->getID() ) {
168174 return true;
169175 }
170176
 177+ wfProfileIn( __METHOD__ );
 178+
171179 //Find out all existing gadget preferences and save them in a map
172180 $preferencesCache = array();
173181 foreach ( $options as $option => $value ) {
@@ -205,7 +213,8 @@
206214 $gadget->setPrefs( $userPrefs );
207215 }
208216 }
209 -
 217+
 218+ wfProfileOut( __METHOD__ );
210219 return true;
211220 }
212221

Status & tagging log