r113622 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r113621‎ | r113622 | r113623 >
Date:13:12, 12 March 2012
Author:nikerabbit
Status:ok
Tags:i18nreview 
Comment:
Cleanup pass for my pedantic mind :)
* Renamed some variable names to more commonly used names
* Restructured to avoid duplication
* Expanded comments
* Whitespace fixes including stylize
* Added TODOs for logging and FIXMEs for message group id validation

Ping r113032 and r113615
Modified paths:
  • /trunk/extensions/Translate/api/ApiAggregateGroups.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/api/ApiAggregateGroups.php
@@ -1,14 +1,17 @@
22 <?php
33 /**
4 - * API module for managing aggregate groups
 4+ * API module for managing aggregate message groups
55 * @file
66 * @author Santhosh Thottingal
7 - * @copyright Copyright © 2011, Santhosh Thottingal
 7+ * @author Niklas Laxström
 8+ * @copyright Copyright © 2012, Santhosh Thottingal
89 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
910 */
1011
1112 /**
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.
1316 *
1417 * @ingroup API TranslateAPI
1518 */
@@ -23,52 +26,64 @@
2427 $this->dieUsage( 'Permission denied', 'permissiondenied' );
2528 }
2629
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' ) );
3438 }
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();
3748 }
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 );
5062 }
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' ) {
5666 TranslateMetadata::set( $aggregateGroup, 'subgroups', false ) ;
5767 TranslateMetadata::set( $aggregateGroup, 'name', false ) ;
5868 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 ) ;
6577 }
66 - if ( trim( $requestParams['groupdescription'] ) ) {
67 - TranslateMetadata::set( $aggregateGroup, 'description' , trim( $requestParams['groupdescription'] ) ) ;
 78+ if ( $desc ) {
 79+ TranslateMetadata::set( $aggregateGroup, 'description', $desc ) ;
6880 }
69 - MessageGroups::clearCache();
7081 }
 82+
 83+ // If we got this far, nothing has failed
7184 $output = array( 'result' => 'ok' );
7285 $this->getResult()->addValue( null, $this->getModuleName(), $output );
 86+ // Cache needs to be cleared after any changes to groups
 87+ MessageGroups::clearCache();
7388 }
7489
7590 public function isWriteMode() {
@@ -85,7 +100,7 @@
86101 public function getAllowedParams() {
87102 return array(
88103 'do' => array(
89 - ApiBase::PARAM_TYPE => array( 'associate', 'dissociate', 'remove' , 'add' ),
 104+ ApiBase::PARAM_TYPE => array( 'associate', 'dissociate', 'remove', 'add' ),
90105 ApiBase::PARAM_REQUIRED => true,
91106 ),
92107 'aggregategroup' => array(
@@ -103,25 +118,26 @@
104119 ),
105120 'token' => array(
106121 ApiBase::PARAM_TYPE => 'string',
107 - ApiBase::PARAM_REQUIRED => true,
 122+ ApiBase::PARAM_REQUIRED => false,
108123 ),
109124 );
110125 }
111126
112127 public function getParamDescription() {
113128 return array(
114 - 'do' => 'Required operation, Either of associate, dissociate, add or remove',
 129+ 'do' => 'What to do with aggregate message group',
115130 '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',
119134 'token' => 'A token previously acquired with action=query&prop=info&intoken=aggregategroups',
120135 );
121136 }
122137
123138
124139 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).';
126142 }
127143
128144 public function getPossibleErrors() {

Follow-up revisions

RevisionCommit summaryAuthorDate
r113629Bugfixes to r113622nikerabbit16:49, 12 March 2012

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r113032Group translatable pages into aggregate groups, - Special page and associated...santhosh14:10, 5 March 2012
r113615Use the aggregate groups in translate_metadata table for showing the group st...santhosh11:00, 12 March 2012

Status & tagging log