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
;
25 use MediaWiki\MediaWikiServices
;
30 abstract class Preprocessor
{
32 const CACHE_VERSION
= 1;
35 * @var array Brace matching rules.
49 'names' => [ 2 => null ],
55 'names' => [ 2 => null ],
62 * Store a document tree in the cache.
68 protected function cacheSetTree( $text, $flags, $tree ) {
69 $config = RequestContext
::getMain()->getConfig();
71 $length = strlen( $text );
72 $threshold = $config->get( 'PreprocessorCacheThreshold' );
73 if ( $threshold === false ||
$length < $threshold ||
$length > 1e6
) {
77 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
78 $key = $cache->makeKey(
79 defined( 'static::CACHE_PREFIX' ) ?
static::CACHE_PREFIX
: static::class,
83 $value = sprintf( "%08d", static::CACHE_VERSION
) . $tree;
85 $cache->set( $key, $value, 86400 );
87 LoggerFactory
::getInstance( 'Preprocessor' )
88 ->info( "Cached preprocessor output (key: $key)" );
92 * Attempt to load a precomputed document tree for some given wikitext
97 * @return PPNode_Hash_Tree|bool
99 protected function cacheGetTree( $text, $flags ) {
100 $config = RequestContext
::getMain()->getConfig();
102 $length = strlen( $text );
103 $threshold = $config->get( 'PreprocessorCacheThreshold' );
104 if ( $threshold === false ||
$length < $threshold ||
$length > 1e6
) {
108 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
110 $key = $cache->makeKey(
111 defined( 'static::CACHE_PREFIX' ) ?
static::CACHE_PREFIX
: static::class,
116 $value = $cache->get( $key );
121 $version = intval( substr( $value, 0, 8 ) );
122 if ( $version !== static::CACHE_VERSION
) {
126 LoggerFactory
::getInstance( 'Preprocessor' )
127 ->info( "Loaded preprocessor output from cache (key: $key)" );
129 return substr( $value, 8 );
133 * Create a new top-level frame for expansion of a page
137 abstract public function newFrame();
140 * Create a new custom frame for programmatic use of parameter replacement
141 * as used in some extensions.
147 abstract public function newCustomFrame( $args );
150 * Create a new custom node for programmatic use of parameter replacement
151 * as used in some extensions.
153 * @param array $values
155 abstract public function newPartNodeArray( $values );
158 * Preprocess text to a PPNode
160 * @param string $text
165 abstract public function preprocessToObj( $text, $flags = 0 );
173 const NO_TEMPLATES
= 2;
174 const STRIP_COMMENTS
= 4;
176 const RECOVER_COMMENTS
= 16;
179 const RECOVER_ORIG
= self
::NO_ARGS | self
::NO_TEMPLATES | self
::NO_IGNORE |
180 self
::RECOVER_COMMENTS | self
::NO_TAGS
;
182 /** This constant exists when $indexOffset is supported in newChild() */
183 const SUPPORTS_INDEX_OFFSET
= 1;
186 * Create a child frame
188 * @param array|bool $args
189 * @param bool|Title $title
190 * @param int $indexOffset A number subtracted from the index attributes of the arguments
194 public function newChild( $args = false, $title = false, $indexOffset = 0 );
197 * Expand a document tree node, caching the result on its parent with the given key
198 * @param string|int $key
199 * @param string|PPNode $root
203 public function cachedExpand( $key, $root, $flags = 0 );
206 * Expand a document tree node
207 * @param string|PPNode $root
211 public function expand( $root, $flags = 0 );
214 * Implode with flags for expand()
217 * @param string|PPNode $args,...
220 public function implodeWithFlags( $sep, $flags /*, ... */ );
223 * Implode with no flags specified
225 * @param string|PPNode $args,...
228 public function implode( $sep /*, ... */ );
231 * Makes an object that, when expand()ed, will be the same as one obtained
234 * @param string|PPNode $args,...
237 public function virtualImplode( $sep /*, ... */ );
240 * Virtual implode with brackets
241 * @param string $start
244 * @param string|PPNode $args,...
247 public function virtualBracketedImplode( $start, $sep, $end /*, ... */ );
250 * Returns true if there are no arguments in this frame
254 public function isEmpty();
257 * Returns all arguments of this frame
260 public function getArguments();
263 * Returns all numbered arguments of this frame
266 public function getNumberedArguments();
269 * Returns all named arguments of this frame
272 public function getNamedArguments();
275 * Get an argument to this frame by name
276 * @param int|string $name
277 * @return string|bool
279 public function getArgument( $name );
282 * Returns true if the infinite loop check is OK, false if a loop is detected
284 * @param Title $title
287 public function loopCheck( $title );
290 * Return true if the frame is a template frame
293 public function isTemplate();
296 * Set the "volatile" flag.
298 * Note that this is somewhat of a "hack" in order to make extensions
299 * with side effects (such as Cite) work with the PHP parser. New
300 * extensions should be written in a way that they do not need this
301 * function, because other parsers (such as Parsoid) are not guaranteed
302 * to respect it, and it may be removed in the future.
306 public function setVolatile( $flag = true );
309 * Get the "volatile" flag.
311 * Callers should avoid caching the result of an expansion if it has the
314 * @see self::setVolatile()
317 public function isVolatile();
320 * Get the TTL of the frame's output.
322 * This is the maximum amount of time, in seconds, that this frame's
323 * output should be cached for. A value of null indicates that no
324 * maximum has been specified.
326 * Note that this TTL only applies to caching frames as parts of pages.
327 * It is not relevant to caching the entire rendered output of a page.
331 public function getTTL();
334 * Set the TTL of the output of this frame and all of its ancestors.
335 * Has no effect if the new TTL is greater than the one already set.
336 * Note that it is the caller's responsibility to change the cache
337 * expiry of the page as a whole, if such behavior is desired.
339 * @see self::getTTL()
342 public function setTTL( $ttl );
345 * Get a title of frame
349 public function getTitle();
353 * There are three types of nodes:
354 * * Tree nodes, which have a name and contain other nodes as children
355 * * Array nodes, which also contain other nodes but aren't considered part of a tree
356 * * Leaf nodes, which contain the actual data
358 * This interface provides access to the tree structure and to the contents of array nodes,
359 * but it does not provide access to the internal structure of leaf nodes. Access to leaf
360 * data is provided via two means:
361 * * PPFrame::expand(), which provides expanded text
362 * * The PPNode::split*() functions, which provide metadata about certain types of tree node
367 * Get an array-type node containing the children of this node.
368 * Returns false if this is not a tree node.
371 public function getChildren();
374 * Get the first child of a tree node. False if there isn't one.
378 public function getFirstChild();
381 * Get the next sibling of any node. False if there isn't one
384 public function getNextSibling();
387 * Get all children of this tree node which have a given name.
388 * Returns an array-type node, or false if this is not a tree node.
389 * @param string $type
390 * @return bool|PPNode
392 public function getChildrenOfType( $type );
395 * Returns the length of the array, or false if this is not an array-type node
397 public function getLength();
400 * Returns an item of an array-type node
402 * @return bool|PPNode
404 public function item( $i );
407 * Get the name of this node. The following names are defined here:
410 * template A double-brace node.
411 * tplarg A triple-brace node.
412 * title The first argument to a template or tplarg node.
413 * part Subsequent arguments to a template or tplarg node.
414 * #nodelist An array-type node
416 * The subclass may define various other names for tree and leaf nodes.
419 public function getName();
422 * Split a "<part>" node into an associative array containing:
428 public function splitArg();
431 * Split an "<ext>" node into an associative array containing name, attr, inner and close
432 * All values in the resulting array are PPNodes. Inner and close are optional.
435 public function splitExt();
438 * Split an "<h>" node
441 public function splitHeading();