MediaWiki  master
Preprocessor.php
Go to the documentation of this file.
1 <?php
26 
30 abstract class Preprocessor {
31 
32  public 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 PhanUndeclaredConstantOfClass
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 PhanUndeclaredConstantOfClass
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 }
Preprocessor\newCustomFrame
newCustomFrame( $args)
Create a new custom frame for programmatic use of parameter replacement as used in some extensions.
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:152
Preprocessor\newPartNodeArray
newPartNodeArray( $values)
Create a new custom node for programmatic use of parameter replacement as used in some extensions.
Preprocessor\$parser
Parser $parser
Definition: Preprocessor.php:37
Preprocessor\$rules
array $rules
Brace matching rules.
Definition: Preprocessor.php:42
Preprocessor
Definition: Preprocessor.php:30
Preprocessor\newFrame
newFrame()
Create a new top-level frame for expansion of a page.
MediaWiki\Logger\LoggerFactory
PSR-3 logger instance factory.
Definition: LoggerFactory.php:45
$args
if( $line===false) $args
Definition: mcc.php:124
Parser
PHP Parser - Processes wiki markup (which uses a more user-friendly syntax, such as "[[link]]" for ma...
Definition: Parser.php:84
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:451
Preprocessor\CACHE_VERSION
const CACHE_VERSION
Definition: Preprocessor.php:32
Preprocessor\cacheSetTree
cacheSetTree( $text, $flags, $tree)
Store a document tree in the cache.
Definition: Preprocessor.php:73
$cache
$cache
Definition: mcc.php:33
Preprocessor\preprocessToObj
preprocessToObj( $text, $flags=0)
Preprocess text to a PPNode.
Preprocessor\cacheGetTree
cacheGetTree( $text, $flags)
Attempt to load a precomputed document tree for some given wikitext from the cache.
Definition: Preprocessor.php:105