http://www.mediawiki.org/wiki/Special:Code/MediaWiki/88644

Revision: 88644
Author:   salvatoreingala
Date:     2011-05-23 15:07:38 +0000 (Mon, 23 May 2011)
Log Message:
-----------
- 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
    branches/salvatoreingala/Gadgets/GadgetsAjax.php
    branches/salvatoreingala/Gadgets/Gadgets_body.php
    branches/salvatoreingala/Gadgets/modules/ext.gadgets.preferences.js

Modified: branches/salvatoreingala/Gadgets/Gadgets.i18n.php
===================================================================
--- branches/salvatoreingala/Gadgets/Gadgets.i18n.php   2011-05-23 15:00:50 UTC 
(rev 88643)
+++ branches/salvatoreingala/Gadgets/Gadgets.i18n.php   2011-05-23 15:07:38 UTC 
(rev 88644)
@@ -44,6 +44,7 @@
 You must have appropriate permissions on destination wiki (including the right 
to edit system messages) and import from file uploads must be enabled.',
        'gadgets-export-download' => 'Download',
        'gadgets-ajax-wrongparams' => 'An AJAX request with wrong parameters 
has been made; this is most likely a bug.',
+       'gadgets-ajax-wrongsyntax' => 'There was an unexpected error while 
reading the saved gadget\'s configuration description.',
        'gadgets-ajax-unlogged' => 'This action is only allowed to registered, 
logged in users.',
 );
 

Modified: branches/salvatoreingala/Gadgets/GadgetsAjax.php
===================================================================
--- branches/salvatoreingala/Gadgets/GadgetsAjax.php    2011-05-23 15:00:50 UTC 
(rev 88643)
+++ branches/salvatoreingala/Gadgets/GadgetsAjax.php    2011-05-23 15:07:38 UTC 
(rev 88644)
@@ -29,22 +29,25 @@
                }
 
                if ( !isset( $gadget ) ) {
-                       return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongparams', 
'parseinline' );                                
+                       return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongparams', 
'parseinline' );
                }
                
                $prefs_json = Gadget::getGadgetPrefsDescription( $gadget );
                
                //If $gadget doesn't exists or it doesn't have preferences, 
something is wrong
-               if ( $prefs_json === NULL || $prefs_json === '' ) {
-                       return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongparams', 
'parseinline' );                                
+               if ( $prefs_json === null || $prefs_json === '' ) {
+                       return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongparams', 
'parseinline' );
                }
                
-               $prefs = json_decode( $prefs_json, TRUE );
+               $prefs = json_decode( $prefs_json, true );
                
-               //TODO: $prefs === NULL?
+               //If it's not valid JSON, signal an error
+               if ( $prefs === null ) {
+                       return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongsyntax', 
'parseinline' );
+               }
                
-               //TODO: check correct usage of RequestContext
-               $form = new HTMLForm( $prefs, new RequestContext(), 
'gadget-prefs' );
+               //TODO: options of "select" and similar fields cannot be passed 
as messages
+               $form = new HTMLForm( $prefs, RequestContext::getMain() );
                
                $form->mFieldData = Gadget::getUserPrefs( $wgUser, $gadget );
 

Modified: branches/salvatoreingala/Gadgets/Gadgets_body.php
===================================================================
--- branches/salvatoreingala/Gadgets/Gadgets_body.php   2011-05-23 15:00:50 UTC 
(rev 88643)
+++ branches/salvatoreingala/Gadgets/Gadgets_body.php   2011-05-23 15:07:38 UTC 
(rev 88644)
@@ -114,6 +114,7 @@
                
                wfProfileIn( __METHOD__ );
 
+               //tweaks in Special:Preferences
                if ( $out->getTitle()->isSpecial( 'Preferences' ) ) {
                        $out->addModules( 'ext.gadgets.preferences' );
                }
@@ -207,6 +208,21 @@
                        $onByDefault = false,
                        $category;
 
+
+       //Mandatory arguments for any kind of preferences
+       private static $prefs_mandatory_args = array(
+                       'type',
+                       'default'
+               );
+
+       //Other mandatory arguments for specific types
+       private static $prefs_mandatory_args_by_type = array(
+                       'select' => array( 'options' ),
+                       'selectorother' => array( 'options' ),
+                       'selectandother' => array( 'options' ),
+                       'multiselect' => array( 'options' )
+               );
+
        /**
         * Creates an instance of this class from definition in 
MediaWiki:Gadgets-definition
         * @param $definition String: Gadget definition
@@ -531,51 +547,65 @@
                return $gadgets;
        }
        
+       
+       public static function isGadgetPrefsDescriptionValid( $prefs_json ) {
+               $prefs = json_decode( $prefs_json, true );
+               
+               if ( $prefs === null ) {
+                       return false;
+               }
+               
+               //TODO: improve validation
+               foreach ( $prefs as $option => $option_definition ) {
+                       foreach ( self::$prefs_mandatory_args as $arg ) {
+                               if ( !isset( $option_definition[$arg] ) ) {
+                                       return false;
+                               }
+                       }
+                       
+                       $type = $option['type'];
+                       
+                       if ( isset( self::$prefs_mandatory_args_by_type[$type] 
) ) {
+                               foreach ( 
self::$prefs_mandatory_args_by_type[$type] as $arg ) {
+                                       if ( !isset( $option_definition[$arg] ) 
) {
+                                               return false;
+                                       }
+                               }
+                       }
+               }
+               
+               return true;
+       }
+       
        //Gets preferences for gadget $gadget;
-       // returns * NULL if the gadget doesn't exists
+       // returns * null if the gadget doesn't exists
        //         * '' if the gadget exists but doesn't have any preferences
        //         * the preference description in JSON format, otherwise
        public static function getGadgetPrefsDescription( $gadget ) {
-               //TODO: load gadget's preference description
-
-               //For now we assume there is only one gadget, named Test
-               if ( $gadget != 'Test' ) {
-                       return NULL;
+               $gadgets_list = Gadget::loadStructuredList();
+               foreach ( $gadgets_list as $section_name => $gadgets ) {
+                       foreach ( $gadgets as $gadget_name => $gadget_data ) {
+                               if ( $gadget_name == $gadget ) {
+                                       //Gadget found; are there any prefs?
+                                       
+                                       $prefs_msg = "Gadget-" . $gadget . 
".preferences";
+                                       
+                                       //TODO: should we cache?
+                                       
+                                       $prefs_json = wfMsgForContentNoTrans( 
$prefs_msg );
+                                       if ( wfEmptyMsg( $prefs_msg, 
$prefs_json ) ) {
+                                               return null;
+                                       }
+                                       
+                                       if ( 
!self::isGadgetPrefsDescriptionValid( $prefs_json ) ){
+                                               return '';
+                                       }
+                                       
+                                       return $prefs_json;
+                               }
+                       }
                }
-               //And here is his preferences description.
-               return <<<EOD
-{
-  "popupDelay": {
-    "type": "float",
-    "default": 0.5,
-    "label-message": "popup-delay-description",
-    "options": {
-      "min": 0,
-      "max": 3
-    }
-  },
-  "popupHideDelay": {
-    "type": "float",
-    "default": 0.5,
-    "label-message": "popup-hidedelay-description",
-    "options": {
-      "min": 0
-    }
-  },
-  "popupModifier": {
-    "type": "select",
-    "default": "",
-    "label-message": "popup-modifier-description",
-    "options": {
-      "popup-modifier-nothing": "",
-      "popup-modifier-ctrl": "ctrl",
-      "popup-modifier-shift": "shift",
-      "popup-modifier-alt": "alt",
-      "popup-modifier-meta": "meta"
-    }
-  }
-}
-EOD;
+               return null; //gadget not found
        }
 
        //Get user's preferences for a specific gadget
@@ -584,11 +614,11 @@
                //for now, we just return defaults
                $prefs_json = Gadget::getGadgetPrefsDescription( $gadget );
                
-               if ( $prefs_json === NULL || $prefs_json === '' ) {
-                       return NULL;
+               if ( $prefs_json === null || $prefs_json === '' ) {
+                       return null;
                }
                
-               $prefs = json_decode( $prefs_json, TRUE );
+               $prefs = json_decode( $prefs_json, true );
                
                $userPrefs = array();
                
@@ -660,12 +690,13 @@
                foreach ( $gadgets_list as $section_name => $gadgets ) {
                        foreach ( $gadgets as $gadget => $gadget_data ) {
                                $prefs = Gadget::getGadgetPrefsDescription( 
$gadget );
-                               if ( $prefs !== NULL && $prefs !== '' ) {
+                               if ( $prefs !== null && $prefs !== '' ) {
                                        $configurable_gadgets[] = $gadget;
                                }
                        }
                }
                
+               //TODO: broken in debug mode
                //create the mw.gadgets object
                $script = "mw.gadgets = {}\n";
                //needed by ext.gadgets.preferences.js

Modified: branches/salvatoreingala/Gadgets/modules/ext.gadgets.preferences.js
===================================================================
--- branches/salvatoreingala/Gadgets/modules/ext.gadgets.preferences.js 
2011-05-23 15:00:50 UTC (rev 88643)
+++ branches/salvatoreingala/Gadgets/modules/ext.gadgets.preferences.js 
2011-05-23 15:07:38 UTC (rev 88644)
@@ -7,13 +7,13 @@
     var gadget = id.substr( "mw-input-wpgadgets-".length );
 
     if ( $.inArray( gadget, mw.gadgets.configurableGadgets ) != -1 ) {
-      $span = $( '<span></span>' );
+      var $span = $( '<span></span>' );
 
       if ( !$( input ).is( ':checked' ) ) {
         $span.hide();
       }
 
-      $link = $( '<a></a>' )
+      var $link = $( '<a></a>' )
               .text( "Configure" ) //TODO: use a message instead
               .click( function() {
                 var post_data = 'action=ajax&rs=GadgetsAjax::getUI' +
@@ -60,7 +60,7 @@
 
       //Toggle visibility on click to the input
       $( input ).click( function() {
-        $span.toggle( 'fast' );
+        $span.fadeToggle( 'fast' );
       } );
     }
   } );


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to