https://www.mediawiki.org/wiki/Special:Code/MediaWiki/114236

Revision: 114236
Author:   santhosh
Date:     2012-03-20 07:14:01 +0000 (Tue, 20 Mar 2012)
Log Message:
-----------
Create the aggregate group Id from the api.          
Try to make the id readable if possible- if the byte length is less than 200, 
otherwise use sha1 substring with prefix.

Modified Paths:
--------------
    trunk/extensions/Translate/api/ApiAggregateGroups.php
    
trunk/extensions/Translate/resources/ext.translate.special.aggregategroups.js

Modified: trunk/extensions/Translate/api/ApiAggregateGroups.php
===================================================================
--- trunk/extensions/Translate/api/ApiAggregateGroups.php       2012-03-20 
06:20:30 UTC (rev 114235)
+++ trunk/extensions/Translate/api/ApiAggregateGroups.php       2012-03-20 
07:14:01 UTC (rev 114236)
@@ -28,7 +28,6 @@
 
                $logger = new LogPage( 'pagetranslation' );
                $params = $this->extractRequestParams();
-               $aggregateGroup = $params['aggregategroup'];
                $action = $params['do'];
                $output = array();
                if ( $action === 'associate' || $action === 'dissociate' ) {
@@ -36,7 +35,10 @@
                        if ( !isset( $params['group'] ) ) {
                                $this->dieUsageMsg( array( 'missingparam', 
'group' ) );
                        }
-
+                       if ( !isset( $params['aggregategroup'] ) ) {
+                               $this->dieUsageMsg( array( 'missingparam', 
'aggregategroup' ) );
+                       }
+                       $aggregateGroup = $params['aggregategroup'];
                        // Get the list of group ids
                        $groupId = $params['group'];
                        $subgroups = TranslateMetadata::get( $aggregateGroup, 
'subgroups' );
@@ -75,28 +77,36 @@
                        );
                        $logger->addEntry( $action, $group->getTitle(), null, 
array( serialize( $logparams ) ) );
                } elseif ( $action === 'remove' ) {
+                       if ( !isset( $params['aggregategroup'] ) ) {
+                               $this->dieUsageMsg( array( 'missingparam', 
'aggregategroup' ) );
+                       }
+                       $aggregateGroup = $params['aggregategroup'];
                        TranslateMetadata::set( $aggregateGroup, 'subgroups', 
false ) ;
                        TranslateMetadata::set( $aggregateGroup, 'name', false 
) ;
                        TranslateMetadata::set( $aggregateGroup, 'description', 
false ) ;
                } elseif ( $action === 'add' ) {
-                       if ( TranslateMetadata::get( $aggregateGroup, 
'subgroups' ) ) {
-                               $this->dieUsage( 'Aggregate message group 
already exists', 'duplicateaggregategroup' );
+                       if ( !isset( $params['groupname'] ) ) {
+                               $this->dieUsageMsg( array( 'missingparam', 
'groupname' ) );
                        }
-                       if ( !self::isValid ( $aggregateGroup ) ) {
-                               $this->dieUsage( '‎Invalid Aggregate message 
group name', 'invalidaggregategroup' );
+                       $name = trim( $params['groupname'] );
+                       if ( strlen( $name ) === 0 ) {
+                               $this->dieUsage( '‎Invalid Aggregate message 
group name', 'invalidaggregategroupname' );
                        }
-                       TranslateMetadata::set( $aggregateGroup, 'subgroups', 
'' ) ;
-                       $name = trim( $params['groupname'] );
                        $desc = trim( $params['groupdescription'] );
-
+                       $aggregategroupId = self::generateAggregateGroupId( 
$name );
+                       if ( TranslateMetadata::get( $aggregategroupId, 
'subgroups' ) ) {
+                               $this->dieUsage( 'Aggregate message group 
already exists', 'duplicateaggregategroup' );
+                       }
+                       TranslateMetadata::set( $aggregategroupId, 'subgroups', 
'' ) ;
                        if ( $name ) {
-                               TranslateMetadata::set( $aggregateGroup, 
'name', $name ) ;
+                               TranslateMetadata::set( $aggregategroupId, 
'name', $name ) ;
                        }
                        if ( $desc ) {
-                               TranslateMetadata::set( $aggregateGroup, 
'description', $desc ) ;
+                               TranslateMetadata::set( $aggregategroupId, 
'description', $desc ) ;
                        }
                        // Once new aggregate group added, we need to show all 
the pages that can be added to that.
                        $output['groups'] = self::getAllPages();
+                       $output['aggregategroupId'] = $aggregategroupId;
                }
 
                // If we got this far, nothing has failed
@@ -113,6 +123,14 @@
                return true;
        }
 
+       protected function generateAggregateGroupId ( $aggregateGroupName, 
$prefix = "agg-" ) {
+               if ( strlen( $aggregateGroupName ) + strlen ( $prefix )  >= 200 
) { // The database field for this has maxlimit 200
+                       return $prefix . substr( sha1( $aggregateGroupName ), 
0, 5 );
+               } else {
+                       return $prefix . preg_replace( 
'/[\x00-\x1f\x23\x27\x2c\x2e\x3c\x3e\x5b\x5d\x7b\x7c\x7d\x7f\s]+/i', '_', 
$aggregateGroupName );
+               }
+       }
+
        public function isWriteMode() {
                return true;
        }
@@ -132,7 +150,6 @@
                        ),
                        'aggregategroup' => array(
                                ApiBase::PARAM_TYPE => 'string',
-                               ApiBase::PARAM_REQUIRED => true,
                        ),
                        'group' => array(
                                ApiBase::PARAM_TYPE => array_keys( 
MessageGroups::getAllGroups() ),

Modified: 
trunk/extensions/Translate/resources/ext.translate.special.aggregategroups.js
===================================================================
--- 
trunk/extensions/Translate/resources/ext.translate.special.aggregategroups.js   
    2012-03-20 06:20:30 UTC (rev 114235)
+++ 
trunk/extensions/Translate/resources/ext.translate.special.aggregategroups.js   
    2012-03-20 07:14:01 UTC (rev 114236)
@@ -91,15 +91,6 @@
                var params = $.extend( getApiParams( $target ), {'do' : 
'remove' } );
                $.post( mw.util.wikiScript( 'api' ), params, successFunction );
        }
-       
-       /*
-        * Replace some special characters like space, dots, comma, brackets 
etc to _ in a string. Also convert it to lowercase.
-        */
-       function createId( s ){
-               if ( s !== undefined ) {
-                       return 'agg-' + s.toLowerCase().replace( 
/[\x00-\x1f\x23\x2c\x2e\x3c\x3e\x5b\x5d\x7b\x7c\x7d\x7f\s]+/g, '_' );
-               }
-       }
 
        $( '.tp-aggregate-add-button' ).click( associate );
        $( '.tp-aggregate-remove-button' ).click( dissociate );
@@ -110,7 +101,6 @@
        } );
 
        $( '#tpt-aggregategroups-save' ). on ( "click", function( event ){
-               var aggregateGroupId = createId( $( 
'input.tp-aggregategroup-add-name' ).val() );
                var aggregateGroupName = $( 'input.tp-aggregategroup-add-name' 
).val();
                var aggregateGroupDesc = $( 
'input.tp-aggregategroup-add-description' ).val();
                var $select = $( 'div.mw-tpa-group select' );
@@ -119,6 +109,7 @@
                        if ( data.error ) {
                                alert( data.error.info );
                        }else{
+                               var aggregateGroupId = 
data.aggregategroups.aggregategroupId;
                                var $removeSpan =  $( '<span>' ).attr( 'id', 
aggregateGroupId ).addClass( 'tp-aggregate-remove-ag-button' );
                                var $div = $( "<div class='mw-tpa-group'>" )
                                        .append ( $( '<h2>' ).text( 
aggregateGroupName ) 
@@ -150,7 +141,6 @@
                        action: "aggregategroups",
                        'do' : 'add',
                        token: $( "#token" ).val(),
-                       aggregategroup: aggregateGroupId,
                        groupname : aggregateGroupName,
                        groupdescription: aggregateGroupDesc,
                        format: "json"


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to