r107584 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r107583‎ | r107584 | r107585 >
Date:18:13, 29 December 2011
Author:khorn
Status:deferred
Tags:fundraising 
Comment:
followup r107551
Tightens up the language & translations, comments out some error_log statements I was using for debugging, removes a trailing "?>"
Modified paths:
  • /trunk/extensions/DonationInterface/gateway_common/DataValidator.php (modified) (history)

Diff [purge]

Index: trunk/extensions/DonationInterface/gateway_common/DataValidator.php
@@ -135,9 +135,9 @@
136136 * @param string $value - The value of the field. So far, only used to say
137137 * more precise things about Credit Cards.
138138 */
139 - public static function getErrorMessage( $field, $type, $value = null ){
 139+ public static function getErrorMessage( $field, $type, $language, $value = null ){
140140 //this is gonna get ugly up in here.
141 - error_log( __FUNCTION__ . " $field, $type, $value " );
 141+ //error_log( __FUNCTION__ . " $field, $type, $value " );
142142
143143 //Empty messages should get:
144144 //'donate_interface-error-msg' => 'Please enter your $1';
@@ -154,7 +154,7 @@
155155 //'donate_interface-error-msg-postal' => 'postal code',
156156
157157 $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 ) ) {
159159 return wfMsg( 'donate_interface-error-msg', wfMsg( $error_message_field_string ) );
160160 }
161161 }
@@ -191,7 +191,7 @@
192192 }
193193
194194 $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 ) ) {
196196 return wfMsg( $error_message_field_string );
197197 }
198198 }
@@ -203,21 +203,14 @@
204204
205205 /**
206206 * 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.
210209 * @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.
212212 */
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();
222215 }
223216
224217
@@ -242,8 +235,7 @@
243236 * We need to run the validation in an order that makes sense.
244237 *
245238 * 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.
248240 * Third: Do validation that depends on multiple fields (making sure you
249241 * validated that all the required fields exist on step 1, regardless of
250242 * $check_not_empty)
@@ -300,6 +292,7 @@
301293 $errors = array();
302294
303295 $self = get_called_class();
 296+ $language = self::getLanguage( $data );
304297
305298 foreach ( $instructions['non_empty'] as $field => $function ){
306299 if ( method_exists( $self, $function ) && $function === 'validate_not_empty' ) {
@@ -307,11 +300,11 @@
308301 $instructions['non_empty'][$field] = true;
309302 } else {
310303 $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 );
312305 }
313306 } else {
314307 $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 );
316309 throw new MWException( __FUNCTION__ . " BAD PROGRAMMER. No $function function. ('non_empty' rule for $field )" );
317310 }
318311 }
@@ -322,11 +315,11 @@
323316 $instructions['valid_type'][$field] = true;
324317 } else {
325318 $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 );
327320 }
328321 } else {
329322 $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 );
331324 throw new MWException( __FUNCTION__ . " BAD PROGRAMMER. No $function function. ('valid_type' rule for $field)" );
332325 }
333326 }
@@ -358,16 +351,17 @@
359352
360353 $instructions['calculated'][$field] = $result;
361354 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] );
363356 }
364357
365358 } else {
366359 $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] );
368361 throw new MWException( __FUNCTION__ . " BAD PROGRAMMER. No $function function. ('calculated' rule for $field)" );
369362 }
370363 }
371 - error_log( print_r( $errors, true ) );
 364+ //error_log( __FUNCTION__ . " " . print_r( $instructions, true ) );
 365+ //error_log( print_r( $errors, true ) );
372366 return $errors;
373367 }
374368
@@ -584,7 +578,6 @@
585579 * @return boolean True if the $value is not missing or empty, otherwise false.
586580 */
587581 protected static function validate_not_empty( $value, $data ){
588 - error_log(__FUNCTION__ . ". Yup!");
589582 if ( !array_key_exists( $value, $data ) || is_null( $data[$value] ) || $data[$value] === '' ){
590583 return false;
591584 }
@@ -671,6 +664,24 @@
672665 }
673666 }
674667
 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+
675688 }
676 -
677 -?>

Follow-up revisions

RevisionCommit summaryAuthorDate
r112245New DataValidator class, and donationinterface.php Only: MFT r107299, r10755...khorn21:42, 23 February 2012

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r107551DataValidator class, initial commit....khorn02:01, 29 December 2011

Status & tagging log