Index: trunk/extensions/Translate/api/ApiAggregateGroups.php |
— | — | @@ -28,7 +28,6 @@ |
29 | 29 | |
30 | 30 | $logger = new LogPage( 'pagetranslation' ); |
31 | 31 | $params = $this->extractRequestParams(); |
32 | | - $aggregateGroup = $params['aggregategroup']; |
33 | 32 | $action = $params['do']; |
34 | 33 | $output = array(); |
35 | 34 | if ( $action === 'associate' || $action === 'dissociate' ) { |
— | — | @@ -36,7 +35,10 @@ |
37 | 36 | if ( !isset( $params['group'] ) ) { |
38 | 37 | $this->dieUsageMsg( array( 'missingparam', 'group' ) ); |
39 | 38 | } |
40 | | - |
| 39 | + if ( !isset( $params['aggregategroup'] ) ) { |
| 40 | + $this->dieUsageMsg( array( 'missingparam', 'aggregategroup' ) ); |
| 41 | + } |
| 42 | + $aggregateGroup = $params['aggregategroup']; |
41 | 43 | // Get the list of group ids |
42 | 44 | $groupId = $params['group']; |
43 | 45 | $subgroups = TranslateMetadata::get( $aggregateGroup, 'subgroups' ); |
— | — | @@ -75,28 +77,36 @@ |
76 | 78 | ); |
77 | 79 | $logger->addEntry( $action, $group->getTitle(), null, array( serialize( $logparams ) ) ); |
78 | 80 | } elseif ( $action === 'remove' ) { |
| 81 | + if ( !isset( $params['aggregategroup'] ) ) { |
| 82 | + $this->dieUsageMsg( array( 'missingparam', 'aggregategroup' ) ); |
| 83 | + } |
| 84 | + $aggregateGroup = $params['aggregategroup']; |
79 | 85 | TranslateMetadata::set( $aggregateGroup, 'subgroups', false ) ; |
80 | 86 | TranslateMetadata::set( $aggregateGroup, 'name', false ) ; |
81 | 87 | TranslateMetadata::set( $aggregateGroup, 'description', false ) ; |
82 | 88 | } 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' ) ); |
85 | 91 | } |
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' ); |
88 | 95 | } |
89 | | - TranslateMetadata::set( $aggregateGroup, 'subgroups', '' ) ; |
90 | | - $name = trim( $params['groupname'] ); |
91 | 96 | $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', '' ) ; |
93 | 102 | if ( $name ) { |
94 | | - TranslateMetadata::set( $aggregateGroup, 'name', $name ) ; |
| 103 | + TranslateMetadata::set( $aggregategroupId, 'name', $name ) ; |
95 | 104 | } |
96 | 105 | if ( $desc ) { |
97 | | - TranslateMetadata::set( $aggregateGroup, 'description', $desc ) ; |
| 106 | + TranslateMetadata::set( $aggregategroupId, 'description', $desc ) ; |
98 | 107 | } |
99 | 108 | // Once new aggregate group added, we need to show all the pages that can be added to that. |
100 | 109 | $output['groups'] = self::getAllPages(); |
| 110 | + $output['aggregategroupId'] = $aggregategroupId; |
101 | 111 | } |
102 | 112 | |
103 | 113 | // If we got this far, nothing has failed |
— | — | @@ -113,6 +123,14 @@ |
114 | 124 | return true; |
115 | 125 | } |
116 | 126 | |
| 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 | + |
117 | 135 | public function isWriteMode() { |
118 | 136 | return true; |
119 | 137 | } |
— | — | @@ -132,7 +150,6 @@ |
133 | 151 | ), |
134 | 152 | 'aggregategroup' => array( |
135 | 153 | ApiBase::PARAM_TYPE => 'string', |
136 | | - ApiBase::PARAM_REQUIRED => true, |
137 | 154 | ), |
138 | 155 | 'group' => array( |
139 | 156 | ApiBase::PARAM_TYPE => array_keys( MessageGroups::getAllGroups() ), |
Index: trunk/extensions/Translate/resources/ext.translate.special.aggregategroups.js |
— | — | @@ -91,15 +91,6 @@ |
92 | 92 | var params = $.extend( getApiParams( $target ), {'do' : 'remove' } ); |
93 | 93 | $.post( mw.util.wikiScript( 'api' ), params, successFunction ); |
94 | 94 | } |
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 | | - } |
104 | 95 | |
105 | 96 | $( '.tp-aggregate-add-button' ).click( associate ); |
106 | 97 | $( '.tp-aggregate-remove-button' ).click( dissociate ); |
— | — | @@ -110,7 +101,6 @@ |
111 | 102 | } ); |
112 | 103 | |
113 | 104 | $( '#tpt-aggregategroups-save' ). on ( "click", function( event ){ |
114 | | - var aggregateGroupId = createId( $( 'input.tp-aggregategroup-add-name' ).val() ); |
115 | 105 | var aggregateGroupName = $( 'input.tp-aggregategroup-add-name' ).val(); |
116 | 106 | var aggregateGroupDesc = $( 'input.tp-aggregategroup-add-description' ).val(); |
117 | 107 | var $select = $( 'div.mw-tpa-group select' ); |
— | — | @@ -119,6 +109,7 @@ |
120 | 110 | if ( data.error ) { |
121 | 111 | alert( data.error.info ); |
122 | 112 | }else{ |
| 113 | + var aggregateGroupId = data.aggregategroups.aggregategroupId; |
123 | 114 | var $removeSpan = $( '<span>' ).attr( 'id', aggregateGroupId ).addClass( 'tp-aggregate-remove-ag-button' ); |
124 | 115 | var $div = $( "<div class='mw-tpa-group'>" ) |
125 | 116 | .append ( $( '<h2>' ).text( aggregateGroupName ) |
— | — | @@ -150,7 +141,6 @@ |
151 | 142 | action: "aggregategroups", |
152 | 143 | 'do' : 'add', |
153 | 144 | token: $( "#token" ).val(), |
154 | | - aggregategroup: aggregateGroupId, |
155 | 145 | groupname : aggregateGroupName, |
156 | 146 | groupdescription: aggregateGroupDesc, |
157 | 147 | format: "json" |