Index: trunk/extensions/Translate/api/ApiAggregateGroups.php |
— | — | @@ -1,14 +1,17 @@ |
2 | 2 | <?php |
3 | 3 | /** |
4 | | - * API module for managing aggregate groups |
| 4 | + * API module for managing aggregate message groups |
5 | 5 | * @file |
6 | 6 | * @author Santhosh Thottingal |
7 | | - * @copyright Copyright © 2011, Santhosh Thottingal |
| 7 | + * @author Niklas Laxström |
| 8 | + * @copyright Copyright © 2012, Santhosh Thottingal |
8 | 9 | * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later |
9 | 10 | */ |
10 | 11 | |
11 | 12 | /** |
12 | | - * API module for managing aggregate groups |
| 13 | + * API module for managing aggregate message groups |
| 14 | + * Only supports aggregate message groups defined inside the wiki. |
| 15 | + * Aggregate message group defined in YAML configuration cannot be altered. |
13 | 16 | * |
14 | 17 | * @ingroup API TranslateAPI |
15 | 18 | */ |
— | — | @@ -23,52 +26,64 @@ |
24 | 27 | $this->dieUsage( 'Permission denied', 'permissiondenied' ); |
25 | 28 | } |
26 | 29 | |
27 | | - $requestParams = $this->extractRequestParams(); |
28 | | - $aggregateGroup = $requestParams['aggregategroup']; |
29 | | - if ( $requestParams['do'] === 'associate' ) { |
30 | | - $group = $requestParams['group']; |
31 | | - $aggregateGroups = TranslateMetadata::get( $aggregateGroup, 'subgroups' ); |
32 | | - if ( trim( $aggregateGroups ) ) { |
33 | | - $aggregateGroups = array_map( 'trim', explode( ',', $aggregateGroups ) ); |
| 30 | + $params = $this->extractRequestParams(); |
| 31 | + $aggregateGroup = $params['aggregategroup']; |
| 32 | + $action = $action; |
| 33 | + |
| 34 | + if ( $action === 'associate' || $action === 'dissociate' ) { |
| 35 | + // Group is mandatory only for these two actions |
| 36 | + if ( !isset( $params['group'] ) ) { |
| 37 | + $this->dieUsageMsg( array( 'missingparam', 'group' ) ); |
34 | 38 | } |
35 | | - else { |
36 | | - $aggregateGroups = array(); |
| 39 | + |
| 40 | + // Get the list of group ids |
| 41 | + $group = $params['group']; |
| 42 | + $subgroups = TranslateMetadata::get( $aggregateGroup, 'subgroups' ); |
| 43 | + if ( $subgroups ) { |
| 44 | + $subgroups = array_map( 'trim', explode( ',', $subgroups ) ); |
| 45 | + } else { |
| 46 | + // For newly created groups the subgroups value might be empty |
| 47 | + $subgroups = array(); |
37 | 48 | } |
38 | | - $aggregateGroups[] = $group; |
39 | | - $aggregateGroups = array_unique( $aggregateGroups ); |
40 | | - $newSubGroups = implode( ',', $aggregateGroups ); |
41 | | - TranslateMetadata::set( $aggregateGroup, 'subgroups' , $newSubGroups ) ; |
42 | | - MessageGroups::clearCache(); |
43 | | - } |
44 | | - if ( $requestParams['do'] === 'dissociate' ) { |
45 | | - $group = $requestParams['group']; |
46 | | - $aggregateGroups = TranslateMetadata::get( $aggregateGroup, 'subgroups' ); |
47 | | - $aggregateGroups = array_flip( explode( ',', $aggregateGroups ) ) ; |
48 | | - if ( isset( $aggregateGroups[$group] ) ) { |
49 | | - unset( $aggregateGroups[$group] ); |
| 49 | + |
| 50 | + // @FIXME: check that the group id is a translatable page |
| 51 | + // @FIXME: handle pages with a comma in their name |
| 52 | + |
| 53 | + // Add or remove from the list |
| 54 | + // @TODO logging |
| 55 | + if ( $action === 'associate' ) { |
| 56 | + $subgroups[] = $group; |
| 57 | + $subgroups = array_unique( $subgroups ); |
| 58 | + } elseif ( $action === 'dissociate' ) { |
| 59 | + $subgroups = array_flip( explode( ',', $subgroups ) ) ; |
| 60 | + unset( $subgroups[$group] ); |
| 61 | + $subgroups = array_flip( $subgroups ); |
50 | 62 | } |
51 | | - $aggregateGroups = array_flip( $aggregateGroups ); |
52 | | - TranslateMetadata::set( $aggregateGroup, 'subgroups' , implode( ',', $aggregateGroups ) ) ; |
53 | | - MessageGroups::clearCache(); |
54 | | - } |
55 | | - if ( $requestParams['do'] === 'remove' ) { |
| 63 | + |
| 64 | + TranslateMetadata::set( $aggregateGroup, 'subgroups', implode( ',', $subgroups ) ) ; |
| 65 | + } elseif ( $action === 'remove' ) { |
56 | 66 | TranslateMetadata::set( $aggregateGroup, 'subgroups', false ) ; |
57 | 67 | TranslateMetadata::set( $aggregateGroup, 'name', false ) ; |
58 | 68 | TranslateMetadata::set( $aggregateGroup, 'description', false ) ; |
59 | | - MessageGroups::clearCache(); |
60 | | - } |
61 | | - if ( $requestParams['do'] === 'add' ) { |
62 | | - TranslateMetadata::set( $aggregateGroup, 'subgroups' , '' ) ; |
63 | | - if ( trim( $requestParams['groupname'] ) ) { |
64 | | - TranslateMetadata::set( $aggregateGroup, 'name' , trim( $requestParams['groupname'] ) ) ; |
| 69 | + } elseif ( $action === 'add' ) { |
| 70 | + // @FIXME: check that the group id unused and valid (like, no commas) |
| 71 | + TranslateMetadata::set( $aggregateGroup, 'subgroups', '' ) ; |
| 72 | + $name = trim( $params['groupname'] ); |
| 73 | + $desc = trim( $params['groupdescription'] ); |
| 74 | + |
| 75 | + if ( $name ) { |
| 76 | + TranslateMetadata::set( $aggregateGroup, 'name', $name ) ; |
65 | 77 | } |
66 | | - if ( trim( $requestParams['groupdescription'] ) ) { |
67 | | - TranslateMetadata::set( $aggregateGroup, 'description' , trim( $requestParams['groupdescription'] ) ) ; |
| 78 | + if ( $desc ) { |
| 79 | + TranslateMetadata::set( $aggregateGroup, 'description', $desc ) ; |
68 | 80 | } |
69 | | - MessageGroups::clearCache(); |
70 | 81 | } |
| 82 | + |
| 83 | + // If we got this far, nothing has failed |
71 | 84 | $output = array( 'result' => 'ok' ); |
72 | 85 | $this->getResult()->addValue( null, $this->getModuleName(), $output ); |
| 86 | + // Cache needs to be cleared after any changes to groups |
| 87 | + MessageGroups::clearCache(); |
73 | 88 | } |
74 | 89 | |
75 | 90 | public function isWriteMode() { |
— | — | @@ -85,7 +100,7 @@ |
86 | 101 | public function getAllowedParams() { |
87 | 102 | return array( |
88 | 103 | 'do' => array( |
89 | | - ApiBase::PARAM_TYPE => array( 'associate', 'dissociate', 'remove' , 'add' ), |
| 104 | + ApiBase::PARAM_TYPE => array( 'associate', 'dissociate', 'remove', 'add' ), |
90 | 105 | ApiBase::PARAM_REQUIRED => true, |
91 | 106 | ), |
92 | 107 | 'aggregategroup' => array( |
— | — | @@ -103,25 +118,26 @@ |
104 | 119 | ), |
105 | 120 | 'token' => array( |
106 | 121 | ApiBase::PARAM_TYPE => 'string', |
107 | | - ApiBase::PARAM_REQUIRED => true, |
| 122 | + ApiBase::PARAM_REQUIRED => false, |
108 | 123 | ), |
109 | 124 | ); |
110 | 125 | } |
111 | 126 | |
112 | 127 | public function getParamDescription() { |
113 | 128 | return array( |
114 | | - 'do' => 'Required operation, Either of associate, dissociate, add or remove', |
| 129 | + 'do' => 'What to do with aggregate message group', |
115 | 130 | 'group' => 'Message group id', |
116 | | - 'aggregategroup' => 'Aggregate group id', |
117 | | - 'groupname' => 'Aggregate group name', |
118 | | - 'groupdescription' => 'Aggregate group description', |
| 131 | + 'aggregategroup' => 'Aggregate message group id', |
| 132 | + 'groupname' => 'Aggregate message group name', |
| 133 | + 'groupdescription' => 'Aggregate message group description', |
119 | 134 | 'token' => 'A token previously acquired with action=query&prop=info&intoken=aggregategroups', |
120 | 135 | ); |
121 | 136 | } |
122 | 137 | |
123 | 138 | |
124 | 139 | public function getDescription() { |
125 | | - return 'Manage aggregate groups'; |
| 140 | + return 'Manage aggregate message groups. You can add and remove aggregate message' . |
| 141 | + 'groups and associate or dissociate message groups from them (one at a time).'; |
126 | 142 | } |
127 | 143 | |
128 | 144 | public function getPossibleErrors() { |