http://www.mediawiki.org/wiki/Special:Code/MediaWiki/90525
Revision: 90525 Author: salvatoreingala Date: 2011-06-21 14:03:02 +0000 (Tue, 21 Jun 2011) Log Message: ----------- - 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 branches/salvatoreingala/Gadgets/backend/GadgetHooks.php Modified: branches/salvatoreingala/Gadgets/backend/Gadget.php =================================================================== --- branches/salvatoreingala/Gadgets/backend/Gadget.php 2011-06-21 13:38:47 UTC (rev 90524) +++ branches/salvatoreingala/Gadgets/backend/Gadget.php 2011-06-21 14:03:02 UTC (rev 90525) @@ -32,6 +32,7 @@ $requiredRights = array(), $onByDefault = false, $category, + $prefsDescription = null, $preferences = null; @@ -227,6 +228,8 @@ $gadget = new Gadget(); $gadget->name = trim( str_replace(' ', '_', $m[1] ) ); $gadget->definition = $definition; + + //Parse gadget options $options = trim( $m[2], ' []' ); foreach ( preg_split( '/\s*\|\s*/', $options, -1, PREG_SPLIT_NO_EMPTY ) as $option ) { $arr = preg_split( '/\s*=\s*/', $option, 2 ); @@ -260,6 +263,17 @@ $gadget->styles[] = $page; } } + + if ( $gadget->resourceLoaded ) { + //Retrieve preference descriptions + $prefsDescriptionMsg = "Gadget-{$gadget->name}.preferences"; + $msg = wfMessage( $prefsDescriptionMsg ); + if ( $msg->exists() ) { + $prefsDescription = FormatJson::decode( $msg->plain(), true ); + $gadget->setPrefsDescription( $prefsDescription ); + } + } + return $gadget; } @@ -516,12 +530,14 @@ return $gadgets; } - $g = wfMsgForContentNoTrans( "gadgets-definition" ); - if ( wfEmptyMsg( "gadgets-definition", $g ) ) { + $msg = wfMessage( "gadgets-definition" ); + if ( !$msg->exists() ) { $gadgets = false; wfProfileOut( __METHOD__ ); return $gadgets; } + + $g = $msg->plain(); } else { $g = $forceNewText; } @@ -558,8 +574,7 @@ //TODO: put the following static methods somewhere else //Checks if the given description of the preferences is valid - public static function isGadgetPrefsDescriptionValid( &$prefsDescriptionJson ) { - $prefsDescription = FormatJson::decode( $prefsDescriptionJson, true ); + public static function isPrefsDescriptionValid( $prefsDescription ) { if ( $prefsDescription === null || !isset( $prefsDescription['fields'] ) ) { return false; @@ -636,22 +651,25 @@ /** * Gets description of preferences for this gadget. * - * @return Mixed null if the gadget exists but doesn't have any preferences or if provided ones are not valid, - * an array with the description of preferences otherwise. + * @return Mixed null or an array with preferences */ public function getPrefsDescription() { - $prefsDescriptionMsg = "Gadget-{$this->name}.preferences"; - - //TODO: use cache - - $prefsDescriptionJson = wfMsgForContentNoTrans( $prefsDescriptionMsg ); - if ( wfEmptyMsg( $prefsDescriptionMsg, $prefsDescriptionJson ) || - !self::isGadgetPrefsDescriptionValid( $prefsDescriptionJson ) ) - { - return null; + return $this->prefsDescription; + } + + /** + * Sets the description of preferences for this gadget. If the given array is not valid, + * then internal preference description is set to null. + * + * @param $prefsDescription mixed an array with new preferences description, or null. + * + */ + public function setPrefsDescription( $prefsDescription ) { + if ( self::isPrefsDescriptionValid( $prefsDescription ) ) { + $this->prefsDescription = $prefsDescription; + } else { + $this->prefsDescription = null; } - - return FormatJson::decode( $prefsDescriptionJson, true ); } //Check if a preference is valid, according to description Modified: branches/salvatoreingala/Gadgets/backend/GadgetHooks.php =================================================================== --- branches/salvatoreingala/Gadgets/backend/GadgetHooks.php 2011-06-21 13:38:47 UTC (rev 90524) +++ branches/salvatoreingala/Gadgets/backend/GadgetHooks.php 2011-06-21 14:03:02 UTC (rev 90525) @@ -24,9 +24,14 @@ */ public static function articleSaveComplete( $article, $user, $text ) { //update cache if MediaWiki:Gadgets-definition was edited + //or if a Mediawiki:Gadget-foo.preferences was edited $title = $article->mTitle; - if( $title->getNamespace() == NS_MEDIAWIKI && $title->getText() == 'Gadgets-definition' ) { - Gadget::loadStructuredList( $text ); + if( $title->getNamespace() == NS_MEDIAWIKI ) { + if ( $title->getText() == 'Gadgets-definition' + || preg_match( '/Gadget-([a-zA-Z](?:[-_:.\w\d ]*[a-zA-Z0-9])?)-config/', $title->getText() ) ) + { + Gadget::loadStructuredList( $text ); + } } return true; } @@ -54,7 +59,7 @@ } } if ( $section !== '' ) { - $section = wfMsgExt( "gadget-section-$section", 'parseinline' ); + $section = wfMessage( "gadget-section-$section" )->parse(); if ( count ( $available ) ) { $options[$section] = $available; } @@ -69,7 +74,7 @@ 'label' => ' ', 'default' => Xml::tags( 'tr', array(), Xml::tags( 'td', array( 'colspan' => 2 ), - wfMsgExt( 'gadgets-prefstext', 'parse' ) ) ), + wfMessage( 'gadgets-prefstext' )->parse() ) ), 'section' => 'gadgets', 'raw' => 1, 'rawrow' => 1, @@ -161,12 +166,15 @@ * @param &$options */ public static function userLoadOptions( $user, &$options ) { + //Only if it's current user $curUser = RequestContext::getMain()->getUser(); if ( $curUser->getID() !== $user->getID() ) { return true; } + wfProfileIn( __METHOD__ ); + //Find out all existing gadget preferences and save them in a map $preferencesCache = array(); foreach ( $options as $option => $value ) { @@ -205,7 +213,8 @@ $gadget->setPrefs( $userPrefs ); } } - + + wfProfileOut( __METHOD__ ); return true; } _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs