3 * Options for the PHP parser
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.
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.
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
25 * @brief Set options of the Parser
27 * All member variables are supposed to be private in theory, although in
28 * practise this is not the case.
35 * Interlanguage links are removed and returned in an array
37 public $mInterwikiMagic;
40 * Allow external images inline?
42 public $mAllowExternalImages;
45 * If not, any exception?
47 public $mAllowExternalImagesFrom;
50 * If not or it doesn't match, should we check an on-wiki whitelist?
52 public $mEnableImageWhitelist;
57 public $mDateFormat = null;
60 * Create "edit section" links?
62 public $mEditSection = true;
65 * Allow inclusion of special pages?
67 public $mAllowSpecialInclusion;
70 * Use tidy to cleanup output HTML?
72 public $mTidy = false;
75 * Which lang to call for PLURAL and GRAMMAR
77 public $mInterfaceMessage = false;
80 * Overrides $mInterfaceMessage with arbitrary language
82 public $mTargetLanguage = null;
85 * Maximum size of template expansions, in bytes
87 public $mMaxIncludeSize;
90 * Maximum number of nodes touched by PPFrame::expand()
92 public $mMaxPPNodeCount;
95 * Maximum number of nodes generated by Preprocessor::preprocessToObj()
97 public $mMaxGeneratedPPNodeCount;
100 * Maximum recursion depth in PPFrame::expand()
102 public $mMaxPPExpandDepth;
105 * Maximum recursion depth for templates within templates
107 public $mMaxTemplateDepth;
110 * Maximum number of calls per parse to expensive parser functions
112 public $mExpensiveParserFunctionLimit;
115 * Remove HTML comments. ONLY APPLIES TO PREPROCESS OPERATIONS
117 public $mRemoveComments = true;
120 * Callback for template fetching. Used as first argument to call_user_func().
122 public $mTemplateCallback =
123 array( 'Parser', 'statelessFetchTemplate' );
126 * Enable limit report in an HTML comment on output
128 public $mEnableLimitReport = false;
131 * Timestamp used for {{CURRENTDAY}} etc.
136 * Target attribute for external links
138 public $mExternalLinkTarget;
141 * Clean up signature texts?
143 * 1) Strip ~~~, ~~~~ and ~~~~~ out of signatures
144 * 2) Substitute all transclusions
146 public $mCleanSignatures;
149 * Transform wiki markup when saving the page?
151 public $mPreSaveTransform = true;
154 * Whether content conversion should be disabled
156 public $mDisableContentConversion;
159 * Whether title conversion should be disabled
161 public $mDisableTitleConversion;
164 * Automatically number headings?
166 public $mNumberHeadings;
169 * Thumb size preferred by the user.
174 * Maximum article size of an article to be marked as "stub"
176 private $mStubThreshold;
179 * Language object of the User language.
190 * Parsing the page for a "preview" operation?
192 public $mIsPreview = false;
195 * Parsing the page for a "preview" operation on a single section?
197 public $mIsSectionPreview = false;
200 * Parsing the printable version of the page?
202 public $mIsPrintable = false;
205 * Extra key that should be present in the caching key.
207 public $mExtraKey = '';
210 * Function to be called when an option is accessed.
212 protected $onAccessCallback = null;
215 * If the page being parsed is a redirect, this should hold the redirect
219 private $redirectTarget = null;
221 public function getInterwikiMagic() {
222 return $this->mInterwikiMagic
;
225 public function getAllowExternalImages() {
226 return $this->mAllowExternalImages
;
229 public function getAllowExternalImagesFrom() {
230 return $this->mAllowExternalImagesFrom
;
233 public function getEnableImageWhitelist() {
234 return $this->mEnableImageWhitelist
;
237 public function getEditSection() {
238 return $this->mEditSection
;
241 public function getNumberHeadings() {
242 $this->optionUsed( 'numberheadings' );
244 return $this->mNumberHeadings
;
247 public function getAllowSpecialInclusion() {
248 return $this->mAllowSpecialInclusion
;
251 public function getTidy() {
255 public function getInterfaceMessage() {
256 return $this->mInterfaceMessage
;
259 public function getTargetLanguage() {
260 return $this->mTargetLanguage
;
263 public function getMaxIncludeSize() {
264 return $this->mMaxIncludeSize
;
267 public function getMaxPPNodeCount() {
268 return $this->mMaxPPNodeCount
;
271 public function getMaxGeneratedPPNodeCount() {
272 return $this->mMaxGeneratedPPNodeCount
;
275 public function getMaxPPExpandDepth() {
276 return $this->mMaxPPExpandDepth
;
279 public function getMaxTemplateDepth() {
280 return $this->mMaxTemplateDepth
;
284 public function getExpensiveParserFunctionLimit() {
285 return $this->mExpensiveParserFunctionLimit
;
288 public function getRemoveComments() {
289 return $this->mRemoveComments
;
292 public function getTemplateCallback() {
293 return $this->mTemplateCallback
;
296 public function getEnableLimitReport() {
297 return $this->mEnableLimitReport
;
300 public function getCleanSignatures() {
301 return $this->mCleanSignatures
;
304 public function getExternalLinkTarget() {
305 return $this->mExternalLinkTarget
;
308 public function getDisableContentConversion() {
309 return $this->mDisableContentConversion
;
312 public function getDisableTitleConversion() {
313 return $this->mDisableTitleConversion
;
316 public function getThumbSize() {
317 $this->optionUsed( 'thumbsize' );
319 return $this->mThumbSize
;
322 public function getStubThreshold() {
323 $this->optionUsed( 'stubthreshold' );
325 return $this->mStubThreshold
;
328 public function getIsPreview() {
329 return $this->mIsPreview
;
332 public function getIsSectionPreview() {
333 return $this->mIsSectionPreview
;
336 public function getIsPrintable() {
337 $this->optionUsed( 'printable' );
339 return $this->mIsPrintable
;
342 public function getUser() {
346 public function getPreSaveTransform() {
347 return $this->mPreSaveTransform
;
350 public function getDateFormat() {
351 $this->optionUsed( 'dateformat' );
352 if ( !isset( $this->mDateFormat
) ) {
353 $this->mDateFormat
= $this->mUser
->getDatePreference();
355 return $this->mDateFormat
;
358 public function getTimestamp() {
359 if ( !isset( $this->mTimestamp
) ) {
360 $this->mTimestamp
= wfTimestampNow();
362 return $this->mTimestamp
;
366 * Get the user language used by the parser for this page.
368 * You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
370 * To avoid side-effects where the page will be rendered based on the language
371 * of the user who last saved, this function will triger a cache fragmentation.
372 * Usage of this method is discouraged for that reason.
374 * When saving, this will return the default language instead of the user's.
376 * {{int: }} uses this which used to produce inconsistent link tables (bug 14404).
381 public function getUserLangObj() {
382 $this->optionUsed( 'userlang' );
383 return $this->mUserLang
;
387 * Same as getUserLangObj() but returns a string instead.
389 * @return string Language code
392 public function getUserLang() {
393 return $this->getUserLangObj()->getCode();
396 public function setInterwikiMagic( $x ) {
397 return wfSetVar( $this->mInterwikiMagic
, $x );
400 public function setAllowExternalImages( $x ) {
401 return wfSetVar( $this->mAllowExternalImages
, $x );
404 public function setAllowExternalImagesFrom( $x ) {
405 return wfSetVar( $this->mAllowExternalImagesFrom
, $x );
408 public function setEnableImageWhitelist( $x ) {
409 return wfSetVar( $this->mEnableImageWhitelist
, $x );
412 public function setDateFormat( $x ) {
413 return wfSetVar( $this->mDateFormat
, $x );
416 public function setEditSection( $x ) {
417 return wfSetVar( $this->mEditSection
, $x );
420 public function setNumberHeadings( $x ) {
421 return wfSetVar( $this->mNumberHeadings
, $x );
424 public function setAllowSpecialInclusion( $x ) {
425 return wfSetVar( $this->mAllowSpecialInclusion
, $x );
428 public function setTidy( $x ) {
429 return wfSetVar( $this->mTidy
, $x );
432 public function setInterfaceMessage( $x ) {
433 return wfSetVar( $this->mInterfaceMessage
, $x );
436 public function setTargetLanguage( $x ) {
437 return wfSetVar( $this->mTargetLanguage
, $x, true );
440 public function setMaxIncludeSize( $x ) {
441 return wfSetVar( $this->mMaxIncludeSize
, $x );
444 public function setMaxPPNodeCount( $x ) {
445 return wfSetVar( $this->mMaxPPNodeCount
, $x );
448 public function setMaxGeneratedPPNodeCount( $x ) {
449 return wfSetVar( $this->mMaxGeneratedPPNodeCount
, $x );
452 public function setMaxTemplateDepth( $x ) {
453 return wfSetVar( $this->mMaxTemplateDepth
, $x );
457 public function setExpensiveParserFunctionLimit( $x ) {
458 return wfSetVar( $this->mExpensiveParserFunctionLimit
, $x );
461 public function setRemoveComments( $x ) {
462 return wfSetVar( $this->mRemoveComments
, $x );
465 public function setTemplateCallback( $x ) {
466 return wfSetVar( $this->mTemplateCallback
, $x );
469 public function enableLimitReport( $x = true ) {
470 return wfSetVar( $this->mEnableLimitReport
, $x );
473 public function setTimestamp( $x ) {
474 return wfSetVar( $this->mTimestamp
, $x );
477 public function setCleanSignatures( $x ) {
478 return wfSetVar( $this->mCleanSignatures
, $x );
481 public function setExternalLinkTarget( $x ) {
482 return wfSetVar( $this->mExternalLinkTarget
, $x );
485 public function disableContentConversion( $x = true ) {
486 return wfSetVar( $this->mDisableContentConversion
, $x );
489 public function disableTitleConversion( $x = true ) {
490 return wfSetVar( $this->mDisableTitleConversion
, $x );
493 public function setUserLang( $x ) {
494 if ( is_string( $x ) ) {
495 $x = Language
::factory( $x );
498 return wfSetVar( $this->mUserLang
, $x );
501 public function setThumbSize( $x ) {
502 return wfSetVar( $this->mThumbSize
, $x );
505 public function setStubThreshold( $x ) {
506 return wfSetVar( $this->mStubThreshold
, $x );
509 public function setPreSaveTransform( $x ) {
510 return wfSetVar( $this->mPreSaveTransform
, $x );
513 public function setIsPreview( $x ) {
514 return wfSetVar( $this->mIsPreview
, $x );
517 public function setIsSectionPreview( $x ) {
518 return wfSetVar( $this->mIsSectionPreview
, $x );
521 public function setIsPrintable( $x ) {
522 return wfSetVar( $this->mIsPrintable
, $x );
526 * Set the redirect target.
528 * Note that setting or changing this does not *make* the page a redirect
529 * or change its target, it merely records the information for reference
533 * @param Title|null $title
535 function setRedirectTarget( $title ) {
536 $this->redirectTarget
= $title;
540 * Get the previously-set redirect target.
545 function getRedirectTarget() {
546 return $this->redirectTarget
;
550 * Extra key that should be present in the parser cache key.
553 public function addExtraKey( $key ) {
554 $this->mExtraKey
.= '!' . $key;
560 * @param Language $lang
562 public function __construct( $user = null, $lang = null ) {
563 if ( $user === null ) {
565 if ( $wgUser === null ) {
571 if ( $lang === null ) {
573 if ( !StubObject
::isRealObject( $wgLang ) ) {
578 $this->initialiseFromUser( $user, $lang );
582 * Get a ParserOptions object from a given user.
583 * Language will be taken from $wgLang.
586 * @return ParserOptions
588 public static function newFromUser( $user ) {
589 return new ParserOptions( $user );
593 * Get a ParserOptions object from a given user and language
596 * @param Language $lang
597 * @return ParserOptions
599 public static function newFromUserAndLang( User
$user, Language
$lang ) {
600 return new ParserOptions( $user, $lang );
604 * Get a ParserOptions object from a IContextSource object
606 * @param IContextSource $context
607 * @return ParserOptions
609 public static function newFromContext( IContextSource
$context ) {
610 return new ParserOptions( $context->getUser(), $context->getLanguage() );
617 * @param Language $lang
619 private function initialiseFromUser( $user, $lang ) {
620 global $wgInterwikiMagic, $wgAllowExternalImages,
621 $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion,
622 $wgMaxArticleSize, $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth,
623 $wgCleanSignatures, $wgExternalLinkTarget, $wgExpensiveParserFunctionLimit,
624 $wgMaxGeneratedPPNodeCount, $wgDisableLangConversion, $wgDisableTitleConversion;
626 wfProfileIn( __METHOD__
);
628 $this->mInterwikiMagic
= $wgInterwikiMagic;
629 $this->mAllowExternalImages
= $wgAllowExternalImages;
630 $this->mAllowExternalImagesFrom
= $wgAllowExternalImagesFrom;
631 $this->mEnableImageWhitelist
= $wgEnableImageWhitelist;
632 $this->mAllowSpecialInclusion
= $wgAllowSpecialInclusion;
633 $this->mMaxIncludeSize
= $wgMaxArticleSize * 1024;
634 $this->mMaxPPNodeCount
= $wgMaxPPNodeCount;
635 $this->mMaxGeneratedPPNodeCount
= $wgMaxGeneratedPPNodeCount;
636 $this->mMaxPPExpandDepth
= $wgMaxPPExpandDepth;
637 $this->mMaxTemplateDepth
= $wgMaxTemplateDepth;
638 $this->mExpensiveParserFunctionLimit
= $wgExpensiveParserFunctionLimit;
639 $this->mCleanSignatures
= $wgCleanSignatures;
640 $this->mExternalLinkTarget
= $wgExternalLinkTarget;
641 $this->mDisableContentConversion
= $wgDisableLangConversion;
642 $this->mDisableTitleConversion
= $wgDisableLangConversion ||
$wgDisableTitleConversion;
644 $this->mUser
= $user;
645 $this->mNumberHeadings
= $user->getOption( 'numberheadings' );
646 $this->mThumbSize
= $user->getOption( 'thumbsize' );
647 $this->mStubThreshold
= $user->getStubThreshold();
648 $this->mUserLang
= $lang;
650 wfProfileOut( __METHOD__
);
654 * Registers a callback for tracking which ParserOptions which are used.
655 * This is a private API with the parser.
656 * @param callable $callback
658 public function registerWatcher( $callback ) {
659 $this->onAccessCallback
= $callback;
663 * Called when an option is accessed.
664 * @param string $optionName Name of the option
666 public function optionUsed( $optionName ) {
667 if ( $this->onAccessCallback
) {
668 call_user_func( $this->onAccessCallback
, $optionName );
673 * Returns the full array of options that would have been used by
675 * Used to get the old parser cache entries when available.
678 public static function legacyOptions() {
690 * Generate a hash string with the values set on these ParserOptions
691 * for the keys given in the array.
692 * This will be used as part of the hash key for the parser cache,
693 * so users sharing the options with vary for the same page share
694 * the same cached data safely.
696 * Extensions which require it should install 'PageRenderingHash' hook,
697 * which will give them a chance to modify this key based on their own
701 * @param array $forOptions
702 * @param Title $title Used to get the content language of the page (since r97636)
703 * @return string Page rendering hash
705 public function optionsHash( $forOptions, $title = null ) {
706 global $wgRenderHashAppend;
708 // FIXME: Once the cache key is reorganized this argument
709 // can be dropped. It was used when the math extension was
713 // Space assigned for the stubthreshold but unused
714 // since it disables the parser cache, its value will always
715 // be 0 when this function is called by parsercache.
716 if ( in_array( 'stubthreshold', $forOptions ) ) {
717 $confstr .= '!' . $this->mStubThreshold
;
722 if ( in_array( 'dateformat', $forOptions ) ) {
723 $confstr .= '!' . $this->getDateFormat();
726 if ( in_array( 'numberheadings', $forOptions ) ) {
727 $confstr .= '!' . ( $this->mNumberHeadings ?
'1' : '' );
732 if ( in_array( 'userlang', $forOptions ) ) {
733 $confstr .= '!' . $this->mUserLang
->getCode();
738 if ( in_array( 'thumbsize', $forOptions ) ) {
739 $confstr .= '!' . $this->mThumbSize
;
744 // add in language specific options, if any
745 // @todo FIXME: This is just a way of retrieving the url/user preferred variant
746 if ( !is_null( $title ) ) {
747 $confstr .= $title->getPageLanguage()->getExtraHashOptions();
750 $confstr .= $wgContLang->getExtraHashOptions();
753 $confstr .= $wgRenderHashAppend;
755 if ( !in_array( 'editsection', $forOptions ) ) {
757 } elseif ( !$this->mEditSection
) {
758 $confstr .= '!edit=0';
761 if ( $this->mIsPrintable
&& in_array( 'printable', $forOptions ) ) {
762 $confstr .= '!printable=1';
765 if ( $this->mExtraKey
!= '' ) {
766 $confstr .= $this->mExtraKey
;
769 // Give a chance for extensions to modify the hash, if they have
770 // extra options or other effects on the parser cache.
771 wfRunHooks( 'PageRenderingHash', array( &$confstr, $this->getUser(), &$forOptions ) );
773 // Make it a valid memcached key fragment
774 $confstr = str_replace( ' ', '_', $confstr );