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 defined( 'self::CACHE_PREFIX' ) ? self
::CACHE_PREFIX
: __CLASS__
,
50 md5( $text ), $flags );
51 $value = sprintf( "%08d", self
::CACHE_VERSION
) . $tree;
53 $cache = ObjectCache
::getInstance( $config->get( 'MainCacheType' ) );
54 $cache->set( $key, $value, 86400 );
56 LoggerFactory
::getInstance( 'Preprocessor' )
57 ->info( "Cached preprocessor output (key: $key)" );
61 * Attempt to load a precomputed document tree for some given wikitext
66 * @return PPNode_Hash_Tree|bool
68 protected function cacheGetTree( $text, $flags ) {
69 $config = RequestContext
::getMain()->getConfig();
71 $length = strlen( $text );
72 $threshold = $config->get( 'PreprocessorCacheThreshold' );
73 if ( $threshold === false ||
$length < $threshold ||
$length > 1e6
) {
77 $cache = ObjectCache
::getInstance( $config->get( 'MainCacheType' ) );
80 defined( 'self::CACHE_PREFIX' ) ? self
::CACHE_PREFIX
: __CLASS__
,
81 md5( $text ), $flags );
83 $value = $cache->get( $key );
88 $version = intval( substr( $value, 0, 8 ) );
89 if ( $version !== self
::CACHE_VERSION
) {
93 LoggerFactory
::getInstance( 'Preprocessor' )
94 ->info( "Loaded preprocessor output from cache (key: $key)" );
96 return substr( $value, 8 );
100 * Create a new top-level frame for expansion of a page
104 abstract public function newFrame();
107 * Create a new custom frame for programmatic use of parameter replacement
108 * as used in some extensions.
114 abstract public function newCustomFrame( $args );
117 * Create a new custom node for programmatic use of parameter replacement
118 * as used in some extensions.
120 * @param array $values
122 abstract public function newPartNodeArray( $values );
125 * Preprocess text to a PPNode
127 * @param string $text
132 abstract public function preprocessToObj( $text, $flags = 0 );
140 const NO_TEMPLATES
= 2;
141 const STRIP_COMMENTS
= 4;
143 const RECOVER_COMMENTS
= 16;
146 const RECOVER_ORIG
= 59; // = 1|2|8|16|32 no constant expression support in PHP yet
148 /** This constant exists when $indexOffset is supported in newChild() */
149 const SUPPORTS_INDEX_OFFSET
= 1;
152 * Create a child frame
154 * @param array|bool $args
155 * @param bool|Title $title
156 * @param int $indexOffset A number subtracted from the index attributes of the arguments
160 public function newChild( $args = false, $title = false, $indexOffset = 0 );
163 * Expand a document tree node, caching the result on its parent with the given key
164 * @param string|int $key
165 * @param string|PPNode $root
169 public function cachedExpand( $key, $root, $flags = 0 );
172 * Expand a document tree node
173 * @param string|PPNode $root
177 public function expand( $root, $flags = 0 );
180 * Implode with flags for expand()
183 * @param string|PPNode $args,...
186 public function implodeWithFlags( $sep, $flags /*, ... */ );
189 * Implode with no flags specified
191 * @param string|PPNode $args,...
194 public function implode( $sep /*, ... */ );
197 * Makes an object that, when expand()ed, will be the same as one obtained
200 * @param string|PPNode $args,...
203 public function virtualImplode( $sep /*, ... */ );
206 * Virtual implode with brackets
207 * @param string $start
210 * @param string|PPNode $args,...
213 public function virtualBracketedImplode( $start, $sep, $end /*, ... */ );
216 * Returns true if there are no arguments in this frame
220 public function isEmpty();
223 * Returns all arguments of this frame
226 public function getArguments();
229 * Returns all numbered arguments of this frame
232 public function getNumberedArguments();
235 * Returns all named arguments of this frame
238 public function getNamedArguments();
241 * Get an argument to this frame by name
242 * @param string $name
245 public function getArgument( $name );
248 * Returns true if the infinite loop check is OK, false if a loop is detected
250 * @param Title $title
253 public function loopCheck( $title );
256 * Return true if the frame is a template frame
259 public function isTemplate();
262 * Set the "volatile" flag.
264 * Note that this is somewhat of a "hack" in order to make extensions
265 * with side effects (such as Cite) work with the PHP parser. New
266 * extensions should be written in a way that they do not need this
267 * function, because other parsers (such as Parsoid) are not guaranteed
268 * to respect it, and it may be removed in the future.
272 public function setVolatile( $flag = true );
275 * Get the "volatile" flag.
277 * Callers should avoid caching the result of an expansion if it has the
280 * @see self::setVolatile()
283 public function isVolatile();
286 * Get the TTL of the frame's output.
288 * This is the maximum amount of time, in seconds, that this frame's
289 * output should be cached for. A value of null indicates that no
290 * maximum has been specified.
292 * Note that this TTL only applies to caching frames as parts of pages.
293 * It is not relevant to caching the entire rendered output of a page.
297 public function getTTL();
300 * Set the TTL of the output of this frame and all of its ancestors.
301 * Has no effect if the new TTL is greater than the one already set.
302 * Note that it is the caller's responsibility to change the cache
303 * expiry of the page as a whole, if such behavior is desired.
305 * @see self::getTTL()
308 public function setTTL( $ttl );
311 * Get a title of frame
315 public function getTitle();
319 * There are three types of nodes:
320 * * Tree nodes, which have a name and contain other nodes as children
321 * * Array nodes, which also contain other nodes but aren't considered part of a tree
322 * * Leaf nodes, which contain the actual data
324 * This interface provides access to the tree structure and to the contents of array nodes,
325 * but it does not provide access to the internal structure of leaf nodes. Access to leaf
326 * data is provided via two means:
327 * * PPFrame::expand(), which provides expanded text
328 * * The PPNode::split*() functions, which provide metadata about certain types of tree node
333 * Get an array-type node containing the children of this node.
334 * Returns false if this is not a tree node.
337 public function getChildren();
340 * Get the first child of a tree node. False if there isn't one.
344 public function getFirstChild();
347 * Get the next sibling of any node. False if there isn't one
350 public function getNextSibling();
353 * Get all children of this tree node which have a given name.
354 * Returns an array-type node, or false if this is not a tree node.
355 * @param string $type
356 * @return bool|PPNode
358 public function getChildrenOfType( $type );
361 * Returns the length of the array, or false if this is not an array-type node
363 public function getLength();
366 * Returns an item of an array-type node
368 * @return bool|PPNode
370 public function item( $i );
373 * Get the name of this node. The following names are defined here:
376 * template A double-brace node.
377 * tplarg A triple-brace node.
378 * title The first argument to a template or tplarg node.
379 * part Subsequent arguments to a template or tplarg node.
380 * #nodelist An array-type node
382 * The subclass may define various other names for tree and leaf nodes.
385 public function getName();
388 * Split a "<part>" node into an associative array containing:
394 public function splitArg();
397 * Split an "<ext>" node into an associative array containing name, attr, inner and close
398 * All values in the resulting array are PPNodes. Inner and close are optional.
401 public function splitExt();
404 * Split an "<h>" node
407 public function splitHeading();