Index: branches/salvatoreingala/Gadgets/backend/Gadget.php |
— | — | @@ -32,6 +32,7 @@ |
33 | 33 | $requiredRights = array(), |
34 | 34 | $onByDefault = false, |
35 | 35 | $category, |
| 36 | + $prefsDescription = null, |
36 | 37 | $preferences = null; |
37 | 38 | |
38 | 39 | |
— | — | @@ -227,6 +228,8 @@ |
228 | 229 | $gadget = new Gadget(); |
229 | 230 | $gadget->name = trim( str_replace(' ', '_', $m[1] ) ); |
230 | 231 | $gadget->definition = $definition; |
| 232 | + |
| 233 | + //Parse gadget options |
231 | 234 | $options = trim( $m[2], ' []' ); |
232 | 235 | foreach ( preg_split( '/\s*\|\s*/', $options, -1, PREG_SPLIT_NO_EMPTY ) as $option ) { |
233 | 236 | $arr = preg_split( '/\s*=\s*/', $option, 2 ); |
— | — | @@ -260,6 +263,17 @@ |
261 | 264 | $gadget->styles[] = $page; |
262 | 265 | } |
263 | 266 | } |
| 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 | + |
264 | 278 | return $gadget; |
265 | 279 | } |
266 | 280 | |
— | — | @@ -516,12 +530,14 @@ |
517 | 531 | return $gadgets; |
518 | 532 | } |
519 | 533 | |
520 | | - $g = wfMsgForContentNoTrans( "gadgets-definition" ); |
521 | | - if ( wfEmptyMsg( "gadgets-definition", $g ) ) { |
| 534 | + $msg = wfMessage( "gadgets-definition" ); |
| 535 | + if ( !$msg->exists() ) { |
522 | 536 | $gadgets = false; |
523 | 537 | wfProfileOut( __METHOD__ ); |
524 | 538 | return $gadgets; |
525 | 539 | } |
| 540 | + |
| 541 | + $g = $msg->plain(); |
526 | 542 | } else { |
527 | 543 | $g = $forceNewText; |
528 | 544 | } |
— | — | @@ -558,8 +574,7 @@ |
559 | 575 | //TODO: put the following static methods somewhere else |
560 | 576 | |
561 | 577 | //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 ) { |
564 | 579 | |
565 | 580 | if ( $prefsDescription === null || !isset( $prefsDescription['fields'] ) ) { |
566 | 581 | return false; |
— | — | @@ -636,22 +651,25 @@ |
637 | 652 | /** |
638 | 653 | * Gets description of preferences for this gadget. |
639 | 654 | * |
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 |
642 | 656 | */ |
643 | 657 | 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; |
653 | 673 | } |
654 | | - |
655 | | - return FormatJson::decode( $prefsDescriptionJson, true ); |
656 | 674 | } |
657 | 675 | |
658 | 676 | //Check if a preference is valid, according to description |
Index: branches/salvatoreingala/Gadgets/backend/GadgetHooks.php |
— | — | @@ -24,9 +24,14 @@ |
25 | 25 | */ |
26 | 26 | public static function articleSaveComplete( $article, $user, $text ) { |
27 | 27 | //update cache if MediaWiki:Gadgets-definition was edited |
| 28 | + //or if a Mediawiki:Gadget-foo.preferences was edited |
28 | 29 | $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 | + } |
31 | 36 | } |
32 | 37 | return true; |
33 | 38 | } |
— | — | @@ -54,7 +59,7 @@ |
55 | 60 | } |
56 | 61 | } |
57 | 62 | if ( $section !== '' ) { |
58 | | - $section = wfMsgExt( "gadget-section-$section", 'parseinline' ); |
| 63 | + $section = wfMessage( "gadget-section-$section" )->parse(); |
59 | 64 | if ( count ( $available ) ) { |
60 | 65 | $options[$section] = $available; |
61 | 66 | } |
— | — | @@ -69,7 +74,7 @@ |
70 | 75 | 'label' => ' ', |
71 | 76 | 'default' => Xml::tags( 'tr', array(), |
72 | 77 | Xml::tags( 'td', array( 'colspan' => 2 ), |
73 | | - wfMsgExt( 'gadgets-prefstext', 'parse' ) ) ), |
| 78 | + wfMessage( 'gadgets-prefstext' )->parse() ) ), |
74 | 79 | 'section' => 'gadgets', |
75 | 80 | 'raw' => 1, |
76 | 81 | 'rawrow' => 1, |
— | — | @@ -161,12 +166,15 @@ |
162 | 167 | * @param &$options |
163 | 168 | */ |
164 | 169 | public static function userLoadOptions( $user, &$options ) { |
| 170 | + |
165 | 171 | //Only if it's current user |
166 | 172 | $curUser = RequestContext::getMain()->getUser(); |
167 | 173 | if ( $curUser->getID() !== $user->getID() ) { |
168 | 174 | return true; |
169 | 175 | } |
170 | 176 | |
| 177 | + wfProfileIn( __METHOD__ ); |
| 178 | + |
171 | 179 | //Find out all existing gadget preferences and save them in a map |
172 | 180 | $preferencesCache = array(); |
173 | 181 | foreach ( $options as $option => $value ) { |
— | — | @@ -205,7 +213,8 @@ |
206 | 214 | $gadget->setPrefs( $userPrefs ); |
207 | 215 | } |
208 | 216 | } |
209 | | - |
| 217 | + |
| 218 | + wfProfileOut( __METHOD__ ); |
210 | 219 | return true; |
211 | 220 | } |
212 | 221 | |