r38299 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r38298‎ | r38299 | r38300 >
Date:15:43, 31 July 2008
Author:yaron
Status:old
Tags:
Comment:
Fixed handling of templates that uses indexes, instead of names, for their
fields - blank fields now get added to the template call, if it's necessary to
add them (i.e., they're not at the very end)
Modified paths:
  • /trunk/extensions/SemanticForms/includes/SF_FormPrinter.inc (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/includes/SF_FormPrinter.inc
@@ -287,13 +287,8 @@
288288 // get the first instance of this template on the page being edited,
289289 // even if there are more
290290 if (preg_match('/\{\{' . $search_template_str . '\s*(?:(?:([\||:].*?)\}\})|()\}\})/mis', $existing_page_content, $matches)) {
291 - $existing_template_text = $matches[1];
292 - // create array of contents of this template
293 - // somewhat of a hack - this array starts out with one element,
294 - // so that adding fields with no corresponding key will give them
295 - // an index starting with 1, not 0, to match MediaWiki's counting
296 - // system
297 - $template_contents = array(null);
 291+ $existing_template_text = trim($matches[1]);
 292+ $template_contents = array();
298293 // cycle through template call, splitting it up by pipes ('|'),
299294 // except when that pipe is part of a piped link
300295 $field = "";
@@ -302,23 +297,22 @@
303298 $c = $existing_template_text[$i];
304299 if (($i == strlen($existing_template_text) - 1) ||
305300 ($c == '|' && $uncompleted_square_brackets == 0)) {
306 - if ($field != null) {
307 - // if this was the last character in the template, append
308 - // this character
309 - if ($i == strlen($existing_template_text) - 1) {
310 - $field .= $c;
311 - }
312 - // either there's an equals sign near the beginning or not -
313 - // handling is similar in either way; if there's no equals
314 - // sign, the index of this field becomes the key
315 - $sub_fields = explode('=', $field, 2);
316 - if (count($sub_fields) > 1) {
317 - $template_contents[trim($sub_fields[0])] = trim($sub_fields[1]);
318 - } else {
319 - $template_contents[] = trim($sub_fields[0]);
320 - }
321 - $field = '';
 301+ // if this was the last character in the template, append
 302+ // this character
 303+ if ($i == strlen($existing_template_text) - 1 &&
 304+ $c != '|') {
 305+ $field .= $c;
322306 }
 307+ // either there's an equals sign near the beginning or not -
 308+ // handling is similar in either way; if there's no equals
 309+ // sign, the index of this field becomes the key
 310+ $sub_fields = explode('=', $field, 2);
 311+ if (count($sub_fields) > 1) {
 312+ $template_contents[trim($sub_fields[0])] = trim($sub_fields[1]);
 313+ } else {
 314+ $template_contents[] = trim($sub_fields[0]);
 315+ }
 316+ $field = '';
323317 } else {
324318 $field .= $c;
325319 if ($c == '[') {
@@ -480,9 +474,9 @@
481475 // the current template being processed, get the current template
482476 // field's value in the existing page
483477 if ($source_is_page && (! empty($existing_template_text))) {
484 - if (isset($template_contents[$field_name]))
 478+ if (isset($template_contents[$field_name])) {
485479 $cur_value = $template_contents[$field_name];
486 - else
 480+ } else
487481 $cur_value = '';
488482 if ($cur_value) {
489483 $cur_value = Sanitizer::safeEncodeAttribute($cur_value);
@@ -529,7 +523,7 @@
530524 }
531525 foreach ($cur_value as $key => $val) {
532526 if ($key !== "is_list") {
533 - if ($cur_value_in_template != "") {
 527+ if ($cur_value_in_template != "") {
534528 $cur_value_in_template .= $delimiter . " ";
535529 }
536530 $cur_value_in_template .= $val;
@@ -690,11 +684,9 @@
691685 }
692686
693687 if ($new_text) {
 688+ // include the field name only for non-numeric field names
694689 if (is_numeric($field_name)) {
695 - // if the value is null, don't include it at all -
696 - // TODO: this isn't quite right
697 - if ($cur_value_in_template != '')
698 - $template_text .= "|$cur_value_in_template";
 690+ $template_text .= "|$cur_value_in_template";
699691 } else {
700692 // if the value is null, don't include it at all
701693 if ($cur_value_in_template != '')
@@ -801,21 +793,25 @@
802794 } // end while
803795
804796 if (! $all_instances_printed ) {
805 - if ( $template_text != '' ) {
 797+ if ($template_text != '') {
 798+ // for mostly aesthetic purposes, if the template call ends with
 799+ // a bunch of pipes (i.e., it's an indexed template with unused
 800+ // parameters at the end), remove the pipes
 801+ $template_text = preg_replace('/\|*$/', '', $template_text);
806802 // add another newline before the final bracket, if this template
807803 // call is already more than one line
808804 if (strpos($template_text, "\n"))
809805 $template_text .= "\n";
810 - $template_text .= "}}";
811 - $data_text .= $template_text . "\n";
812 - // if there is a placeholder in the text, we know that we are
813 - // doing a replace
814 - if ($existing_page_content && strpos($existing_page_content, '{{{insertionpoint}}}', 0) !== false) {
815 - $existing_page_content = preg_replace('/\{\{\{insertionpoint\}\}\}(\r?\n?)/',
816 - preg_replace('/\}\}/m', '}�',
817 - preg_replace('/\{\{/m', '�{', $template_text)) .
818 - "\n{{{insertionpoint}}}",
819 - $existing_page_content);
 806+ $template_text .= "}}";
 807+ $data_text .= $template_text . "\n";
 808+ // if there is a placeholder in the text, we know that we are
 809+ // doing a replace
 810+ if ($existing_page_content && strpos($existing_page_content, '{{{insertionpoint}}}', 0) !== false) {
 811+ $existing_page_content = preg_replace('/\{\{\{insertionpoint\}\}\}(\r?\n?)/',
 812+ preg_replace('/\}\}/m', '}�',
 813+ preg_replace('/\{\{/m', '�{', $template_text)) .
 814+ "\n{{{insertionpoint}}}",
 815+ $existing_page_content);
820816 // otherwise, if it's a partial form, we have to add the new
821817 // text somewhere
822818 } elseif ($form_is_partial && $wgRequest->getCheck('partial') ) {

Status & tagging log