r114236 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r114235‎ | r114236 | r114237 >
Date:07:14, 20 March 2012
Author:santhosh
Status:ok
Tags:
Comment:
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 (modified) (history)
  • /trunk/extensions/Translate/resources/ext.translate.special.aggregategroups.js (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/api/ApiAggregateGroups.php
@@ -28,7 +28,6 @@
2929
3030 $logger = new LogPage( 'pagetranslation' );
3131 $params = $this->extractRequestParams();
32 - $aggregateGroup = $params['aggregategroup'];
3332 $action = $params['do'];
3433 $output = array();
3534 if ( $action === 'associate' || $action === 'dissociate' ) {
@@ -36,7 +35,10 @@
3736 if ( !isset( $params['group'] ) ) {
3837 $this->dieUsageMsg( array( 'missingparam', 'group' ) );
3938 }
40 -
 39+ if ( !isset( $params['aggregategroup'] ) ) {
 40+ $this->dieUsageMsg( array( 'missingparam', 'aggregategroup' ) );
 41+ }
 42+ $aggregateGroup = $params['aggregategroup'];
4143 // Get the list of group ids
4244 $groupId = $params['group'];
4345 $subgroups = TranslateMetadata::get( $aggregateGroup, 'subgroups' );
@@ -75,28 +77,36 @@
7678 );
7779 $logger->addEntry( $action, $group->getTitle(), null, array( serialize( $logparams ) ) );
7880 } elseif ( $action === 'remove' ) {
 81+ if ( !isset( $params['aggregategroup'] ) ) {
 82+ $this->dieUsageMsg( array( 'missingparam', 'aggregategroup' ) );
 83+ }
 84+ $aggregateGroup = $params['aggregategroup'];
7985 TranslateMetadata::set( $aggregateGroup, 'subgroups', false ) ;
8086 TranslateMetadata::set( $aggregateGroup, 'name', false ) ;
8187 TranslateMetadata::set( $aggregateGroup, 'description', false ) ;
8288 } elseif ( $action === 'add' ) {
83 - if ( TranslateMetadata::get( $aggregateGroup, 'subgroups' ) ) {
84 - $this->dieUsage( 'Aggregate message group already exists', 'duplicateaggregategroup' );
 89+ if ( !isset( $params['groupname'] ) ) {
 90+ $this->dieUsageMsg( array( 'missingparam', 'groupname' ) );
8591 }
86 - if ( !self::isValid ( $aggregateGroup ) ) {
87 - $this->dieUsage( '‎Invalid Aggregate message group name', 'invalidaggregategroup' );
 92+ $name = trim( $params['groupname'] );
 93+ if ( strlen( $name ) === 0 ) {
 94+ $this->dieUsage( '‎Invalid Aggregate message group name', 'invalidaggregategroupname' );
8895 }
89 - TranslateMetadata::set( $aggregateGroup, 'subgroups', '' ) ;
90 - $name = trim( $params['groupname'] );
9196 $desc = trim( $params['groupdescription'] );
92 -
 97+ $aggregategroupId = self::generateAggregateGroupId( $name );
 98+ if ( TranslateMetadata::get( $aggregategroupId, 'subgroups' ) ) {
 99+ $this->dieUsage( 'Aggregate message group already exists', 'duplicateaggregategroup' );
 100+ }
 101+ TranslateMetadata::set( $aggregategroupId, 'subgroups', '' ) ;
93102 if ( $name ) {
94 - TranslateMetadata::set( $aggregateGroup, 'name', $name ) ;
 103+ TranslateMetadata::set( $aggregategroupId, 'name', $name ) ;
95104 }
96105 if ( $desc ) {
97 - TranslateMetadata::set( $aggregateGroup, 'description', $desc ) ;
 106+ TranslateMetadata::set( $aggregategroupId, 'description', $desc ) ;
98107 }
99108 // Once new aggregate group added, we need to show all the pages that can be added to that.
100109 $output['groups'] = self::getAllPages();
 110+ $output['aggregategroupId'] = $aggregategroupId;
101111 }
102112
103113 // If we got this far, nothing has failed
@@ -113,6 +123,14 @@
114124 return true;
115125 }
116126
 127+ protected function generateAggregateGroupId ( $aggregateGroupName, $prefix = "agg-" ) {
 128+ if ( strlen( $aggregateGroupName ) + strlen ( $prefix ) >= 200 ) { // The database field for this has maxlimit 200
 129+ return $prefix . substr( sha1( $aggregateGroupName ), 0, 5 );
 130+ } else {
 131+ return $prefix . preg_replace( '/[\x00-\x1f\x23\x27\x2c\x2e\x3c\x3e\x5b\x5d\x7b\x7c\x7d\x7f\s]+/i', '_', $aggregateGroupName );
 132+ }
 133+ }
 134+
117135 public function isWriteMode() {
118136 return true;
119137 }
@@ -132,7 +150,6 @@
133151 ),
134152 'aggregategroup' => array(
135153 ApiBase::PARAM_TYPE => 'string',
136 - ApiBase::PARAM_REQUIRED => true,
137154 ),
138155 'group' => array(
139156 ApiBase::PARAM_TYPE => array_keys( MessageGroups::getAllGroups() ),
Index: trunk/extensions/Translate/resources/ext.translate.special.aggregategroups.js
@@ -91,15 +91,6 @@
9292 var params = $.extend( getApiParams( $target ), {'do' : 'remove' } );
9393 $.post( mw.util.wikiScript( 'api' ), params, successFunction );
9494 }
95 -
96 - /*
97 - * Replace some special characters like space, dots, comma, brackets etc to _ in a string. Also convert it to lowercase.
98 - */
99 - function createId( s ){
100 - if ( s !== undefined ) {
101 - return 'agg-' + s.toLowerCase().replace( /[\x00-\x1f\x23\x2c\x2e\x3c\x3e\x5b\x5d\x7b\x7c\x7d\x7f\s]+/g, '_' );
102 - }
103 - }
10495
10596 $( '.tp-aggregate-add-button' ).click( associate );
10697 $( '.tp-aggregate-remove-button' ).click( dissociate );
@@ -110,7 +101,6 @@
111102 } );
112103
113104 $( '#tpt-aggregategroups-save' ). on ( "click", function( event ){
114 - var aggregateGroupId = createId( $( 'input.tp-aggregategroup-add-name' ).val() );
115105 var aggregateGroupName = $( 'input.tp-aggregategroup-add-name' ).val();
116106 var aggregateGroupDesc = $( 'input.tp-aggregategroup-add-description' ).val();
117107 var $select = $( 'div.mw-tpa-group select' );
@@ -119,6 +109,7 @@
120110 if ( data.error ) {
121111 alert( data.error.info );
122112 }else{
 113+ var aggregateGroupId = data.aggregategroups.aggregategroupId;
123114 var $removeSpan = $( '<span>' ).attr( 'id', aggregateGroupId ).addClass( 'tp-aggregate-remove-ag-button' );
124115 var $div = $( "<div class='mw-tpa-group'>" )
125116 .append ( $( '<h2>' ).text( aggregateGroupName )
@@ -150,7 +141,6 @@
151142 action: "aggregategroups",
152143 'do' : 'add',
153144 token: $( "#token" ).val(),
154 - aggregategroup: aggregateGroupId,
155145 groupname : aggregateGroupName,
156146 groupdescription: aggregateGroupDesc,
157147 format: "json"

Status & tagging log