jenkins-bot has submitted this change and it was merged.

Change subject: Added support for disambiguating mapped labels.
......................................................................


Added support for disambiguating mapped labels.

Change-Id: If42db62973e6f0299d0a308ad30e55ec74230192
---
M includes/SF_FormPrinter.php
M includes/SF_Utils.php
2 files changed, 124 insertions(+), 4 deletions(-)

Approvals:
  Yaron Koren: Checked; Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/SF_FormPrinter.php b/includes/SF_FormPrinter.php
index 5548be2..2863ddc 100644
--- a/includes/SF_FormPrinter.php
+++ b/includes/SF_FormPrinter.php
@@ -295,6 +295,7 @@
                // in the query string, the form ends up being
                // parsed twice.
                if ( array_key_exists( 'is_list', $value ) ) {
+                       unset($value['is_list']);
                        return implode( "$delimiter ", $value );
                }
 
@@ -841,6 +842,7 @@
                                        $is_restricted = false;
                                        $is_uploadable = false;
                                        $is_list = false;
+                                       $delimiter = null;
                                        $input_type = null;
                                        $field_args = array();
                                        $show_on_select = array();
@@ -975,6 +977,9 @@
                                                                        
$wgUser->getEffectiveGroups(), array_map( 'trim', explode( ',', 
$sub_components[1] ) )
                                                                );
                                                        }
+                                                       if ( !is_null( 
$possible_values ) && array_key_exists( 'mapping template', $field_args ) ) {
+                                                               
$possible_values = SFUtils::getLabels( $possible_values, $field_args['mapping 
template'] );
+                                                       }
                                                }
                                        } // end for
                                        // Backwards compatibility
@@ -1019,10 +1024,36 @@
                                                } elseif ( array_key_exists( 
$field_name, $template_instance_query_values ) ) {
                                                        $field_query_val = 
$template_instance_query_values[$field_name];
                                                }
-                                               if ( $form_submitted || ( 
$field_query_val != '' && ! is_array( $field_query_val ) ) ) {
-                                                       $cur_value = 
$field_query_val;
-                                               } elseif ( $form_submitted || ( 
$field_query_val != '' && is_array( $field_query_val ) ) ) {
-                                                       $cur_value = 
$this->getStringFromPassedInArray( $field_query_val, $delimiter );
+                                               if ( $form_submitted && 
$field_query_val != '' ) {
+                                                       $mapping_template = 
null;
+                                                       if ( array_key_exists( 
'mapping_template', $template_instance_query_values ) &&
+                                                               
array_key_exists( $field_name, 
$template_instance_query_values['mapping_template'] ) ) {
+                                                               
$mapping_template = 
$template_instance_query_values['mapping_template'][$field_name];
+                                                       }
+                                                       if ( is_array( 
$field_query_val ) ) {
+                                                               $cur_values = 
array();
+                                                               foreach ( 
$field_query_val as $key => $value ) {
+                                                                       if ( 
!is_null( $mapping_template ) && !is_null( $possible_values ) ) {
+                                                                               
$cur_values = array();
+                                                                               
foreach ( $field_query_val as $key => $val ) {
+                                                                               
        if ( $key === 'is_list' ) {
+                                                                               
                $cur_values[$key] = $val;
+                                                                               
        } else {
+                                                                               
                $cur_values[] = SFUtils::labelToValue( $val, $possible_values, 
$mapping_template );
+                                                                               
        }
+                                                                               
}
+                                                                       } else {
+                                                                               
$cur_values[$key] = $value;
+                                                                       }
+                                                               }
+                                                               $cur_value = 
$this->getStringFromPassedInArray( $cur_values, $delimiter );
+                                                       } else {
+                                                               if ( !is_null( 
$mapping_template ) && !is_null( $possible_values ) ) {
+                                                                       
$cur_value = SFUtils::labelToValue( $field_query_val, $possible_values, 
$mapping_template );
+                                                               } else {
+                                                                       
$cur_value = $field_query_val;
+                                                               }
+                                                       }
                                                }
                                        }
 
@@ -1188,6 +1219,9 @@
                                                if ( $form_submitted ) {
                                                        wfRunHooks( 
'sfCreateFormField', array( &$form_field, &$cur_value_in_template, true ) );
                                                } else {
+                                                       if ( !empty( $cur_value 
) && array_key_exists( 'mapping template', $field_args ) ) {
+                                                               $cur_value = 
SFUtils::valuesToLabels( $cur_value, $field_args['mapping template'], 
$delimiter, $possible_values );
+                                                       }
                                                        wfRunHooks( 
'sfCreateFormField', array( &$form_field, &$cur_value, false ) );
                                                }
                                                // if this is not part of a 
'multiple' template, increment the
@@ -1288,6 +1322,10 @@
                                                        }
                                                }
 
+                                               if ( array_key_exists( 'mapping 
template', $field_args ) ) {
+                                                       $new_text .= 
Html::hidden( $template_name . '[mapping_template][' . $field_name . ']', 
$field_args['mapping template'] );
+                                               }
+
                                                if ( $new_text ) {
                                                        // Include the field 
name only for non-numeric field names.
                                                        if ( is_numeric( 
$field_name ) ) {
diff --git a/includes/SF_Utils.php b/includes/SF_Utils.php
index b083d30..c59bb6d 100644
--- a/includes/SF_Utils.php
+++ b/includes/SF_Utils.php
@@ -591,6 +591,88 @@
                }
        }
 
+       /**
+        * Helper function to get an array of labels from an array of values 
given a mapping template
+        */
+       public static function getLabels( $values, $templateName ) {
+               global $wgParser;
+               $labels = array();
+               $title = Title::makeTitleSafe( NS_TEMPLATE, $templateName );
+               $templateExists = $title->exists();
+               foreach ( $values as $value ) {
+                       if ( $templateExists ) {
+                               $label = $wgParser->recursiveTagParse( '{{' .  
$templateName .
+                                       '|' .  $value . '}}' );
+                               if ( $label == '' ) {
+                                       $labels[$value] = $value;
+                               } else {
+                                       $labels[$value] = $label;
+                               }
+                       } else {
+                               $labels[$value] = $value;
+                       }
+               }
+               if ( count( $labels ) == count( array_unique( $labels ) ) ) {
+                       return $labels;
+               }
+               $fixed_labels = array();
+               foreach ( $labels as $value => $label ) {
+                       $fixed_labels[$value] = $labels[$value];
+               }
+               $counts = array_count_values( $fixed_labels );
+               foreach ( $counts as $current_label => $count ) {
+                       if ( $count > 1 ) {
+                               $matching_keys = array_keys( $labels, 
$current_label );
+                               foreach ( $matching_keys as $key ) {
+                                       $fixed_labels[$key] .= ' (' . $key . 
')';
+                               }
+                       }
+               }
+               if ( count( $fixed_labels ) == count( array_unique( 
$fixed_labels ) ) ) {
+                       return $fixed_labels;
+               }
+               foreach ( $labels as $value => $label ) {
+                       $labels[$value] .= ' (' . $value . ')';
+               }
+               return $labels;
+       }
+
+       /**
+        * Helper function to use mapping template to turn label back into value
+        */
+       public static function labelToValue( $label, $possible_values, 
$templateName ) {
+               $value = array_search( $label, $possible_values );
+               if ( $value === false ) {
+                       return $label;
+               } else {
+                       return $value;
+               }
+       }
+
+       /**
+        * Helper function to map the current value with the mapping template, 
if the mapping template is set
+        */
+       public static function valuesToLabels( $valueString, $templateName, 
$delimiter, $possible_values ) {
+               if ( !is_null($delimiter ) ) {
+                       $values = array_map( 'trim', explode( $delimiter, 
$valueString ) );
+               } else {
+                       $values = array( $valueString );
+               }
+               $labels = array();
+               foreach ( $values as $value ) {
+                       if ( array_key_exists( $value, $possible_values ) ) {
+                               $labels[] = $possible_values[$value];
+                       } else {
+                               $labels[] = $value;
+                       }
+               }
+               if ( count( $labels ) > 1 ) {
+                       return $labels;
+               } else {
+                       return $labels[0];
+               }
+       }
+
        public static function getValuesFromExternalURL( $external_url_alias, 
$substring ) {
                global $sfgAutocompletionURLs;
                if ( empty( $sfgAutocompletionURLs ) ) {

-- 
To view, visit https://gerrit.wikimedia.org/r/113666
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: If42db62973e6f0299d0a308ad30e55ec74230192
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/SemanticForms
Gerrit-Branch: master
Gerrit-Owner: Cicalese <cical...@mitre.org>
Gerrit-Reviewer: Cicalese <cical...@mitre.org>
Gerrit-Reviewer: Foxtrott <s7ep...@gmail.com>
Gerrit-Reviewer: Yaron Koren <yaro...@gmail.com>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to