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

Revision: 90003
Author:   salvatoreingala
Date:     2011-06-13 19:56:53 +0000 (Mon, 13 Jun 2011)
Log Message:
-----------
Rewritten AJAX interaction with API modules instead of 'action=ajax' requests.

Modified Paths:
--------------
    branches/salvatoreingala/Gadgets/Gadgets.php
    branches/salvatoreingala/Gadgets/Gadgets_body.php
    branches/salvatoreingala/Gadgets/modules/ext.gadgets.preferences.js

Added Paths:
-----------
    branches/salvatoreingala/Gadgets/ApiGetGadgetPrefs.php
    branches/salvatoreingala/Gadgets/ApiSetGadgetPrefs.php

Removed Paths:
-------------
    branches/salvatoreingala/Gadgets/GadgetsAjax.php

Added: branches/salvatoreingala/Gadgets/ApiGetGadgetPrefs.php
===================================================================
--- branches/salvatoreingala/Gadgets/ApiGetGadgetPrefs.php                      
        (rev 0)
+++ branches/salvatoreingala/Gadgets/ApiGetGadgetPrefs.php      2011-06-13 
19:56:53 UTC (rev 90003)
@@ -0,0 +1,89 @@
+<?php
+
+/**
+ * 
+ * API for setting Gadget's preferences
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ */
+
+class ApiGetGadgetPrefs extends ApiBase {
+
+       public function execute() {
+               $user = RequestContext::getMain()->getUser();
+               
+               $params = $this->extractRequestParams();
+               //Check permissions
+               if ( !$user->isLoggedIn() ) {
+                       $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;
+                       }
+               }
+               
+               if ( !$found ) {
+                       $this->dieUsage( 'Gadget not found', 'notfound' );
+               }
+               
+               $prefsDescriptionJson = Gadget::getGadgetPrefsDescription( 
$gadget );
+               $prefsDescription = FormatJson::decode( $prefsDescriptionJson, 
true );
+               
+               if ( $prefsDescription === null ) {
+                       $this->dieUsage( "Gadget $gadget does not have any 
preference.", 'noprefs' );
+               }
+
+               $userPrefs = Gadget::getUserPrefs( $user, $gadget );
+               
+               //Add user preferences to preference description
+               foreach ( $userPrefs as $pref => $value ) {
+                       $prefsDescription['fields'][$pref]['value'] = $value;
+               }
+
+               $this->getResult()->addValue( null, $this->getModuleName(), 
$prefsDescription );
+       }
+
+       public function getAllowedParams() {
+               return array(
+                       'gadget'        => array(
+                               ApiBase::PARAM_TYPE => 'string',
+                               ApiBase::PARAM_REQUIRED => true
+                       )
+               );
+       }
+
+       public function getParamDescription() {
+               return array(
+                       'gadget'        => 'The name of the gadget'
+               );
+       }
+
+       public function getDescription() {
+               return 'Allows user code to get preferences for gadgets, along 
with preference descriptions';
+       }
+
+       public function getVersion() {
+               return __CLASS__ . ': $Id$';
+       }
+}


Property changes on: branches/salvatoreingala/Gadgets/ApiGetGadgetPrefs.php
___________________________________________________________________
Added: svn:eol-style
   + native

Added: branches/salvatoreingala/Gadgets/ApiSetGadgetPrefs.php
===================================================================
--- branches/salvatoreingala/Gadgets/ApiSetGadgetPrefs.php                      
        (rev 0)
+++ branches/salvatoreingala/Gadgets/ApiSetGadgetPrefs.php      2011-06-13 
19:56:53 UTC (rev 90003)
@@ -0,0 +1,120 @@
+<?php
+
+/**
+ * 
+ * API for setting Gadget's preferences
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ */
+
+class ApiSetGadgetPrefs extends ApiBase {
+
+       public function execute() {
+               $user = RequestContext::getMain()->getUser();
+               
+               $params = $this->extractRequestParams();
+               //Check permissions
+               if ( !$user->isLoggedIn() ) {
+                       $this->dieUsage( 'You must be logged-in to set 
gadget\'s preferences', 'notloggedin' );
+               }
+
+               //Check token
+               if ( !$user->matchEditToken( $params['token'] ) ) {
+                       $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;
+                       }
+               }
+               
+               if ( !$found ) {
+                       $this->dieUsage( 'Gadget not found', 'notfound' );
+               }
+
+               $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 );
+
+               if ( $result === true ) {
+                       $this->getResult()->addValue(
+                               null, $this->getModuleName(), array( 'result' 
=> 'Success' ) );
+               } else {
+                       $this->dieUsage( 'Invalid preferences.', 'invalidprefs' 
);
+               }
+       }
+
+       public function mustBePosted() {
+               return true;
+       }
+       
+       public function isWriteMode() {
+               return true;
+       }
+
+       public function getAllowedParams() {
+               return array(
+                       'gadget'        => array(
+                               ApiBase::PARAM_TYPE => 'string',
+                               ApiBase::PARAM_REQUIRED => true
+                       ),
+                       'prefs'         => array(
+                               ApiBase::PARAM_TYPE => 'string',
+                               ApiBase::PARAM_REQUIRED => true
+                       ),
+                       'token'         => array(
+                               ApiBase::PARAM_TYPE => 'string',
+                               ApiBase::PARAM_REQUIRED => true
+                       ),
+               );
+       }
+
+       public function getParamDescription() {
+               return array(
+                       'gadget'        => 'The name of the gadget',
+                       'prefs'         => 'The new preferences in JSON format',
+                       'token'         => 'An edit token'
+               );
+       }
+
+       public function getDescription() {
+               return 'Allows user code to set preferences for gadgets';
+       }
+
+       public function needsToken() {
+               return true;
+       }
+       
+       public function getSalt() {
+               return '';
+       }
+
+       public function getVersion() {
+               return __CLASS__ . ': $Id$';
+       }
+}


Property changes on: branches/salvatoreingala/Gadgets/ApiSetGadgetPrefs.php
___________________________________________________________________
Added: svn:eol-style
   + native

Modified: branches/salvatoreingala/Gadgets/Gadgets.php
===================================================================
--- branches/salvatoreingala/Gadgets/Gadgets.php        2011-06-13 19:55:59 UTC 
(rev 90002)
+++ branches/salvatoreingala/Gadgets/Gadgets.php        2011-06-13 19:56:53 UTC 
(rev 90003)
@@ -17,8 +17,8 @@
        die( 1 );
 }
 
-if ( version_compare( $wgVersion, '1.17alpha', '<' ) ) {
-       die( "This version of Extension:Gadgets requires MediaWiki 1.17+\n" );
+if ( version_compare( $wgVersion, '1.19alpha', '<' ) ) {
+       die( "This version of Extension:Gadgets requires MediaWiki 1.19+\n" );
 }
 
 $wgExtensionCredits['other'][] = array(
@@ -47,13 +47,14 @@
 $wgAutoloadClasses['GadgetResourceLoaderModule'] = $dir . 'Gadgets_body.php';
 $wgAutoloadClasses['SpecialGadgets'] = $dir . 'SpecialGadgets.php';
 $wgAutoloadClasses['GadgetsGlobalModule'] = $dir . 'Gadgets_body.php';
-$wgAutoloadClasses['GadgetsAjax'] = $dir . 'GadgetsAjax.php';
+$wgAutoloadClasses['ApiSetGadgetPrefs'] = $dir . 'ApiSetGadgetPrefs.php';
+$wgAutoloadClasses['ApiGetGadgetPrefs'] = $dir . 'ApiGetGadgetPrefs.php';
 
 $wgSpecialPages['Gadgets'] = 'SpecialGadgets';
 $wgSpecialPageGroups['Gadgets'] = 'wiki';
 
-$wgAPIListModules['gadgetcategories'] = 'ApiQueryGadgetCategories';
-$wgAPIListModules['gadgets'] = 'ApiQueryGadgets';
+$wgAPIModules['setgadgetprefs'] = 'ApiSetGadgetPrefs';
+$wgAPIModules['getgadgetprefs'] = 'ApiGetGadgetPrefs';
 
 $wgAjaxExportList[] = 'GadgetsAjax::getPreferences';
 $wgAjaxExportList[] = 'GadgetsAjax::setPreferences';

Deleted: branches/salvatoreingala/Gadgets/GadgetsAjax.php
===================================================================
--- branches/salvatoreingala/Gadgets/GadgetsAjax.php    2011-06-13 19:55:59 UTC 
(rev 90002)
+++ branches/salvatoreingala/Gadgets/GadgetsAjax.php    2011-06-13 19:56:53 UTC 
(rev 90003)
@@ -1,122 +0,0 @@
-<?php
-
-class GadgetsAjax {
-       
-       //Common validation code
-       //Checks if the user is logged and check params syntax
-       //returns error string if vaildation is failed, true otherwise
-       private static function validateSyntax( $args ) {
-               $user = RequestContext::getMain()->getUser();
-               if ( $user->isAnon() ) {
-                       return '<err#>' . wfMsgExt( 'gadgets-ajax-notallowed', 
'parseinline' );
-               }
-
-               //checks if all params are of the form 'param|value'
-               foreach ( $args as $arg ) {
-                       $set = explode( '|', $arg, 2 );
-                       if ( count( $set ) != 2 ) {
-                               return '<err#>' . wfMsgExt( 
'gadgets-ajax-wrongparams', 'parseinline' );
-                       }
-               }
-               
-               return true;
-       }
-       
-       public static function getPreferences( /* args */ ) {
-               //params are in the format "param|val"
-               $args = func_get_args();
-
-               $res = self::validateSyntax( $args );
-               if ( $res !== true ) {
-                       return $res;
-               }
-
-               foreach ( $args as $arg ) {
-                       list( $par, $val ) = explode( '|', $arg, 2 );;
-                       
-                       switch( $par ) {
-                               case "gadget":
-                                       $gadget = $val;
-                                       break;
-                               default:
-                                       return '<err#>' . wfMsgExt( 
'gadgets-ajax-wrongparams', 'parseinline' );
-                       }
-               }
-
-               if ( !isset( $gadget ) ) {
-                       return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongparams', 
'parseinline' );
-               }
-               
-               
-               $prefsDescriptionJson = Gadget::getGadgetPrefsDescription( 
$gadget );
-               $prefsDescription = FormatJson::decode( $prefsDescriptionJson, 
true );
-               
-               if ( $prefsDescription === null ) {
-                       //either the gadget doesn't exists or it exists but it 
has no prefs
-                       return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongparams', 
'parseinline' );
-               }
-
-               $user = RequestContext::getMain()->getUser();
-               $userPrefs = Gadget::getUserPrefs( $user, $gadget );
-
-               //Add user preferences to preference description
-               foreach ( $userPrefs as $pref => $value ) {
-                       $prefsDescription['fields'][$pref]['value'] = $value;
-               }               
-
-               return FormatJson::encode( $prefsDescription );
-       }
-       
-       public static function setPreferences( /* args */ ) {
-               //TODO: should probably add tokens
-               
-               //params are in the format "param|val"
-               $args = func_get_args();
-
-               $res = self::validateSyntax( $args );
-               if ( $res !== true ) {
-                       return $res;
-               }
-
-               foreach ( $args as $arg ) {
-                       list( $par, $val ) = explode( '|', $arg, 2 );;
-                       
-                       switch( $par ) {
-                               case "gadget":
-                                       $gadget = $val;
-                                       break;
-                               case "json":
-                                       $json = $val;
-                                       break;
-                               default:
-                                       return '<err#>' . wfMsgExt( 
'gadgets-ajax-wrongparams', 'parseinline' );
-                       }
-               }
-
-               if ( !isset( $gadget ) || !isset( $json ) ) {
-                       return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongparams', 
'parseinline' );
-               }
-               
-               $prefsDescriptionJson = Gadget::getGadgetPrefsDescription( 
$gadget );
-               $prefsDescription = FormatJson::decode( $prefsDescriptionJson, 
true );
-               
-               if ( $prefsDescription === null ) {
-                       //either the gadget doesn't exists or it exists but it 
has no prefs
-                       return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongparams', 
'parseinline' );
-               }
-               
-               $userPrefs = FormatJson::decode( $json, true );
-               
-               if ( $userPrefs === null ) {
-                       return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongparams', 
'parseinline' );
-               }
-               
-               $user = RequestContext::getMain()->getUser();
-               
-               if ( Gadget::setUserPrefs( $user, $gadget, $userPrefs ) ) {
-                       return 'true';                  
-               } else {
-                       return 'false';
-               }
-       }
-}

Modified: branches/salvatoreingala/Gadgets/Gadgets_body.php
===================================================================
--- branches/salvatoreingala/Gadgets/Gadgets_body.php   2011-06-13 19:55:59 UTC 
(rev 90002)
+++ branches/salvatoreingala/Gadgets/Gadgets_body.php   2011-06-13 19:56:53 UTC 
(rev 90003)
@@ -996,7 +996,6 @@
        //Set user's preferences for a specific 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 );
                
                if ( $prefsDescriptionJson === null || $prefsDescriptionJson 
=== '' ) {
@@ -1141,7 +1140,7 @@
                $gadgetsList = Gadget::loadStructuredList();
                
                foreach ( $gadgetsList as $sectionName => $gadgets ) {
-                       foreach ( $gadgets as $gadget => $gadget_data ) {
+                       foreach ( $gadgets as $gadget => $gadgetData ) {
                                $prefs = Gadget::getGadgetPrefsDescription( 
$gadget );
                                if ( $prefs !== null && $prefs !== '' ) {
                                        $configurableGadgets[] = $gadget;

Modified: branches/salvatoreingala/Gadgets/modules/ext.gadgets.preferences.js
===================================================================
--- branches/salvatoreingala/Gadgets/modules/ext.gadgets.preferences.js 
2011-06-13 19:55:59 UTC (rev 90002)
+++ branches/salvatoreingala/Gadgets/modules/ext.gadgets.preferences.js 
2011-06-13 19:56:53 UTC (rev 90003)
@@ -5,19 +5,21 @@
        
        //"Save" button click handler
        function saveConfig( $dialog, gadget, config ) {
-               var json = $.toJSON( config );
+               var prefsJson = $.toJSON( config );
                
-               var postData = 'action=ajax&rs=GadgetsAjax::setPreferences' +
-                               '&rsargs[]=gadget|' + encodeURIComponent( 
gadget ) +
-                               '&rsargs[]=json|' + encodeURIComponent( json );
-
                $.ajax( {
-                       url: mw.config.get( 'wgScriptPath' ) + '/index.php',
+                       url: mw.config.get( 'wgScriptPath' ) + '/api.php',
                        type: "POST",
-                       data: postData,
+                       data: {
+                               'action': 'setgadgetprefs',
+                               'gadget': gadget,
+                               'prefs': prefsJson,
+                               'token': mw.user.tokens.get('editToken'),
+                               'format': 'json'
+                       },
                        dataType: "json",
                        success: function( response ) {
-                               if ( response === true ) {
+                               if ( typeof response.error == 'undefined' ) {
                                        alert( mw.msg( 'gadgets-save-success' ) 
);
                                        $dialog.dialog( 'close' );
                                } else {
@@ -44,21 +46,28 @@
                        var $link = $( '<a></a>' )
                                .text( mw.msg( 'gadgets-configure' ) )
                                .click( function() {
-                                       var postData = 
'action=ajax&rs=GadgetsAjax::getPreferences' +
-                                                       '&rsargs[]=gadget|' + 
encodeURIComponent( gadget );
-                                                       
                                        $.ajax( {
-                                               url: mw.config.get( 
'wgScriptPath' ) + '/index.php',
+                                               url: mw.config.get( 
'wgScriptPath' ) + '/api.php',
                                                type: "POST",
-                                               data: postData,
+                                               data: {
+                                                       'action': 
'getgadgetprefs',
+                                                       'gadget': gadget,
+                                                       'format': 'json'
+                                               },
                                                dataType: "json", // response 
type
                                                success: function( response ) {
-                                                       //TODO: malformed 
response?
                                                        
+                                                       if ( typeof 
response.getgadgetprefs != 'object' ) {
+                                                               alert( mw.msg( 
'gadgets-unexpected-error' ) )
+                                                               return;
+                                                       }
+                                                       
                                                        //Create and show dialog
                                                        
-                                                       var dialogBody = $( 
response ).formBuilder();
+                                                       var prefs = 
response.getgadgetprefs;
                                                        
+                                                       var dialogBody = $( 
prefs ).formBuilder();
+                                                       
                                                        $( dialogBody ).submit( 
function() {
                                                                return false; 
//prevent form submission
                                                        } );


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

Reply via email to