Index: branches/salvatoreingala/Gadgets/GadgetsAjax.php |
— | — | @@ -1,122 +0,0 @@ |
2 | | -<?php |
3 | | - |
4 | | -class GadgetsAjax { |
5 | | - |
6 | | - //Common validation code |
7 | | - //Checks if the user is logged and check params syntax |
8 | | - //returns error string if vaildation is failed, true otherwise |
9 | | - private static function validateSyntax( $args ) { |
10 | | - $user = RequestContext::getMain()->getUser(); |
11 | | - if ( $user->isAnon() ) { |
12 | | - return '<err#>' . wfMsgExt( 'gadgets-ajax-notallowed', 'parseinline' ); |
13 | | - } |
14 | | - |
15 | | - //checks if all params are of the form 'param|value' |
16 | | - foreach ( $args as $arg ) { |
17 | | - $set = explode( '|', $arg, 2 ); |
18 | | - if ( count( $set ) != 2 ) { |
19 | | - return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongparams', 'parseinline' ); |
20 | | - } |
21 | | - } |
22 | | - |
23 | | - return true; |
24 | | - } |
25 | | - |
26 | | - public static function getPreferences( /* args */ ) { |
27 | | - //params are in the format "param|val" |
28 | | - $args = func_get_args(); |
29 | | - |
30 | | - $res = self::validateSyntax( $args ); |
31 | | - if ( $res !== true ) { |
32 | | - return $res; |
33 | | - } |
34 | | - |
35 | | - foreach ( $args as $arg ) { |
36 | | - list( $par, $val ) = explode( '|', $arg, 2 );; |
37 | | - |
38 | | - switch( $par ) { |
39 | | - case "gadget": |
40 | | - $gadget = $val; |
41 | | - break; |
42 | | - default: |
43 | | - return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongparams', 'parseinline' ); |
44 | | - } |
45 | | - } |
46 | | - |
47 | | - if ( !isset( $gadget ) ) { |
48 | | - return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongparams', 'parseinline' ); |
49 | | - } |
50 | | - |
51 | | - |
52 | | - $prefsDescriptionJson = Gadget::getGadgetPrefsDescription( $gadget ); |
53 | | - $prefsDescription = FormatJson::decode( $prefsDescriptionJson, true ); |
54 | | - |
55 | | - if ( $prefsDescription === null ) { |
56 | | - //either the gadget doesn't exists or it exists but it has no prefs |
57 | | - return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongparams', 'parseinline' ); |
58 | | - } |
59 | | - |
60 | | - $user = RequestContext::getMain()->getUser(); |
61 | | - $userPrefs = Gadget::getUserPrefs( $user, $gadget ); |
62 | | - |
63 | | - //Add user preferences to preference description |
64 | | - foreach ( $userPrefs as $pref => $value ) { |
65 | | - $prefsDescription['fields'][$pref]['value'] = $value; |
66 | | - } |
67 | | - |
68 | | - return FormatJson::encode( $prefsDescription ); |
69 | | - } |
70 | | - |
71 | | - public static function setPreferences( /* args */ ) { |
72 | | - //TODO: should probably add tokens |
73 | | - |
74 | | - //params are in the format "param|val" |
75 | | - $args = func_get_args(); |
76 | | - |
77 | | - $res = self::validateSyntax( $args ); |
78 | | - if ( $res !== true ) { |
79 | | - return $res; |
80 | | - } |
81 | | - |
82 | | - foreach ( $args as $arg ) { |
83 | | - list( $par, $val ) = explode( '|', $arg, 2 );; |
84 | | - |
85 | | - switch( $par ) { |
86 | | - case "gadget": |
87 | | - $gadget = $val; |
88 | | - break; |
89 | | - case "json": |
90 | | - $json = $val; |
91 | | - break; |
92 | | - default: |
93 | | - return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongparams', 'parseinline' ); |
94 | | - } |
95 | | - } |
96 | | - |
97 | | - if ( !isset( $gadget ) || !isset( $json ) ) { |
98 | | - return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongparams', 'parseinline' ); |
99 | | - } |
100 | | - |
101 | | - $prefsDescriptionJson = Gadget::getGadgetPrefsDescription( $gadget ); |
102 | | - $prefsDescription = FormatJson::decode( $prefsDescriptionJson, true ); |
103 | | - |
104 | | - if ( $prefsDescription === null ) { |
105 | | - //either the gadget doesn't exists or it exists but it has no prefs |
106 | | - return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongparams', 'parseinline' ); |
107 | | - } |
108 | | - |
109 | | - $userPrefs = FormatJson::decode( $json, true ); |
110 | | - |
111 | | - if ( $userPrefs === null ) { |
112 | | - return '<err#>' . wfMsgExt( 'gadgets-ajax-wrongparams', 'parseinline' ); |
113 | | - } |
114 | | - |
115 | | - $user = RequestContext::getMain()->getUser(); |
116 | | - |
117 | | - if ( Gadget::setUserPrefs( $user, $gadget, $userPrefs ) ) { |
118 | | - return 'true'; |
119 | | - } else { |
120 | | - return 'false'; |
121 | | - } |
122 | | - } |
123 | | -} |
Index: branches/salvatoreingala/Gadgets/Gadgets_body.php |
— | — | @@ -996,7 +996,6 @@ |
997 | 997 | //Set user's preferences for a specific gadget. |
998 | 998 | //Returns false if preferences are rejected (that is, they don't pass validation) |
999 | 999 | public static function setUserPrefs( $user, $gadget, &$preferences ) { |
1000 | | - |
1001 | 1000 | $prefsDescriptionJson = Gadget::getGadgetPrefsDescription( $gadget ); |
1002 | 1001 | |
1003 | 1002 | if ( $prefsDescriptionJson === null || $prefsDescriptionJson === '' ) { |
— | — | @@ -1141,7 +1140,7 @@ |
1142 | 1141 | $gadgetsList = Gadget::loadStructuredList(); |
1143 | 1142 | |
1144 | 1143 | foreach ( $gadgetsList as $sectionName => $gadgets ) { |
1145 | | - foreach ( $gadgets as $gadget => $gadget_data ) { |
| 1144 | + foreach ( $gadgets as $gadget => $gadgetData ) { |
1146 | 1145 | $prefs = Gadget::getGadgetPrefsDescription( $gadget ); |
1147 | 1146 | if ( $prefs !== null && $prefs !== '' ) { |
1148 | 1147 | $configurableGadgets[] = $gadget; |
Index: branches/salvatoreingala/Gadgets/Gadgets.php |
— | — | @@ -17,8 +17,8 @@ |
18 | 18 | die( 1 ); |
19 | 19 | } |
20 | 20 | |
21 | | -if ( version_compare( $wgVersion, '1.17alpha', '<' ) ) { |
22 | | - die( "This version of Extension:Gadgets requires MediaWiki 1.17+\n" ); |
| 21 | +if ( version_compare( $wgVersion, '1.19alpha', '<' ) ) { |
| 22 | + die( "This version of Extension:Gadgets requires MediaWiki 1.19+\n" ); |
23 | 23 | } |
24 | 24 | |
25 | 25 | $wgExtensionCredits['other'][] = array( |
— | — | @@ -47,13 +47,14 @@ |
48 | 48 | $wgAutoloadClasses['GadgetResourceLoaderModule'] = $dir . 'Gadgets_body.php'; |
49 | 49 | $wgAutoloadClasses['SpecialGadgets'] = $dir . 'SpecialGadgets.php'; |
50 | 50 | $wgAutoloadClasses['GadgetsGlobalModule'] = $dir . 'Gadgets_body.php'; |
51 | | -$wgAutoloadClasses['GadgetsAjax'] = $dir . 'GadgetsAjax.php'; |
| 51 | +$wgAutoloadClasses['ApiSetGadgetPrefs'] = $dir . 'ApiSetGadgetPrefs.php'; |
| 52 | +$wgAutoloadClasses['ApiGetGadgetPrefs'] = $dir . 'ApiGetGadgetPrefs.php'; |
52 | 53 | |
53 | 54 | $wgSpecialPages['Gadgets'] = 'SpecialGadgets'; |
54 | 55 | $wgSpecialPageGroups['Gadgets'] = 'wiki'; |
55 | 56 | |
56 | | -$wgAPIListModules['gadgetcategories'] = 'ApiQueryGadgetCategories'; |
57 | | -$wgAPIListModules['gadgets'] = 'ApiQueryGadgets'; |
| 57 | +$wgAPIModules['setgadgetprefs'] = 'ApiSetGadgetPrefs'; |
| 58 | +$wgAPIModules['getgadgetprefs'] = 'ApiGetGadgetPrefs'; |
58 | 59 | |
59 | 60 | $wgAjaxExportList[] = 'GadgetsAjax::getPreferences'; |
60 | 61 | $wgAjaxExportList[] = 'GadgetsAjax::setPreferences'; |
Index: branches/salvatoreingala/Gadgets/ApiSetGadgetPrefs.php |
— | — | @@ -0,0 +1,120 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * |
| 6 | + * API for setting Gadget's preferences |
| 7 | + * |
| 8 | + * This program is free software; you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU General Public License as published by |
| 10 | + * the Free Software Foundation; either version 2 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License along |
| 19 | + * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 | + * http://www.gnu.org/copyleft/gpl.html |
| 22 | + */ |
| 23 | + |
| 24 | +class ApiSetGadgetPrefs extends ApiBase { |
| 25 | + |
| 26 | + public function execute() { |
| 27 | + $user = RequestContext::getMain()->getUser(); |
| 28 | + |
| 29 | + $params = $this->extractRequestParams(); |
| 30 | + //Check permissions |
| 31 | + if ( !$user->isLoggedIn() ) { |
| 32 | + $this->dieUsage( 'You must be logged-in to set gadget\'s preferences', 'notloggedin' ); |
| 33 | + } |
| 34 | + |
| 35 | + //Check token |
| 36 | + if ( !$user->matchEditToken( $params['token'] ) ) { |
| 37 | + $this->dieUsageMsg( 'sessionfailure' ); |
| 38 | + } |
| 39 | + |
| 40 | + $gadget = $params['gadget']; |
| 41 | + $prefsJson = $params['prefs']; |
| 42 | + |
| 43 | + //Checks if the gadget actually exists |
| 44 | + $gadgetsList = Gadget::loadStructuredList(); |
| 45 | + $found = false; |
| 46 | + foreach ( $gadgetsList as $section => $gadgets ) { |
| 47 | + if ( isset( $gadgets[$gadget] ) ) { |
| 48 | + $found = true; |
| 49 | + break; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + if ( !$found ) { |
| 54 | + $this->dieUsage( 'Gadget not found', 'notfound' ); |
| 55 | + } |
| 56 | + |
| 57 | + $prefs = FormatJson::decode( $prefsJson, true ); |
| 58 | + |
| 59 | + if ( !is_array( $prefs ) ) { |
| 60 | + $this->dieUsage( 'The \'pref\' parameter must be valid JSON', 'notjson' ); |
| 61 | + } |
| 62 | + |
| 63 | + $result = Gadget::setUserPrefs( $user, $gadget, $prefs ); |
| 64 | + |
| 65 | + if ( $result === true ) { |
| 66 | + $this->getResult()->addValue( |
| 67 | + null, $this->getModuleName(), array( 'result' => 'Success' ) ); |
| 68 | + } else { |
| 69 | + $this->dieUsage( 'Invalid preferences.', 'invalidprefs' ); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public function mustBePosted() { |
| 74 | + return true; |
| 75 | + } |
| 76 | + |
| 77 | + public function isWriteMode() { |
| 78 | + return true; |
| 79 | + } |
| 80 | + |
| 81 | + public function getAllowedParams() { |
| 82 | + return array( |
| 83 | + 'gadget' => array( |
| 84 | + ApiBase::PARAM_TYPE => 'string', |
| 85 | + ApiBase::PARAM_REQUIRED => true |
| 86 | + ), |
| 87 | + 'prefs' => array( |
| 88 | + ApiBase::PARAM_TYPE => 'string', |
| 89 | + ApiBase::PARAM_REQUIRED => true |
| 90 | + ), |
| 91 | + 'token' => array( |
| 92 | + ApiBase::PARAM_TYPE => 'string', |
| 93 | + ApiBase::PARAM_REQUIRED => true |
| 94 | + ), |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + public function getParamDescription() { |
| 99 | + return array( |
| 100 | + 'gadget' => 'The name of the gadget', |
| 101 | + 'prefs' => 'The new preferences in JSON format', |
| 102 | + 'token' => 'An edit token' |
| 103 | + ); |
| 104 | + } |
| 105 | + |
| 106 | + public function getDescription() { |
| 107 | + return 'Allows user code to set preferences for gadgets'; |
| 108 | + } |
| 109 | + |
| 110 | + public function needsToken() { |
| 111 | + return true; |
| 112 | + } |
| 113 | + |
| 114 | + public function getSalt() { |
| 115 | + return ''; |
| 116 | + } |
| 117 | + |
| 118 | + public function getVersion() { |
| 119 | + return __CLASS__ . ': $Id$'; |
| 120 | + } |
| 121 | +} |
Property changes on: branches/salvatoreingala/Gadgets/ApiSetGadgetPrefs.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 122 | + native |
Index: branches/salvatoreingala/Gadgets/modules/ext.gadgets.preferences.js |
— | — | @@ -5,19 +5,21 @@ |
6 | 6 | |
7 | 7 | //"Save" button click handler |
8 | 8 | function saveConfig( $dialog, gadget, config ) { |
9 | | - var json = $.toJSON( config ); |
| 9 | + var prefsJson = $.toJSON( config ); |
10 | 10 | |
11 | | - var postData = 'action=ajax&rs=GadgetsAjax::setPreferences' + |
12 | | - '&rsargs[]=gadget|' + encodeURIComponent( gadget ) + |
13 | | - '&rsargs[]=json|' + encodeURIComponent( json ); |
14 | | - |
15 | 11 | $.ajax( { |
16 | | - url: mw.config.get( 'wgScriptPath' ) + '/index.php', |
| 12 | + url: mw.config.get( 'wgScriptPath' ) + '/api.php', |
17 | 13 | type: "POST", |
18 | | - data: postData, |
| 14 | + data: { |
| 15 | + 'action': 'setgadgetprefs', |
| 16 | + 'gadget': gadget, |
| 17 | + 'prefs': prefsJson, |
| 18 | + 'token': mw.user.tokens.get('editToken'), |
| 19 | + 'format': 'json' |
| 20 | + }, |
19 | 21 | dataType: "json", |
20 | 22 | success: function( response ) { |
21 | | - if ( response === true ) { |
| 23 | + if ( typeof response.error == 'undefined' ) { |
22 | 24 | alert( mw.msg( 'gadgets-save-success' ) ); |
23 | 25 | $dialog.dialog( 'close' ); |
24 | 26 | } else { |
— | — | @@ -44,21 +46,28 @@ |
45 | 47 | var $link = $( '<a></a>' ) |
46 | 48 | .text( mw.msg( 'gadgets-configure' ) ) |
47 | 49 | .click( function() { |
48 | | - var postData = 'action=ajax&rs=GadgetsAjax::getPreferences' + |
49 | | - '&rsargs[]=gadget|' + encodeURIComponent( gadget ); |
50 | | - |
51 | 50 | $.ajax( { |
52 | | - url: mw.config.get( 'wgScriptPath' ) + '/index.php', |
| 51 | + url: mw.config.get( 'wgScriptPath' ) + '/api.php', |
53 | 52 | type: "POST", |
54 | | - data: postData, |
| 53 | + data: { |
| 54 | + 'action': 'getgadgetprefs', |
| 55 | + 'gadget': gadget, |
| 56 | + 'format': 'json' |
| 57 | + }, |
55 | 58 | dataType: "json", // response type |
56 | 59 | success: function( response ) { |
57 | | - //TODO: malformed response? |
58 | 60 | |
| 61 | + if ( typeof response.getgadgetprefs != 'object' ) { |
| 62 | + alert( mw.msg( 'gadgets-unexpected-error' ) ) |
| 63 | + return; |
| 64 | + } |
| 65 | + |
59 | 66 | //Create and show dialog |
60 | 67 | |
61 | | - var dialogBody = $( response ).formBuilder(); |
| 68 | + var prefs = response.getgadgetprefs; |
62 | 69 | |
| 70 | + var dialogBody = $( prefs ).formBuilder(); |
| 71 | + |
63 | 72 | $( dialogBody ).submit( function() { |
64 | 73 | return false; //prevent form submission |
65 | 74 | } ); |
Index: branches/salvatoreingala/Gadgets/ApiGetGadgetPrefs.php |
— | — | @@ -0,0 +1,89 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +/** |
| 5 | + * |
| 6 | + * API for setting Gadget's preferences |
| 7 | + * |
| 8 | + * This program is free software; you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU General Public License as published by |
| 10 | + * the Free Software Foundation; either version 2 of the License, or |
| 11 | + * (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU General Public License along |
| 19 | + * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 21 | + * http://www.gnu.org/copyleft/gpl.html |
| 22 | + */ |
| 23 | + |
| 24 | +class ApiGetGadgetPrefs extends ApiBase { |
| 25 | + |
| 26 | + public function execute() { |
| 27 | + $user = RequestContext::getMain()->getUser(); |
| 28 | + |
| 29 | + $params = $this->extractRequestParams(); |
| 30 | + //Check permissions |
| 31 | + if ( !$user->isLoggedIn() ) { |
| 32 | + $this->dieUsage( 'You must be logged-in to get gadget\'s preferences', 'notloggedin' ); |
| 33 | + } |
| 34 | + |
| 35 | + $gadget = $params['gadget']; |
| 36 | + |
| 37 | + //Checks if the gadget actually exists |
| 38 | + $gadgetsList = Gadget::loadStructuredList(); |
| 39 | + $found = false; |
| 40 | + foreach ( $gadgetsList as $section => $gadgets ) { |
| 41 | + if ( isset( $gadgets[$gadget] ) ) { |
| 42 | + $found = true; |
| 43 | + break; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + if ( !$found ) { |
| 48 | + $this->dieUsage( 'Gadget not found', 'notfound' ); |
| 49 | + } |
| 50 | + |
| 51 | + $prefsDescriptionJson = Gadget::getGadgetPrefsDescription( $gadget ); |
| 52 | + $prefsDescription = FormatJson::decode( $prefsDescriptionJson, true ); |
| 53 | + |
| 54 | + if ( $prefsDescription === null ) { |
| 55 | + $this->dieUsage( "Gadget $gadget does not have any preference.", 'noprefs' ); |
| 56 | + } |
| 57 | + |
| 58 | + $userPrefs = Gadget::getUserPrefs( $user, $gadget ); |
| 59 | + |
| 60 | + //Add user preferences to preference description |
| 61 | + foreach ( $userPrefs as $pref => $value ) { |
| 62 | + $prefsDescription['fields'][$pref]['value'] = $value; |
| 63 | + } |
| 64 | + |
| 65 | + $this->getResult()->addValue( null, $this->getModuleName(), $prefsDescription ); |
| 66 | + } |
| 67 | + |
| 68 | + public function getAllowedParams() { |
| 69 | + return array( |
| 70 | + 'gadget' => array( |
| 71 | + ApiBase::PARAM_TYPE => 'string', |
| 72 | + ApiBase::PARAM_REQUIRED => true |
| 73 | + ) |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + public function getParamDescription() { |
| 78 | + return array( |
| 79 | + 'gadget' => 'The name of the gadget' |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + public function getDescription() { |
| 84 | + return 'Allows user code to get preferences for gadgets, along with preference descriptions'; |
| 85 | + } |
| 86 | + |
| 87 | + public function getVersion() { |
| 88 | + return __CLASS__ . ': $Id$'; |
| 89 | + } |
| 90 | +} |
Property changes on: branches/salvatoreingala/Gadgets/ApiGetGadgetPrefs.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 91 | + native |