r88644 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r88643‎ | r88644 | r88645 >
Date:15:07, 23 May 2011
Author:salvatoreingala
Status:deferred (Comments)
Tags:
Comment:
- Added code to get preference descriptions (from MediaWiki:Gadget-<gadgetname>.preferences)
- Added code for (partial) validation of preference descriptions
- Bugfixes and code improvements here and there
Modified paths:
  • /branches/salvatoreingala/Gadgets/Gadgets.i18n.php (modified) (history)
  • /branches/salvatoreingala/Gadgets/GadgetsAjax.php (modified) (history)
  • /branches/salvatoreingala/Gadgets/Gadgets_body.php (modified) (history)
  • /branches/salvatoreingala/Gadgets/modules/ext.gadgets.preferences.js (modified) (history)

Diff [purge]

Index: branches/salvatoreingala/Gadgets/Gadgets_body.php
@@ -114,6 +114,7 @@
115115
116116 wfProfileIn( __METHOD__ );
117117
 118+ //tweaks in Special:Preferences
118119 if ( $out->getTitle()->isSpecial( 'Preferences' ) ) {
119120 $out->addModules( 'ext.gadgets.preferences' );
120121 }
@@ -207,6 +208,21 @@
208209 $onByDefault = false,
209210 $category;
210211
 212+
 213+ //Mandatory arguments for any kind of preferences
 214+ private static $prefs_mandatory_args = array(
 215+ 'type',
 216+ 'default'
 217+ );
 218+
 219+ //Other mandatory arguments for specific types
 220+ private static $prefs_mandatory_args_by_type = array(
 221+ 'select' => array( 'options' ),
 222+ 'selectorother' => array( 'options' ),
 223+ 'selectandother' => array( 'options' ),
 224+ 'multiselect' => array( 'options' )
 225+ );
 226+
211227 /**
212228 * Creates an instance of this class from definition in MediaWiki:Gadgets-definition
213229 * @param $definition String: Gadget definition
@@ -531,51 +547,65 @@
532548 return $gadgets;
533549 }
534550
 551+
 552+ public static function isGadgetPrefsDescriptionValid( $prefs_json ) {
 553+ $prefs = json_decode( $prefs_json, true );
 554+
 555+ if ( $prefs === null ) {
 556+ return false;
 557+ }
 558+
 559+ //TODO: improve validation
 560+ foreach ( $prefs as $option => $option_definition ) {
 561+ foreach ( self::$prefs_mandatory_args as $arg ) {
 562+ if ( !isset( $option_definition[$arg] ) ) {
 563+ return false;
 564+ }
 565+ }
 566+
 567+ $type = $option['type'];
 568+
 569+ if ( isset( self::$prefs_mandatory_args_by_type[$type] ) ) {
 570+ foreach ( self::$prefs_mandatory_args_by_type[$type] as $arg ) {
 571+ if ( !isset( $option_definition[$arg] ) ) {
 572+ return false;
 573+ }
 574+ }
 575+ }
 576+ }
 577+
 578+ return true;
 579+ }
 580+
535581 //Gets preferences for gadget $gadget;
536 - // returns * NULL if the gadget doesn't exists
 582+ // returns * null if the gadget doesn't exists
537583 // * '' if the gadget exists but doesn't have any preferences
538584 // * the preference description in JSON format, otherwise
539585 public static function getGadgetPrefsDescription( $gadget ) {
540 - //TODO: load gadget's preference description
541 -
542 - //For now we assume there is only one gadget, named Test
543 - if ( $gadget != 'Test' ) {
544 - return NULL;
 586+ $gadgets_list = Gadget::loadStructuredList();
 587+ foreach ( $gadgets_list as $section_name => $gadgets ) {
 588+ foreach ( $gadgets as $gadget_name => $gadget_data ) {
 589+ if ( $gadget_name == $gadget ) {
 590+ //Gadget found; are there any prefs?
 591+
 592+ $prefs_msg = "Gadget-" . $gadget . ".preferences";
 593+
 594+ //TODO: should we cache?
 595+
 596+ $prefs_json = wfMsgForContentNoTrans( $prefs_msg );
 597+ if ( wfEmptyMsg( $prefs_msg, $prefs_json ) ) {
 598+ return null;
 599+ }
 600+
 601+ if ( !self::isGadgetPrefsDescriptionValid( $prefs_json ) ){
 602+ return '';
 603+ }
 604+
 605+ return $prefs_json;
 606+ }
 607+ }
545608 }
546 - //And here is his preferences description.
547 - return <<<EOD
548 -{
549 - "popupDelay": {
550 - "type": "float",
551 - "default": 0.5,
552 - "label-message": "popup-delay-description",
553 - "options": {
554 - "min": 0,
555 - "max": 3
556 - }
557 - },
558 - "popupHideDelay": {
559 - "type": "float",
560 - "default": 0.5,
561 - "label-message": "popup-hidedelay-description",
562 - "options": {
563 - "min": 0
564 - }
565 - },
566 - "popupModifier": {
567 - "type": "select",
568 - "default": "",
569 - "label-message": "popup-modifier-description",
570 - "options": {
571 - "popup-modifier-nothing": "",
572 - "popup-modifier-ctrl": "ctrl",
573 - "popup-modifier-shift": "shift",
574 - "popup-modifier-alt": "alt",
575 - "popup-modifier-meta": "meta"
576 - }
577 - }
578 -}
579 -EOD;
 609+ return null; //gadget not found
580610 }
581611
582612 //Get user's preferences for a specific gadget
@@ -584,11 +614,11 @@
585615 //for now, we just return defaults
586616 $prefs_json = Gadget::getGadgetPrefsDescription( $gadget );
587617
588 - if ( $prefs_json === NULL || $prefs_json === '' ) {
589 - return NULL;
 618+ if ( $prefs_json === null || $prefs_json === '' ) {
 619+ return null;
590620 }
591621
592 - $prefs = json_decode( $prefs_json, TRUE );
 622+ $prefs = json_decode( $prefs_json, true );
593623
594624 $userPrefs = array();
595625
@@ -660,12 +690,13 @@
661691 foreach ( $gadgets_list as $section_name => $gadgets ) {
662692 foreach ( $gadgets as $gadget => $gadget_data ) {
663693 $prefs = Gadget::getGadgetPrefsDescription( $gadget );
664 - if ( $prefs !== NULL && $prefs !== '' ) {
 694+ if ( $prefs !== null && $prefs !== '' ) {
665695 $configurable_gadgets[] = $gadget;
666696 }
667697 }
668698 }
669699
 700+ //TODO: broken in debug mode
670701 //create the mw.gadgets object
671702 $script = "mw.gadgets = {}\n";
672703 //needed by ext.gadgets.preferences.js
Index: branches/salvatoreingala/Gadgets/Gadgets.i18n.php
@@ -44,6 +44,7 @@
4545 You must have appropriate permissions on destination wiki (including the right to edit system messages) and import from file uploads must be enabled.',
4646 'gadgets-export-download' => 'Download',
4747 'gadgets-ajax-wrongparams' => 'An AJAX request with wrong parameters has been made; this is most likely a bug.',
 48+ 'gadgets-ajax-wrongsyntax' => 'There was an unexpected error while reading the saved gadget\'s configuration description.',
4849 'gadgets-ajax-unlogged' => 'This action is only allowed to registered, logged in users.',
4950 );
5051
Index: branches/salvatoreingala/Gadgets/GadgetsAjax.php
@@ -29,22 +29,25 @@
3030 }
3131
3232 if ( !isset( $gadget ) ) {
33 - return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongparams', 'parseinline' );
 33+ return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongparams', 'parseinline' );
3434 }
3535
3636 $prefs_json = Gadget::getGadgetPrefsDescription( $gadget );
3737
3838 //If $gadget doesn't exists or it doesn't have preferences, something is wrong
39 - if ( $prefs_json === NULL || $prefs_json === '' ) {
40 - return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongparams', 'parseinline' );
 39+ if ( $prefs_json === null || $prefs_json === '' ) {
 40+ return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongparams', 'parseinline' );
4141 }
4242
43 - $prefs = json_decode( $prefs_json, TRUE );
 43+ $prefs = json_decode( $prefs_json, true );
4444
45 - //TODO: $prefs === NULL?
 45+ //If it's not valid JSON, signal an error
 46+ if ( $prefs === null ) {
 47+ return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongsyntax', 'parseinline' );
 48+ }
4649
47 - //TODO: check correct usage of RequestContext
48 - $form = new HTMLForm( $prefs, new RequestContext(), 'gadget-prefs' );
 50+ //TODO: options of "select" and similar fields cannot be passed as messages
 51+ $form = new HTMLForm( $prefs, RequestContext::getMain() );
4952
5053 $form->mFieldData = Gadget::getUserPrefs( $wgUser, $gadget );
5154
Index: branches/salvatoreingala/Gadgets/modules/ext.gadgets.preferences.js
@@ -7,13 +7,13 @@
88 var gadget = id.substr( "mw-input-wpgadgets-".length );
99
1010 if ( $.inArray( gadget, mw.gadgets.configurableGadgets ) != -1 ) {
11 - $span = $( '<span></span>' );
 11+ var $span = $( '<span></span>' );
1212
1313 if ( !$( input ).is( ':checked' ) ) {
1414 $span.hide();
1515 }
1616
17 - $link = $( '<a></a>' )
 17+ var $link = $( '<a></a>' )
1818 .text( "Configure" ) //TODO: use a message instead
1919 .click( function() {
2020 var post_data = 'action=ajax&rs=GadgetsAjax::getUI' +
@@ -60,7 +60,7 @@
6161
6262 //Toggle visibility on click to the input
6363 $( input ).click( function() {
64 - $span.toggle( 'fast' );
 64+ $span.fadeToggle( 'fast' );
6565 } );
6666 }
6767 } );

Follow-up revisions

RevisionCommit summaryAuthorDate
r88647- Fixed coding style issues in previous commits (r88282 & r88644)...salvatoreingala16:36, 23 May 2011

Comments

#Comment by MaxSem (talk | contribs)   15:51, 23 May 2011
  • Please follow our coding conventions: $camelCase instead of $unix_style and so on.
  • json_decode() may be broken, use FormatJson::decode()

Status & tagging log