revert r111028 (attempt to fix bug 34254)
[lhc/web/wiklou.git] / includes / parser / ParserOptions.php
1 <?php
2 /**
3 * \brief Options for the PHP parser
4 *
5 * @file
6 * @ingroup Parser
7 */
8
9 /**
10 * \brief Set options of the Parser
11 *
12 * All member variables are supposed to be private in theory, although in practise this is not the case.
13 *
14 * @ingroup Parser
15 */
16 class ParserOptions {
17
18 /**
19 * Use DateFormatter to format dates
20 */
21 var $mUseDynamicDates;
22
23 /**
24 * Interlanguage links are removed and returned in an array
25 */
26 var $mInterwikiMagic;
27
28 /**
29 * Allow external images inline?
30 */
31 var $mAllowExternalImages;
32
33 /**
34 * If not, any exception?
35 */
36 var $mAllowExternalImagesFrom;
37
38 /**
39 * If not or it doesn't match, should we check an on-wiki whitelist?
40 */
41 var $mEnableImageWhitelist;
42
43 /**
44 * Date format index
45 */
46 var $mDateFormat = null;
47
48 /**
49 * Create "edit section" links?
50 */
51 var $mEditSection = true;
52
53 /**
54 * Allow inclusion of special pages?
55 */
56 var $mAllowSpecialInclusion;
57
58 /**
59 * Use tidy to cleanup output HTML?
60 */
61 var $mTidy = false;
62
63 /**
64 * Which lang to call for PLURAL and GRAMMAR
65 */
66 var $mInterfaceMessage = false;
67
68 /**
69 * Overrides $mInterfaceMessage with arbitrary language
70 */
71 var $mTargetLanguage = null;
72
73 /**
74 * Maximum size of template expansions, in bytes
75 */
76 var $mMaxIncludeSize;
77
78 /**
79 * Maximum number of nodes touched by PPFrame::expand()
80 */
81 var $mMaxPPNodeCount;
82
83 /**
84 * Maximum recursion depth in PPFrame::expand()
85 */
86 var $mMaxPPExpandDepth;
87
88 /**
89 * Maximum recursion depth for templates within templates
90 */
91 var $mMaxTemplateDepth;
92
93 /**
94 * Remove HTML comments. ONLY APPLIES TO PREPROCESS OPERATIONS
95 */
96 var $mRemoveComments = true;
97
98 /**
99 * Callback for template fetching. Used as first argument to call_user_func().
100 */
101 var $mTemplateCallback =
102 array( 'Parser', 'statelessFetchTemplate' );
103
104 /**
105 * Enable limit report in an HTML comment on output
106 */
107 var $mEnableLimitReport = false;
108
109 /**
110 * Timestamp used for {{CURRENTDAY}} etc.
111 */
112 var $mTimestamp;
113
114 /**
115 * Target attribute for external links
116 */
117 var $mExternalLinkTarget;
118
119 /**
120 * Clean up signature texts?
121 *
122 * 1) Strip ~~~, ~~~~ and ~~~~~ out of signatures
123 * 2) Substitute all transclusions
124 */
125 var $mCleanSignatures;
126
127 /**
128 * Transform wiki markup when saving the page?
129 */
130 var $mPreSaveTransform = true;
131
132 /**
133 * Automatically number headings?
134 */
135 var $mNumberHeadings;
136
137 /**
138 * User math preference (as integer). Not used (1.19)
139 */
140 var $mMath;
141
142 /**
143 * Thumb size preferred by the user.
144 */
145 var $mThumbSize;
146
147 /**
148 * Maximum article size of an article to be marked as "stub"
149 */
150 private $mStubThreshold;
151
152 /**
153 * Language object of the User language.
154 */
155 var $mUserLang;
156
157 /**
158 * @var User
159 * Stored user object
160 */
161 var $mUser;
162
163 /**
164 * Parsing the page for a "preview" operation?
165 */
166 var $mIsPreview = false;
167
168 /**
169 * Parsing the page for a "preview" operation on a single section?
170 */
171 var $mIsSectionPreview = false;
172
173 /**
174 * Parsing the printable version of the page?
175 */
176 var $mIsPrintable = false;
177
178 /**
179 * Extra key that should be present in the caching key.
180 */
181 var $mExtraKey = '';
182
183 /**
184 * Function to be called when an option is accessed.
185 */
186 protected $onAccessCallback = null;
187
188 function getUseDynamicDates() { return $this->mUseDynamicDates; }
189 function getInterwikiMagic() { return $this->mInterwikiMagic; }
190 function getAllowExternalImages() { return $this->mAllowExternalImages; }
191 function getAllowExternalImagesFrom() { return $this->mAllowExternalImagesFrom; }
192 function getEnableImageWhitelist() { return $this->mEnableImageWhitelist; }
193 function getEditSection() { return $this->mEditSection; }
194 function getNumberHeadings() { $this->optionUsed( 'numberheadings' );
195 return $this->mNumberHeadings; }
196 function getAllowSpecialInclusion() { return $this->mAllowSpecialInclusion; }
197 function getTidy() { return $this->mTidy; }
198 function getInterfaceMessage() { return $this->mInterfaceMessage; }
199 function getTargetLanguage() { return $this->mTargetLanguage; }
200 function getMaxIncludeSize() { return $this->mMaxIncludeSize; }
201 function getMaxPPNodeCount() { return $this->mMaxPPNodeCount; }
202 function getMaxPPExpandDepth() { return $this->mMaxPPExpandDepth; }
203 function getMaxTemplateDepth() { return $this->mMaxTemplateDepth; }
204 function getRemoveComments() { return $this->mRemoveComments; }
205 function getTemplateCallback() { return $this->mTemplateCallback; }
206 function getEnableLimitReport() { return $this->mEnableLimitReport; }
207 function getCleanSignatures() { return $this->mCleanSignatures; }
208 function getExternalLinkTarget() { return $this->mExternalLinkTarget; }
209 function getMath() { $this->optionUsed( 'math' );
210 return $this->mMath; }
211 function getThumbSize() { $this->optionUsed( 'thumbsize' );
212 return $this->mThumbSize; }
213 function getStubThreshold() { $this->optionUsed( 'stubthreshold' );
214 return $this->mStubThreshold; }
215
216 function getIsPreview() { return $this->mIsPreview; }
217 function getIsSectionPreview() { return $this->mIsSectionPreview; }
218 function getIsPrintable() { $this->optionUsed( 'printable' );
219 return $this->mIsPrintable; }
220 function getUser() { return $this->mUser; }
221 function getPreSaveTransform() { return $this->mPreSaveTransform; }
222
223 /**
224 * @param $title Title
225 * @return Skin
226 * @deprecated since 1.18 Use Linker::* instead
227 */
228 function getSkin( $title = null ) {
229 wfDeprecated( __METHOD__, '1.18' );
230 return new DummyLinker;
231 }
232
233 function getDateFormat() {
234 $this->optionUsed( 'dateformat' );
235 if ( !isset( $this->mDateFormat ) ) {
236 $this->mDateFormat = $this->mUser->getDatePreference();
237 }
238 return $this->mDateFormat;
239 }
240
241 function getTimestamp() {
242 if ( !isset( $this->mTimestamp ) ) {
243 $this->mTimestamp = wfTimestampNow();
244 }
245 return $this->mTimestamp;
246 }
247
248 /**
249 * You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
250 * Using this fragments the cache and is discouraged. Yes, {{int: }} uses this,
251 * producing inconsistent tables (Bug 14404).
252 *
253 * @return Language object
254 * @since 1.19
255 */
256 function getUserLangObj() {
257 $this->optionUsed( 'userlang' );
258 return $this->mUserLang;
259 }
260
261 /**
262 * Same as getUserLangObj() but returns a string instead.
263 *
264 * @return String Language code
265 * @since 1.17
266 */
267 function getUserLang() {
268 return $this->getUserLangObj()->getCode();
269 }
270
271 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
272 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
273 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
274 function setAllowExternalImagesFrom( $x ) { return wfSetVar( $this->mAllowExternalImagesFrom, $x ); }
275 function setEnableImageWhitelist( $x ) { return wfSetVar( $this->mEnableImageWhitelist, $x ); }
276 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
277 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
278 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
279 function setAllowSpecialInclusion( $x ) { return wfSetVar( $this->mAllowSpecialInclusion, $x ); }
280 function setTidy( $x ) { return wfSetVar( $this->mTidy, $x ); }
281
282 /** @deprecated in 1.19; will be removed in 1.20 */
283 function setSkin( $x ) { wfDeprecated( __METHOD__, '1.19' ); }
284 function setInterfaceMessage( $x ) { return wfSetVar( $this->mInterfaceMessage, $x ); }
285 function setTargetLanguage( $x ) { return wfSetVar( $this->mTargetLanguage, $x, true ); }
286 function setMaxIncludeSize( $x ) { return wfSetVar( $this->mMaxIncludeSize, $x ); }
287 function setMaxPPNodeCount( $x ) { return wfSetVar( $this->mMaxPPNodeCount, $x ); }
288 function setMaxTemplateDepth( $x ) { return wfSetVar( $this->mMaxTemplateDepth, $x ); }
289 function setRemoveComments( $x ) { return wfSetVar( $this->mRemoveComments, $x ); }
290 function setTemplateCallback( $x ) { return wfSetVar( $this->mTemplateCallback, $x ); }
291 function enableLimitReport( $x = true ) { return wfSetVar( $this->mEnableLimitReport, $x ); }
292 function setTimestamp( $x ) { return wfSetVar( $this->mTimestamp, $x ); }
293 function setCleanSignatures( $x ) { return wfSetVar( $this->mCleanSignatures, $x ); }
294 function setExternalLinkTarget( $x ) { return wfSetVar( $this->mExternalLinkTarget, $x ); }
295 function setMath( $x ) { return wfSetVar( $this->mMath, $x ); }
296 function setUserLang( $x ) {
297 if ( is_string( $x ) ) {
298 $x = Language::factory( $x );
299 }
300 return wfSetVar( $this->mUserLang, $x );
301 }
302 function setThumbSize( $x ) { return wfSetVar( $this->mThumbSize, $x ); }
303 function setStubThreshold( $x ) { return wfSetVar( $this->mStubThreshold, $x ); }
304 function setPreSaveTransform( $x ) { return wfSetVar( $this->mPreSaveTransform, $x ); }
305
306 function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview, $x ); }
307 function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview, $x ); }
308 function setIsPrintable( $x ) { return wfSetVar( $this->mIsPrintable, $x ); }
309
310 /**
311 * Extra key that should be present in the parser cache key.
312 */
313 function addExtraKey( $key ) {
314 $this->mExtraKey .= '!' . $key;
315 }
316
317 /**
318 * Constructor
319 * @param $user User object
320 * @param $lang Language object
321 */
322 function __construct( $user = null, $lang = null ) {
323 if ( $user === null ) {
324 global $wgUser;
325 if ( $wgUser === null ) {
326 $user = new User;
327 } else {
328 $user = $wgUser;
329 }
330 }
331 if ( $lang === null ) {
332 global $wgLang;
333 if ( !StubObject::isRealObject( $wgLang ) ) {
334 $wgLang->_unstub();
335 }
336 $lang = $wgLang;
337 }
338 $this->initialiseFromUser( $user, $lang );
339 }
340
341 /**
342 * Get a ParserOptions object from a given user.
343 * Language will be taken from $wgLang.
344 *
345 * @param $user User object
346 * @return ParserOptions object
347 */
348 public static function newFromUser( $user ) {
349 return new ParserOptions( $user );
350 }
351
352 /**
353 * Get a ParserOptions object from a given user and language
354 *
355 * @param $user User object
356 * @param $lang Language object
357 * @return ParserOptions object
358 */
359 public static function newFromUserAndLang( User $user, Language $lang ) {
360 return new ParserOptions( $user, $lang );
361 }
362
363 /**
364 * Get a ParserOptions object from a IContextSource object
365 *
366 * @param $context IContextSource object
367 * @return ParserOptions object
368 */
369 public static function newFromContext( IContextSource $context ) {
370 return new ParserOptions( $context->getUser(), $context->getLanguage() );
371 }
372
373 /**
374 * Get user options
375 *
376 * @param $user User object
377 * @param $lang Language object
378 */
379 private function initialiseFromUser( $user, $lang ) {
380 global $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages,
381 $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion,
382 $wgMaxArticleSize, $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth,
383 $wgCleanSignatures, $wgExternalLinkTarget;
384
385 wfProfileIn( __METHOD__ );
386
387 $this->mUseDynamicDates = $wgUseDynamicDates;
388 $this->mInterwikiMagic = $wgInterwikiMagic;
389 $this->mAllowExternalImages = $wgAllowExternalImages;
390 $this->mAllowExternalImagesFrom = $wgAllowExternalImagesFrom;
391 $this->mEnableImageWhitelist = $wgEnableImageWhitelist;
392 $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion;
393 $this->mMaxIncludeSize = $wgMaxArticleSize * 1024;
394 $this->mMaxPPNodeCount = $wgMaxPPNodeCount;
395 $this->mMaxPPExpandDepth = $wgMaxPPExpandDepth;
396 $this->mMaxTemplateDepth = $wgMaxTemplateDepth;
397 $this->mCleanSignatures = $wgCleanSignatures;
398 $this->mExternalLinkTarget = $wgExternalLinkTarget;
399
400 $this->mUser = $user;
401 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
402 $this->mMath = $user->getOption( 'math' );
403 $this->mThumbSize = $user->getOption( 'thumbsize' );
404 $this->mStubThreshold = $user->getStubThreshold();
405 $this->mUserLang = $lang;
406
407 wfProfileOut( __METHOD__ );
408 }
409
410 /**
411 * Registers a callback for tracking which ParserOptions which are used.
412 * This is a private API with the parser.
413 */
414 function registerWatcher( $callback ) {
415 $this->onAccessCallback = $callback;
416 }
417
418 /**
419 * Called when an option is accessed.
420 */
421 protected function optionUsed( $optionName ) {
422 if ( $this->onAccessCallback ) {
423 call_user_func( $this->onAccessCallback, $optionName );
424 }
425 }
426
427 /**
428 * Returns the full array of options that would have been used by
429 * in 1.16.
430 * Used to get the old parser cache entries when available.
431 * @return array
432 */
433 public static function legacyOptions() {
434 global $wgUseDynamicDates;
435 $legacyOpts = array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
436 if ( $wgUseDynamicDates ) {
437 $legacyOpts[] = 'dateformat';
438 }
439 return $legacyOpts;
440 }
441
442 /**
443 * Generate a hash string with the values set on these ParserOptions
444 * for the keys given in the array.
445 * This will be used as part of the hash key for the parser cache,
446 * so users sharign the options with vary for the same page share
447 * the same cached data safely.
448 *
449 * Replaces User::getPageRenderingHash()
450 *
451 * Extensions which require it should install 'PageRenderingHash' hook,
452 * which will give them a chance to modify this key based on their own
453 * settings.
454 *
455 * @since 1.17
456 * @param $forOptions Array
457 * @param $title Title: used to get the content language of the page (since r97636)
458 * @return string Page rendering hash
459 */
460 public function optionsHash( $forOptions, $title = null ) {
461 global $wgRenderHashAppend;
462
463 $confstr = '';
464
465 if ( in_array( 'math', $forOptions ) ) {
466 $confstr .= $this->mMath;
467 } else {
468 $confstr .= '*';
469 }
470
471
472 // Space assigned for the stubthreshold but unused
473 // since it disables the parser cache, its value will always
474 // be 0 when this function is called by parsercache.
475 if ( in_array( 'stubthreshold', $forOptions ) ) {
476 $confstr .= '!' . $this->mStubThreshold;
477 } else {
478 $confstr .= '!*' ;
479 }
480
481 if ( in_array( 'dateformat', $forOptions ) ) {
482 $confstr .= '!' . $this->getDateFormat();
483 }
484
485 if ( in_array( 'numberheadings', $forOptions ) ) {
486 $confstr .= '!' . ( $this->mNumberHeadings ? '1' : '' );
487 } else {
488 $confstr .= '!*';
489 }
490
491 if ( in_array( 'userlang', $forOptions ) ) {
492 $confstr .= '!' . $this->mUserLang->getCode();
493 } else {
494 $confstr .= '!*';
495 }
496
497 if ( in_array( 'thumbsize', $forOptions ) ) {
498 $confstr .= '!' . $this->mThumbSize;
499 } else {
500 $confstr .= '!*';
501 }
502
503 // add in language specific options, if any
504 // @todo FIXME: This is just a way of retrieving the url/user preferred variant
505 if( !is_null( $title ) ) {
506 $confstr .= $title->getPageLanguage()->getExtraHashOptions();
507 } else {
508 global $wgContLang;
509 $confstr .= $wgContLang->getExtraHashOptions();
510 }
511
512 $confstr .= $wgRenderHashAppend;
513
514 if ( !in_array( 'editsection', $forOptions ) ) {
515 $confstr .= '!*';
516 } elseif ( !$this->mEditSection ) {
517 $confstr .= '!edit=0';
518 }
519
520 if ( $this->mIsPrintable && in_array( 'printable', $forOptions ) ) {
521 $confstr .= '!printable=1';
522 }
523
524 if ( $this->mExtraKey != '' )
525 $confstr .= $this->mExtraKey;
526
527 // Give a chance for extensions to modify the hash, if they have
528 // extra options or other effects on the parser cache.
529 wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
530
531 // Make it a valid memcached key fragment
532 $confstr = str_replace( ' ', '_', $confstr );
533
534 return $confstr;
535 }
536 }