/
PF_TemplateInForm.php
553 lines (498 loc) · 18.2 KB
/
PF_TemplateInForm.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
<?php
/**
* Represents a template in a user-defined form.
* @author Yaron Koren
* @file
* @ingroup PF
*/
class PFTemplateInForm {
private $mTemplateName;
private $mLabel;
private $mIntro;
private $mAddButtonText;
private $mDisplay;
private $mEventTitleField;
private $mEventDateField;
private $mEventStartDateField;
private $mEventEndDateField;
private $mAllowMultiple;
private $mStrictParsing;
private $mMinAllowed;
private $mMaxAllowed;
private $mFields = [];
private $mEmbedInTemplate;
private $mEmbedInField;
private $mPlaceholder;
private $mHeight = '200px';
/**
* Conceptually, it would make more sense to define and store this
* parameter per-field, rather than per-template. However, it's a lot
* easier to handle it in just one place, rather than in every form
* input class. Also, this allows, in theory, to set the order of the
* fields being displayed - though that's not being done yet.
*/
private $mDisplayedFieldsWhenMinimized;
/**
* These fields are for a specific usage of a form (or more
* specifically, a template in a form) to edit a particular page.
* Perhaps they should go in another class.
*/
private $mSearchTemplateStr;
private $mPregMatchTemplateStr;
private $mFullTextInPage;
private $mValuesFromPage = [];
private $mValuesFromSubmit = [];
private $mNumInstancesFromSubmit = 0;
private $mPageCallsThisTemplate = false;
private $mInstanceNum = 0;
private $mAllInstancesPrinted = false;
private $mGridValues = [];
static function create( $name, $label = null, $allowMultiple = null, $maxAllowed = null, $formFields = null ) {
$tif = new PFTemplateInForm();
$tif->mTemplateName = str_replace( '_', ' ', $name );
if ( $formFields === null ) {
$template = PFTemplate::newFromName( $tif->mTemplateName );
$fields = $template->getTemplateFields();
foreach ( $fields as $field ) {
$tif->mFields[] = PFFormField::create( $field );
}
} else {
$tif->mFields = $formFields;
}
$tif->mLabel = $label;
$tif->mAllowMultiple = $allowMultiple;
$tif->mMaxAllowed = $maxAllowed;
return $tif;
}
public static function newFromFormTag( $tag_components ) {
global $wgPageFormsEmbeddedTemplates;
$parser = PFUtils::getParser();
$tif = new PFTemplateInForm();
$tif->mTemplateName = str_replace( '_', ' ', trim( PFFormPrinter::getParsedValue( $parser, $tag_components[1] ) ) );
$tif->mAddButtonText = wfMessage( 'pf_formedit_addanother' )->text();
if ( array_key_exists( $tif->mTemplateName, $wgPageFormsEmbeddedTemplates ) ) {
[ $tif->mEmbedInTemplate, $tif->mEmbedInField ] =
$wgPageFormsEmbeddedTemplates[$tif->mTemplateName];
$tif->mPlaceholder = PFFormPrinter::placeholderFormat( $tif->mEmbedInTemplate, $tif->mEmbedInField );
}
// Cycle through the other components.
for ( $i = 2; $i < count( $tag_components ); $i++ ) {
$component = $tag_components[$i];
if ( $component == 'multiple' ) {
$tif->mAllowMultiple = true;
} elseif ( $component == 'strict' ) {
$tif->mStrictParsing = true;
}
$sub_components = array_map( 'trim', explode( '=', $component, 2 ) );
if ( count( $sub_components ) == 2 ) {
if ( $sub_components[0] == 'label' ) {
$tif->mLabel = PFFormPrinter::getParsedValue( $parser, $sub_components[1] );
} elseif ( $sub_components[0] == 'intro' ) {
$tif->mIntro = $sub_components[1];
} elseif ( $sub_components[0] == 'minimum instances' ) {
$tif->mMinAllowed = $sub_components[1];
} elseif ( $sub_components[0] == 'maximum instances' ) {
$tif->mMaxAllowed = $sub_components[1];
} elseif ( $sub_components[0] == 'add button text' ) {
$tif->mAddButtonText = PFFormPrinter::getParsedValue( $parser, $sub_components[1] );
} elseif ( $sub_components[0] == 'embed in field' ) {
// Placeholder on form template level. Assume that the template form def
// will have a multiple+placeholder parameters, and get the placeholder value.
// We expect something like TemplateName[fieldName], and convert it to the
// TemplateName___fieldName form used internally.
preg_match( '/\s*(.*)\[(.*)\]\s*/', $sub_components[1], $matches );
if ( count( $matches ) > 2 ) {
$tif->mEmbedInTemplate = $matches[1];
$tif->mEmbedInField = $matches[2];
$tif->mPlaceholder = PFFormPrinter::placeholderFormat( $tif->mEmbedInTemplate, $tif->mEmbedInField );
}
} elseif ( $sub_components[0] == 'display' ) {
$tif->mDisplay = $sub_components[1];
} elseif ( $sub_components[0] == 'height' ) {
$tif->mHeight = $sub_components[1];
} elseif ( $sub_components[0] == 'displayed fields when minimized' ) {
$tif->mDisplayedFieldsWhenMinimized = $sub_components[1];
} elseif ( $sub_components[0] == 'event title field' ) {
$tif->mEventTitleField = $sub_components[1];
} elseif ( $sub_components[0] == 'event date field' ) {
$tif->mEventDateField = $sub_components[1];
} elseif ( $sub_components[0] == 'event start date field' ) {
$tif->mEventStartDateField = $sub_components[1];
} elseif ( $sub_components[0] == 'event end date field' ) {
$tif->mEventEndDateField = $sub_components[1];
}
}
}
return $tif;
}
function getTemplateName() {
return $this->mTemplateName;
}
function getHeight() {
return $this->mHeight;
}
function getFields() {
return $this->mFields;
}
function getEmbedInTemplate() {
return $this->mEmbedInTemplate;
}
function getEmbedInField() {
return $this->mEmbedInField;
}
function getLabel() {
return $this->mLabel;
}
function getIntro() {
return $this->mIntro;
}
function getAddButtonText() {
return $this->mAddButtonText;
}
function getDisplay() {
return $this->mDisplay;
}
function getEventTitleField() {
return $this->mEventTitleField;
}
function getEventDateField() {
return $this->mEventDateField;
}
function getEventStartDateField() {
return $this->mEventStartDateField;
}
function getEventEndDateField() {
return $this->mEventEndDateField;
}
function getPlaceholder() {
return $this->mPlaceholder;
}
function getDisplayedFieldsWhenMinimized() {
return $this->mDisplayedFieldsWhenMinimized;
}
function allowsMultiple() {
return $this->mAllowMultiple;
}
function strictParsing() {
return $this->mStrictParsing;
}
function getMinInstancesAllowed() {
return $this->mMinAllowed;
}
function getMaxInstancesAllowed() {
return $this->mMaxAllowed;
}
function createMarkup() {
$text = "{{{for template|" . $this->mTemplateName;
if ( $this->mAllowMultiple ) {
$text .= "|multiple";
}
if ( $this->mLabel != '' ) {
$text .= "|label=" . $this->mLabel;
}
$text .= "}}}\n";
// For now, HTML for templates differs for multiple-instance
// templates; this may change if handling of form definitions
// gets more sophisticated.
if ( !$this->mAllowMultiple ) {
$text .= "{| class=\"formtable\"\n";
}
foreach ( $this->mFields as $i => $field ) {
$is_last_field = ( $i == count( $this->mFields ) - 1 );
$text .= $field->createMarkup( $this->mAllowMultiple, $is_last_field );
}
if ( !$this->mAllowMultiple ) {
$text .= "|}\n";
}
$text .= "{{{end template}}}\n";
return $text;
}
/**
* This method, and all methods below it, are intended for an instance
* of a template in a specific form, and perhaps should be moved into
* another class.
*
* @return string
*/
function getFullTextInPage() {
return $this->mFullTextInPage;
}
function pageCallsThisTemplate() {
return $this->mPageCallsThisTemplate;
}
function hasValueFromPageForField( $field_name ) {
return array_key_exists( $field_name, $this->mValuesFromPage );
}
function getAndRemoveValueFromPageForField( $field_name ) {
$value = $this->mValuesFromPage[$field_name];
unset( $this->mValuesFromPage[$field_name] );
return $value;
}
function getValuesFromPage() {
return $this->mValuesFromPage;
}
function getInstanceNum() {
return $this->mInstanceNum;
}
function getGridValues() {
return $this->mGridValues;
}
function incrementInstanceNum() {
$this->mInstanceNum++;
}
function allInstancesPrinted() {
return $this->mAllInstancesPrinted;
}
function addGridValue( $field_name, $cur_value ) {
if ( !array_key_exists( $this->mInstanceNum, $this->mGridValues ) ) {
$this->mGridValues[$this->mInstanceNum] = [];
}
$this->mGridValues[$this->mInstanceNum][$field_name] = $cur_value;
}
function addField( $form_field ) {
$this->mFields[] = $form_field;
}
/**
* This makes it possible for += and -= to modify values based on existing values.
*
* @param string $field_name
* @param string $new_value
* @param string|null $modifier
*/
function changeFieldValues( $field_name, $new_value, $modifier = null ) {
$this->mValuesFromPage[$field_name] = $new_value;
if ( $modifier !== null && array_key_exists( $field_name . $modifier, $this->mValuesFromPage ) ) {
// clean up old values with + or - in them from the array
unset( $this->mValuesFromPage[$field_name . $modifier] );
}
}
function setFieldValuesFromSubmit() {
global $wgRequest;
// Reset values for every new instance, if this is a
// multiple-instance template.
if ( $this->mInstanceNum > 0 ) {
$this->mValuesFromSubmit = [];
}
$query_template_name = str_replace( ' ', '_', $this->mTemplateName );
// Also replace periods with underlines, since that's what
// POST does to strings anyway.
$query_template_name = str_replace( '.', '_', $query_template_name );
// ...and escape apostrophes.
// (Or don't.)
// $query_template_name = str_replace( "'", "\'", $query_template_name );
$allValuesFromSubmit = $wgRequest->getArray( $query_template_name );
if ( $allValuesFromSubmit === null ) {
return;
}
// If this is a multiple-instance template, get the values for
// this instance of the template.
if ( $this->mAllowMultiple ) {
// If this data came from a spreadsheet, unescape some characters.
$spreadsheetTemplates = $wgRequest->getArray( 'spreadsheet_templates' );
if ( is_array( $spreadsheetTemplates ) && array_key_exists( $query_template_name, $spreadsheetTemplates ) ) {
foreach ( $allValuesFromSubmit as &$rowValues ) {
foreach ( $rowValues as &$curValue ) {
$curValue = str_replace( [ '<', '>' ], [ '<', '>' ], $curValue );
}
}
}
$valuesFromSubmitKeys = [];
foreach ( array_keys( $allValuesFromSubmit ) as $key ) {
if ( $key != 'num' ) {
$valuesFromSubmitKeys[] = $key;
}
}
$this->mNumInstancesFromSubmit = count( $valuesFromSubmitKeys );
if ( $this->mNumInstancesFromSubmit > $this->mInstanceNum ) {
$instanceKey = $valuesFromSubmitKeys[$this->mInstanceNum];
$this->mValuesFromSubmit = $allValuesFromSubmit[$instanceKey];
}
} else {
$this->mValuesFromSubmit = $allValuesFromSubmit;
}
}
function getValuesFromSubmit() {
return $this->mValuesFromSubmit;
}
/**
* Remove all the bits that should not be parsed - those
* contained in <pre> tags, etc. - and place them in an array,
* so that they can be added back in later. This will prevent
* the brackets, curly braces and pipes within those bits from
* interfering with the parsing we need to do.
*
* @param string $str
* @param string[] &$replacements
* @return string
*/
static function removeUnparsedText( $str, &$replacements ) {
$startAndEndTags = [
[ '<pre', 'pre>' ],
[ '<syntaxhighlight', 'syntaxhighlight>' ],
[ '<source', 'source>' ],
[ '<ref', 'ref>' ],
[ '<nowiki', 'nowiki>' ]
];
foreach ( $startAndEndTags as $tags ) {
[ $startTag, $endTag ] = $tags;
$startTagLoc = -1;
while ( ( $startTagLoc + strlen( $startTag ) < strlen( $str ) ) &&
( ( $startTagLoc = strpos( $str, $startTag, $startTagLoc + strlen( $startTag ) ) ) !== false ) ) {
// Ignore "singleton" tags, like '<ref name="abc" />'.
$possibleSingletonTagEnd = strpos( $str, '/>', $startTagLoc );
if ( $possibleSingletonTagEnd !== false && $possibleSingletonTagEnd < strpos( $str, '>', $startTagLoc ) ) {
continue;
}
$endTagLoc = strpos( $str, $endTag, $startTagLoc + strlen( $startTag ) );
// Also ignore unclosed tags.
if ( $endTagLoc === false ) {
continue;
}
$fullTagTextLength = $endTagLoc + strlen( $endTag ) - $startTagLoc;
$replacements[] = substr( $str, $startTagLoc, $fullTagTextLength );
$replacementNum = count( $replacements ) - 1;
$str = substr_replace( $str, "\1" . $replacementNum . "\2", $startTagLoc, $fullTagTextLength );
}
}
return $str;
}
/**
* @param string $str
* @param string[] $replacements
* @return string
*/
static function restoreUnparsedText( $str, $replacements ) {
foreach ( $replacements as $i => $fullTagText ) {
$str = str_replace( "\1" . $i . "\2", $fullTagText, $str );
}
return $str;
}
function setFieldValuesFromPage( $existing_page_content ) {
$unparsedTextReplacements = [];
$existing_page_content = self::removeUnparsedText( $existing_page_content, $unparsedTextReplacements );
$matches = [];
$search_pattern = '/{{' . $this->mPregMatchTemplateStr . '\s*[\|}]/i';
$content_str = str_replace( '_', ' ', $existing_page_content );
preg_match( $search_pattern, $content_str, $matches, PREG_OFFSET_CAPTURE );
// is this check necessary?
if ( array_key_exists( 0, $matches ) && array_key_exists( 1, $matches[0] ) ) {
$start_char = $matches[0][1];
$fields_start_char = $start_char + 2 + strlen( $this->mSearchTemplateStr );
// Skip ahead to the first real character.
while ( in_array( $existing_page_content[$fields_start_char], [ ' ', '\n' ] ) ) {
$fields_start_char++;
}
// If the next character is a pipe, skip that too.
if ( $existing_page_content[$fields_start_char] == '|' ) {
$fields_start_char++;
}
$this->mValuesFromPage = [ '0' => '' ];
// Cycle through template call, splitting it up by pipes ('|'),
// except when that pipe is part of a piped link.
$field = "";
$uncompleted_square_brackets = 0;
$uncompleted_curly_brackets = 2;
$template_ended = false;
for ( $i = $fields_start_char; !$template_ended && ( $i < strlen( $existing_page_content ) ); $i++ ) {
$c = $existing_page_content[$i];
if ( $i + 1 < strlen( $existing_page_content ) ) {
$nextc = $existing_page_content[$i + 1];
} else {
$nextc = null;
}
if ( $i > 0 ) {
$prevc = $existing_page_content[$i - 1];
} else {
$prevc = null;
}
if ( $c == '[' && ( $nextc == '[' || $prevc == '[' ) ) {
$uncompleted_square_brackets++;
} elseif ( $c == ']' && ( $nextc == ']' || $prevc == ']' ) && $uncompleted_square_brackets > 0 ) {
$uncompleted_square_brackets--;
} elseif ( $c == '{' && ( $nextc == '{' || $prevc == '{' ) ) {
$uncompleted_curly_brackets++;
} elseif ( $c == '}' && ( $nextc == '}' || $prevc == '}' ) && $uncompleted_curly_brackets > 0 ) {
$uncompleted_curly_brackets--;
}
// handle an end to a field and/or template declaration
$template_ended = ( $uncompleted_curly_brackets == 0 && $uncompleted_square_brackets == 0 );
$field_ended = ( $c == '|' && $uncompleted_square_brackets == 0 && $uncompleted_curly_brackets <= 2 );
if ( $template_ended || $field_ended ) {
// If this was the last character in the template, remove
// the closing curly brackets.
if ( $template_ended ) {
$field = substr( $field, 0, -1 );
}
$field = self::restoreUnparsedText( $field, $unparsedTextReplacements );
// Either there's an equals sign near the beginning or not -
// handling is similar in either way; if there's no equals
// sign, the index of this field becomes the key.
$sub_fields = explode( '=', $field, 2 );
if ( count( $sub_fields ) > 1 ) {
$this->mValuesFromPage[trim( $sub_fields[0] )] = trim( $sub_fields[1] );
} else {
$this->mValuesFromPage[] = trim( $sub_fields[0] );
}
$field = '';
} else {
$field .= $c;
}
}
// If there are uncompleted opening brackets, the whole form will get messed up -
// throw an exception.
// (If there are too many *closing* brackets, some template stuff will end up in
// the "free text" field - which is bad, but it's harder for the code to detect
// the problem - though hopefully, easier for users.)
if ( $uncompleted_curly_brackets > 0 || $uncompleted_square_brackets > 0 ) {
throw new MWException( "PageFormsMismatchedBrackets" );
}
$fullText = substr( $existing_page_content, $start_char, $i - $start_char );
$this->mFullTextInPage = self::restoreUnparsedText( $fullText, $unparsedTextReplacements );
}
}
/**
* Set some vars based on the current contents of the page being
* edited - or at least vars that only need to be set if there's
* an existing page.
* @param string $existing_page_content
*/
function setPageRelatedInfo( $existing_page_content ) {
// Replace underlines with spaces in template name, to allow for
// searching on either.
$this->mSearchTemplateStr = str_replace( '_', ' ', $this->mTemplateName );
$this->mPregMatchTemplateStr = str_replace(
[ '/', '(', ')', '^' ],
[ '\/', '\(', '\)', '\^' ],
$this->mSearchTemplateStr );
$this->mPageCallsThisTemplate = preg_match( '/{{' . $this->mPregMatchTemplateStr . '\s*[\|}]/i', str_replace( '_', ' ', $existing_page_content ) );
}
function checkIfAllInstancesPrinted( $form_submitted, $source_is_page ) {
// Find additional instances of this template in the page
// (if it's an existing page) or the query string (if it's a
// new page).
// If there's at least one, re-parse this section of the
// definition form for the subsequent template instance;
// if there's none, don't include fields at all.
// @TODO - There has to be a more efficient way to handle
// multiple instances of templates, one that doesn't involve
// re-parsing the same tags, but I don't know what it is.
// (Also add additional, blank instances if there's a minimum
// number required in this form, and we haven't reached it yet.)
if ( !$this->mAllowMultiple ) {
return;
}
if ( $form_submitted && $this->mInstanceNum < $this->mNumInstancesFromSubmit ) {
return;
}
if ( !$form_submitted && $this->mInstanceNum < $this->mMinAllowed ) {
return;
}
if ( !$form_submitted && $source_is_page && $this->mPageCallsThisTemplate ) {
return;
}
if ( !$form_submitted && !$source_is_page && $this->mValuesFromSubmit != null ) {
return;
}
$this->mAllInstancesPrinted = true;
}
}