39e9ec7fa0b951debc55d255bdab06e16b62ad60
[lhc/web/wiklou.git] / includes / parser / ParserOptions.php
1 <?php
2 /**
3 * Options for the PHP parser
4 *
5 * @file
6 * @ingroup Parser
7 */
8
9 /**
10 * Set options of the Parser
11 * @todo document
12 * @ingroup Parser
13 */
14 class ParserOptions {
15 # All variables are supposed to be private in theory, although in practise this is not the case.
16 var $mUseDynamicDates; # Use DateFormatter to format dates
17 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
18 var $mAllowExternalImages; # Allow external images inline
19 var $mAllowExternalImagesFrom; # If not, any exception?
20 var $mEnableImageWhitelist; # If not or it doesn't match, should we check an on-wiki whitelist?
21 var $mSkin = null; # Reference to the preferred skin
22 var $mDateFormat = null; # Date format index
23 var $mEditSection = true; # Create "edit section" links
24 var $mAllowSpecialInclusion; # Allow inclusion of special pages
25 var $mTidy = false; # Ask for tidy cleanup
26 var $mInterfaceMessage = false; # Which lang to call for PLURAL and GRAMMAR
27 var $mTargetLanguage = null; # Overrides above setting with arbitrary language
28 var $mMaxIncludeSize; # Maximum size of template expansions, in bytes
29 var $mMaxPPNodeCount; # Maximum number of nodes touched by PPFrame::expand()
30 var $mMaxPPExpandDepth; # Maximum recursion depth in PPFrame::expand()
31 var $mMaxTemplateDepth; # Maximum recursion depth for templates within templates
32 var $mRemoveComments = true; # Remove HTML comments. ONLY APPLIES TO PREPROCESS OPERATIONS
33 var $mTemplateCallback = # Callback for template fetching
34 array( 'Parser', 'statelessFetchTemplate' );
35 var $mEnableLimitReport = false; # Enable limit report in an HTML comment on output
36 var $mTimestamp; # Timestamp used for {{CURRENTDAY}} etc.
37 var $mExternalLinkTarget; # Target attribute for external links
38 var $mCleanSignatures; #
39 var $mPreSaveTransform = true; # Transform wiki markup when saving the page.
40
41 var $mNumberHeadings; # Automatically number headings
42 var $mMath; # User math preference (as integer)
43 var $mThumbSize; # Thumb size preferred by the user.
44 private $mStubThreshold; # Maximum article size of an article to be marked as "stub"
45 var $mUserLang; # Language code of the User language.
46
47 /**
48 * @var User
49 */
50 var $mUser; # Stored user object
51 var $mIsPreview = false; # Parsing the page for a "preview" operation
52 var $mIsSectionPreview = false; # Parsing the page for a "preview" operation on a single section
53 var $mIsPrintable = false; # Parsing the printable version of the page
54
55 var $mExtraKey = ''; # Extra key that should be present in the caching key.
56
57 protected $onAccessCallback = null;
58
59 function getUseDynamicDates() { return $this->mUseDynamicDates; }
60 function getInterwikiMagic() { return $this->mInterwikiMagic; }
61 function getAllowExternalImages() { return $this->mAllowExternalImages; }
62 function getAllowExternalImagesFrom() { return $this->mAllowExternalImagesFrom; }
63 function getEnableImageWhitelist() { return $this->mEnableImageWhitelist; }
64 function getEditSection() { return $this->mEditSection; }
65 function getNumberHeadings() { $this->optionUsed('numberheadings');
66 return $this->mNumberHeadings; }
67 function getAllowSpecialInclusion() { return $this->mAllowSpecialInclusion; }
68 function getTidy() { return $this->mTidy; }
69 function getInterfaceMessage() { return $this->mInterfaceMessage; }
70 function getTargetLanguage() { return $this->mTargetLanguage; }
71 function getMaxIncludeSize() { return $this->mMaxIncludeSize; }
72 function getMaxPPNodeCount() { return $this->mMaxPPNodeCount; }
73 function getMaxPPExpandDepth() { return $this->mMaxPPExpandDepth; }
74 function getMaxTemplateDepth() { return $this->mMaxTemplateDepth; }
75 function getRemoveComments() { return $this->mRemoveComments; }
76 function getTemplateCallback() { return $this->mTemplateCallback; }
77 function getEnableLimitReport() { return $this->mEnableLimitReport; }
78 function getCleanSignatures() { return $this->mCleanSignatures; }
79 function getExternalLinkTarget() { return $this->mExternalLinkTarget; }
80 function getMath() { $this->optionUsed('math');
81 return $this->mMath; }
82 function getThumbSize() { $this->optionUsed('thumbsize');
83 return $this->mThumbSize; }
84 function getStubThreshold() { $this->optionUsed('stubthreshold');
85 return $this->mStubThreshold; }
86
87 function getIsPreview() { return $this->mIsPreview; }
88 function getIsSectionPreview() { return $this->mIsSectionPreview; }
89 function getIsPrintable() { $this->optionUsed('printable');
90 return $this->mIsPrintable; }
91 function getUser() { return $this->mUser; }
92 function getPreSaveTransform() { return $this->mPreSaveTransform; }
93
94 /**
95 * @param $title Title
96 * @return Skin
97 */
98 function getSkin( $title = null ) {
99 if ( !isset( $this->mSkin ) ) {
100 $this->mSkin = $this->mUser->getSkin( $title );
101 }
102 return $this->mSkin;
103 }
104
105 function getDateFormat() {
106 $this->optionUsed('dateformat');
107 if ( !isset( $this->mDateFormat ) ) {
108 $this->mDateFormat = $this->mUser->getDatePreference();
109 }
110 return $this->mDateFormat;
111 }
112
113 function getTimestamp() {
114 if ( !isset( $this->mTimestamp ) ) {
115 $this->mTimestamp = wfTimestampNow();
116 }
117 return $this->mTimestamp;
118 }
119
120 /**
121 * You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
122 * Using this fragments the cache and is discouraged. Yes, {{int: }} uses this,
123 * producing inconsistent tables (Bug 14404).
124 */
125 function getUserLang() {
126 $this->optionUsed('userlang');
127 return $this->mUserLang;
128 }
129
130 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
131 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
132 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
133 function setAllowExternalImagesFrom( $x ) { return wfSetVar( $this->mAllowExternalImagesFrom, $x ); }
134 function setEnableImageWhitelist( $x ) { return wfSetVar( $this->mEnableImageWhitelist, $x ); }
135 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
136 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
137 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
138 function setAllowSpecialInclusion( $x ) { return wfSetVar( $this->mAllowSpecialInclusion, $x ); }
139 function setTidy( $x ) { return wfSetVar( $this->mTidy, $x); }
140 function setSkin( $x ) { $this->mSkin = $x; }
141 function setInterfaceMessage( $x ) { return wfSetVar( $this->mInterfaceMessage, $x); }
142 function setTargetLanguage( $x ) { return wfSetVar( $this->mTargetLanguage, $x); }
143 function setMaxIncludeSize( $x ) { return wfSetVar( $this->mMaxIncludeSize, $x ); }
144 function setMaxPPNodeCount( $x ) { return wfSetVar( $this->mMaxPPNodeCount, $x ); }
145 function setMaxTemplateDepth( $x ) { return wfSetVar( $this->mMaxTemplateDepth, $x ); }
146 function setRemoveComments( $x ) { return wfSetVar( $this->mRemoveComments, $x ); }
147 function setTemplateCallback( $x ) { return wfSetVar( $this->mTemplateCallback, $x ); }
148 function enableLimitReport( $x = true ) { return wfSetVar( $this->mEnableLimitReport, $x ); }
149 function setTimestamp( $x ) { return wfSetVar( $this->mTimestamp, $x ); }
150 function setCleanSignatures( $x ) { return wfSetVar( $this->mCleanSignatures, $x ); }
151 function setExternalLinkTarget( $x ) { return wfSetVar( $this->mExternalLinkTarget, $x ); }
152 function setMath( $x ) { return wfSetVar( $this->mMath, $x ); }
153 function setUserLang( $x ) { return wfSetVar( $this->mUserLang, $x ); }
154 function setThumbSize( $x ) { return wfSetVar( $this->mThumbSize, $x ); }
155 function setStubThreshold( $x ) { return wfSetVar( $this->mStubThreshold, $x ); }
156 function setPreSaveTransform( $x ) { return wfSetVar( $this->mPreSaveTransform, $x ); }
157
158 function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview, $x ); }
159 function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview, $x ); }
160 function setIsPrintable( $x ) { return wfSetVar( $this->mIsPrintable, $x ); }
161
162 /**
163 * Extra key that should be present in the parser cache key.
164 */
165 function addExtraKey( $key ) {
166 $this->mExtraKey .= '!' . $key;
167 }
168
169 function __construct( $user = null ) {
170 $this->initialiseFromUser( $user );
171 }
172
173 /**
174 * Get parser options
175 *
176 * @param $user User object
177 * @return ParserOptions object
178 */
179 static function newFromUser( $user ) {
180 return new ParserOptions( $user );
181 }
182
183 /** Get user options */
184 function initialiseFromUser( $userInput ) {
185 global $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
186 global $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion, $wgMaxArticleSize;
187 global $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth, $wgCleanSignatures;
188 global $wgExternalLinkTarget, $wgLang;
189
190 wfProfileIn( __METHOD__ );
191
192 if ( !$userInput ) {
193 global $wgUser;
194 if ( isset( $wgUser ) ) {
195 $user = $wgUser;
196 } else {
197 $user = new User;
198 }
199 } else {
200 $user =& $userInput;
201 }
202
203 $this->mUser = $user;
204
205 $this->mUseDynamicDates = $wgUseDynamicDates;
206 $this->mInterwikiMagic = $wgInterwikiMagic;
207 $this->mAllowExternalImages = $wgAllowExternalImages;
208 $this->mAllowExternalImagesFrom = $wgAllowExternalImagesFrom;
209 $this->mEnableImageWhitelist = $wgEnableImageWhitelist;
210 $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion;
211 $this->mMaxIncludeSize = $wgMaxArticleSize * 1024;
212 $this->mMaxPPNodeCount = $wgMaxPPNodeCount;
213 $this->mMaxPPExpandDepth = $wgMaxPPExpandDepth;
214 $this->mMaxTemplateDepth = $wgMaxTemplateDepth;
215 $this->mCleanSignatures = $wgCleanSignatures;
216 $this->mExternalLinkTarget = $wgExternalLinkTarget;
217
218 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
219 $this->mMath = $user->getOption( 'math' );
220 $this->mThumbSize = $user->getOption( 'thumbsize' );
221 $this->mStubThreshold = $user->getStubThreshold();
222 $this->mUserLang = $wgLang->getCode();
223
224 wfProfileOut( __METHOD__ );
225 }
226
227 /**
228 * Registers a callback for tracking which ParserOptions which are used.
229 * This is a private API with the parser.
230 */
231 function registerWatcher( $callback ) {
232 $this->onAccessCallback = $callback;
233 }
234
235 /**
236 * Called when an option is accessed.
237 */
238 protected function optionUsed( $optionName ) {
239 if ( $this->onAccessCallback ) {
240 call_user_func( $this->onAccessCallback, $optionName );
241 }
242 }
243
244 /**
245 * Returns the full array of options that would have been used by
246 * in 1.16.
247 * Used to get the old parser cache entries when available.
248 */
249 public static function legacyOptions() {
250 global $wgUseDynamicDates;
251 $legacyOpts = array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
252 if ( $wgUseDynamicDates ) {
253 $legacyOpts[] = 'dateformat';
254 }
255 return $legacyOpts;
256 }
257
258 /**
259 * Generate a hash string with the values set on these ParserOptions
260 * for the keys given in the array.
261 * This will be used as part of the hash key for the parser cache,
262 * so users sharign the options with vary for the same page share
263 * the same cached data safely.
264 *
265 * Replaces User::getPageRenderingHash()
266 *
267 * Extensions which require it should install 'PageRenderingHash' hook,
268 * which will give them a chance to modify this key based on their own
269 * settings.
270 *
271 * @since 1.17
272 * @return \string Page rendering hash
273 */
274 public function optionsHash( $forOptions ) {
275 global $wgContLang, $wgRenderHashAppend;
276
277 $confstr = '';
278
279 if ( in_array( 'math', $forOptions ) )
280 $confstr .= $this->mMath;
281 else
282 $confstr .= '*';
283
284
285 // Space assigned for the stubthreshold but unused
286 // since it disables the parser cache, its value will always
287 // be 0 when this function is called by parsercache.
288 if ( in_array( 'stubthreshold', $forOptions ) )
289 $confstr .= '!' . $this->mStubThreshold;
290 else
291 $confstr .= '!*' ;
292
293 if ( in_array( 'dateformat', $forOptions ) )
294 $confstr .= '!' . $this->getDateFormat();
295
296 if ( in_array( 'numberheadings', $forOptions ) )
297 $confstr .= '!' . ( $this->mNumberHeadings ? '1' : '' );
298 else
299 $confstr .= '!*';
300
301 if ( in_array( 'userlang', $forOptions ) )
302 $confstr .= '!' . $this->mUserLang;
303 else
304 $confstr .= '!*';
305
306 if ( in_array( 'thumbsize', $forOptions ) )
307 $confstr .= '!' . $this->mThumbSize;
308 else
309 $confstr .= '!*';
310
311 // add in language specific options, if any
312 // FIXME: This is just a way of retrieving the url/user preferred variant
313 $confstr .= $wgContLang->getExtraHashOptions();
314
315 // Since the skin could be overloading link(), it should be
316 // included here but in practice, none of our skins do that.
317 // $confstr .= "!" . $this->mSkin->getSkinName();
318
319 $confstr .= $wgRenderHashAppend;
320
321 if ( !in_array( 'editsection', $forOptions ) ) {
322 $confstr .= '!*';
323 } elseif ( !$this->mEditSection ) {
324 $confstr .= '!edit=0';
325 }
326
327 if ( $this->mIsPrintable && in_array( 'printable', $forOptions ) )
328 $confstr .= '!printable=1';
329
330 if ( $this->mExtraKey != '' )
331 $confstr .= $this->mExtraKey;
332
333 // Give a chance for extensions to modify the hash, if they have
334 // extra options or other effects on the parser cache.
335 wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
336
337 // Make it a valid memcached key fragment
338 $confstr = str_replace( ' ', '_', $confstr );
339
340 return $confstr;
341 }
342 }