MediaWiki  master
Preprocessor.php
Go to the documentation of this file.
1 <?php
26 
30 abstract class Preprocessor {
31 
32  const CACHE_VERSION = 1;
33 
37  public $parser;
38 
42  protected $rules = [
43  '{' => [
44  'end' => '}',
45  'names' => [
46  2 => 'template',
47  3 => 'tplarg',
48  ],
49  'min' => 2,
50  'max' => 3,
51  ],
52  '[' => [
53  'end' => ']',
54  'names' => [ 2 => null ],
55  'min' => 2,
56  'max' => 2,
57  ],
58  '-{' => [
59  'end' => '}-',
60  'names' => [ 2 => null ],
61  'min' => 2,
62  'max' => 2,
63  ],
64  ];
65 
73  protected function cacheSetTree( $text, $flags, $tree ) {
74  $config = RequestContext::getMain()->getConfig();
75 
76  $length = strlen( $text );
77  $threshold = $config->get( 'PreprocessorCacheThreshold' );
78  if ( $threshold === false || $length < $threshold || $length > 1e6 ) {
79  return;
80  }
81 
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,
86  md5( $text ),
87  $flags
88  );
89  $value = sprintf( "%08d", static::CACHE_VERSION ) . $tree;
90 
91  $cache->set( $key, $value, 86400 );
92 
93  LoggerFactory::getInstance( 'Preprocessor' )
94  ->info( "Cached preprocessor output (key: $key)" );
95  }
96 
105  protected function cacheGetTree( $text, $flags ) {
106  $config = RequestContext::getMain()->getConfig();
107 
108  $length = strlen( $text );
109  $threshold = $config->get( 'PreprocessorCacheThreshold' );
110  if ( $threshold === false || $length < $threshold || $length > 1e6 ) {
111  return false;
112  }
113 
114  $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
115 
116  $key = $cache->makeKey(
117  // @phan-suppress-next-line PhanUndeclaredConstant
118  defined( 'static::CACHE_PREFIX' ) ? static::CACHE_PREFIX : static::class,
119  md5( $text ),
120  $flags
121  );
122 
123  $value = $cache->get( $key );
124  if ( !$value ) {
125  return false;
126  }
127 
128  $version = intval( substr( $value, 0, 8 ) );
129  if ( $version !== static::CACHE_VERSION ) {
130  return false;
131  }
132 
133  LoggerFactory::getInstance( 'Preprocessor' )
134  ->info( "Loaded preprocessor output from cache (key: $key)" );
135 
136  return substr( $value, 8 );
137  }
138 
144  abstract public function newFrame();
145 
154  abstract public function newCustomFrame( $args );
155 
162  abstract public function newPartNodeArray( $values );
163 
172  abstract public function preprocessToObj( $text, $flags = 0 );
173 }
array $rules
Brace matching rules.
cacheGetTree( $text, $flags)
Attempt to load a precomputed document tree for some given wikitext from the cache.
if( $line===false) $args
Definition: cdb.php:64
Parser $parser
static getMain()
Get the RequestContext object associated with the main request.
newCustomFrame( $args)
Create a new custom frame for programmatic use of parameter replacement as used in some extensions...
$cache
Definition: mcc.php:33
const CACHE_VERSION
array cacheSetTree( $text, $flags, $tree)
Store a document tree in the cache.
newPartNodeArray( $values)
Create a new custom node for programmatic use of parameter replacement as used in some extensions...
newFrame()
Create a new top-level frame for expansion of a page.
preprocessToObj( $text, $flags=0)
Preprocess text to a PPNode.