r79035 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r79034‎ | r79035 | r79036 >
Date:22:56, 26 December 2010
Author:yaron
Status:deferred
Tags:
Comment:
Improved comments and formatting
Modified paths:
  • /trunk/extensions/SemanticForms/includes/SF_FormPrinter.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/includes/SF_FormPrinter.php
@@ -144,6 +144,16 @@
145145 }
146146 }
147147
 148+ /**
 149+ * This function is the real heart of the entire Semantic Forms
 150+ * extension. It handles two main actions: (1) displaying a form on the
 151+ * screen, given a form definition and possibly page contents (if an
 152+ * existing page is being edited); and (2) creating actual page contents,
 153+ * if the form was already submitted by the user.
 154+ *
 155+ * It also does some related tasks, like figuring out the page name (if
 156+ * only a page formula exists).
 157+ */
148158 function formHTML( $form_def, $form_submitted, $source_is_page, $form_id = null, $existing_page_content = null, $page_name = null, $page_name_formula = null, $is_query = false, $embedded = false ) {
149159 global $wgRequest, $wgUser, $wgParser;
150160 global $sfgTabIndex; // used to represent the current tab index in the form
@@ -184,9 +194,9 @@
185195 }
186196 }
187197
188 - // disable all form elements if user doesn't have edit permission -
 198+ // Disable all form elements if user doesn't have edit permission -
189199 // two different checks are needed, because editing permissions can be
190 - // set in different ways
 200+ // set in different ways.
191201 // HACK - sometimes we don't know the page name in advance, but we still
192202 // need to set a title here for testing permissions
193203 if ( $embedded ) {
@@ -218,9 +228,9 @@
219229 $form_text = "";
220230 if ( $userCanEditPage || $is_query ) {
221231 $form_is_disabled = false;
222 - // show "Your IP address will be recorded" warning if user is
 232+ // Show "Your IP address will be recorded" warning if user is
223233 // anonymous, and it's not a query -
224 - // wikitext for bolding has to be replaced with HTML
 234+ // wiki-text for bolding has to be replaced with HTML.
225235 if ( $wgUser->isAnon() && ! $is_query ) {
226236 $anon_edit_warning = preg_replace( "/'''(.*)'''/", "<strong>$1</strong>", wfMsg( 'anoneditwarning' ) );
227237 $form_text .= "<p>$anon_edit_warning</p>\n";
@@ -231,14 +241,14 @@
232242 $wgOut->addHTML( "\n<hr />\n" );
233243 }
234244
235 - // Remove <noinclude> sections and <includeonly> tags from form definition
 245+ // Remove <noinclude> sections and <includeonly> tags from form definition.
236246 $form_def = StringUtils::delimiterReplace( '<noinclude>', '</noinclude>', '', $form_def );
237247 $form_def = strtr( $form_def, array( '<includeonly>' => '', '</includeonly>' => '' ) );
238248
239 - // parse wiki-text
240 - // add '<nowiki>' tags around every triple-bracketed form definition
 249+ // Parse wiki-text.
 250+ // Add '<nowiki>' tags around every triple-bracketed form definition
241251 // element, so that the wiki parser won't touch it - the parser will
242 - // remove the '<nowiki>' tags, leaving us with what we need
 252+ // remove the '<nowiki>' tags, leaving us with what we need.
243253 $form_def = "__NOEDITSECTION__" . strtr( $form_def, array( '{{{' => '<nowiki>{{{', '}}}' => '}}}</nowiki>' ) );
244254 $old_strip_state = $wgParser->mStripState;
245255 $wgParser->mStripState = new StripState();
@@ -272,13 +282,13 @@
273283 $free_text_preload_page = null;
274284 $free_text_components = array();
275285 $all_values_for_template = array();
276 - // unencode and HTML-encoded representations of curly brackets and
 286+ // Unencode any HTML-encoded representations of curly brackets and
277287 // pipes - this is a hack to allow for forms to include templates
278 - // that themselves contain form elements - the escaping is needed
279 - // to make sure that those elements don't get parsed too early
 288+ // that themselves contain form elements - the escaping was needed
 289+ // to make sure that those elements don't get parsed too early.
280290 $form_def = str_replace( array( '&#123;', '&#124;', '&#125;' ), array( '{', '|', '}' ), $form_def );
281 - // and another hack - replace the 'free text' standard input with
282 - // a field declaration to get it to be handled as a field
 291+ // And another hack - replace the 'free text' standard input with
 292+ // a field declaration to get it to be handled as a field.
283293 $form_def = str_replace( 'standard input|free text', 'field|<freetext>', $form_def );
284294 while ( $brackets_loc = strpos( $form_def, "{{{", $start_position ) ) {
285295 $brackets_end_loc = strpos( $form_def, "}}}", $brackets_loc );
@@ -327,10 +337,10 @@
328338 $tif->template_name = $template_name;
329339 $query_template_name = str_replace( ' ', '_', $template_name );
330340 $add_button_text = wfMsg( 'sf_formedit_addanother' );
331 - // also replace periods with underlines, since that's what
332 - // POST does to strings anyway
 341+ // Also replace periods with underlines, since that's what
 342+ // POST does to strings anyway.
333343 $query_template_name = str_replace( '.', '_', $query_template_name );
334 - // cycle through the other components
 344+ // Cycle through the other components.
335345 for ( $i = 2; $i < count( $tag_components ); $i++ ) {
336346 $component = $tag_components[$i];
337347 if ( $component == 'multiple' ) $allow_multiple = true;
@@ -344,7 +354,8 @@
345355 }
346356 }
347357 }
348 - // if this is the first instance, add the label in the form
 358+ // If this is the first instance, add the label into the form, if
 359+ // there is one.
349360 if ( ( $old_template_name != $template_name ) && isset( $template_label ) ) {
350361 $form_text .= "<fieldset>\n";
351362 $form_text .= "<legend>$template_label</legend>\n";
@@ -383,12 +394,12 @@
384395 }
385396 // get the first instance of this template on the page being edited,
386397 // even if there are more
387 - if ( $found_instance ) {
 398+ if ( $found_instance ) {
388399 $matches = array();
389400 $search_pattern = '/{{' . $preg_match_template_str . '\s*[\|}]/i';
390401 $content_str = str_replace( '_', ' ', $existing_page_content );
391402 preg_match($search_pattern, $content_str, $matches, PREG_OFFSET_CAPTURE);
392 - // is this check necessary?
 403+ // is this check necessary?
393404 if ( array_key_exists( 0, $matches ) && array_key_exists( 1, $matches[0] ) ) {
394405 $start_char = $matches[0][1];
395406 $fields_start_char = $start_char + 2 + strlen( $search_template_str );
@@ -450,7 +461,7 @@
451462 } else {
452463 $existing_page_content = self::strReplaceFirst( $existing_template_text, '', $existing_page_content );
453464 }
454 - // if this is not a multiple-instance template, and we've found
 465+ // If this is not a multiple-instance template, and we've found
455466 // a match in the source page, there's a good chance that this
456467 // page was created with this form - note that, so we don't
457468 // send the user a warning
@@ -459,19 +470,19 @@
460471 // - on second thought, allow even the presence of multiple-
461472 // instance templates to validate that this is the correct
462473 // form: the problem is that some forms contain *only* mutliple-
463 - // instance templates
 474+ // instance templates.
464475 // if (! $allow_multiple) {
465476 $source_page_matches_this_form = true;
466477 // }
467478 }
468479 }
469480 }
470 - // if the input is from the form (meaning the user has hit one
 481+ // If the input is from the form (meaning the user has hit one
471482 // of the bottom row of buttons), and we're dealing with a
472483 // multiple template, get the values for this instance of this
473484 // template, then delete them from the array, so we can get the
474485 // next group next time - the next() command for arrays doesn't
475 - // seem to work here
 486+ // seem to work here.
476487 if ( ( ! $source_is_page ) && $allow_multiple && $wgRequest ) {
477488 $all_instances_printed = true;
478489 if ( $old_template_name != $template_name ) {
@@ -590,10 +601,11 @@
591602 $property_name = $sub_components[1];
592603 $dummy_field = new SFTemplateField();
593604 $dummy_field->setSemanticProperty( $property_name );
594 - if ( $dummy_field->propertyIsOfType( '_wpg' ) )
 605+ if ( $dummy_field->propertyIsOfType( '_wpg' ) ) {
595606 $field_args['autocomplete field type'] = 'relation';
596 - else
 607+ } else {
597608 $field_args['autocomplete field type'] = 'attribute';
 609+ }
598610 $field_args['autocompletion source'] = $sub_components[1];
599611 } elseif ( $sub_components[0] == 'autocomplete on' ) { // for backwards-compatibility
600612 $field_args['autocomplete field type'] = 'category';
@@ -657,7 +669,7 @@
658670 }
659671 } else {
660672 $cur_value = '';
661 - }
 673+ }
662674
663675 if ( empty( $cur_value ) ) {
664676 if ( $default_value ) {
@@ -683,14 +695,14 @@
684696 }
685697 }
686698
687 - // handle the free text field - if it was declared as
 699+ // Handle the free text field - if it was declared as
688700 // "field|free text" (a deprecated usage), it has to be outside
689 - // of a template
 701+ // of a template.
690702 if ( ( $template_name == '' && $field_name == 'free text' ) ||
691703 $field_name == '<freetext>' ) {
692 - // add placeholders for the free text in both the form and
 704+ // Add placeholders for the free text in both the form and
693705 // the page, using <free_text> tags - once all the free text
694 - // is known (at the end), it will get substituted in
 706+ // is known (at the end), it will get substituted in.
695707 if ( $is_hidden ) {
696708 $new_text = SFFormUtils::hiddenFieldHTML( 'free_text', '!free_text!' );
697709 } else {
@@ -858,13 +870,14 @@
859871 // it's a restricted field and user doesn't have sysop privileges
860872 $is_disabled = ( $form_is_disabled ||
861873 ( $is_restricted && ( ! $wgUser || ! $wgUser->isAllowed( 'editrestrictedfields' ) ) ) );
862 - // create an SFFormField instance based on all the
863 - // parameters in the form definition, and any information from
864 - // the template definition (contained in the $all_fields parameter)
865 - $form_field = SFFormField::createFromDefinition( $field_name, $input_name,
866 - $is_mandatory, $is_hidden, $is_uploadable, $possible_values, $is_disabled,
867 - $is_list, $input_type, $field_args, $all_fields, $strict_parsing );
868 - // if a property was set in the form definition, overwrite whatever
 874+ // Create an SFFormField instance based on all the parameters
 875+ // in the form definition, and any information from the template
 876+ // definition (contained in the $all_fields parameter).
 877+ $form_field = SFFormField::createFromDefinition( $field_name,
 878+ $input_name, $is_mandatory, $is_hidden, $is_uploadable,
 879+ $possible_values, $is_disabled, $is_list, $input_type,
 880+ $field_args, $all_fields, $strict_parsing );
 881+ // If a property was set in the form definition, overwrite whatever
869882 // is set in the template field - this is somewhat of a hack, since
870883 // parameters set in the form definition are meant to go into the
871884 // SFFormField object, not the SFTemplateField object it contains;
@@ -1115,21 +1128,21 @@
11161129 $remove_text = wfMsg( 'sf_formedit_remove' );
11171130 $form_text .= <<<END
11181131 <div class="multipleTemplate">
1119 - $section
1120 - <input type="button" value="$remove_text" tabindex="$sfgTabIndex" class="remover" />
1121 - </div>
 1132+ $section
 1133+ <input type="button" value="$remove_text" tabindex="$sfgTabIndex" class="remover" />
 1134+ </div>
11221135
11231136 END;
11241137 // this will cause the section to be re-parsed on the next go
11251138 $section_num--;
1126 - } else {
 1139+ } else {
11271140 // this is the last instance of this template - stick an 'add'
11281141 // button in the form
1129 - $form_text .= <<<END
 1142+ $form_text .= <<<END
11301143 <div id="starter_$query_template_name" class="multipleTemplateStarter" style="display: none;">
1131 - $section
1132 - </div>
1133 - <div id="main_$query_template_name"></div>
 1144+ $section
 1145+ </div>
 1146+ <div id="main_$query_template_name"></div>
11341147
11351148 END;
11361149 $adderID = "adder_$sfgFieldNum";
@@ -1209,7 +1222,7 @@
12101223 // add a warning in, if we're editing an existing page and that page
12111224 // appears to not have been created with this form
12121225 if ( $this->mPageTitle->exists() && ( $existing_page_content != '' ) && ! $source_page_matches_this_form ) {
1213 - $form_text = ' <div class="warningMessage">' . wfMsg( 'sf_formedit_formwarning', $this->mPageTitle->getFullURL() ) . "</div>\n" . $form_text;
 1226+ $form_text = "\t" . '<div class="warningMessage">' . wfMsg( 'sf_formedit_formwarning', $this->mPageTitle->getFullURL() ) . "</div>\n" . $form_text;
12141227 }
12151228
12161229 // add form bottom, if no custom "standard inputs" have been defined
@@ -1222,12 +1235,13 @@
12231236 $starttime = wfTimestampNow();
12241237 $page_article = new Article( $this->mPageTitle );
12251238 $edittime = $page_article->getTimestamp();
1226 - if ( !$is_query )
1227 - $form_text .= <<<END
 1239+ if ( !$is_query ) {
 1240+ $form_text .= <<<END
12281241
12291242 <input type="hidden" value="$starttime" name="wpStarttime" />
12301243 <input type="hidden" value="$edittime" name="wpEdittime" />
12311244 END;
 1245+ }
12321246 $form_text .= <<<END
12331247 </form>
12341248
@@ -1245,25 +1259,25 @@
12461260 }
12471261 }
12481262
1249 - // send the autocomplete values to the browser, along with the mappings
1250 - // of which values should apply to which fields
1251 - // if doing a replace, the data text is actually the modified original page
 1263+ // Send the autocomplete values to the browser, along with the mappings
 1264+ // of which values should apply to which fields.
 1265+ // If doing a replace, the data text is actually the modified original page
12521266 if ( $wgRequest->getCheck( 'partial' ) )
12531267 $data_text = $existing_page_content;
12541268
12551269 global $wgParser;
12561270 $new_text = "";
1257 - if ( !$embedded )
 1271+ if ( !$embedded ) {
12581272 $new_text = $wgParser->preprocess( str_replace( "{{!}}", "|", $form_page_title ), $this->mPageTitle, new ParserOptions() );
 1273+ }
12591274
1260 - // keep it simple - if the form has already been submitted, i.e. this is
1261 - // just the redirect page, get rid of all the Javascript, to avoid JS errors
 1275+ // If the form has already been submitted, i.e. this is just the redirect
 1276+ // page, get rid of all the Javascript, to avoid JS errors.
12621277 if ( $form_submitted ) {
12631278 $javascript_text = '';
12641279 }
12651280
1266 - return array( $form_text, $javascript_text,
1267 - $data_text, $new_text, $generated_page_name );
 1281+ return array( $form_text, $javascript_text, $data_text, $new_text, $generated_page_name );
12681282 }
12691283
12701284 /**

Status & tagging log