Index: trunk/extensions/SemanticForms/includes/SF_FormPrinter.inc |
— | — | @@ -287,13 +287,8 @@ |
288 | 288 | // get the first instance of this template on the page being edited, |
289 | 289 | // even if there are more |
290 | 290 | 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(); |
298 | 293 | // cycle through template call, splitting it up by pipes ('|'), |
299 | 294 | // except when that pipe is part of a piped link |
300 | 295 | $field = ""; |
— | — | @@ -302,23 +297,22 @@ |
303 | 298 | $c = $existing_template_text[$i]; |
304 | 299 | if (($i == strlen($existing_template_text) - 1) || |
305 | 300 | ($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; |
322 | 306 | } |
| 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 = ''; |
323 | 317 | } else { |
324 | 318 | $field .= $c; |
325 | 319 | if ($c == '[') { |
— | — | @@ -480,9 +474,9 @@ |
481 | 475 | // the current template being processed, get the current template |
482 | 476 | // field's value in the existing page |
483 | 477 | if ($source_is_page && (! empty($existing_template_text))) { |
484 | | - if (isset($template_contents[$field_name])) |
| 478 | + if (isset($template_contents[$field_name])) { |
485 | 479 | $cur_value = $template_contents[$field_name]; |
486 | | - else |
| 480 | + } else |
487 | 481 | $cur_value = ''; |
488 | 482 | if ($cur_value) { |
489 | 483 | $cur_value = Sanitizer::safeEncodeAttribute($cur_value); |
— | — | @@ -529,7 +523,7 @@ |
530 | 524 | } |
531 | 525 | foreach ($cur_value as $key => $val) { |
532 | 526 | if ($key !== "is_list") { |
533 | | - if ($cur_value_in_template != "") { |
| 527 | + if ($cur_value_in_template != "") { |
534 | 528 | $cur_value_in_template .= $delimiter . " "; |
535 | 529 | } |
536 | 530 | $cur_value_in_template .= $val; |
— | — | @@ -690,11 +684,9 @@ |
691 | 685 | } |
692 | 686 | |
693 | 687 | if ($new_text) { |
| 688 | + // include the field name only for non-numeric field names |
694 | 689 | 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"; |
699 | 691 | } else { |
700 | 692 | // if the value is null, don't include it at all |
701 | 693 | if ($cur_value_in_template != '') |
— | — | @@ -801,21 +793,25 @@ |
802 | 794 | } // end while |
803 | 795 | |
804 | 796 | 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); |
806 | 802 | // add another newline before the final bracket, if this template |
807 | 803 | // call is already more than one line |
808 | 804 | if (strpos($template_text, "\n")) |
809 | 805 | $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); |
820 | 816 | // otherwise, if it's a partial form, we have to add the new |
821 | 817 | // text somewhere |
822 | 818 | } elseif ($form_is_partial && $wgRequest->getCheck('partial') ) { |