Index: trunk/extensions/DonationInterface/gateway_common/DataValidator.php |
— | — | @@ -135,9 +135,9 @@ |
136 | 136 | * @param string $value - The value of the field. So far, only used to say |
137 | 137 | * more precise things about Credit Cards. |
138 | 138 | */ |
139 | | - public static function getErrorMessage( $field, $type, $value = null ){ |
| 139 | + public static function getErrorMessage( $field, $type, $language, $value = null ){ |
140 | 140 | //this is gonna get ugly up in here. |
141 | | - error_log( __FUNCTION__ . " $field, $type, $value " ); |
| 141 | + //error_log( __FUNCTION__ . " $field, $type, $value " ); |
142 | 142 | |
143 | 143 | //Empty messages should get: |
144 | 144 | //'donate_interface-error-msg' => 'Please enter your $1'; |
— | — | @@ -154,7 +154,7 @@ |
155 | 155 | //'donate_interface-error-msg-postal' => 'postal code', |
156 | 156 | |
157 | 157 | $error_message_field_string = 'donate_interface-error-msg-' . $message_field; |
158 | | - if ( $message_field != 'general' && self::wmfMessageExists( $error_message_field_string ) ) { |
| 158 | + if ( $message_field != 'general' && self::wmfMessageExists( $error_message_field_string, $language ) ) { |
159 | 159 | return wfMsg( 'donate_interface-error-msg', wfMsg( $error_message_field_string ) ); |
160 | 160 | } |
161 | 161 | } |
— | — | @@ -191,7 +191,7 @@ |
192 | 192 | } |
193 | 193 | |
194 | 194 | $error_message_field_string = 'donate_interface-error-msg-' . $suffix; |
195 | | - if ( self::wmfMessageExists( $error_message_field_string ) ) { |
| 195 | + if ( self::wmfMessageExists( $error_message_field_string, $language ) ) { |
196 | 196 | return wfMsg( $error_message_field_string ); |
197 | 197 | } |
198 | 198 | } |
— | — | @@ -203,21 +203,14 @@ |
204 | 204 | |
205 | 205 | /** |
206 | 206 | * wmfMessageExists returns true if a translatable message has been defined |
207 | | - * for the string that has been passed in, false if none is present. |
208 | | - * TODO: See what this does in other languages when the string exists in |
209 | | - * English, but not in the uselang. |
| 207 | + * for the string and language that have been passed in, false if none is |
| 208 | + * present. |
210 | 209 | * @param string $msg_key The message string to look up. |
211 | | - * @return boolean - true if message, exists, otherwise false. |
| 210 | + * @param string $language A valid mediawiki language code. |
| 211 | + * @return boolean - true if message exists, otherwise false. |
212 | 212 | */ |
213 | | - public static function wmfMessageExists( $msg_key ){ |
214 | | - //we may have some problems here if this returns false positives if a |
215 | | - //message exists in English, but not in the language we're looking for. |
216 | | - //..but, they're not problems we don't already have. |
217 | | - if ( wfEmptyMsg( $msg_key ) ) { |
218 | | - return false; |
219 | | - } else { |
220 | | - return true; |
221 | | - } |
| 213 | + public static function wmfMessageExists( $msg_key, $language ){ |
| 214 | + return wfMessage( $msg_key )->inLanguage( $language )->exists(); |
222 | 215 | } |
223 | 216 | |
224 | 217 | |
— | — | @@ -242,8 +235,7 @@ |
243 | 236 | * We need to run the validation in an order that makes sense. |
244 | 237 | * |
245 | 238 | * First: If we need to validate that some things are not empty, do that. |
246 | | - * Second: Do regular data type validation (on things that are not empty, |
247 | | - * keeping in mind we may or may not have exploded on those yet. |
| 239 | + * Second: Do regular data type validation on things that are not empty. |
248 | 240 | * Third: Do validation that depends on multiple fields (making sure you |
249 | 241 | * validated that all the required fields exist on step 1, regardless of |
250 | 242 | * $check_not_empty) |
— | — | @@ -300,6 +292,7 @@ |
301 | 293 | $errors = array(); |
302 | 294 | |
303 | 295 | $self = get_called_class(); |
| 296 | + $language = self::getLanguage( $data ); |
304 | 297 | |
305 | 298 | foreach ( $instructions['non_empty'] as $field => $function ){ |
306 | 299 | if ( method_exists( $self, $function ) && $function === 'validate_not_empty' ) { |
— | — | @@ -307,11 +300,11 @@ |
308 | 301 | $instructions['non_empty'][$field] = true; |
309 | 302 | } else { |
310 | 303 | $instructions['non_empty'][$field] = false; |
311 | | - $errors[ self::getErrorToken( $field ) ] = self::getErrorMessage( $field, 'non_empty' ); |
| 304 | + $errors[ self::getErrorToken( $field ) ] = self::getErrorMessage( $field, 'non_empty', $language ); |
312 | 305 | } |
313 | 306 | } else { |
314 | 307 | $instructions['non_empty'][$field] === 'exception'; |
315 | | - $errors[ self::getErrorToken( $field ) ] = self::getErrorMessage( $field, 'non_empty' ); |
| 308 | + $errors[ self::getErrorToken( $field ) ] = self::getErrorMessage( $field, 'non_empty', $language ); |
316 | 309 | throw new MWException( __FUNCTION__ . " BAD PROGRAMMER. No $function function. ('non_empty' rule for $field )" ); |
317 | 310 | } |
318 | 311 | } |
— | — | @@ -322,11 +315,11 @@ |
323 | 316 | $instructions['valid_type'][$field] = true; |
324 | 317 | } else { |
325 | 318 | $instructions['valid_type'][$field] = false; |
326 | | - $errors[ self::getErrorToken( $field ) ] = self::getErrorMessage( $field, 'valid_type' ); |
| 319 | + $errors[ self::getErrorToken( $field ) ] = self::getErrorMessage( $field, 'valid_type', $language ); |
327 | 320 | } |
328 | 321 | } else { |
329 | 322 | $instructions['valid_type'][$field] === 'exception'; |
330 | | - $errors[ self::getErrorToken( $field ) ] = self::getErrorMessage( $field, 'valid_type' ); |
| 323 | + $errors[ self::getErrorToken( $field ) ] = self::getErrorMessage( $field, 'valid_type', $language ); |
331 | 324 | throw new MWException( __FUNCTION__ . " BAD PROGRAMMER. No $function function. ('valid_type' rule for $field)" ); |
332 | 325 | } |
333 | 326 | } |
— | — | @@ -358,16 +351,17 @@ |
359 | 352 | |
360 | 353 | $instructions['calculated'][$field] = $result; |
361 | 354 | if ($result === false){ //implying we did the check, and it failed. |
362 | | - $errors[ self::getErrorToken( $field ) ] = self::getErrorMessage( $field, 'calculated' ); |
| 355 | + $errors[ self::getErrorToken( $field ) ] = self::getErrorMessage( $field, 'calculated', $language, $data[$field] ); |
363 | 356 | } |
364 | 357 | |
365 | 358 | } else { |
366 | 359 | $instructions['calculated'][$field] === 'exception'; |
367 | | - $errors[ self::getErrorToken( $field ) ] = self::getErrorMessage( $field, 'calculated' ); |
| 360 | + $errors[ self::getErrorToken( $field ) ] = self::getErrorMessage( $field, 'calculated', $language, $data[$field] ); |
368 | 361 | throw new MWException( __FUNCTION__ . " BAD PROGRAMMER. No $function function. ('calculated' rule for $field)" ); |
369 | 362 | } |
370 | 363 | } |
371 | | - error_log( print_r( $errors, true ) ); |
| 364 | + //error_log( __FUNCTION__ . " " . print_r( $instructions, true ) ); |
| 365 | + //error_log( print_r( $errors, true ) ); |
372 | 366 | return $errors; |
373 | 367 | } |
374 | 368 | |
— | — | @@ -584,7 +578,6 @@ |
585 | 579 | * @return boolean True if the $value is not missing or empty, otherwise false. |
586 | 580 | */ |
587 | 581 | protected static function validate_not_empty( $value, $data ){ |
588 | | - error_log(__FUNCTION__ . ". Yup!"); |
589 | 582 | if ( !array_key_exists( $value, $data ) || is_null( $data[$value] ) || $data[$value] === '' ){ |
590 | 583 | return false; |
591 | 584 | } |
— | — | @@ -671,6 +664,24 @@ |
672 | 665 | } |
673 | 666 | } |
674 | 667 | |
| 668 | + /** |
| 669 | + * getLanguage |
| 670 | + * Returns a valid mediawiki language code to use for all the |
| 671 | + * DonationInterface translations. |
| 672 | + * Will only look at the currently configured language if the 'language' key |
| 673 | + * doesn't exist in the data set: Users may not have a language preference |
| 674 | + * set if we're bouncing between mediawiki instances for payments. |
| 675 | + * @param array $data A normalized DonationInterface data set. |
| 676 | + * @return string A valid mediawiki language code. |
| 677 | + */ |
| 678 | + public static function getLanguage( $data ) { |
| 679 | + global $wgLang; |
| 680 | + if ( array_key_exists( 'language', $data ) |
| 681 | + && Language::isValidBuiltInCode( $data['language'] ) ) { |
| 682 | + return $data['language']; |
| 683 | + } else { |
| 684 | + return $wgLang->getCode(); |
| 685 | + } |
| 686 | + } |
| 687 | + |
675 | 688 | } |
676 | | - |
677 | | -?> |