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

Revision: 90127
Author:   salvatoreingala
Date:     2011-06-15 18:03:54 +0000 (Wed, 15 Jun 2011)
Log Message:
-----------
Replaced static methods to get/set preferences and preference descriptions with 
member functions.

Modified Paths:
--------------
    branches/salvatoreingala/Gadgets/ApiGetGadgetPrefs.php
    branches/salvatoreingala/Gadgets/ApiSetGadgetPrefs.php
    branches/salvatoreingala/Gadgets/Gadgets_body.php

Modified: branches/salvatoreingala/Gadgets/ApiGetGadgetPrefs.php
===================================================================
--- branches/salvatoreingala/Gadgets/ApiGetGadgetPrefs.php      2011-06-15 
17:59:02 UTC (rev 90126)
+++ branches/salvatoreingala/Gadgets/ApiGetGadgetPrefs.php      2011-06-15 
18:03:54 UTC (rev 90127)
@@ -31,30 +31,22 @@
                        $this->dieUsage( 'You must be logged-in to get 
gadget\'s preferences', 'notloggedin' );
                }
 
-               $gadget = $params['gadget'];
-
-               //Checks if the gadget actually exists
-               $gadgetsList = Gadget::loadStructuredList();
-               $found = false;
-               foreach ( $gadgetsList as $section => $gadgets ) {
-                       if ( isset( $gadgets[$gadget] ) ) {
-                               $found = true;
-                               break;
-                       }
-               }
+               $gadgetName = $params['gadget'];
+               $gadgets = Gadget::loadList();
+               $gadget = $gadgets && isset( $gadgets[$gadgetName] ) ? 
$gadgets[$gadgetName] : null;
                
-               if ( !$found ) {
+               if ( $gadget === null ) {
                        $this->dieUsage( 'Gadget not found', 'notfound' );
                }
                
-               $prefsDescriptionJson = Gadget::getGadgetPrefsDescription( 
$gadget );
+               $prefsDescriptionJson = $gadget->getPrefsDescription();
                $prefsDescription = FormatJson::decode( $prefsDescriptionJson, 
true );
                
                if ( $prefsDescription === null ) {
-                       $this->dieUsage( "Gadget $gadget does not have any 
preference.", 'noprefs' );
+                       $this->dieUsage( 'Gadget ' . $gadget->getName() . ' 
does not have any preference.', 'noprefs' );
                }
 
-               $userPrefs = Gadget::getUserPrefs( $user, $gadget );
+               $userPrefs = $gadget->getUserPrefs( $user );
                
                //Add user preferences to preference description
                foreach ( $userPrefs as $pref => $value ) {

Modified: branches/salvatoreingala/Gadgets/ApiSetGadgetPrefs.php
===================================================================
--- branches/salvatoreingala/Gadgets/ApiSetGadgetPrefs.php      2011-06-15 
17:59:02 UTC (rev 90126)
+++ branches/salvatoreingala/Gadgets/ApiSetGadgetPrefs.php      2011-06-15 
18:03:54 UTC (rev 90127)
@@ -36,30 +36,22 @@
                        $this->dieUsageMsg( 'sessionfailure' );
                }
 
-               $gadget = $params['gadget'];
-               $prefsJson = $params['prefs'];
-
-               //Checks if the gadget actually exists
-               $gadgetsList = Gadget::loadStructuredList();
-               $found = false;
-               foreach ( $gadgetsList as $section => $gadgets ) {
-                       if ( isset( $gadgets[$gadget] ) ) {
-                               $found = true;
-                               break;
-                       }
-               }
+               $gadgetName = $params['gadget'];
+               $gadgets = Gadget::loadList();
+               $gadget = $gadgets && isset( $gadgets[$gadgetName] ) ? 
$gadgets[$gadgetName] : null;
                
-               if ( !$found ) {
+               if ( $gadget === null ) {
                        $this->dieUsage( 'Gadget not found', 'notfound' );
                }
 
+               $prefsJson = $params['prefs'];
                $prefs = FormatJson::decode( $prefsJson, true );
                
                if ( !is_array( $prefs ) ) {
                        $this->dieUsage( 'The \'pref\' parameter must be valid 
JSON', 'notjson' );
                }
 
-               $result = Gadget::setUserPrefs( $user, $gadget, $prefs );
+               $result = $gadget->setUserPrefs( $user, $prefs );
 
                if ( $result === true ) {
                        $this->getResult()->addValue(

Modified: branches/salvatoreingala/Gadgets/Gadgets_body.php
===================================================================
--- branches/salvatoreingala/Gadgets/Gadgets_body.php   2011-06-15 17:59:02 UTC 
(rev 90126)
+++ branches/salvatoreingala/Gadgets/Gadgets_body.php   2011-06-15 18:03:54 UTC 
(rev 90127)
@@ -729,7 +729,6 @@
                return $gadgets;
        }
        
-       
        //TODO: put the following static methods somewhere else
        
        //Checks if the given description of the preferences is valid
@@ -809,34 +808,21 @@
        }
        
        //Gets preferences for gadget $gadget;
-       // returns * null if the gadget doesn't exists
-       //         * '' if the gadget exists but doesn't have any preferences
+       // returns * '' if the gadget exists but doesn't have any preferences 
(or provided ones are not valid)
        //         * the preference description in JSON format, otherwise
-       public static function getGadgetPrefsDescription( $gadget ) {
-               $gadgetsList = Gadget::loadStructuredList();
-               foreach ( $gadgetsList as $sectionName => $gadgets ) {
-                       foreach ( $gadgets as $gadgetName => $gadgetData ) {
-                               if ( $gadgetName == $gadget ) {
-                                       //Gadget found; are there any prefs?
-                                       
-                                       $prefsMsg = "Gadget-" . $gadget . 
".preferences";
-                                       
-                                       //TODO: should we cache?
-                                       
-                                       $prefsJson = wfMsgForContentNoTrans( 
$prefsMsg );
-                                       if ( wfEmptyMsg( $prefsMsg, $prefsJson 
) ) {
-                                               return null;
-                                       }
-                                       
-                                       if ( 
!self::isGadgetPrefsDescriptionValid( $prefsJson ) ){
-                                               return '';
-                                       }
-                                       
-                                       return $prefsJson;
-                               }
-                       }
+       public function getPrefsDescription() {
+               $prefsMsg = "Gadget-{$this->name}.preferences";
+               
+               //TODO: use cache
+               
+               $prefsJson = wfMsgForContentNoTrans( $prefsMsg );
+               if ( wfEmptyMsg( $prefsMsg, $prefsJson ) ||
+                       !self::isGadgetPrefsDescriptionValid( $prefsJson ) )
+               {
+                       return '';
                }
-               return null; //gadget not found
+               
+               return $prefsJson;
        }
 
        //Check if a preference is valid, according to description
@@ -953,13 +939,13 @@
                }
        }
 
-       //Get user's preferences for a specific gadget
-       public static function getUserPrefs( $user, $gadget ) {
+       //Get user's preferences for this gadget
+       public function getUserPrefs( $user ) {
                //TODO: cache!
 
-               $prefsDescriptionJson = Gadget::getGadgetPrefsDescription( 
$gadget );
+               $prefsDescriptionJson = $this->getPrefsDescription();
                
-               if ( $prefsDescriptionJson === null || $prefsDescriptionJson 
=== '' ) {
+               if ( $prefsDescriptionJson === '' ) {
                        return null;
                }
                
@@ -969,7 +955,7 @@
                $dbr = wfGetDB( DB_SLAVE );
                
                $id = $user->getId();
-               $property = "gadget-{$gadget}-config";
+               $property = "gadget-{$this->name}-config";
 
                $res = $dbr->selectRow(
                        'user_properties',
@@ -993,12 +979,12 @@
                return $userPrefs;
        }
 
-       //Set user's preferences for a specific gadget.
+       //Set user's preferences for this gadget.
        //Returns false if preferences are rejected (that is, they don't pass 
validation)
-       public static function setUserPrefs( $user, $gadget, &$preferences ) {
-               $prefsDescriptionJson = Gadget::getGadgetPrefsDescription( 
$gadget );
+       public function setUserPrefs( $user, &$preferences ) {
+               $prefsDescriptionJson = $this->getPrefsDescription();
                
-               if ( $prefsDescriptionJson === null || $prefsDescriptionJson 
=== '' ) {
+               if ( $prefsDescriptionJson === '' ) {
                        return false; //nothing to save
                }
                
@@ -1017,7 +1003,7 @@
                $dbw = wfGetDB( DB_MASTER );
                
                $id = $user->getId();
-               $property = "gadget-{$gadget}-config";
+               $property = "gadget-{$this->name}-config";
                
                $row = array(
                                'up_user'     => $id,
@@ -1095,12 +1081,13 @@
        
        public function getScript( ResourceLoaderContext $context ) {
                $moduleName = $this->getName();
-               $gadget = substr( $moduleName, strlen( 'ext.gadget.' ) );
+               $gadgetName = substr( $moduleName, strlen( 'ext.gadget.' ) );
+               $gadgets = Gadget::loadList();
+               $gadget = $gadgets[$gadgetName];
                
-               
                $user = RequestContext::getMain()->getUser();
                
-               $prefs = Gadget::getUserPrefs( $user, $gadget );
+               $prefs = $gadget->getUserPrefs( $user );
                
                //Enclose gadget's code in a closure, with "this" bound to the
                //configuration object (or to "window" for non-configurable 
gadgets)
@@ -1123,7 +1110,7 @@
        }
        
        
-       //TODO: should depend on gadget's last modification time, also
+       //TODO: should depend on last modification time of gadget's 
configuration page, also
        public function getModifiedTime( ResourceLoaderContext $context ) {
                $touched = RequestContext::getMain()->getUser()->getTouched();
                
@@ -1139,11 +1126,11 @@
                $configurableGadgets = array();
                $gadgetsList = Gadget::loadStructuredList();
                
-               foreach ( $gadgetsList as $sectionName => $gadgets ) {
-                       foreach ( $gadgets as $gadget => $gadgetData ) {
-                               $prefs = Gadget::getGadgetPrefsDescription( 
$gadget );
-                               if ( $prefs !== null && $prefs !== '' ) {
-                                       $configurableGadgets[] = $gadget;
+               foreach ( $gadgetsList as $section => $gadgets ) {
+                       foreach ( $gadgets as $gadgetName => $gadget ) {
+                               $prefs = $gadget->getPrefsDescription();
+                               if ( $prefs !== '' ) {
+                                       $configurableGadgets[] = 
$gadget->getName();
                                }
                        }
                }


_______________________________________________
MediaWiki-CVS mailing list
MediaWiki-CVS@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to