r82319 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r82318‎ | r82319 | r82320 >
Date:04:29, 17 February 2011
Author:yaron
Status:deferred
Tags:
Comment:
Replaced registration of input types with a new streamlined approach, using the new form-input class setup and a new function, registerInputType()
Modified paths:
  • /trunk/extensions/SemanticForms/includes/SF_FormPrinter.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/includes/SF_FormPrinter.php
@@ -19,57 +19,26 @@
2020 function __construct() {
2121 global $smwgContLang;
2222
23 - // initialize the set of hooks for the entry-field functions to call for
24 - // fields of both a specific semantic "type" and a defined "input type"
25 - // in the form definition
 23+ // Initialize variables.
2624 $this->mSemanticTypeHooks = array();
27 - if ( $smwgContLang != null ) {
28 - $datatypeLabels = $smwgContLang->getDatatypeLabels();
29 - $string_type = $datatypeLabels['_str'];
30 - $text_type = $datatypeLabels['_txt'];
31 - // type introduced in SMW 1.2
32 - if ( array_key_exists( '_cod', $datatypeLabels ) )
33 - $code_type = $datatypeLabels['_cod'];
34 - else
35 - $code_type = 'code';
36 - $url_type = $datatypeLabels['_uri'];
37 - $email_type = $datatypeLabels['_ema'];
38 - $number_type = $datatypeLabels['_num'];
39 - $bool_type = $datatypeLabels['_boo'];
40 - $date_type = $datatypeLabels['_dat'];
41 - $enum_type = 'enumeration'; // not a real type
42 - $page_type = $datatypeLabels['_wpg'];
43 - $this->setSemanticTypeHook( $string_type, false, array( 'SFTextInput', 'getText' ), array( 'field_type' => 'string' ) );
44 - $this->setSemanticTypeHook( $string_type, true, array( 'SFTextInput', 'getText' ), array( 'field_type' => 'string', 'is_list' => 'true', 'size' => '100' ) );
45 - $this->setSemanticTypeHook( $text_type, false, array( 'SFTextAreaInput', 'getText' ), array() );
46 - $this->setSemanticTypeHook( $code_type, false, array( 'SFTextAreaInput', 'getText' ), array() );
47 - $this->setSemanticTypeHook( $url_type, false, array( 'SFTextInput', 'getText' ), array( 'field_type' => 'URL' ) );
48 - $this->setSemanticTypeHook( $email_type, false, array( 'SFTextInput', 'getText' ), array( 'field_type' => 'email' ) );
49 - $this->setSemanticTypeHook( $number_type, false, array( 'SFTextInput', 'getText' ), array( 'field_type' => 'number' ) );
50 - $this->setSemanticTypeHook( $bool_type, false, array( 'SFCheckboxInput', 'getText' ), array() );
51 - $this->setSemanticTypeHook( $date_type, false, array( 'SFDateInput', 'getText' ), array() );
52 - $this->setSemanticTypeHook( $enum_type, false, array( 'SFDropdownInput', 'getText' ), array() );
53 - $this->setSemanticTypeHook( $enum_type, true, array( 'SFCheckboxesInput', 'getText' ), array() );
54 - $this->setSemanticTypeHook( $page_type, false, array( 'SFTextWithAutocompleteInput', 'getText' ), array( 'field_type' => 'page' ) );
55 - $this->setSemanticTypeHook( $page_type, true, array( 'SFTextWithAutocompleteInput', 'getText' ), array( 'field_type' => 'page', 'size' => '100', 'is_list' => 'true' ) );
56 - }
5725 $this->mInputTypeHooks = array();
58 - $this->setInputTypeHook( 'text', array( 'SFTextInput', 'getText' ), array() );
59 - $this->setInputTypeHook( 'textarea', array( 'SFTextAreaInput', 'getText' ), array() );
60 - $this->setInputTypeHook( 'date', array( 'SFDateInput', 'getText' ), array() );
61 - $this->setInputTypeHook( 'datetime', array( 'SFDateTimeInput', 'getText' ), array( 'include_timezone' => false ) );
62 - $this->setInputTypeHook( 'datetime with timezone', array( 'SFDateTimeInput', 'getText' ), array( 'include_timezone' => true ) );
63 - $this->setInputTypeHook( 'year', array( 'SFTextInput', 'getText' ), array( 'size' => 4 ) );
64 - $this->setInputTypeHook( 'checkbox', array( 'SFCheckboxInput', 'getText' ), array() );
65 - $this->setInputTypeHook( 'radiobutton', array( 'SFRadioButtonInput', 'getText' ), array() );
66 - $this->setInputTypeHook( 'checkboxes', array( 'SFCheckboxesInput', 'getText' ), array() );
67 - $this->setInputTypeHook( 'listbox', array( 'SFListBoxInput', 'getText' ), array() );
68 - $this->setInputTypeHook( 'combobox', array( 'SFComboBoxInput', 'getText' ), array() );
69 - $this->setInputTypeHook( 'category', array( 'SFCategoryInput', 'getText' ), array() );
70 - $this->setInputTypeHook( 'categories', array( 'SFCategoriesInput', 'getText' ), array() );
 26+ $this->mInputTypeClasses = array();
 27+ $this->standardInputsIncluded = false;
7128
72 - // initialize other variables
73 - $this->standardInputsIncluded = false;
 29+ $this->registerInputType( 'SFTextInput' );
 30+ $this->registerInputType( 'SFTextWithAutocompleteInput' );
 31+ $this->registerInputType( 'SFTextAreaInput' );
 32+ $this->registerInputType( 'SFTextAreaWithAutocompleteInput' );
 33+ $this->registerInputType( 'SFDateInput' );
 34+ $this->registerInputType( 'SFDateTimeInput' );
 35+ $this->registerInputType( 'SFCheckboxInput' );
 36+ $this->registerInputType( 'SFDropdownInput' );
 37+ $this->registerInputType( 'SFRadioButtonInput' );
 38+ $this->registerInputType( 'SFCheckboxesInput' );
 39+ $this->registerInputType( 'SFListBoxInput' );
 40+ $this->registerInputType( 'SFComboBoxInput' );
 41+ $this->registerInputType( 'SFCategoryInput' );
 42+ $this->registerInputType( 'SFCategoriesInput' );
7443 }
7544
7645 function setSemanticTypeHook( $type, $is_list, $function_name, $default_args ) {
@@ -80,7 +49,87 @@
8150 $this->mInputTypeHooks[$input_type] = array( $function_name, $default_args );
8251 }
8352
 53+ /**
 54+ * Register all information about the passed-in form input class.
 55+ */
 56+ function registerInputType( $inputTypeClass ) {
 57+ global $smwgContLang;
 58+
 59+ $inputTypeName = call_user_func( array( $inputTypeClass, 'getName' ) );
 60+ $this->mInputTypeClasses[$inputTypeName] = $inputTypeClass;
 61+ $this->setInputTypeHook( $inputTypeName, array( $inputTypeClass, 'getHTML' ), array() );
 62+ $alternateInputTypes = call_user_func( array( $inputTypeClass, 'getAlternateInputTypes' ) );
 63+ foreach ( $alternateInputTypes as $altInputTypeName => $additionalValues ) {
 64+ $this->setInputTypeHook( $altInputTypeName, array( $inputTypeClass, 'getHTML' ), $additionalValues );
 65+ }
 66+ $defaultProperties = call_user_func( array( $inputTypeClass, 'getDefaultPropTypes' ) );
 67+ foreach ( $defaultProperties as $propertyTypeID => $additionalValues ) {
 68+ if ( $smwgContLang != null ) {
 69+ $datatypeLabels = $smwgContLang->getDatatypeLabels();
 70+ $propertyType = $datatypeLabels[$propertyTypeID];
 71+ $this->setSemanticTypeHook( $propertyType, false, array( $inputTypeClass, 'getHTML' ), $additionalValues );
 72+ }
 73+ }
 74+ $defaultPropertyLists = call_user_func( array( $inputTypeClass, 'getDefaultPropTypeLists' ) );
 75+ foreach ( $defaultPropertyLists as $propertyTypeID => $additionalValues ) {
 76+ if ( $smwgContLang != null ) {
 77+ $datatypeLabels = $smwgContLang->getDatatypeLabels();
 78+ $propertyType = $datatypeLabels[$propertyTypeID];
 79+ $this->setSemanticTypeHook( $propertyType, true, array( $inputTypeClass, 'getHTML' ), $additionalValues );
 80+ }
 81+ }
 82+ }
8483
 84+ function getInputType( $inputTypeName ) {
 85+ if ( array_key_exists( $inputTypeName, $this->mInputTypeClasses ) ) {
 86+ return $this->mInputTypeClasses[$inputTypeName];
 87+ } else {
 88+ return null;
 89+ }
 90+ }
 91+
 92+ // TODO - this is very inefficient; the information should be stored in a
 93+ // separate array, populated within registerInputType().
 94+ public function getDefaultInputType( $isList, $propertyType ) {
 95+ foreach ( $this->mInputTypeClasses as $inputTypeName => $className ) {
 96+ if ( $isList ) {
 97+ $defaultPropTypes = call_user_func( array( $className, 'getDefaultPropTypeLists' ) );
 98+ if ( array_key_exists( $propertyType, $defaultPropTypes ) ) {
 99+ return $inputTypeName;
 100+ }
 101+ } else {
 102+ $defaultPropTypes = call_user_func( array( $className, 'getDefaultPropTypes' ) );
 103+ if ( array_key_exists( $propertyType, $defaultPropTypes ) ) {
 104+ return $inputTypeName;
 105+ }
 106+ }
 107+ }
 108+ }
 109+
 110+ // TODO - this is very inefficient; the information should be stored in a
 111+ // separate array, populated within registerInputType().
 112+ public function getPossibleInputTypes( $isList, $propertyType ) {
 113+ $possibleInputTypes = array();
 114+ foreach ( $this->mInputTypeClasses as $inputTypeName => $className ) {
 115+ if ( $isList ) {
 116+ $handledPropTypes = call_user_func( array( $className, 'getOtherPropTypeListsHandled' ) );
 117+ if ( in_array( $propertyType, $handledPropTypes ) ) {
 118+ $possibleInputTypes[] = $inputTypeName;
 119+ }
 120+ } else {
 121+ $handledPropTypes = call_user_func( array( $className, 'getOtherPropTypesHandled' ) );
 122+ if ( in_array( $propertyType, $handledPropTypes ) ) {
 123+ $possibleInputTypes[] = $inputTypeName;
 124+ }
 125+ }
 126+ }
 127+ return $possibleInputTypes;
 128+ }
 129+
 130+ public function getAllInputTypes() {
 131+ return array_keys( $this->mInputTypeClasses );
 132+ }
 133+
85134 /**
86135 * Show the set of previous deletions for the page being added.
87136 * This function is copied almost exactly from EditPage::showDeletionLog() -
@@ -204,9 +253,9 @@
205254 // flag for placing "<onlyinclude>" tags in form output
206255 $onlyinclude_free_text = false;
207256
208 - // if we have existing content and we're not in an active replacement
 257+ // If we have existing content and we're not in an active replacement
209258 // situation, preserve the original content. We do this because we want
210 - // to pass the original content on IF this is a partial form
 259+ // to pass the original content on IF this is a partial form.
211260 // TODO: A better approach here would be to pass the revision id of the
212261 // existing page content through the replace value, which would
213262 // minimize the html traffic and would allow us to do a concurrent
@@ -439,11 +488,11 @@
440489 $start_char = $matches[0][1];
441490 $fields_start_char = $start_char + 2 + strlen( $search_template_str );
442491 // Skip ahead to the first real character.
443 - while ( in_array( $existing_page_content[$fields_start_char], array( ' ', '\n' ) ) ) {
 492+ while ( in_array( $existing_page_content[$fields_start_char], array( ' ', '\n' ) ) ) {
444493 $fields_start_char++;
445494 }
446 - // If the next character is a pipe, skip that too.
447 - if( $existing_page_content[$fields_start_char] == '|' ) {
 495+ // If the next character is a pipe, skip that too.
 496+ if( $existing_page_content[$fields_start_char] == '|' ) {
448497 $fields_start_char++;
449498 }
450499 $template_contents = array( '0' => '' );
@@ -634,14 +683,9 @@
635684 }
636685 }
637686 } elseif ( $sub_components[0] == 'autocomplete on property' ) {
638 - // HACK - we need to figure out if this property is a
639 - // relation or attribute, i.e. whether it points to wiki
640 - // pages or not; so construct an SFTemplateField object
641 - // with this property, and determine it that way
642687 $property_name = $sub_components[1];
643 - $dummy_field = new SFTemplateField();
644 - $dummy_field->setSemanticProperty( $property_name );
645 - if ( $dummy_field->propertyIsOfType( '_wpg' ) ) {
 688+ $propValue = SMWPropertyValue::makeUserProperty( $this->semantic_property );
 689+ if ( $propValue->getPropertyID() == '_wpg' ) {
646690 $field_args['autocomplete field type'] = 'relation';
647691 } else {
648692 $field_args['autocomplete field type'] = 'attribute';
@@ -757,7 +801,7 @@
758802 } else {
759803 $default_value = $cur_value;
760804 }
761 - $new_text = SFTextAreaInput::getText( $default_value, 'free_text', false, ( $form_is_disabled || $is_restricted ), $field_args );
 805+ $new_text = SFTextAreaInput::getHTML( $default_value, 'free_text', false, ( $form_is_disabled || $is_restricted ), $field_args );
762806 if ( in_array( 'edittools', $free_text_components ) ) {
763807 // borrowed from EditPage::showEditTools()
764808 $options[] = 'parse';
@@ -948,7 +992,7 @@
949993 ( $cur_value == '' || $cur_value == 'now' ) ) {
950994 if ( $input_type == 'date' || $input_type == 'datetime' ||
951995 $input_type == 'datetime with timezone' || $input_type == 'year' ||
952 - ( $input_type == '' && $form_field->template_field->propertyIsOfType( '_dat' ) ) ) {
 996+ ( $input_type == '' && $form_field->template_field->field_type_id == '_dat' ) ) {
953997 // Get current time, for the time zone specified in the wiki.
954998 global $wgLocaltimezone;
955999 if ( isset( $wgLocaltimezone ) ) {
@@ -1316,7 +1360,7 @@
13171361 global $wgParser;
13181362 $new_text = "";
13191363 if ( !$embedded ) {
1320 - $new_text = $wgParser->recursiveTagParse (str_replace( "{{!}}", "|", $form_page_title ) );
 1364+ $new_text = $wgParser->recursiveTagParse (str_replace( "{{!}}", "|", $form_page_title ) );
13211365 }
13221366
13231367 // If the form has already been submitted, i.e. this is just the redirect
@@ -1377,7 +1421,7 @@
13781422 if ( ! array_key_exists( 'size', $other_args ) )
13791423 $other_args['size'] = 100;
13801424 }
1381 - $text = SFTextInput::getText( $cur_value, $form_field->input_name, $form_field->is_mandatory, $form_field->is_disabled, $other_args );
 1425+ $text = SFTextInput::getHTML( $cur_value, $form_field->input_name, $form_field->is_mandatory, $form_field->is_disabled, $other_args );
13821426 }
13831427 }
13841428 return $text;