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 );