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
23 use Wikimedia\ScopedCallback
;
26 * @brief Set options of the Parser
28 * All member variables are supposed to be private in theory, although in
29 * practice this is not the case.
36 * Interlanguage links are removed and returned in an array
38 private $mInterwikiMagic;
41 * Allow external images inline?
43 private $mAllowExternalImages;
46 * If not, any exception?
48 private $mAllowExternalImagesFrom;
51 * If not or it doesn't match, should we check an on-wiki whitelist?
53 private $mEnableImageWhitelist;
58 private $mDateFormat = null;
61 * Create "edit section" links?
63 private $mEditSection = true;
66 * Allow inclusion of special pages?
68 private $mAllowSpecialInclusion;
71 * Use tidy to cleanup output HTML?
73 private $mTidy = false;
76 * Which lang to call for PLURAL and GRAMMAR
78 private $mInterfaceMessage = false;
81 * Overrides $mInterfaceMessage with arbitrary language
83 private $mTargetLanguage = null;
86 * Maximum size of template expansions, in bytes
88 private $mMaxIncludeSize;
91 * Maximum number of nodes touched by PPFrame::expand()
93 private $mMaxPPNodeCount;
96 * Maximum number of nodes generated by Preprocessor::preprocessToObj()
98 private $mMaxGeneratedPPNodeCount;
101 * Maximum recursion depth in PPFrame::expand()
103 private $mMaxPPExpandDepth;
106 * Maximum recursion depth for templates within templates
108 private $mMaxTemplateDepth;
111 * Maximum number of calls per parse to expensive parser functions
113 private $mExpensiveParserFunctionLimit;
116 * Remove HTML comments. ONLY APPLIES TO PREPROCESS OPERATIONS
118 private $mRemoveComments = true;
121 * @var callable Callback for current revision fetching; first argument to call_user_func().
123 private $mCurrentRevisionCallback =
124 [ 'Parser', 'statelessFetchRevision' ];
127 * @var callable Callback for template fetching; first argument to call_user_func().
129 private $mTemplateCallback =
130 [ 'Parser', 'statelessFetchTemplate' ];
133 * @var callable|null Callback to generate a guess for {{REVISIONID}}
135 private $mSpeculativeRevIdCallback;
138 * Enable limit report in an HTML comment on output
140 private $mEnableLimitReport = false;
143 * Timestamp used for {{CURRENTDAY}} etc.
148 * Target attribute for external links
150 private $mExternalLinkTarget;
153 * Clean up signature texts?
154 * @see Parser::cleanSig
156 private $mCleanSignatures;
159 * Transform wiki markup when saving the page?
161 private $mPreSaveTransform = true;
164 * Whether content conversion should be disabled
166 private $mDisableContentConversion;
169 * Whether title conversion should be disabled
171 private $mDisableTitleConversion;
174 * Automatically number headings?
176 private $mNumberHeadings;
179 * Thumb size preferred by the user.
184 * Maximum article size of an article to be marked as "stub"
186 private $mStubThreshold;
189 * Language object of the User language.
200 * Parsing the page for a "preview" operation?
202 private $mIsPreview = false;
205 * Parsing the page for a "preview" operation on a single section?
207 private $mIsSectionPreview = false;
210 * Parsing the printable version of the page?
212 private $mIsPrintable = false;
215 * Extra key that should be present in the caching key.
217 private $mExtraKey = '';
220 * Are magic ISBN links enabled?
222 private $mMagicISBNLinks = true;
225 * Are magic PMID links enabled?
227 private $mMagicPMIDLinks = true;
230 * Are magic RFC links enabled?
232 private $mMagicRFCLinks = true;
235 * Function to be called when an option is accessed.
237 private $onAccessCallback = null;
240 * If the page being parsed is a redirect, this should hold the redirect
244 private $redirectTarget = null;
246 public function getInterwikiMagic() {
247 return $this->mInterwikiMagic
;
250 public function getAllowExternalImages() {
251 return $this->mAllowExternalImages
;
254 public function getAllowExternalImagesFrom() {
255 return $this->mAllowExternalImagesFrom
;
258 public function getEnableImageWhitelist() {
259 return $this->mEnableImageWhitelist
;
262 public function getEditSection() {
263 return $this->mEditSection
;
266 public function getNumberHeadings() {
267 $this->optionUsed( 'numberheadings' );
269 return $this->mNumberHeadings
;
272 public function getAllowSpecialInclusion() {
273 return $this->mAllowSpecialInclusion
;
276 public function getTidy() {
280 public function getInterfaceMessage() {
281 return $this->mInterfaceMessage
;
284 public function getTargetLanguage() {
285 return $this->mTargetLanguage
;
288 public function getMaxIncludeSize() {
289 return $this->mMaxIncludeSize
;
292 public function getMaxPPNodeCount() {
293 return $this->mMaxPPNodeCount
;
296 public function getMaxGeneratedPPNodeCount() {
297 return $this->mMaxGeneratedPPNodeCount
;
300 public function getMaxPPExpandDepth() {
301 return $this->mMaxPPExpandDepth
;
304 public function getMaxTemplateDepth() {
305 return $this->mMaxTemplateDepth
;
309 public function getExpensiveParserFunctionLimit() {
310 return $this->mExpensiveParserFunctionLimit
;
313 public function getRemoveComments() {
314 return $this->mRemoveComments
;
318 public function getCurrentRevisionCallback() {
319 return $this->mCurrentRevisionCallback
;
322 public function getTemplateCallback() {
323 return $this->mTemplateCallback
;
327 public function getSpeculativeRevIdCallback() {
328 return $this->mSpeculativeRevIdCallback
;
331 public function getEnableLimitReport() {
332 return $this->mEnableLimitReport
;
335 public function getCleanSignatures() {
336 return $this->mCleanSignatures
;
339 public function getExternalLinkTarget() {
340 return $this->mExternalLinkTarget
;
343 public function getDisableContentConversion() {
344 return $this->mDisableContentConversion
;
347 public function getDisableTitleConversion() {
348 return $this->mDisableTitleConversion
;
351 public function getThumbSize() {
352 $this->optionUsed( 'thumbsize' );
354 return $this->mThumbSize
;
357 public function getStubThreshold() {
358 $this->optionUsed( 'stubthreshold' );
360 return $this->mStubThreshold
;
363 public function getIsPreview() {
364 return $this->mIsPreview
;
367 public function getIsSectionPreview() {
368 return $this->mIsSectionPreview
;
371 public function getIsPrintable() {
372 $this->optionUsed( 'printable' );
374 return $this->mIsPrintable
;
377 public function getUser() {
381 public function getPreSaveTransform() {
382 return $this->mPreSaveTransform
;
385 public function getDateFormat() {
386 $this->optionUsed( 'dateformat' );
387 if ( !isset( $this->mDateFormat
) ) {
388 $this->mDateFormat
= $this->mUser
->getDatePreference();
390 return $this->mDateFormat
;
393 public function getTimestamp() {
394 if ( !isset( $this->mTimestamp
) ) {
395 $this->mTimestamp
= wfTimestampNow();
397 return $this->mTimestamp
;
401 * Get the user language used by the parser for this page and split the parser cache.
403 * @warning: Calling this causes the parser cache to be fragmented by user language!
404 * To avoid cache fragmentation, output should not depend on the user language.
405 * Use Parser::getFunctionLang() or Parser::getTargetLanguage() instead!
407 * @note This function will trigger a cache fragmentation by recording the
408 * 'userlang' option, see optionUsed(). This is done to avoid cache pollution
409 * when the page is rendered based on the language of the user.
411 * @note When saving, this will return the default language instead of the user's.
412 * {{int: }} uses this which used to produce inconsistent link tables (bug 14404).
417 public function getUserLangObj() {
418 $this->optionUsed( 'userlang' );
419 return $this->mUserLang
;
423 * Same as getUserLangObj() but returns a string instead.
425 * @warning: Calling this causes the parser cache to be fragmented by user language!
426 * To avoid cache fragmentation, output should not depend on the user language.
427 * Use Parser::getFunctionLang() or Parser::getTargetLanguage() instead!
429 * @see getUserLangObj()
431 * @return string Language code
434 public function getUserLang() {
435 return $this->getUserLangObj()->getCode();
442 public function getMagicISBNLinks() {
443 return $this->mMagicISBNLinks
;
450 public function getMagicPMIDLinks() {
451 return $this->mMagicPMIDLinks
;
457 public function getMagicRFCLinks() {
458 return $this->mMagicRFCLinks
;
460 public function setInterwikiMagic( $x ) {
461 return wfSetVar( $this->mInterwikiMagic
, $x );
464 public function setAllowExternalImages( $x ) {
465 return wfSetVar( $this->mAllowExternalImages
, $x );
468 public function setAllowExternalImagesFrom( $x ) {
469 return wfSetVar( $this->mAllowExternalImagesFrom
, $x );
472 public function setEnableImageWhitelist( $x ) {
473 return wfSetVar( $this->mEnableImageWhitelist
, $x );
476 public function setDateFormat( $x ) {
477 return wfSetVar( $this->mDateFormat
, $x );
480 public function setEditSection( $x ) {
481 return wfSetVar( $this->mEditSection
, $x );
484 public function setNumberHeadings( $x ) {
485 return wfSetVar( $this->mNumberHeadings
, $x );
488 public function setAllowSpecialInclusion( $x ) {
489 return wfSetVar( $this->mAllowSpecialInclusion
, $x );
492 public function setTidy( $x ) {
493 return wfSetVar( $this->mTidy
, $x );
496 public function setInterfaceMessage( $x ) {
497 return wfSetVar( $this->mInterfaceMessage
, $x );
500 public function setTargetLanguage( $x ) {
501 return wfSetVar( $this->mTargetLanguage
, $x, true );
504 public function setMaxIncludeSize( $x ) {
505 return wfSetVar( $this->mMaxIncludeSize
, $x );
508 public function setMaxPPNodeCount( $x ) {
509 return wfSetVar( $this->mMaxPPNodeCount
, $x );
512 public function setMaxGeneratedPPNodeCount( $x ) {
513 return wfSetVar( $this->mMaxGeneratedPPNodeCount
, $x );
516 public function setMaxTemplateDepth( $x ) {
517 return wfSetVar( $this->mMaxTemplateDepth
, $x );
521 public function setExpensiveParserFunctionLimit( $x ) {
522 return wfSetVar( $this->mExpensiveParserFunctionLimit
, $x );
525 public function setRemoveComments( $x ) {
526 return wfSetVar( $this->mRemoveComments
, $x );
530 public function setCurrentRevisionCallback( $x ) {
531 return wfSetVar( $this->mCurrentRevisionCallback
, $x );
535 public function setSpeculativeRevIdCallback( $x ) {
536 return wfSetVar( $this->mSpeculativeRevIdCallback
, $x );
539 public function setTemplateCallback( $x ) {
540 return wfSetVar( $this->mTemplateCallback
, $x );
543 public function enableLimitReport( $x = true ) {
544 return wfSetVar( $this->mEnableLimitReport
, $x );
547 public function setTimestamp( $x ) {
548 return wfSetVar( $this->mTimestamp
, $x );
551 public function setCleanSignatures( $x ) {
552 return wfSetVar( $this->mCleanSignatures
, $x );
555 public function setExternalLinkTarget( $x ) {
556 return wfSetVar( $this->mExternalLinkTarget
, $x );
559 public function disableContentConversion( $x = true ) {
560 return wfSetVar( $this->mDisableContentConversion
, $x );
563 public function disableTitleConversion( $x = true ) {
564 return wfSetVar( $this->mDisableTitleConversion
, $x );
567 public function setUserLang( $x ) {
568 if ( is_string( $x ) ) {
569 $x = Language
::factory( $x );
572 return wfSetVar( $this->mUserLang
, $x );
575 public function setThumbSize( $x ) {
576 return wfSetVar( $this->mThumbSize
, $x );
579 public function setStubThreshold( $x ) {
580 return wfSetVar( $this->mStubThreshold
, $x );
583 public function setPreSaveTransform( $x ) {
584 return wfSetVar( $this->mPreSaveTransform
, $x );
587 public function setIsPreview( $x ) {
588 return wfSetVar( $this->mIsPreview
, $x );
591 public function setIsSectionPreview( $x ) {
592 return wfSetVar( $this->mIsSectionPreview
, $x );
595 public function setIsPrintable( $x ) {
596 return wfSetVar( $this->mIsPrintable
, $x );
600 * Set the redirect target.
602 * Note that setting or changing this does not *make* the page a redirect
603 * or change its target, it merely records the information for reference
607 * @param Title|null $title
609 function setRedirectTarget( $title ) {
610 $this->redirectTarget
= $title;
614 * Get the previously-set redirect target.
619 function getRedirectTarget() {
620 return $this->redirectTarget
;
624 * Extra key that should be present in the parser cache key.
627 public function addExtraKey( $key ) {
628 $this->mExtraKey
.= '!' . $key;
634 * @param Language $lang
636 public function __construct( $user = null, $lang = null ) {
637 if ( $user === null ) {
639 if ( $wgUser === null ) {
645 if ( $lang === null ) {
647 if ( !StubObject
::isRealObject( $wgLang ) ) {
652 $this->initialiseFromUser( $user, $lang );
656 * Get a ParserOptions object for an anonymous user
658 * @return ParserOptions
660 public static function newFromAnon() {
662 return new ParserOptions( new User
, $wgContLang );
666 * Get a ParserOptions object from a given user.
667 * Language will be taken from $wgLang.
670 * @return ParserOptions
672 public static function newFromUser( $user ) {
673 return new ParserOptions( $user );
677 * Get a ParserOptions object from a given user and language
680 * @param Language $lang
681 * @return ParserOptions
683 public static function newFromUserAndLang( User
$user, Language
$lang ) {
684 return new ParserOptions( $user, $lang );
688 * Get a ParserOptions object from a IContextSource object
690 * @param IContextSource $context
691 * @return ParserOptions
693 public static function newFromContext( IContextSource
$context ) {
694 return new ParserOptions( $context->getUser(), $context->getLanguage() );
701 * @param Language $lang
703 private function initialiseFromUser( $user, $lang ) {
704 global $wgInterwikiMagic, $wgAllowExternalImages,
705 $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion,
706 $wgMaxArticleSize, $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth,
707 $wgCleanSignatures, $wgExternalLinkTarget, $wgExpensiveParserFunctionLimit,
708 $wgMaxGeneratedPPNodeCount, $wgDisableLangConversion, $wgDisableTitleConversion,
711 // *UPDATE* ParserOptions::matches() if any of this changes as needed
712 $this->mInterwikiMagic
= $wgInterwikiMagic;
713 $this->mAllowExternalImages
= $wgAllowExternalImages;
714 $this->mAllowExternalImagesFrom
= $wgAllowExternalImagesFrom;
715 $this->mEnableImageWhitelist
= $wgEnableImageWhitelist;
716 $this->mAllowSpecialInclusion
= $wgAllowSpecialInclusion;
717 $this->mMaxIncludeSize
= $wgMaxArticleSize * 1024;
718 $this->mMaxPPNodeCount
= $wgMaxPPNodeCount;
719 $this->mMaxGeneratedPPNodeCount
= $wgMaxGeneratedPPNodeCount;
720 $this->mMaxPPExpandDepth
= $wgMaxPPExpandDepth;
721 $this->mMaxTemplateDepth
= $wgMaxTemplateDepth;
722 $this->mExpensiveParserFunctionLimit
= $wgExpensiveParserFunctionLimit;
723 $this->mCleanSignatures
= $wgCleanSignatures;
724 $this->mExternalLinkTarget
= $wgExternalLinkTarget;
725 $this->mDisableContentConversion
= $wgDisableLangConversion;
726 $this->mDisableTitleConversion
= $wgDisableLangConversion ||
$wgDisableTitleConversion;
727 $this->mMagicISBNLinks
= $wgEnableMagicLinks['ISBN'];
728 $this->mMagicPMIDLinks
= $wgEnableMagicLinks['PMID'];
729 $this->mMagicRFCLinks
= $wgEnableMagicLinks['RFC'];
731 $this->mUser
= $user;
732 $this->mNumberHeadings
= $user->getOption( 'numberheadings' );
733 $this->mThumbSize
= $user->getOption( 'thumbsize' );
734 $this->mStubThreshold
= $user->getStubThreshold();
735 $this->mUserLang
= $lang;
739 * Check if these options match that of another options set
741 * This ignores report limit settings that only affect HTML comments
743 * @param ParserOptions $other
747 public function matches( ParserOptions
$other ) {
748 $fields = array_keys( get_class_vars( __CLASS__
) );
749 $fields = array_diff( $fields, [
750 'mEnableLimitReport', // only effects HTML comments
751 'onAccessCallback', // only used for ParserOutput option tracking
753 foreach ( $fields as $field ) {
754 if ( !is_object( $this->$field ) && $this->$field !== $other->$field ) {
758 // Check the object and lazy-loaded options
760 $this->mUserLang
->equals( $other->mUserLang
) &&
761 $this->getDateFormat() === $other->getDateFormat()
766 * Registers a callback for tracking which ParserOptions which are used.
767 * This is a private API with the parser.
768 * @param callable $callback
770 public function registerWatcher( $callback ) {
771 $this->onAccessCallback
= $callback;
775 * Called when an option is accessed.
776 * Calls the watcher that was set using registerWatcher().
777 * Typically, the watcher callback is ParserOutput::registerOption().
778 * The information registered that way will be used by ParserCache::save().
780 * @param string $optionName Name of the option
782 public function optionUsed( $optionName ) {
783 if ( $this->onAccessCallback
) {
784 call_user_func( $this->onAccessCallback
, $optionName );
789 * Returns the full array of options that would have been used by
791 * Used to get the old parser cache entries when available.
794 public static function legacyOptions() {
806 * Generate a hash string with the values set on these ParserOptions
807 * for the keys given in the array.
808 * This will be used as part of the hash key for the parser cache,
809 * so users sharing the options with vary for the same page share
810 * the same cached data safely.
812 * Extensions which require it should install 'PageRenderingHash' hook,
813 * which will give them a chance to modify this key based on their own
817 * @param array $forOptions
818 * @param Title $title Used to get the content language of the page (since r97636)
819 * @return string Page rendering hash
821 public function optionsHash( $forOptions, $title = null ) {
822 global $wgRenderHashAppend;
824 // FIXME: Once the cache key is reorganized this argument
825 // can be dropped. It was used when the math extension was
829 // Space assigned for the stubthreshold but unused
830 // since it disables the parser cache, its value will always
831 // be 0 when this function is called by parsercache.
832 if ( in_array( 'stubthreshold', $forOptions ) ) {
833 $confstr .= '!' . $this->mStubThreshold
;
838 if ( in_array( 'dateformat', $forOptions ) ) {
839 $confstr .= '!' . $this->getDateFormat();
842 if ( in_array( 'numberheadings', $forOptions ) ) {
843 $confstr .= '!' . ( $this->mNumberHeadings ?
'1' : '' );
848 if ( in_array( 'userlang', $forOptions ) ) {
849 $confstr .= '!' . $this->mUserLang
->getCode();
854 if ( in_array( 'thumbsize', $forOptions ) ) {
855 $confstr .= '!' . $this->mThumbSize
;
860 // add in language specific options, if any
861 // @todo FIXME: This is just a way of retrieving the url/user preferred variant
862 if ( !is_null( $title ) ) {
863 $confstr .= $title->getPageLanguage()->getExtraHashOptions();
866 $confstr .= $wgContLang->getExtraHashOptions();
869 $confstr .= $wgRenderHashAppend;
871 // @note: as of Feb 2015, core never sets the editsection flag, since it uses
872 // <mw:editsection> tags to inject editsections on the fly. However, extensions
873 // may be using it by calling ParserOption::optionUsed resp. ParserOutput::registerOption
874 // directly. At least Wikibase does at this point in time.
875 if ( !in_array( 'editsection', $forOptions ) ) {
877 } elseif ( !$this->mEditSection
) {
878 $confstr .= '!edit=0';
881 if ( $this->mIsPrintable
&& in_array( 'printable', $forOptions ) ) {
882 $confstr .= '!printable=1';
885 if ( $this->mExtraKey
!= '' ) {
886 $confstr .= $this->mExtraKey
;
889 // Give a chance for extensions to modify the hash, if they have
890 // extra options or other effects on the parser cache.
891 Hooks
::run( 'PageRenderingHash', [ &$confstr, $this->getUser(), &$forOptions ] );
893 // Make it a valid memcached key fragment
894 $confstr = str_replace( ' ', '_', $confstr );
900 * Sets a hook to force that a page exists, and sets a current revision callback to return
901 * a revision with custom content when the current revision of the page is requested.
904 * @param Title $title
905 * @param Content $content
906 * @param User $user The user that the fake revision is attributed to
907 * @return ScopedCallback to unset the hook
909 public function setupFakeRevision( $title, $content, $user ) {
910 $oldCallback = $this->setCurrentRevisionCallback(
912 $titleToCheck, $parser = false ) use ( $title, $content, $user, &$oldCallback
914 if ( $titleToCheck->equals( $title ) ) {
915 return new Revision( [
916 'page' => $title->getArticleID(),
917 'user_text' => $user->getName(),
918 'user' => $user->getId(),
919 'parent_id' => $title->getLatestRevID(),
921 'content' => $content
924 return call_user_func( $oldCallback, $titleToCheck, $parser );
930 $wgHooks['TitleExists'][] =
931 function ( $titleToCheck, &$exists ) use ( $title ) {
932 if ( $titleToCheck->equals( $title ) ) {
936 end( $wgHooks['TitleExists'] );
937 $key = key( $wgHooks['TitleExists'] );
938 LinkCache
::singleton()->clearBadLink( $title->getPrefixedDBkey() );
939 return new ScopedCallback( function () use ( $title, $key ) {
941 unset( $wgHooks['TitleExists'][$key] );
942 LinkCache
::singleton()->clearLink( $title );