r26339 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r26338‎ | r26339 | r26340 >
Date:01:02, 3 October 2007
Author:yaron
Status:old
Tags:
Comment:
setSemanticTypeHook() now takes in additional 'is_list' argument, moved
SMW 0.7-specific code to its own section, minor PHP fixes
Modified paths:
  • /trunk/extensions/SemanticForms/includes/SF_FormPrinter.inc (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/includes/SF_FormPrinter.inc
@@ -23,14 +23,16 @@
2424 // in the form definition
2525 $this->mSemanticTypeHooks = array();
2626 if ($smwgContLang != null) {
27 - $this->setSemanticTypeHook($smwgContLang->getDatatypeLabel('smw_string'), array('SFFormPrinter', 'textEntryHTML'), array('size' => 35));
28 - $this->setSemanticTypeHook($smwgContLang->getDatatypeLabel('smw_url'), array('SFFormPrinter', 'textEntryHTML'), array('size' => 100));
29 - $this->setSemanticTypeHook($smwgContLang->getDatatypeLabel('smw_float'), array('SFFormPrinter', 'textEntryHTML'), array('size' => 10));
30 - $this->setSemanticTypeHook($smwgContLang->getDatatypeLabel('smw_int'), array('SFFormPrinter', 'textEntryHTML'), array('size' => 10));
31 - $this->setSemanticTypeHook($smwgContLang->getDatatypeLabel('smw_bool'), array('SFFormPrinter', 'checkboxHTML'), array());
32 - $this->setSemanticTypeHook($smwgContLang->getDatatypeLabel('smw_datetime'), array('SFFormPrinter', 'dateEntryHTML'), array());
33 - $this->setSemanticTypeHook($smwgContLang->getDatatypeLabel('smw_enum'), array('SFFormPrinter', 'dropdownHTML'), array());
34 - $this->setSemanticTypeHook('relation', array('SFFormPrinter', 'textEntryWithAutocompleteHTML'), array('size' => 35));
 27+ $this->setSemanticTypeHook($smwgContLang->getDatatypeLabel('smw_string'), false, array('SFFormPrinter', 'textEntryHTML'), array('size' => 35));
 28+ $this->setSemanticTypeHook($smwgContLang->getDatatypeLabel('smw_string'), true, array('SFFormPrinter', 'textEntryHTML'), array('size' => 35));
 29+ $this->setSemanticTypeHook($smwgContLang->getDatatypeLabel('smw_url'), false, array('SFFormPrinter', 'textEntryHTML'), array('size' => 100));
 30+ $this->setSemanticTypeHook($smwgContLang->getDatatypeLabel('smw_float'), false, array('SFFormPrinter', 'textEntryHTML'), array('size' => 10));
 31+ $this->setSemanticTypeHook($smwgContLang->getDatatypeLabel('smw_int'), false, array('SFFormPrinter', 'textEntryHTML'), array('size' => 10));
 32+ $this->setSemanticTypeHook($smwgContLang->getDatatypeLabel('smw_bool'), false, array('SFFormPrinter', 'checkboxHTML'), array());
 33+ $this->setSemanticTypeHook($smwgContLang->getDatatypeLabel('smw_datetime'), false, array('SFFormPrinter', 'dateEntryHTML'), array());
 34+ $this->setSemanticTypeHook($smwgContLang->getDatatypeLabel('smw_enum'), false, array('SFFormPrinter', 'dropdownHTML'), array());
 35+ $this->setSemanticTypeHook($smwgContLang->getDatatypeLabel('smw_enum'), true, array('SFFormPrinter', 'checkboxesHTML'), array());
 36+ $this->setSemanticTypeHook('relation', false, array('SFFormPrinter', 'textEntryWithAutocompleteHTML'), array('size' => 35));
3537 }
3638 $this->mInputTypeHooks = array();
3739 $this->setInputTypeHook('text', array('SFFormPrinter', 'textEntryHTML'), array('size' => 35));
@@ -40,15 +42,15 @@
4143 $this->setInputTypeHook('datetime with timezone', array('SFFormPrinter', 'dateTimeEntryHTML'), array('include_timezone' => true));
4244 $this->setInputTypeHook('checkbox', array('SFFormPrinter', 'checkboxHTML'), array());
4345 $this->setInputTypeHook('radiobutton', array('SFFormPrinter', 'radioButtonHTML'), array());
 46+ $this->setInputTypeHook('checkboxes', array('SFFormPrinter', 'checkboxesHTML'), array());
4447 $this->setInputTypeHook('listbox', array('SFFormPrinter', 'listboxHTML'), array());
45 - $this->setInputTypeHook('checkboxes', array('SFFormPrinter', 'checkboxesHTML'), array());
4648
4749 // initialize other variables
4850 $this->standardInputsIncluded = false;
4951 }
5052
51 - function setSemanticTypeHook($type, $function_name, $default_args) {
52 - $this->mSemanticTypeHooks[$type] = array($function_name, $default_args);
 53+ function setSemanticTypeHook($type, $is_list, $function_name, $default_args) {
 54+ $this->mSemanticTypeHooks[$type][$is_list] = array($function_name, $default_args);
5355 }
5456
5557 function setInputTypeHook($input_type, $function_name, $default_args) {
@@ -933,8 +935,11 @@
934936 list($text, $javascript_text) = $this->textEntryHTML($cur_value, $form_field->input_name, $form_field->is_mandatory, $form_field->is_disabled, $field_args);
935937 } else { // input type not defined in form, and not a relation
936938 $field_type = $template_field->field_type;
937 - if ($field_type != '' && array_key_exists($field_type, $this->mSemanticTypeHooks)) {
938 - $hook_values = $this->mSemanticTypeHooks[$field_type];
 939+ $is_list = $template_field->is_list;
 940+ if ($field_type != '' &&
 941+ array_key_exists($field_type, $this->mSemanticTypeHooks) &&
 942+ isset($this->mSemanticTypeHooks[$field_type][$is_list])) {
 943+ $hook_values = $this->mSemanticTypeHooks[$field_type][$is_list];
939944 $funcArgs = array();
940945 $funcArgs[] = $cur_value;
941946 $funcArgs[] = $form_field->input_name;
@@ -960,15 +965,37 @@
961966 return array($text, $javascript_text);
962967 }
963968
 969+ function getAllPagesForRelation_0_7($relation_name) {
 970+ global $sfgMaxAutocompleteValues;
 971+
 972+ $fname = "SFFormPrinter::getAllPagesForRelation_0_7";
 973+ $pages = array();
 974+ $db = wfGetDB( DB_SLAVE );
 975+ $sql_options = array();
 976+ $sql_options['LIMIT'] = $sfgMaxAutocompleteValues;
 977+ $conditions = "relation_title = '$relation_name'";
 978+ $sql_options['ORDER BY'] = 'object_title';
 979+ $res = $db->select( $db->tableName('smw_relations'),
 980+ 'DISTINCT object_title',
 981+ $conditions, $fname, $sql_options);
 982+ while ($row = $db->fetchRow($res)) {
 983+ $cur_value = str_replace("'", "\'", $row[0]);
 984+ $pages[] = str_replace('_', ' ', $cur_value);
 985+ }
 986+ $db->freeResult($res);
 987+ return $pages;
 988+ }
 989+
964990 /*
965991 * Get all the pages that belong to a category and all its subcategories,
966992 * down a certain number of levels - heavily based on SMW's
967993 * SMWInlineQuery::includeSubcategories()
968994 */
969 - private function getAllPagesForCategory($dbr, $top_category, $num_levels) {
 995+ function getAllPagesForCategory($top_category, $num_levels) {
970996 if (0 == $num_levels) return $top_category;
971997 global $sfgMaxAutocompleteValues;
972998
 999+ $db = wfGetDB( DB_SLAVE );
9731000 $fname = "SFFormPrinter::getAllPagesForCategory";
9741001 $categories = array($top_category);
9751002 $checkcategories = array($top_category);
@@ -976,14 +1003,14 @@
9771004 for ($level = $num_levels; $level > 0; $level--) {
9781005 $newcategories = array();
9791006 foreach ($checkcategories as $category) {
980 - $res = $dbr->select( // make the query
 1007+ $res = $db->select( // make the query
9811008 array('categorylinks', 'page'),
9821009 array('page_title', 'page_namespace'),
9831010 array('cl_from = page_id',
984 - 'cl_to = '. $dbr->addQuotes($category)),
 1011+ 'cl_to = '. $db->addQuotes($category)),
9851012 $fname);
9861013 if ($res) {
987 - while ($res && $row = $dbr->fetchRow($res)) {
 1014+ while ($res && $row = $db->fetchRow($res)) {
9881015 if (array_key_exists('page_title', $row)) {
9891016 $page_namespace = $row['page_namespace'];
9901017 if ($page_namespace == NS_CATEGORY) {
@@ -1000,7 +1027,7 @@
10011028 }
10021029 }
10031030 }
1004 - $dbr->freeResult( $res );
 1031+ $db->freeResult( $res );
10051032 }
10061033 }
10071034 if (count($newcategories) == 0) {
@@ -1014,34 +1041,25 @@
10151042 }
10161043
10171044 function createAutocompleteValuesString($field_name, $autocomplete_field_type) {
1018 - global $sfgMaxAutocompleteValues;
1019 - $fname = 'SFFormPrinter::createAutocompleteValuesString';
1020 -
10211045 $names_array = array();
1022 - $db = wfGetDB( DB_SLAVE );
1023 - $sql_options = array();
1024 - $sql_options['LIMIT'] = $sfgMaxAutocompleteValues;
10251046 // the query depends on whether this field is a relation, category or
10261047 // namespace
10271048 if ($autocomplete_field_type == 'relation') {
1028 - $conditions = "relation_title = '$field_name'";
1029 - $sql_options['ORDER BY'] = 'object_title';
1030 - $res = $db->select( $db->tableName('smw_relations'),
1031 - 'DISTINCT object_title',
1032 - $conditions, $fname, $sql_options);
1033 - while ($row = $db->fetchRow($res)) {
1034 - $cur_value = str_replace("'", "\'", $row[0]);
1035 - $names_array[] = str_replace('_', ' ', $cur_value);
 1049+ $smw_version = SMW_VERSION;
 1050+ if ($smw_version{0} == '0') {
 1051+ $names_array = $this->getAllPagesForRelation_0_7($field_name);
 1052+ } else {
 1053+ // no handling yet
10361054 }
1037 - $db->freeResult($res);
10381055 } elseif ($autocomplete_field_type == 'category') {
1039 - $names_array = $this->getAllPagesForCategory($db, $field_name, 10);
 1056+ $names_array = $this->getAllPagesForCategory($field_name, 10);
10401057 } else { // i.e., $autocomplete_field_type == 'namespace'
10411058 // cycle through all the namespace names for this language, and if
10421059 // one matches the namespace specified in the form, add the names
10431060 // of all the pages in that namespace to $names_array
10441061 global $wgContLang;
10451062 $namespaces = $wgContLang->getNamespaces();
 1063+ $db = wfGetDB( DB_SLAVE );
10461064 foreach ($namespaces as $ns_code => $ns_name) {
10471065 if ($ns_name == $field_name) {
10481066 $conditions = "page_namespace = $ns_code";
@@ -1062,8 +1080,9 @@
10631081 }
10641082
10651083 function textEntryHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args) {
1066 - // if it's an autocomplete, call the with-autocomplete function insted
1067 - if (array_key_exists('autocomplete on', $other_args) || array_key_exists('autocomplete on namespace', $other_args)) {
 1084+ // if it's an autocomplete, call the with-autocomplete function instead
 1085+ if ((array_key_exists('autocomplete on', $other_args) && $other_args['autocomplete on'] != "") ||
 1086+ array_key_exists('autocomplete on namespace', $other_args)) {
10681087 return SFFormPrinter::textEntryWithAutocompleteHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args);
10691088 }
10701089
@@ -1194,7 +1213,8 @@
11951214
11961215 function textEntryWithAutocompleteHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args) {
11971216 // if 'no autocomplete' was specified, call the regular text entry instead
1198 - if ($other_args['no_autocomplete'] == true)
 1217+ if (array_key_exists('no_autocomplete', $other_args) &&
 1218+ $other_args['no_autocomplete'] == true)
11991219 return SFFormPrinter::textEntryHTML($cur_value, $input_name, $is_mandatory, $is_disabled, $other_args);
12001220
12011221 global $sfgTabIndex, $sfgFieldNum;
@@ -1226,7 +1246,7 @@
12271247 $autocomplete_values = array();
12281248 $autocomplete_values['mappings'] = array();
12291249 $options_str_key = $semantic_field_name . ',' . $autocomplete_field_type;
1230 - if (!$autocomplete_values[$options_str_key])
 1250+ if (! array_key_exists($options_str_key, $autocomplete_values))
12311251 $autocomplete_values[$options_str_key] = $this->createAutocompleteValuesString(str_replace(' ', '_', $semantic_field_name), $autocomplete_field_type);
12321252 $autocomplete_values['mappings'][$sfgFieldNum] = $options_str_key;
12331253 foreach ($autocomplete_values as $autocomplete_key => $autocomplete_string) {
@@ -1267,7 +1287,6 @@
12681288 function monthDropdownHTML($cur_month, $input_name, $is_disabled) {
12691289 global $sfgTabIndex, $sfgFieldNum;
12701290
1271 - $className = ($is_mandatory) ? "mandatoryField" : "createboxInput";
12721291 $disabled_text = ($is_disabled) ? "disabled" : "";
12731292 $text = ' <select tabindex="' . $sfgTabIndex . '" id="input_' . $sfgFieldNum . '_1" name="' . $input_name . "[month]\" $disabled_text>\n";
12741293 $month_names = array(

Status & tagging log