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 * Store a document tree in the cache.
39 protected function cacheSetTree( $text, $flags, $tree ) {
40 $config = RequestContext
::getMain()->getConfig();
42 $length = strlen( $text );
43 $threshold = $config->get( 'PreprocessorCacheThreshold' );
44 if ( $threshold === false ||
$length < $threshold ||
$length > 1e6
) {
49 // TODO: Once we require PHP 5.5, use static::class instead of
50 // get_called_class() or get_class( $this ).
51 defined( 'static::CACHE_PREFIX' ) ?
static::CACHE_PREFIX
: get_called_class(),
52 md5( $text ), $flags );
53 $value = sprintf( "%08d", static::CACHE_VERSION
) . $tree;
55 $cache = ObjectCache
::getInstance( $config->get( 'MainCacheType' ) );
56 $cache->set( $key, $value, 86400 );
58 LoggerFactory
::getInstance( 'Preprocessor' )
59 ->info( "Cached preprocessor output (key: $key)" );
63 * Attempt to load a precomputed document tree for some given wikitext
68 * @return PPNode_Hash_Tree|bool
70 protected function cacheGetTree( $text, $flags ) {
71 $config = RequestContext
::getMain()->getConfig();
73 $length = strlen( $text );
74 $threshold = $config->get( 'PreprocessorCacheThreshold' );
75 if ( $threshold === false ||
$length < $threshold ||
$length > 1e6
) {
79 $cache = ObjectCache
::getInstance( $config->get( 'MainCacheType' ) );
82 // TODO: Once we require PHP 5.5, use static::class instead of
83 // get_called_class() or get_class( $this ).
84 defined( 'static::CACHE_PREFIX' ) ?
static::CACHE_PREFIX
: get_called_class(),
85 md5( $text ), $flags );
87 $value = $cache->get( $key );
92 $version = intval( substr( $value, 0, 8 ) );
93 if ( $version !== static::CACHE_VERSION
) {
97 LoggerFactory
::getInstance( 'Preprocessor' )
98 ->info( "Loaded preprocessor output from cache (key: $key)" );
100 return substr( $value, 8 );
104 * Create a new top-level frame for expansion of a page
108 abstract public function newFrame();
111 * Create a new custom frame for programmatic use of parameter replacement
112 * as used in some extensions.
118 abstract public function newCustomFrame( $args );
121 * Create a new custom node for programmatic use of parameter replacement
122 * as used in some extensions.
124 * @param array $values
126 abstract public function newPartNodeArray( $values );
129 * Preprocess text to a PPNode
131 * @param string $text
136 abstract public function preprocessToObj( $text, $flags = 0 );
144 const NO_TEMPLATES
= 2;
145 const STRIP_COMMENTS
= 4;
147 const RECOVER_COMMENTS
= 16;
150 const RECOVER_ORIG
= 59; // = 1|2|8|16|32 no constant expression support in PHP yet
152 /** This constant exists when $indexOffset is supported in newChild() */
153 const SUPPORTS_INDEX_OFFSET
= 1;
156 * Create a child frame
158 * @param array|bool $args
159 * @param bool|Title $title
160 * @param int $indexOffset A number subtracted from the index attributes of the arguments
164 public function newChild( $args = false, $title = false, $indexOffset = 0 );
167 * Expand a document tree node, caching the result on its parent with the given key
168 * @param string|int $key
169 * @param string|PPNode $root
173 public function cachedExpand( $key, $root, $flags = 0 );
176 * Expand a document tree node
177 * @param string|PPNode $root
181 public function expand( $root, $flags = 0 );
184 * Implode with flags for expand()
187 * @param string|PPNode $args,...
190 public function implodeWithFlags( $sep, $flags /*, ... */ );
193 * Implode with no flags specified
195 * @param string|PPNode $args,...
198 public function implode( $sep /*, ... */ );
201 * Makes an object that, when expand()ed, will be the same as one obtained
204 * @param string|PPNode $args,...
207 public function virtualImplode( $sep /*, ... */ );
210 * Virtual implode with brackets
211 * @param string $start
214 * @param string|PPNode $args,...
217 public function virtualBracketedImplode( $start, $sep, $end /*, ... */ );
220 * Returns true if there are no arguments in this frame
224 public function isEmpty();
227 * Returns all arguments of this frame
230 public function getArguments();
233 * Returns all numbered arguments of this frame
236 public function getNumberedArguments();
239 * Returns all named arguments of this frame
242 public function getNamedArguments();
245 * Get an argument to this frame by name
246 * @param string $name
249 public function getArgument( $name );
252 * Returns true if the infinite loop check is OK, false if a loop is detected
254 * @param Title $title
257 public function loopCheck( $title );
260 * Return true if the frame is a template frame
263 public function isTemplate();
266 * Set the "volatile" flag.
268 * Note that this is somewhat of a "hack" in order to make extensions
269 * with side effects (such as Cite) work with the PHP parser. New
270 * extensions should be written in a way that they do not need this
271 * function, because other parsers (such as Parsoid) are not guaranteed
272 * to respect it, and it may be removed in the future.
276 public function setVolatile( $flag = true );
279 * Get the "volatile" flag.
281 * Callers should avoid caching the result of an expansion if it has the
284 * @see self::setVolatile()
287 public function isVolatile();
290 * Get the TTL of the frame's output.
292 * This is the maximum amount of time, in seconds, that this frame's
293 * output should be cached for. A value of null indicates that no
294 * maximum has been specified.
296 * Note that this TTL only applies to caching frames as parts of pages.
297 * It is not relevant to caching the entire rendered output of a page.
301 public function getTTL();
304 * Set the TTL of the output of this frame and all of its ancestors.
305 * Has no effect if the new TTL is greater than the one already set.
306 * Note that it is the caller's responsibility to change the cache
307 * expiry of the page as a whole, if such behavior is desired.
309 * @see self::getTTL()
312 public function setTTL( $ttl );
315 * Get a title of frame
319 public function getTitle();
323 * There are three types of nodes:
324 * * Tree nodes, which have a name and contain other nodes as children
325 * * Array nodes, which also contain other nodes but aren't considered part of a tree
326 * * Leaf nodes, which contain the actual data
328 * This interface provides access to the tree structure and to the contents of array nodes,
329 * but it does not provide access to the internal structure of leaf nodes. Access to leaf
330 * data is provided via two means:
331 * * PPFrame::expand(), which provides expanded text
332 * * The PPNode::split*() functions, which provide metadata about certain types of tree node
337 * Get an array-type node containing the children of this node.
338 * Returns false if this is not a tree node.
341 public function getChildren();
344 * Get the first child of a tree node. False if there isn't one.
348 public function getFirstChild();
351 * Get the next sibling of any node. False if there isn't one
354 public function getNextSibling();
357 * Get all children of this tree node which have a given name.
358 * Returns an array-type node, or false if this is not a tree node.
359 * @param string $type
360 * @return bool|PPNode
362 public function getChildrenOfType( $type );
365 * Returns the length of the array, or false if this is not an array-type node
367 public function getLength();
370 * Returns an item of an array-type node
372 * @return bool|PPNode
374 public function item( $i );
377 * Get the name of this node. The following names are defined here:
380 * template A double-brace node.
381 * tplarg A triple-brace node.
382 * title The first argument to a template or tplarg node.
383 * part Subsequent arguments to a template or tplarg node.
384 * #nodelist An array-type node
386 * The subclass may define various other names for tree and leaf nodes.
389 public function getName();
392 * Split a "<part>" node into an associative array containing:
398 public function splitArg();
401 * Split an "<ext>" node into an associative array containing name, attr, inner and close
402 * All values in the resulting array are PPNodes. Inner and close are optional.
405 public function splitExt();
408 * Split an "<h>" node
411 public function splitHeading();