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 // @phan-suppress-next-line PhanUndeclaredConstant
80 defined( 'static::CACHE_PREFIX' ) ?
static::CACHE_PREFIX
: static::class,
84 $value = sprintf( "%08d", static::CACHE_VERSION
) . $tree;
86 $cache->set( $key, $value, 86400 );
88 LoggerFactory
::getInstance( 'Preprocessor' )
89 ->info( "Cached preprocessor output (key: $key)" );
93 * Attempt to load a precomputed document tree for some given wikitext
98 * @return PPNode_Hash_Tree|bool
100 protected function cacheGetTree( $text, $flags ) {
101 $config = RequestContext
::getMain()->getConfig();
103 $length = strlen( $text );
104 $threshold = $config->get( 'PreprocessorCacheThreshold' );
105 if ( $threshold === false ||
$length < $threshold ||
$length > 1e6
) {
109 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
111 $key = $cache->makeKey(
112 // @phan-suppress-next-line PhanUndeclaredConstant
113 defined( 'static::CACHE_PREFIX' ) ?
static::CACHE_PREFIX
: static::class,
118 $value = $cache->get( $key );
123 $version = intval( substr( $value, 0, 8 ) );
124 if ( $version !== static::CACHE_VERSION
) {
128 LoggerFactory
::getInstance( 'Preprocessor' )
129 ->info( "Loaded preprocessor output from cache (key: $key)" );
131 return substr( $value, 8 );
135 * Create a new top-level frame for expansion of a page
139 abstract public function newFrame();
142 * Create a new custom frame for programmatic use of parameter replacement
143 * as used in some extensions.
149 abstract public function newCustomFrame( $args );
152 * Create a new custom node for programmatic use of parameter replacement
153 * as used in some extensions.
155 * @param array $values
157 abstract public function newPartNodeArray( $values );
160 * Preprocess text to a PPNode
162 * @param string $text
167 abstract public function preprocessToObj( $text, $flags = 0 );