3 * Interfaces for preprocessors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 use MediaWiki\Logger\LoggerFactory
;
29 abstract class Preprocessor
{
31 const CACHE_VERSION
= 1;
34 * @var array Brace matching rules.
48 'names' => [ 2 => null ],
54 'names' => [ 1 => null ],
61 * Store a document tree in the cache.
66 protected function cacheSetTree( $text, $flags, $tree ) {
67 $config = RequestContext
::getMain()->getConfig();
69 $length = strlen( $text );
70 $threshold = $config->get( 'PreprocessorCacheThreshold' );
71 if ( $threshold === false ||
$length < $threshold ||
$length > 1e6
) {
76 defined( 'static::CACHE_PREFIX' ) ?
static::CACHE_PREFIX
: static::class,
77 md5( $text ), $flags );
78 $value = sprintf( "%08d", static::CACHE_VERSION
) . $tree;
80 $cache = ObjectCache
::getInstance( $config->get( 'MainCacheType' ) );
81 $cache->set( $key, $value, 86400 );
83 LoggerFactory
::getInstance( 'Preprocessor' )
84 ->info( "Cached preprocessor output (key: $key)" );
88 * Attempt to load a precomputed document tree for some given wikitext
93 * @return PPNode_Hash_Tree|bool
95 protected function cacheGetTree( $text, $flags ) {
96 $config = RequestContext
::getMain()->getConfig();
98 $length = strlen( $text );
99 $threshold = $config->get( 'PreprocessorCacheThreshold' );
100 if ( $threshold === false ||
$length < $threshold ||
$length > 1e6
) {
104 $cache = ObjectCache
::getInstance( $config->get( 'MainCacheType' ) );
107 defined( 'static::CACHE_PREFIX' ) ?
static::CACHE_PREFIX
: static::class,
108 md5( $text ), $flags );
110 $value = $cache->get( $key );
115 $version = intval( substr( $value, 0, 8 ) );
116 if ( $version !== static::CACHE_VERSION
) {
120 LoggerFactory
::getInstance( 'Preprocessor' )
121 ->info( "Loaded preprocessor output from cache (key: $key)" );
123 return substr( $value, 8 );
127 * Create a new top-level frame for expansion of a page
131 abstract public function newFrame();
134 * Create a new custom frame for programmatic use of parameter replacement
135 * as used in some extensions.
141 abstract public function newCustomFrame( $args );
144 * Create a new custom node for programmatic use of parameter replacement
145 * as used in some extensions.
147 * @param array $values
149 abstract public function newPartNodeArray( $values );
152 * Preprocess text to a PPNode
154 * @param string $text
159 abstract public function preprocessToObj( $text, $flags = 0 );
167 const NO_TEMPLATES
= 2;
168 const STRIP_COMMENTS
= 4;
170 const RECOVER_COMMENTS
= 16;
173 const RECOVER_ORIG
= 59; // = 1|2|8|16|32 no constant expression support in PHP yet
175 /** This constant exists when $indexOffset is supported in newChild() */
176 const SUPPORTS_INDEX_OFFSET
= 1;
179 * Create a child frame
181 * @param array|bool $args
182 * @param bool|Title $title
183 * @param int $indexOffset A number subtracted from the index attributes of the arguments
187 public function newChild( $args = false, $title = false, $indexOffset = 0 );
190 * Expand a document tree node, caching the result on its parent with the given key
191 * @param string|int $key
192 * @param string|PPNode $root
196 public function cachedExpand( $key, $root, $flags = 0 );
199 * Expand a document tree node
200 * @param string|PPNode $root
204 public function expand( $root, $flags = 0 );
207 * Implode with flags for expand()
210 * @param string|PPNode $args,...
213 public function implodeWithFlags( $sep, $flags /*, ... */ );
216 * Implode with no flags specified
218 * @param string|PPNode $args,...
221 public function implode( $sep /*, ... */ );
224 * Makes an object that, when expand()ed, will be the same as one obtained
227 * @param string|PPNode $args,...
230 public function virtualImplode( $sep /*, ... */ );
233 * Virtual implode with brackets
234 * @param string $start
237 * @param string|PPNode $args,...
240 public function virtualBracketedImplode( $start, $sep, $end /*, ... */ );
243 * Returns true if there are no arguments in this frame
247 public function isEmpty();
250 * Returns all arguments of this frame
253 public function getArguments();
256 * Returns all numbered arguments of this frame
259 public function getNumberedArguments();
262 * Returns all named arguments of this frame
265 public function getNamedArguments();
268 * Get an argument to this frame by name
269 * @param int|string $name
270 * @return string|bool
272 public function getArgument( $name );
275 * Returns true if the infinite loop check is OK, false if a loop is detected
277 * @param Title $title
280 public function loopCheck( $title );
283 * Return true if the frame is a template frame
286 public function isTemplate();
289 * Set the "volatile" flag.
291 * Note that this is somewhat of a "hack" in order to make extensions
292 * with side effects (such as Cite) work with the PHP parser. New
293 * extensions should be written in a way that they do not need this
294 * function, because other parsers (such as Parsoid) are not guaranteed
295 * to respect it, and it may be removed in the future.
299 public function setVolatile( $flag = true );
302 * Get the "volatile" flag.
304 * Callers should avoid caching the result of an expansion if it has the
307 * @see self::setVolatile()
310 public function isVolatile();
313 * Get the TTL of the frame's output.
315 * This is the maximum amount of time, in seconds, that this frame's
316 * output should be cached for. A value of null indicates that no
317 * maximum has been specified.
319 * Note that this TTL only applies to caching frames as parts of pages.
320 * It is not relevant to caching the entire rendered output of a page.
324 public function getTTL();
327 * Set the TTL of the output of this frame and all of its ancestors.
328 * Has no effect if the new TTL is greater than the one already set.
329 * Note that it is the caller's responsibility to change the cache
330 * expiry of the page as a whole, if such behavior is desired.
332 * @see self::getTTL()
335 public function setTTL( $ttl );
338 * Get a title of frame
342 public function getTitle();
346 * There are three types of nodes:
347 * * Tree nodes, which have a name and contain other nodes as children
348 * * Array nodes, which also contain other nodes but aren't considered part of a tree
349 * * Leaf nodes, which contain the actual data
351 * This interface provides access to the tree structure and to the contents of array nodes,
352 * but it does not provide access to the internal structure of leaf nodes. Access to leaf
353 * data is provided via two means:
354 * * PPFrame::expand(), which provides expanded text
355 * * The PPNode::split*() functions, which provide metadata about certain types of tree node
360 * Get an array-type node containing the children of this node.
361 * Returns false if this is not a tree node.
364 public function getChildren();
367 * Get the first child of a tree node. False if there isn't one.
371 public function getFirstChild();
374 * Get the next sibling of any node. False if there isn't one
377 public function getNextSibling();
380 * Get all children of this tree node which have a given name.
381 * Returns an array-type node, or false if this is not a tree node.
382 * @param string $type
383 * @return bool|PPNode
385 public function getChildrenOfType( $type );
388 * Returns the length of the array, or false if this is not an array-type node
390 public function getLength();
393 * Returns an item of an array-type node
395 * @return bool|PPNode
397 public function item( $i );
400 * Get the name of this node. The following names are defined here:
403 * template A double-brace node.
404 * tplarg A triple-brace node.
405 * title The first argument to a template or tplarg node.
406 * part Subsequent arguments to a template or tplarg node.
407 * #nodelist An array-type node
409 * The subclass may define various other names for tree and leaf nodes.
412 public function getName();
415 * Split a "<part>" node into an associative array containing:
421 public function splitArg();
424 * Split an "<ext>" node into an associative array containing name, attr, inner and close
425 * All values in the resulting array are PPNodes. Inner and close are optional.
428 public function splitExt();
431 * Split an "<h>" node
434 public function splitHeading();