https://www.mediawiki.org/wiki/Special:Code/MediaWiki/113622
Revision: 113622
Author: nikerabbit
Date: 2012-03-12 13:12:23 +0000 (Mon, 12 Mar 2012)
Log Message:
-----------
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: trunk/extensions/Translate/api/ApiAggregateGroups.php
===================================================================
--- trunk/extensions/Translate/api/ApiAggregateGroups.php 2012-03-12
13:08:51 UTC (rev 113621)
+++ trunk/extensions/Translate/api/ApiAggregateGroups.php 2012-03-12
13:12:23 UTC (rev 113622)
@@ -1,14 +1,17 @@
<?php
/**
- * API module for managing aggregate groups
+ * API module for managing aggregate message groups
* @file
* @author Santhosh Thottingal
- * @copyright Copyright © 2011, Santhosh Thottingal
+ * @author Niklas Laxström
+ * @copyright Copyright © 2012, Santhosh Thottingal
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
2.0 or later
*/
/**
- * API module for managing aggregate groups
+ * API module for managing aggregate message groups
+ * Only supports aggregate message groups defined inside the wiki.
+ * Aggregate message group defined in YAML configuration cannot be altered.
*
* @ingroup API TranslateAPI
*/
@@ -23,52 +26,64 @@
$this->dieUsage( 'Permission denied',
'permissiondenied' );
}
- $requestParams = $this->extractRequestParams();
- $aggregateGroup = $requestParams['aggregategroup'];
- if ( $requestParams['do'] === 'associate' ) {
- $group = $requestParams['group'];
- $aggregateGroups = TranslateMetadata::get(
$aggregateGroup, 'subgroups' );
- if ( trim( $aggregateGroups ) ) {
- $aggregateGroups = array_map( 'trim',
explode( ',', $aggregateGroups ) );
+ $params = $this->extractRequestParams();
+ $aggregateGroup = $params['aggregategroup'];
+ $action = $action;
+
+ if ( $action === 'associate' || $action === 'dissociate' ) {
+ // Group is mandatory only for these two actions
+ if ( !isset( $params['group'] ) ) {
+ $this->dieUsageMsg( array( 'missingparam',
'group' ) );
}
- else {
- $aggregateGroups = array();
+
+ // Get the list of group ids
+ $group = $params['group'];
+ $subgroups = TranslateMetadata::get( $aggregateGroup,
'subgroups' );
+ if ( $subgroups ) {
+ $subgroups = array_map( 'trim', explode( ',',
$subgroups ) );
+ } else {
+ // For newly created groups the subgroups value
might be empty
+ $subgroups = array();
}
- $aggregateGroups[] = $group;
- $aggregateGroups = array_unique( $aggregateGroups );
- $newSubGroups = implode( ',', $aggregateGroups );
- TranslateMetadata::set( $aggregateGroup, 'subgroups' ,
$newSubGroups ) ;
- MessageGroups::clearCache();
- }
- if ( $requestParams['do'] === 'dissociate' ) {
- $group = $requestParams['group'];
- $aggregateGroups = TranslateMetadata::get(
$aggregateGroup, 'subgroups' );
- $aggregateGroups = array_flip( explode( ',',
$aggregateGroups ) ) ;
- if ( isset( $aggregateGroups[$group] ) ) {
- unset( $aggregateGroups[$group] );
+
+ // @FIXME: check that the group id is a translatable
page
+ // @FIXME: handle pages with a comma in their name
+
+ // Add or remove from the list
+ // @TODO logging
+ if ( $action === 'associate' ) {
+ $subgroups[] = $group;
+ $subgroups = array_unique( $subgroups );
+ } elseif ( $action === 'dissociate' ) {
+ $subgroups = array_flip( explode( ',',
$subgroups ) ) ;
+ unset( $subgroups[$group] );
+ $subgroups = array_flip( $subgroups );
}
- $aggregateGroups = array_flip( $aggregateGroups );
- TranslateMetadata::set( $aggregateGroup, 'subgroups' ,
implode( ',', $aggregateGroups ) ) ;
- MessageGroups::clearCache();
- }
- if ( $requestParams['do'] === 'remove' ) {
+
+ TranslateMetadata::set( $aggregateGroup, 'subgroups',
implode( ',', $subgroups ) ) ;
+ } elseif ( $action === 'remove' ) {
TranslateMetadata::set( $aggregateGroup, 'subgroups',
false ) ;
TranslateMetadata::set( $aggregateGroup, 'name', false
) ;
TranslateMetadata::set( $aggregateGroup, 'description',
false ) ;
- MessageGroups::clearCache();
- }
- if ( $requestParams['do'] === 'add' ) {
- TranslateMetadata::set( $aggregateGroup, 'subgroups' ,
'' ) ;
- if ( trim( $requestParams['groupname'] ) ) {
- TranslateMetadata::set( $aggregateGroup, 'name'
, trim( $requestParams['groupname'] ) ) ;
+ } elseif ( $action === 'add' ) {
+ // @FIXME: check that the group id unused and valid
(like, no commas)
+ TranslateMetadata::set( $aggregateGroup, 'subgroups',
'' ) ;
+ $name = trim( $params['groupname'] );
+ $desc = trim( $params['groupdescription'] );
+
+ if ( $name ) {
+ TranslateMetadata::set( $aggregateGroup,
'name', $name ) ;
}
- if ( trim( $requestParams['groupdescription'] ) ) {
- TranslateMetadata::set( $aggregateGroup,
'description' , trim( $requestParams['groupdescription'] ) ) ;
+ if ( $desc ) {
+ TranslateMetadata::set( $aggregateGroup,
'description', $desc ) ;
}
- MessageGroups::clearCache();
}
+
+ // If we got this far, nothing has failed
$output = array( 'result' => 'ok' );
$this->getResult()->addValue( null, $this->getModuleName(),
$output );
+ // Cache needs to be cleared after any changes to groups
+ MessageGroups::clearCache();
}
public function isWriteMode() {
@@ -85,7 +100,7 @@
public function getAllowedParams() {
return array(
'do' => array(
- ApiBase::PARAM_TYPE => array( 'associate',
'dissociate', 'remove' , 'add' ),
+ ApiBase::PARAM_TYPE => array( 'associate',
'dissociate', 'remove', 'add' ),
ApiBase::PARAM_REQUIRED => true,
),
'aggregategroup' => array(
@@ -103,25 +118,26 @@
),
'token' => array(
ApiBase::PARAM_TYPE => 'string',
- ApiBase::PARAM_REQUIRED => true,
+ ApiBase::PARAM_REQUIRED => false,
),
);
}
public function getParamDescription() {
return array(
- 'do' => 'Required operation, Either of associate,
dissociate, add or remove',
+ 'do' => 'What to do with aggregate message group',
'group' => 'Message group id',
- 'aggregategroup' => 'Aggregate group id',
- 'groupname' => 'Aggregate group name',
- 'groupdescription' => 'Aggregate group description',
+ 'aggregategroup' => 'Aggregate message group id',
+ 'groupname' => 'Aggregate message group name',
+ 'groupdescription' => 'Aggregate message group
description',
'token' => 'A token previously acquired with
action=query&prop=info&intoken=aggregategroups',
);
}
public function getDescription() {
- return 'Manage aggregate groups';
+ return 'Manage aggregate message groups. You can add and remove
aggregate message' .
+ 'groups and associate or dissociate message groups from
them (one at a time).';
}
public function getPossibleErrors() {
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs