summaryrefslogtreecommitdiff
path: root/includes/cbt/CBTProcessor.php
blob: 4fa1a93b882db3870f68bae100ec0899eb74a41f (plain)
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
<?php

/**
 * PHP version of the callback template processor
 * This is currently used as a test rig and is likely to be used for
 * compatibility purposes later, where the C++ extension is not available.
 */

define( 'CBT_WHITE', " \t\r\n" );
define( 'CBT_BRACE', '{}' );
define( 'CBT_DELIM', CBT_WHITE . CBT_BRACE );
define( 'CBT_DEBUG', 0 );

$GLOBALS['cbtExecutingGenerated'] = 0;

/**
 * Attempting to be a MediaWiki-independent module
 */
if ( !function_exists( 'wfProfileIn' ) ) {
	function wfProfileIn() {}
}
if ( !function_exists( 'wfProfileOut' ) ) {
	function wfProfileOut() {}
}

/**
 * Escape text for inclusion in template
 */
function cbt_escape( $text ) {
	return strtr( $text, array( '{' => '{[}', '}' => '{]}' ) );
}

/**
 * Create a CBTValue
 */
function cbt_value( $text = '', $deps = array(), $isTemplate = false ) {
	global $cbtExecutingGenerated;
	if ( $cbtExecutingGenerated ) {
		return $text;
	} else {
		return new CBTValue( $text, $deps, $isTemplate );
	}
}

/**
 * A dependency-tracking value class
 * Callback functions should return one of these, unless they have
 * no dependencies in which case they can return a string.
 */
class CBTValue {
	var $mText, $mDeps, $mIsTemplate;

	/**
	 * Create a new value
	 * @param $text String: , default ''.
	 * @param $deps Array: what this value depends on
	 * @param $isTemplate Bool: whether the result needs compilation/execution, default 'false'.
	 */
	function CBTValue( $text = '', $deps = array(), $isTemplate = false ) {
		$this->mText = $text;
		if ( !is_array( $deps ) ) {
			$this->mDeps = array( $deps ) ;
		} else {
			$this->mDeps = $deps;
		}
		$this->mIsTemplate = $isTemplate;
	}

	/** Concatenate two values, merging their dependencies */
	function cat( $val ) {
		if ( is_object( $val ) ) {
			$this->addDeps( $val );
			$this->mText .= $val->mText;
		} else {
			$this->mText .= $val;
		}
	}

	/** Add the dependencies of another value to this one */
	function addDeps( $values ) {
		if ( !is_array( $values ) ) {
			$this->mDeps = array_merge( $this->mDeps, $values->mDeps );
		} else {
			foreach ( $values as $val ) {
				if ( !is_object( $val ) ) {
					var_dump( debug_backtrace() );
					exit;
				}
				$this->mDeps = array_merge( $this->mDeps, $val->mDeps );
			}
		}
	}

	/** Remove a list of dependencies */
	function removeDeps( $deps ) {
		$this->mDeps = array_diff( $this->mDeps, $deps );
	}

	function setText( $text ) {
		$this->mText = $text;
	}

	function getText() {
		return $this->mText;
	}

	function getDeps() {
		return $this->mDeps;
	}

	/** If the value is a template, execute it */
	function execute( &$processor ) {
		if ( $this->mIsTemplate ) {
			$myProcessor = new CBTProcessor( $this->mText,  $processor->mFunctionObj, $processor->mIgnorableDeps );
			$myProcessor->mCompiling = $processor->mCompiling;
			$val = $myProcessor->doText( 0, strlen( $this->mText ) );
			if ( $myProcessor->getLastError() ) {
				$processor->error( $myProcessor->getLastError() );
				$this->mText = '';
			} else {
				$this->mText = $val->mText;
				$this->addDeps( $val );
			}
			if ( !$processor->mCompiling ) {
				$this->mIsTemplate = false;
			}
		}
	}

	/** If the value is plain text, escape it for inclusion in a template */
	function templateEscape() {
		if ( !$this->mIsTemplate ) {
			$this->mText = cbt_escape( $this->mText );
		}
	}

	/** Return true if the value has no dependencies */
	function isStatic() {
		return count( $this->mDeps ) == 0;
	}
}

/**
 * Template processor, for compilation and execution
 */
class CBTProcessor {
	var $mText,                     # The text being processed
		$mFunctionObj,              # The object containing callback functions
		$mCompiling = false,        # True if compiling to a template, false if executing to text
		$mIgnorableDeps = array(),  # Dependency names which should be treated as static
		$mFunctionCache = array(),  # A cache of function results keyed by argument hash
		$mLastError = false,        # Last error message or false for no error
		$mErrorPos = 0,             # Last error position

		/** Built-in functions */
		$mBuiltins = array(
		'if'       => 'bi_if',
		'true'     => 'bi_true',
		'['        => 'bi_lbrace',
		'lbrace'   => 'bi_lbrace',
		']'        => 'bi_rbrace',
		'rbrace'   => 'bi_rbrace',
		'escape'   => 'bi_escape',
		'~'        => 'bi_escape',
	);

	/**
	 * Create a template processor for a given text, callback object and static dependency list
	 */
	function CBTProcessor( $text, $functionObj, $ignorableDeps = array() ) {
		$this->mText = $text;
		$this->mFunctionObj = $functionObj;
		$this->mIgnorableDeps = $ignorableDeps;
	}

	/**
	 * Execute the template.
	 * If $compile is true, produces an optimised template where functions with static
	 * dependencies have been replaced by their return values.
	 */
	function execute( $compile = false ) {
		$fname = 'CBTProcessor::execute';
		wfProfileIn( $fname );
		$this->mCompiling = $compile;
		$this->mLastError = false;
		$val = $this->doText( 0, strlen( $this->mText ) );
		$text = $val->getText();
		if ( $this->mLastError !== false ) {
			$pos = $this->mErrorPos;

			// Find the line number at which the error occurred
			$startLine = 0;
			$endLine = 0;
			$line = 0;
			do {
				if ( $endLine ) {
					$startLine = $endLine + 1;
				}
				$endLine = strpos( $this->mText, "\n", $startLine );
				++$line;
			} while ( $endLine !== false && $endLine < $pos );

			$text = "Template error at line $line: $this->mLastError\n<pre>\n";

			$context = rtrim( str_replace( "\t", " ", substr( $this->mText, $startLine, $endLine - $startLine ) ) );
			$text .= htmlspecialchars( $context ) . "\n" . str_repeat( ' ', $pos - $startLine ) . "^\n</pre>\n";
		}
		wfProfileOut( $fname );
		return $text;
	}

	/** Shortcut for execute(true) */
	function compile() {
		$fname = 'CBTProcessor::compile';
		wfProfileIn( $fname );
		$s = $this->execute( true );
		wfProfileOut( $fname );
		return $s;
	}

	/** Shortcut for doOpenText( $start, $end, false */
	function doText( $start, $end ) {
		return $this->doOpenText( $start, $end, false );
	}

	/**
	 * Escape text for a template if we are producing a template. Do nothing
	 * if we are producing plain text.
	 */
	 function templateEscape( $text ) {
		if ( $this->mCompiling ) {
			return cbt_escape( $text );
		} else {
			return $text;
		}
	}

	/**
	 * Recursive workhorse for text mode.
	 *
	 * Processes text mode starting from offset $p, until either $end is
	 * reached or a closing brace is found. If $needClosing is false, a
	 * closing brace will flag an error, if $needClosing is true, the lack
	 * of a closing brace will flag an error.
	 *
	 * The parameter $p is advanced to the position after the closing brace,
	 * or after the end. A CBTValue is returned.
	 *
	 * @private
	 */
	function doOpenText( &$p, $end, $needClosing = true ) {
		$fname = 'CBTProcessor::doOpenText';
		wfProfileIn( $fname );
		$in =& $this->mText;
		$start = $p;
		$ret = new CBTValue( '', array(), $this->mCompiling );

		$foundClosing = false;
		while ( $p < $end ) {
			$matchLength = strcspn( $in, CBT_BRACE, $p, $end - $p );
			$pToken = $p + $matchLength;

			if ( $pToken >= $end ) {
				// No more braces, output remainder
				$ret->cat( substr( $in, $p ) );
				$p = $end;
				break;
			}

			// Output the text before the brace
			$ret->cat( substr( $in, $p, $matchLength ) );

			// Advance the pointer
			$p = $pToken + 1;

			// Check for closing brace
			if ( $in[$pToken] == '}' ) {
				$foundClosing = true;
				break;
			}

			// Handle the "{fn}" special case
			if ( $pToken > 0 && $in[$pToken-1] == '"' ) {
				wfProfileOut( $fname );
				$val = $this->doOpenFunction( $p, $end );
				wfProfileIn( $fname );
				if ( $p < $end && $in[$p] == '"' ) {
					$val->setText( htmlspecialchars( $val->getText() ) );
				}
				$ret->cat( $val );
			} else {
				// Process the function mode component
				wfProfileOut( $fname );
				$ret->cat( $this->doOpenFunction( $p, $end ) );
				wfProfileIn( $fname );
			}
		}
		if ( $foundClosing && !$needClosing ) {
			$this->error( 'Errant closing brace', $p );
		} elseif ( !$foundClosing && $needClosing ) {
			$this->error( 'Unclosed text section', $start );
		}
		wfProfileOut( $fname );
		return $ret;
	}

	/**
	 * Recursive workhorse for function mode.
	 *
	 * Processes function mode starting from offset $p, until either $end is
	 * reached or a closing brace is found. If $needClosing is false, a
	 * closing brace will flag an error, if $needClosing is true, the lack
	 * of a closing brace will flag an error.
	 *
	 * The parameter $p is advanced to the position after the closing brace,
	 * or after the end. A CBTValue is returned.
	 *
	 * @private
	 */
	function doOpenFunction( &$p, $end, $needClosing = true ) {
		$in =& $this->mText;
		$start = $p;
		$tokens = array();
		$unexecutedTokens = array();

		$foundClosing = false;
		while ( $p < $end ) {
			$char = $in[$p];
			if ( $char == '{' ) {
				// Switch to text mode
				++$p;
				$tokenStart = $p;
				$token = $this->doOpenText( $p, $end );
				$tokens[] = $token;
				$unexecutedTokens[] = '{' . substr( $in, $tokenStart, $p - $tokenStart - 1 ) . '}';
			} elseif ( $char == '}' ) {
				// Block end
				++$p;
				$foundClosing = true;
				break;
			} elseif ( false !== strpos( CBT_WHITE, $char ) ) {
				// Whitespace
				// Consume the rest of the whitespace
				$p += strspn( $in, CBT_WHITE, $p, $end - $p );
			} else {
				// Token, find the end of it
				$tokenLength = strcspn( $in, CBT_DELIM, $p, $end - $p );
				$token = new CBTValue( substr( $in, $p, $tokenLength ) );
				// Execute the token as a function if it's not the function name
				if ( count( $tokens ) ) {
					$tokens[] = $this->doFunction( array( $token ), $p );
				} else {
					$tokens[] = $token;
				}
				$unexecutedTokens[] = $token->getText();

				$p += $tokenLength;
			}
		}
		if ( !$foundClosing && $needClosing ) {
			$this->error( 'Unclosed function', $start );
			return '';
		}

		$val = $this->doFunction( $tokens, $start );
		if ( $this->mCompiling && !$val->isStatic() ) {
			$compiled = '';
			$first = true;
			foreach( $tokens as $i => $token ) {
				if ( $first ) {
					$first = false;
				} else {
					$compiled .= ' ';
				}
				if ( $token->isStatic() ) {
					if ( $i !== 0 ) {
						$compiled .= '{' . $token->getText() . '}';
					} else {
						$compiled .= $token->getText();
					}
				} else {
					$compiled .= $unexecutedTokens[$i];
				}
			}

			// The dynamic parts of the string are still represented as functions, and
			// function invocations have no dependencies. Thus the compiled result has
			// no dependencies.
			$val = new CBTValue( "{{$compiled}}", array(), true );
		}
		return $val;
	}

	/**
	 * Execute a function, caching and returning the result value.
	 * $tokens is an array of CBTValue objects. $tokens[0] is the function
	 * name, the others are arguments. $p is the string position, and is used
	 * for error messages only.
	 */
	function doFunction( $tokens, $p ) {
		if ( count( $tokens ) == 0 ) {
			return new CBTValue;
		}
		$fname = 'CBTProcessor::doFunction';
		wfProfileIn( $fname );

		$ret = new CBTValue;

		// All functions implicitly depend on their arguments, and the function name
		// While this is not strictly necessary for all functions, it's true almost
		// all the time and so convenient to do automatically.
		$ret->addDeps( $tokens );

		$this->mCurrentPos = $p;
		$func = array_shift( $tokens );
		$func = $func->getText();

		// Extract the text component from all the tokens
		// And convert any templates to plain text
		$textArgs = array();
		foreach ( $tokens as $token ) {
			$token->execute( $this );
			$textArgs[] = $token->getText();
		}

		// Try the local cache
		$cacheKey = $func . "\n" . implode( "\n", $textArgs );
		if ( isset( $this->mFunctionCache[$cacheKey] ) ) {
			$val = $this->mFunctionCache[$cacheKey];
		} elseif ( isset( $this->mBuiltins[$func] ) ) {
			$func = $this->mBuiltins[$func];
			$val = call_user_func_array( array( &$this, $func ), $tokens );
			$this->mFunctionCache[$cacheKey] = $val;
		} elseif ( method_exists( $this->mFunctionObj, $func ) ) {
			$profName = get_class( $this->mFunctionObj ) . '::' . $func;
			wfProfileIn( "$fname-callback" );
			wfProfileIn( $profName );
			$val = call_user_func_array( array( &$this->mFunctionObj, $func ), $textArgs );
			wfProfileOut( $profName );
			wfProfileOut( "$fname-callback" );
			$this->mFunctionCache[$cacheKey] = $val;
		} else {
			$this->error( "Call of undefined function \"$func\"", $p );
			$val = new CBTValue;
		}
		if ( !is_object( $val ) ) {
			$val = new CBTValue((string)$val);
		}

		if ( CBT_DEBUG ) {
			$unexpanded = $val;
		}

		// If the output was a template, execute it
		$val->execute( $this );

		if ( $this->mCompiling ) {
			// Escape any braces so that the output will be a valid template
			$val->templateEscape();
		}
		$val->removeDeps( $this->mIgnorableDeps );
		$ret->addDeps( $val );
		$ret->setText( $val->getText() );

		if ( CBT_DEBUG ) {
			wfDebug( "doFunction $func args = "
				. var_export( $tokens, true )
				. "unexpanded return = "
				. var_export( $unexpanded, true )
				. "expanded return = "
				. var_export( $ret, true )
			);
		}

		wfProfileOut( $fname );
		return $ret;
	}

	/**
	 * Set a flag indicating that an error has been found.
	 */
	function error( $text, $pos = false ) {
		$this->mLastError = $text;
		if ( $pos === false ) {
			$this->mErrorPos = $this->mCurrentPos;
		} else {
			$this->mErrorPos = $pos;
		}
	}

	function getLastError() {
		return $this->mLastError;
	}

	/** 'if' built-in function */
	function bi_if( $condition, $trueBlock, $falseBlock = null ) {
		if ( is_null( $condition ) ) {
			$this->error( "Missing condition in if" );
			return '';
		}

		if ( $condition->getText() != '' ) {
			return new CBTValue( $trueBlock->getText(),
				array_merge( $condition->getDeps(), $trueBlock->getDeps() ),
				$trueBlock->mIsTemplate );
		} else {
			if ( !is_null( $falseBlock ) ) {
				return new CBTValue( $falseBlock->getText(),
					array_merge( $condition->getDeps(), $falseBlock->getDeps() ),
		   			$falseBlock->mIsTemplate );
			} else {
				return new CBTValue( '', $condition->getDeps() );
			}
		}
	}

	/** 'true' built-in function */
	function bi_true() {
		return "true";
	}

	/** left brace built-in */
	function bi_lbrace() {
		return '{';
	}

	/** right brace built-in */
	function bi_rbrace() {
		return '}';
	}

	/**
	 * escape built-in.
	 * Escape text for inclusion in an HTML attribute
	 */
	function bi_escape( $val ) {
		return new CBTValue( htmlspecialchars( $val->getText() ), $val->getDeps() );
	}
}