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;
40 * @var array Brace matching rules.
54 'names' => [ 2 => null ],
60 'names' => [ 2 => null ],
67 * Store a document tree in the cache.
73 protected function cacheSetTree( $text, $flags, $tree ) {
74 $config = RequestContext
::getMain()->getConfig();
76 $length = strlen( $text );
77 $threshold = $config->get( 'PreprocessorCacheThreshold' );
78 if ( $threshold === false ||
$length < $threshold ||
$length > 1e6
) {
82 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
83 $key = $cache->makeKey(
84 // @phan-suppress-next-line PhanUndeclaredConstant
85 defined( 'static::CACHE_PREFIX' ) ?
static::CACHE_PREFIX
: static::class,
89 $value = sprintf( "%08d", static::CACHE_VERSION
) . $tree;
91 $cache->set( $key, $value, 86400 );
93 LoggerFactory
::getInstance( 'Preprocessor' )
94 ->info( "Cached preprocessor output (key: $key)" );
98 * Attempt to load a precomputed document tree for some given wikitext
101 * @param string $text
103 * @return PPNode_Hash_Tree|bool
105 protected function cacheGetTree( $text, $flags ) {
106 $config = RequestContext
::getMain()->getConfig();
108 $length = strlen( $text );
109 $threshold = $config->get( 'PreprocessorCacheThreshold' );
110 if ( $threshold === false ||
$length < $threshold ||
$length > 1e6
) {
114 $cache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
116 $key = $cache->makeKey(
117 // @phan-suppress-next-line PhanUndeclaredConstant
118 defined( 'static::CACHE_PREFIX' ) ?
static::CACHE_PREFIX
: static::class,
123 $value = $cache->get( $key );
128 $version = intval( substr( $value, 0, 8 ) );
129 if ( $version !== static::CACHE_VERSION
) {
133 LoggerFactory
::getInstance( 'Preprocessor' )
134 ->info( "Loaded preprocessor output from cache (key: $key)" );
136 return substr( $value, 8 );
140 * Create a new top-level frame for expansion of a page
144 abstract public function newFrame();
147 * Create a new custom frame for programmatic use of parameter replacement
148 * as used in some extensions.
154 abstract public function newCustomFrame( $args );
157 * Create a new custom node for programmatic use of parameter replacement
158 * as used in some extensions.
160 * @param array $values
162 abstract public function newPartNodeArray( $values );
165 * Preprocess text to a PPNode
167 * @param string $text
172 abstract public function preprocessToObj( $text, $flags = 0 );