Merge "Fix typo"
[lhc/web/wiklou.git] / includes / parser / ParserOptions.php
1 <?php
2 /**
3 * Options for the PHP parser
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 /**
25 * \brief Set options of the Parser
26 *
27 * All member variables are supposed to be private in theory, although in
28 * practise this is not the case.
29 *
30 * @ingroup Parser
31 */
32 class ParserOptions {
33
34 /**
35 * Interlanguage links are removed and returned in an array
36 */
37 var $mInterwikiMagic;
38
39 /**
40 * Allow external images inline?
41 */
42 var $mAllowExternalImages;
43
44 /**
45 * If not, any exception?
46 */
47 var $mAllowExternalImagesFrom;
48
49 /**
50 * If not or it doesn't match, should we check an on-wiki whitelist?
51 */
52 var $mEnableImageWhitelist;
53
54 /**
55 * Date format index
56 */
57 var $mDateFormat = null;
58
59 /**
60 * Create "edit section" links?
61 */
62 var $mEditSection = true;
63
64 /**
65 * Allow inclusion of special pages?
66 */
67 var $mAllowSpecialInclusion;
68
69 /**
70 * Use tidy to cleanup output HTML?
71 */
72 var $mTidy = false;
73
74 /**
75 * Which lang to call for PLURAL and GRAMMAR
76 */
77 var $mInterfaceMessage = false;
78
79 /**
80 * Overrides $mInterfaceMessage with arbitrary language
81 */
82 var $mTargetLanguage = null;
83
84 /**
85 * Maximum size of template expansions, in bytes
86 */
87 var $mMaxIncludeSize;
88
89 /**
90 * Maximum number of nodes touched by PPFrame::expand()
91 */
92 var $mMaxPPNodeCount;
93
94 /**
95 * Maximum number of nodes generated by Preprocessor::preprocessToObj()
96 */
97 var $mMaxGeneratedPPNodeCount;
98
99 /**
100 * Maximum recursion depth in PPFrame::expand()
101 */
102 var $mMaxPPExpandDepth;
103
104 /**
105 * Maximum recursion depth for templates within templates
106 */
107 var $mMaxTemplateDepth;
108
109 /**
110 * Maximum number of calls per parse to expensive parser functions
111 */
112 var $mExpensiveParserFunctionLimit;
113
114 /**
115 * Remove HTML comments. ONLY APPLIES TO PREPROCESS OPERATIONS
116 */
117 var $mRemoveComments = true;
118
119 /**
120 * Callback for template fetching. Used as first argument to call_user_func().
121 */
122 var $mTemplateCallback =
123 array( 'Parser', 'statelessFetchTemplate' );
124
125 /**
126 * Enable limit report in an HTML comment on output
127 */
128 var $mEnableLimitReport = false;
129
130 /**
131 * Timestamp used for {{CURRENTDAY}} etc.
132 */
133 var $mTimestamp;
134
135 /**
136 * Target attribute for external links
137 */
138 var $mExternalLinkTarget;
139
140 /**
141 * Clean up signature texts?
142 *
143 * 1) Strip ~~~, ~~~~ and ~~~~~ out of signatures
144 * 2) Substitute all transclusions
145 */
146 var $mCleanSignatures;
147
148 /**
149 * Transform wiki markup when saving the page?
150 */
151 var $mPreSaveTransform = true;
152
153 /**
154 * Whether content conversion should be disabled
155 */
156 var $mDisableContentConversion;
157
158 /**
159 * Whether title conversion should be disabled
160 */
161 var $mDisableTitleConversion;
162
163 /**
164 * Automatically number headings?
165 */
166 var $mNumberHeadings;
167
168 /**
169 * Thumb size preferred by the user.
170 */
171 var $mThumbSize;
172
173 /**
174 * Maximum article size of an article to be marked as "stub"
175 */
176 private $mStubThreshold;
177
178 /**
179 * Language object of the User language.
180 */
181 var $mUserLang;
182
183 /**
184 * @var User
185 * Stored user object
186 */
187 var $mUser;
188
189 /**
190 * Parsing the page for a "preview" operation?
191 */
192 var $mIsPreview = false;
193
194 /**
195 * Parsing the page for a "preview" operation on a single section?
196 */
197 var $mIsSectionPreview = false;
198
199 /**
200 * Parsing the printable version of the page?
201 */
202 var $mIsPrintable = false;
203
204 /**
205 * Extra key that should be present in the caching key.
206 */
207 var $mExtraKey = '';
208
209 /**
210 * Function to be called when an option is accessed.
211 */
212 protected $onAccessCallback = null;
213
214 function getInterwikiMagic() {
215 return $this->mInterwikiMagic;
216 }
217
218 function getAllowExternalImages() {
219 return $this->mAllowExternalImages;
220 }
221
222 function getAllowExternalImagesFrom() {
223 return $this->mAllowExternalImagesFrom;
224 }
225
226 function getEnableImageWhitelist() {
227 return $this->mEnableImageWhitelist;
228 }
229
230 function getEditSection() {
231 return $this->mEditSection;
232 }
233
234 function getNumberHeadings() {
235 $this->optionUsed( 'numberheadings' );
236
237 return $this->mNumberHeadings;
238 }
239
240 function getAllowSpecialInclusion() {
241 return $this->mAllowSpecialInclusion;
242 }
243
244 function getTidy() {
245 return $this->mTidy;
246 }
247
248 function getInterfaceMessage() {
249 return $this->mInterfaceMessage;
250 }
251
252 function getTargetLanguage() {
253 return $this->mTargetLanguage;
254 }
255
256 function getMaxIncludeSize() {
257 return $this->mMaxIncludeSize;
258 }
259
260 function getMaxPPNodeCount() {
261 return $this->mMaxPPNodeCount;
262 }
263
264 function getMaxGeneratedPPNodeCount() {
265 return $this->mMaxGeneratedPPNodeCount;
266 }
267
268 function getMaxPPExpandDepth() {
269 return $this->mMaxPPExpandDepth;
270 }
271
272 function getMaxTemplateDepth() {
273 return $this->mMaxTemplateDepth;
274 }
275
276 /* @since 1.20 */
277 function getExpensiveParserFunctionLimit() {
278 return $this->mExpensiveParserFunctionLimit;
279 }
280
281 function getRemoveComments() {
282 return $this->mRemoveComments;
283 }
284
285 function getTemplateCallback() {
286 return $this->mTemplateCallback;
287 }
288
289 function getEnableLimitReport() {
290 return $this->mEnableLimitReport;
291 }
292
293 function getCleanSignatures() {
294 return $this->mCleanSignatures;
295 }
296
297 function getExternalLinkTarget() {
298 return $this->mExternalLinkTarget;
299 }
300
301 function getDisableContentConversion() {
302 return $this->mDisableContentConversion;
303 }
304
305 function getDisableTitleConversion() {
306 return $this->mDisableTitleConversion;
307 }
308
309 function getThumbSize() {
310 $this->optionUsed( 'thumbsize' );
311
312 return $this->mThumbSize;
313 }
314
315 function getStubThreshold() {
316 $this->optionUsed( 'stubthreshold' );
317
318 return $this->mStubThreshold;
319 }
320
321 function getIsPreview() {
322 return $this->mIsPreview;
323 }
324
325 function getIsSectionPreview() {
326 return $this->mIsSectionPreview;
327 }
328
329 function getIsPrintable() {
330 $this->optionUsed( 'printable' );
331
332 return $this->mIsPrintable;
333 }
334
335 function getUser() {
336 return $this->mUser;
337 }
338
339 function getPreSaveTransform() {
340 return $this->mPreSaveTransform;
341 }
342
343 function getDateFormat() {
344 $this->optionUsed( 'dateformat' );
345 if ( !isset( $this->mDateFormat ) ) {
346 $this->mDateFormat = $this->mUser->getDatePreference();
347 }
348 return $this->mDateFormat;
349 }
350
351 function getTimestamp() {
352 if ( !isset( $this->mTimestamp ) ) {
353 $this->mTimestamp = wfTimestampNow();
354 }
355 return $this->mTimestamp;
356 }
357
358 /**
359 * Get the user language used by the parser for this page.
360 *
361 * You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
362 *
363 * To avoid side-effects where the page will be rendered based on the language
364 * of the user who last saved, this function will triger a cache fragmentation.
365 * Usage of this method is discouraged for that reason.
366 *
367 * When saving, this will return the default language instead of the user's.
368 *
369 * {{int: }} uses this which used to produce inconsistent link tables (bug 14404).
370 *
371 * @return Language
372 * @since 1.19
373 */
374 function getUserLangObj() {
375 $this->optionUsed( 'userlang' );
376 return $this->mUserLang;
377 }
378
379 /**
380 * Same as getUserLangObj() but returns a string instead.
381 *
382 * @return string Language code
383 * @since 1.17
384 */
385 function getUserLang() {
386 return $this->getUserLangObj()->getCode();
387 }
388
389 function setInterwikiMagic( $x ) {
390 return wfSetVar( $this->mInterwikiMagic, $x );
391 }
392
393 function setAllowExternalImages( $x ) {
394 return wfSetVar( $this->mAllowExternalImages, $x );
395 }
396
397 function setAllowExternalImagesFrom( $x ) {
398 return wfSetVar( $this->mAllowExternalImagesFrom, $x );
399 }
400
401 function setEnableImageWhitelist( $x ) {
402 return wfSetVar( $this->mEnableImageWhitelist, $x );
403 }
404
405 function setDateFormat( $x ) {
406 return wfSetVar( $this->mDateFormat, $x );
407 }
408
409 function setEditSection( $x ) {
410 return wfSetVar( $this->mEditSection, $x );
411 }
412
413 function setNumberHeadings( $x ) {
414 return wfSetVar( $this->mNumberHeadings, $x );
415 }
416
417 function setAllowSpecialInclusion( $x ) {
418 return wfSetVar( $this->mAllowSpecialInclusion, $x );
419 }
420
421 function setTidy( $x ) {
422 return wfSetVar( $this->mTidy, $x );
423 }
424
425 /** @deprecated since 1.19 */
426 function setSkin( $x ) {
427 wfDeprecated( __METHOD__, '1.19' );
428 }
429
430 function setInterfaceMessage( $x ) {
431 return wfSetVar( $this->mInterfaceMessage, $x );
432 }
433
434 function setTargetLanguage( $x ) {
435 return wfSetVar( $this->mTargetLanguage, $x, true );
436 }
437
438 function setMaxIncludeSize( $x ) {
439 return wfSetVar( $this->mMaxIncludeSize, $x );
440 }
441
442 function setMaxPPNodeCount( $x ) {
443 return wfSetVar( $this->mMaxPPNodeCount, $x );
444 }
445
446 function setMaxGeneratedPPNodeCount( $x ) {
447 return wfSetVar( $this->mMaxGeneratedPPNodeCount, $x );
448 }
449
450 function setMaxTemplateDepth( $x ) {
451 return wfSetVar( $this->mMaxTemplateDepth, $x );
452 }
453
454 /* @since 1.20 */
455 function setExpensiveParserFunctionLimit( $x ) {
456 return wfSetVar( $this->mExpensiveParserFunctionLimit, $x );
457 }
458
459 function setRemoveComments( $x ) {
460 return wfSetVar( $this->mRemoveComments, $x );
461 }
462
463 function setTemplateCallback( $x ) {
464 return wfSetVar( $this->mTemplateCallback, $x );
465 }
466
467 function enableLimitReport( $x = true ) {
468 return wfSetVar( $this->mEnableLimitReport, $x );
469 }
470
471 function setTimestamp( $x ) {
472 return wfSetVar( $this->mTimestamp, $x );
473 }
474
475 function setCleanSignatures( $x ) {
476 return wfSetVar( $this->mCleanSignatures, $x );
477 }
478
479 function setExternalLinkTarget( $x ) {
480 return wfSetVar( $this->mExternalLinkTarget, $x );
481 }
482
483 function disableContentConversion( $x = true ) {
484 return wfSetVar( $this->mDisableContentConversion, $x );
485 }
486
487 function disableTitleConversion( $x = true ) {
488 return wfSetVar( $this->mDisableTitleConversion, $x );
489 }
490
491 function setUserLang( $x ) {
492 if ( is_string( $x ) ) {
493 $x = Language::factory( $x );
494 }
495
496 return wfSetVar( $this->mUserLang, $x );
497 }
498
499 function setThumbSize( $x ) {
500 return wfSetVar( $this->mThumbSize, $x );
501 }
502
503 function setStubThreshold( $x ) {
504 return wfSetVar( $this->mStubThreshold, $x );
505 }
506
507 function setPreSaveTransform( $x ) {
508 return wfSetVar( $this->mPreSaveTransform, $x );
509 }
510
511 function setIsPreview( $x ) {
512 return wfSetVar( $this->mIsPreview, $x );
513 }
514
515 function setIsSectionPreview( $x ) {
516 return wfSetVar( $this->mIsSectionPreview, $x );
517 }
518
519 function setIsPrintable( $x ) {
520 return wfSetVar( $this->mIsPrintable, $x );
521 }
522
523 /**
524 * Extra key that should be present in the parser cache key.
525 */
526 function addExtraKey( $key ) {
527 $this->mExtraKey .= '!' . $key;
528 }
529
530 /**
531 * Constructor
532 * @param User $user
533 * @param Language $lang
534 */
535 function __construct( $user = null, $lang = null ) {
536 if ( $user === null ) {
537 global $wgUser;
538 if ( $wgUser === null ) {
539 $user = new User;
540 } else {
541 $user = $wgUser;
542 }
543 }
544 if ( $lang === null ) {
545 global $wgLang;
546 if ( !StubObject::isRealObject( $wgLang ) ) {
547 $wgLang->_unstub();
548 }
549 $lang = $wgLang;
550 }
551 $this->initialiseFromUser( $user, $lang );
552 }
553
554 /**
555 * Get a ParserOptions object from a given user.
556 * Language will be taken from $wgLang.
557 *
558 * @param User $user
559 * @return ParserOptions
560 */
561 public static function newFromUser( $user ) {
562 return new ParserOptions( $user );
563 }
564
565 /**
566 * Get a ParserOptions object from a given user and language
567 *
568 * @param User $user
569 * @param Language $lang
570 * @return ParserOptions
571 */
572 public static function newFromUserAndLang( User $user, Language $lang ) {
573 return new ParserOptions( $user, $lang );
574 }
575
576 /**
577 * Get a ParserOptions object from a IContextSource object
578 *
579 * @param IContextSource $context
580 * @return ParserOptions
581 */
582 public static function newFromContext( IContextSource $context ) {
583 return new ParserOptions( $context->getUser(), $context->getLanguage() );
584 }
585
586 /**
587 * Get user options
588 *
589 * @param User $user
590 * @param Language $lang
591 */
592 private function initialiseFromUser( $user, $lang ) {
593 global $wgInterwikiMagic, $wgAllowExternalImages,
594 $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion,
595 $wgMaxArticleSize, $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth,
596 $wgCleanSignatures, $wgExternalLinkTarget, $wgExpensiveParserFunctionLimit,
597 $wgMaxGeneratedPPNodeCount, $wgDisableLangConversion, $wgDisableTitleConversion;
598
599 wfProfileIn( __METHOD__ );
600
601 $this->mInterwikiMagic = $wgInterwikiMagic;
602 $this->mAllowExternalImages = $wgAllowExternalImages;
603 $this->mAllowExternalImagesFrom = $wgAllowExternalImagesFrom;
604 $this->mEnableImageWhitelist = $wgEnableImageWhitelist;
605 $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion;
606 $this->mMaxIncludeSize = $wgMaxArticleSize * 1024;
607 $this->mMaxPPNodeCount = $wgMaxPPNodeCount;
608 $this->mMaxGeneratedPPNodeCount = $wgMaxGeneratedPPNodeCount;
609 $this->mMaxPPExpandDepth = $wgMaxPPExpandDepth;
610 $this->mMaxTemplateDepth = $wgMaxTemplateDepth;
611 $this->mExpensiveParserFunctionLimit = $wgExpensiveParserFunctionLimit;
612 $this->mCleanSignatures = $wgCleanSignatures;
613 $this->mExternalLinkTarget = $wgExternalLinkTarget;
614 $this->mDisableContentConversion = $wgDisableLangConversion;
615 $this->mDisableTitleConversion = $wgDisableLangConversion || $wgDisableTitleConversion;
616
617 $this->mUser = $user;
618 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
619 $this->mThumbSize = $user->getOption( 'thumbsize' );
620 $this->mStubThreshold = $user->getStubThreshold();
621 $this->mUserLang = $lang;
622
623 wfProfileOut( __METHOD__ );
624 }
625
626 /**
627 * Registers a callback for tracking which ParserOptions which are used.
628 * This is a private API with the parser.
629 * @param callable $callback
630 */
631 function registerWatcher( $callback ) {
632 $this->onAccessCallback = $callback;
633 }
634
635 /**
636 * Called when an option is accessed.
637 * @param string $optionName Name of the option
638 */
639 public function optionUsed( $optionName ) {
640 if ( $this->onAccessCallback ) {
641 call_user_func( $this->onAccessCallback, $optionName );
642 }
643 }
644
645 /**
646 * Returns the full array of options that would have been used by
647 * in 1.16.
648 * Used to get the old parser cache entries when available.
649 * @return array
650 */
651 public static function legacyOptions() {
652 return array(
653 'stubthreshold',
654 'numberheadings',
655 'userlang',
656 'thumbsize',
657 'editsection',
658 'printable'
659 );
660 }
661
662 /**
663 * Generate a hash string with the values set on these ParserOptions
664 * for the keys given in the array.
665 * This will be used as part of the hash key for the parser cache,
666 * so users sharing the options with vary for the same page share
667 * the same cached data safely.
668 *
669 * Extensions which require it should install 'PageRenderingHash' hook,
670 * which will give them a chance to modify this key based on their own
671 * settings.
672 *
673 * @since 1.17
674 * @param array $forOptions
675 * @param Title $title Used to get the content language of the page (since r97636)
676 * @return string Page rendering hash
677 */
678 public function optionsHash( $forOptions, $title = null ) {
679 global $wgRenderHashAppend;
680
681 // FIXME: Once the cache key is reorganized this argument
682 // can be dropped. It was used when the math extension was
683 // part of core.
684 $confstr = '*';
685
686 // Space assigned for the stubthreshold but unused
687 // since it disables the parser cache, its value will always
688 // be 0 when this function is called by parsercache.
689 if ( in_array( 'stubthreshold', $forOptions ) ) {
690 $confstr .= '!' . $this->mStubThreshold;
691 } else {
692 $confstr .= '!*';
693 }
694
695 if ( in_array( 'dateformat', $forOptions ) ) {
696 $confstr .= '!' . $this->getDateFormat();
697 }
698
699 if ( in_array( 'numberheadings', $forOptions ) ) {
700 $confstr .= '!' . ( $this->mNumberHeadings ? '1' : '' );
701 } else {
702 $confstr .= '!*';
703 }
704
705 if ( in_array( 'userlang', $forOptions ) ) {
706 $confstr .= '!' . $this->mUserLang->getCode();
707 } else {
708 $confstr .= '!*';
709 }
710
711 if ( in_array( 'thumbsize', $forOptions ) ) {
712 $confstr .= '!' . $this->mThumbSize;
713 } else {
714 $confstr .= '!*';
715 }
716
717 // add in language specific options, if any
718 // @todo FIXME: This is just a way of retrieving the url/user preferred variant
719 if ( !is_null( $title ) ) {
720 $confstr .= $title->getPageLanguage()->getExtraHashOptions();
721 } else {
722 global $wgContLang;
723 $confstr .= $wgContLang->getExtraHashOptions();
724 }
725
726 $confstr .= $wgRenderHashAppend;
727
728 if ( !in_array( 'editsection', $forOptions ) ) {
729 $confstr .= '!*';
730 } elseif ( !$this->mEditSection ) {
731 $confstr .= '!edit=0';
732 }
733
734 if ( $this->mIsPrintable && in_array( 'printable', $forOptions ) ) {
735 $confstr .= '!printable=1';
736 }
737
738 if ( $this->mExtraKey != '' ) {
739 $confstr .= $this->mExtraKey;
740 }
741
742 // Give a chance for extensions to modify the hash, if they have
743 // extra options or other effects on the parser cache.
744 wfRunHooks( 'PageRenderingHash', array( &$confstr, $this->getUser(), &$forOptions ) );
745
746 // Make it a valid memcached key fragment
747 $confstr = str_replace( ' ', '_', $confstr );
748
749 return $confstr;
750 }
751 }