Split parser related files to have one class in one file
[lhc/web/wiklou.git] / includes / parser / Preprocessor.php
1 <?php
2 /**
3 * Interfaces for preprocessors
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup Parser
22 */
23
24 use MediaWiki\Logger\LoggerFactory;
25 use MediaWiki\MediaWikiServices;
26
27 /**
28 * @ingroup Parser
29 */
30 abstract class Preprocessor {
31
32 const CACHE_VERSION = 1;
33
34 /**
35 * @var array Brace matching rules.
36 */
37 protected $rules = [
38 '{' => [
39 'end' => '}',
40 'names' => [
41 2 => 'template',
42 3 => 'tplarg',
43 ],
44 'min' => 2,
45 'max' => 3,
46 ],
47 '[' => [
48 'end' => ']',
49 'names' => [ 2 => null ],
50 'min' => 2,
51 'max' => 2,
52 ],
53 '-{' => [
54 'end' => '}-',
55 'names' => [ 2 => null ],
56 'min' => 2,
57 'max' => 2,
58 ],
59 ];
60
61 /**
62 * Store a document tree in the cache.
63 *
64 * @param string $text
65 * @param int $flags
66 * @param string $tree
67 */
68 protected function cacheSetTree( $text, $flags, $tree ) {
69 $config = RequestContext::getMain()->getConfig();
70
71 $length = strlen( $text );
72 $threshold = $config->get( 'PreprocessorCacheThreshold' );
73 if ( $threshold === false || $length < $threshold || $length > 1e6 ) {
74 return;
75 }
76
77 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
78 $key = $cache->makeKey(
79 defined( 'static::CACHE_PREFIX' ) ? static::CACHE_PREFIX : static::class,
80 md5( $text ),
81 $flags
82 );
83 $value = sprintf( "%08d", static::CACHE_VERSION ) . $tree;
84
85 $cache->set( $key, $value, 86400 );
86
87 LoggerFactory::getInstance( 'Preprocessor' )
88 ->info( "Cached preprocessor output (key: $key)" );
89 }
90
91 /**
92 * Attempt to load a precomputed document tree for some given wikitext
93 * from the cache.
94 *
95 * @param string $text
96 * @param int $flags
97 * @return PPNode_Hash_Tree|bool
98 */
99 protected function cacheGetTree( $text, $flags ) {
100 $config = RequestContext::getMain()->getConfig();
101
102 $length = strlen( $text );
103 $threshold = $config->get( 'PreprocessorCacheThreshold' );
104 if ( $threshold === false || $length < $threshold || $length > 1e6 ) {
105 return false;
106 }
107
108 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
109
110 $key = $cache->makeKey(
111 defined( 'static::CACHE_PREFIX' ) ? static::CACHE_PREFIX : static::class,
112 md5( $text ),
113 $flags
114 );
115
116 $value = $cache->get( $key );
117 if ( !$value ) {
118 return false;
119 }
120
121 $version = intval( substr( $value, 0, 8 ) );
122 if ( $version !== static::CACHE_VERSION ) {
123 return false;
124 }
125
126 LoggerFactory::getInstance( 'Preprocessor' )
127 ->info( "Loaded preprocessor output from cache (key: $key)" );
128
129 return substr( $value, 8 );
130 }
131
132 /**
133 * Create a new top-level frame for expansion of a page
134 *
135 * @return PPFrame
136 */
137 abstract public function newFrame();
138
139 /**
140 * Create a new custom frame for programmatic use of parameter replacement
141 * as used in some extensions.
142 *
143 * @param array $args
144 *
145 * @return PPFrame
146 */
147 abstract public function newCustomFrame( $args );
148
149 /**
150 * Create a new custom node for programmatic use of parameter replacement
151 * as used in some extensions.
152 *
153 * @param array $values
154 */
155 abstract public function newPartNodeArray( $values );
156
157 /**
158 * Preprocess text to a PPNode
159 *
160 * @param string $text
161 * @param int $flags
162 *
163 * @return PPNode
164 */
165 abstract public function preprocessToObj( $text, $flags = 0 );
166 }