Index: branches/RL2/extensions/Gadgets/Gadgets.php |
— | — | @@ -90,6 +90,7 @@ |
91 | 91 | $wgExtensionMessagesFiles['GadgetsNamespaces'] = $dir . 'Gadgets.namespaces.php'; |
92 | 92 | $wgExtensionAliasesFiles['Gadgets'] = $dir . 'Gadgets.alias.php'; |
93 | 93 | |
| 94 | +$wgAutoloadClasses['ApiGadgetManager'] = $dir . 'api/ApiGadgetManager.php'; |
94 | 95 | $wgAutoloadClasses['ApiQueryGadgetCategories'] = $dir . 'api/ApiQueryGadgetCategories.php'; |
95 | 96 | $wgAutoloadClasses['ApiQueryGadgets'] = $dir . 'api/ApiQueryGadgets.php'; |
96 | 97 | $wgAutoloadClasses['ForeignDBGadgetRepo'] = $dir . 'backend/ForeignDBGadgetRepo.php'; |
— | — | @@ -103,6 +104,7 @@ |
104 | 105 | $wgSpecialPages['Gadgets'] = 'SpecialGadgets'; |
105 | 106 | $wgSpecialPageGroups['Gadgets'] = 'wiki'; |
106 | 107 | |
| 108 | +$wgAPIModules['gadgetmanager'] = 'ApiGadgetManager'; |
107 | 109 | $wgAPIListModules['gadgetcategories'] = 'ApiQueryGadgetCategories'; |
108 | 110 | $wgAPIListModules['gadgets'] = 'ApiQueryGadgets'; |
109 | 111 | |
Index: branches/RL2/extensions/Gadgets/api/ApiGadgetManager.php |
— | — | @@ -0,0 +1,140 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Created on July 31st, 2011 |
| 5 | + * API for Gadgets extension |
| 6 | + * |
| 7 | + * This program is free software; you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU General Public License as published by |
| 9 | + * the Free Software Foundation; either version 2 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License along |
| 18 | + * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | + * http://www.gnu.org/copyleft/gpl.html |
| 21 | + */ |
| 22 | + |
| 23 | +class ApiGadgetManager extends ApiBase { |
| 24 | + public function __construct( $main, $action ) { |
| 25 | + parent::__construct( $main, $action ); |
| 26 | + } |
| 27 | + |
| 28 | + public function execute() { |
| 29 | + global $wgUser; |
| 30 | + $params = $this->extractRequestParams(); |
| 31 | + |
| 32 | + $op = $params['op']; |
| 33 | + if ( !$wgUser->isAllowed( "gadgets-manager-$op" ) ) { |
| 34 | + $this->dieUsage( "You are not allowed to $op gadgets", 'permissiondenied' ); |
| 35 | + } |
| 36 | + |
| 37 | + if ( $op === 'modify' ) { |
| 38 | + if ( $params['edittimestamp'] === null ) { |
| 39 | + $this->dieUsageMsg( array( 'missingparam', 'edittimestamp' ) ); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + $repo = new LocalGadgetRepo( array() ); |
| 44 | + if ( $op === 'create' || $op === 'modify' ) { |
| 45 | + if ( $params['json'] === null ) { |
| 46 | + $this->dieUsageMsg( array( 'missingparam', 'json' ) ); |
| 47 | + } |
| 48 | + $json = FormatJson::decode( $params['json'], true ); |
| 49 | + if ( $json === null ) { |
| 50 | + $this->dieUsage( 'Invalid JSON specified for json parameter', 'invalidjson' ); |
| 51 | + } |
| 52 | + if ( !Gadget::isValidPropertiesArray( $json ) ) { |
| 53 | + $this->dieUsage( 'Invalid properties array specified for json parameter', 'invalidproperties' ); |
| 54 | + } |
| 55 | + |
| 56 | + // FIXME: Passing lasttimestamp into the constructor like this is a bit hacky |
| 57 | + $gadget = new Gadget( $params['name'], $repo, $json, $params['edittimestamp'] ); |
| 58 | + } |
| 59 | + |
| 60 | + if ( $op === 'create' ) { |
| 61 | + $status = $repo->addGadget( $gadget ); |
| 62 | + } else if ( $op === 'modify' ) { |
| 63 | + $status = $repo->modifyGadget( $gadget ); |
| 64 | + } else if ( $op === 'delete' ) { |
| 65 | + $status = $repo->deleteGadget( $params['name'] ); |
| 66 | + } |
| 67 | + |
| 68 | + if ( !$status->isGood() ) { |
| 69 | + $errors = $status->getErrorsArray(); |
| 70 | + $this->dieUsageMsg( $errors[0] ); // TODO: Actually register all the error messages in ApiBase::$messageMap somehow |
| 71 | + } |
| 72 | + |
| 73 | + $r = array( 'result' => 'success', 'name' => $params['name'], 'op' => $op ); |
| 74 | + $this->getResult()->addValue( null, $this->getModuleName(), $r ); |
| 75 | + } |
| 76 | + |
| 77 | + public function mustBePosted() { |
| 78 | + return true; |
| 79 | + } |
| 80 | + |
| 81 | + public function isWriteMode() { |
| 82 | + return true; |
| 83 | + } |
| 84 | + |
| 85 | + public function getAllowedParams() { |
| 86 | + return array( |
| 87 | + 'op' => array( |
| 88 | + ApiBase::PARAM_TYPE => array( 'create', 'modify', 'delete' ), |
| 89 | + ApiBase::PARAM_REQUIRED => true |
| 90 | + ), |
| 91 | + 'name' => array( |
| 92 | + ApiBase::PARAM_TYPE => 'string', |
| 93 | + ApiBase::PARAM_REQUIRED => true |
| 94 | + ), |
| 95 | + 'json' => array( |
| 96 | + ApiBase::PARAM_TYPE => 'string' |
| 97 | + ), |
| 98 | + 'token' => null, |
| 99 | + 'edittimestamp' => array( |
| 100 | + ApiBase::PARAM_TYPE => 'timestamp' |
| 101 | + ) |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + public function getParamDescription() { |
| 106 | + return array( |
| 107 | + 'op' => 'Operation to carry out', |
| 108 | + 'name' => 'Gadget name', |
| 109 | + 'json' => 'If op=create or op=modify, JSON blob with the new gadget metadata. Ignored if op=delete', |
| 110 | + 'edittimestamp' => 'If op=modify, the last modified timestamp of the gadget metadata, exactly as given by list=gadgets', |
| 111 | + 'token' => 'Edit token', |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + public function getDescription() { |
| 116 | + return 'Create, modify and delete gadgets.'; |
| 117 | + } |
| 118 | + |
| 119 | + public function getPossibleErrors() { |
| 120 | + return array_merge( parent::getPossibleErrors(), array( |
| 121 | + // TODO |
| 122 | + ) ); |
| 123 | + } |
| 124 | + |
| 125 | + public function needsToken() { |
| 126 | + return true; |
| 127 | + } |
| 128 | + |
| 129 | + public function getTokenSalt() { |
| 130 | + return ''; |
| 131 | + } |
| 132 | + |
| 133 | + protected function getExamples() { |
| 134 | + return array( |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + public function getVersion() { |
| 139 | + return __CLASS__ . ': $Id$'; |
| 140 | + } |
| 141 | +} |
Property changes on: branches/RL2/extensions/Gadgets/api/ApiGadgetManager.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 142 | + native |
Added: svn:keywords |
2 | 143 | + Id |