r90003 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r90002‎ | r90003 | r90004 >
Date:19:56, 13 June 2011
Author:salvatoreingala
Status:deferred
Tags:
Comment:
Rewritten AJAX interaction with API modules instead of 'action=ajax' requests.
Modified paths:
  • /branches/salvatoreingala/Gadgets/ApiGetGadgetPrefs.php (added) (history)
  • /branches/salvatoreingala/Gadgets/ApiSetGadgetPrefs.php (added) (history)
  • /branches/salvatoreingala/Gadgets/Gadgets.php (modified) (history)
  • /branches/salvatoreingala/Gadgets/GadgetsAjax.php (deleted) (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/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 @@
997997 //Set user's preferences for a specific gadget.
998998 //Returns false if preferences are rejected (that is, they don't pass validation)
999999 public static function setUserPrefs( $user, $gadget, &$preferences ) {
1000 -
10011000 $prefsDescriptionJson = Gadget::getGadgetPrefsDescription( $gadget );
10021001
10031002 if ( $prefsDescriptionJson === null || $prefsDescriptionJson === '' ) {
@@ -1141,7 +1140,7 @@
11421141 $gadgetsList = Gadget::loadStructuredList();
11431142
11441143 foreach ( $gadgetsList as $sectionName => $gadgets ) {
1145 - foreach ( $gadgets as $gadget => $gadget_data ) {
 1144+ foreach ( $gadgets as $gadget => $gadgetData ) {
11461145 $prefs = Gadget::getGadgetPrefsDescription( $gadget );
11471146 if ( $prefs !== null && $prefs !== '' ) {
11481147 $configurableGadgets[] = $gadget;
Index: branches/salvatoreingala/Gadgets/Gadgets.php
@@ -17,8 +17,8 @@
1818 die( 1 );
1919 }
2020
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" );
2323 }
2424
2525 $wgExtensionCredits['other'][] = array(
@@ -47,13 +47,14 @@
4848 $wgAutoloadClasses['GadgetResourceLoaderModule'] = $dir . 'Gadgets_body.php';
4949 $wgAutoloadClasses['SpecialGadgets'] = $dir . 'SpecialGadgets.php';
5050 $wgAutoloadClasses['GadgetsGlobalModule'] = $dir . 'Gadgets_body.php';
51 -$wgAutoloadClasses['GadgetsAjax'] = $dir . 'GadgetsAjax.php';
 51+$wgAutoloadClasses['ApiSetGadgetPrefs'] = $dir . 'ApiSetGadgetPrefs.php';
 52+$wgAutoloadClasses['ApiGetGadgetPrefs'] = $dir . 'ApiGetGadgetPrefs.php';
5253
5354 $wgSpecialPages['Gadgets'] = 'SpecialGadgets';
5455 $wgSpecialPageGroups['Gadgets'] = 'wiki';
5556
56 -$wgAPIListModules['gadgetcategories'] = 'ApiQueryGadgetCategories';
57 -$wgAPIListModules['gadgets'] = 'ApiQueryGadgets';
 57+$wgAPIModules['setgadgetprefs'] = 'ApiSetGadgetPrefs';
 58+$wgAPIModules['getgadgetprefs'] = 'ApiGetGadgetPrefs';
5859
5960 $wgAjaxExportList[] = 'GadgetsAjax::getPreferences';
6061 $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
1122 + native
Index: branches/salvatoreingala/Gadgets/modules/ext.gadgets.preferences.js
@@ -5,19 +5,21 @@
66
77 //"Save" button click handler
88 function saveConfig( $dialog, gadget, config ) {
9 - var json = $.toJSON( config );
 9+ var prefsJson = $.toJSON( config );
1010
11 - var postData = 'action=ajax&rs=GadgetsAjax::setPreferences' +
12 - '&rsargs[]=gadget|' + encodeURIComponent( gadget ) +
13 - '&rsargs[]=json|' + encodeURIComponent( json );
14 -
1511 $.ajax( {
16 - url: mw.config.get( 'wgScriptPath' ) + '/index.php',
 12+ url: mw.config.get( 'wgScriptPath' ) + '/api.php',
1713 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+ },
1921 dataType: "json",
2022 success: function( response ) {
21 - if ( response === true ) {
 23+ if ( typeof response.error == 'undefined' ) {
2224 alert( mw.msg( 'gadgets-save-success' ) );
2325 $dialog.dialog( 'close' );
2426 } else {
@@ -44,21 +46,28 @@
4547 var $link = $( '<a></a>' )
4648 .text( mw.msg( 'gadgets-configure' ) )
4749 .click( function() {
48 - var postData = 'action=ajax&rs=GadgetsAjax::getPreferences' +
49 - '&rsargs[]=gadget|' + encodeURIComponent( gadget );
50 -
5150 $.ajax( {
52 - url: mw.config.get( 'wgScriptPath' ) + '/index.php',
 51+ url: mw.config.get( 'wgScriptPath' ) + '/api.php',
5352 type: "POST",
54 - data: postData,
 53+ data: {
 54+ 'action': 'getgadgetprefs',
 55+ 'gadget': gadget,
 56+ 'format': 'json'
 57+ },
5558 dataType: "json", // response type
5659 success: function( response ) {
57 - //TODO: malformed response?
5860
 61+ if ( typeof response.getgadgetprefs != 'object' ) {
 62+ alert( mw.msg( 'gadgets-unexpected-error' ) )
 63+ return;
 64+ }
 65+
5966 //Create and show dialog
6067
61 - var dialogBody = $( response ).formBuilder();
 68+ var prefs = response.getgadgetprefs;
6269
 70+ var dialogBody = $( prefs ).formBuilder();
 71+
6372 $( dialogBody ).submit( function() {
6473 return false; //prevent form submission
6574 } );
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
191 + native

Follow-up revisions

RevisionCommit summaryAuthorDate
r90006Added svn:keywords for API modules, fixing r90003salvatoreingala20:09, 13 June 2011

Status & tagging log