3 * Options for the PHP parser
10 * Set options of the Parser
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; #
40 var $mNumberHeadings; # Automatically number headings
41 var $mMath; # User math preference (as integer)
42 var $mThumbSize; # Thumb size preferred by the user.
43 var $mUserLang; # Language code of the User language.
45 var $mUser; # Stored user object
46 var $mIsPreview = false; # Parsing the page for a "preview" operation
47 var $mIsSectionPreview = false; # Parsing the page for a "preview" operation on a single section
48 var $mIsPrintable = false; # Parsing the printable version of the page
50 var $mExtraKey = ''; # Extra key that should be present in the caching key.
52 protected $accessedOptions;
54 function getUseDynamicDates() { return $this->mUseDynamicDates
; }
55 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
56 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
57 function getAllowExternalImagesFrom() { return $this->mAllowExternalImagesFrom
; }
58 function getEnableImageWhitelist() { return $this->mEnableImageWhitelist
; }
59 function getEditSection() { $this->accessedOptions
['editsection'] = true;
60 return $this->mEditSection
; }
61 function getNumberHeadings() { $this->accessedOptions
['numberheadings'] = true;
62 return $this->mNumberHeadings
; }
63 function getAllowSpecialInclusion() { return $this->mAllowSpecialInclusion
; }
64 function getTidy() { return $this->mTidy
; }
65 function getInterfaceMessage() { return $this->mInterfaceMessage
; }
66 function getTargetLanguage() { return $this->mTargetLanguage
; }
67 function getMaxIncludeSize() { return $this->mMaxIncludeSize
; }
68 function getMaxPPNodeCount() { return $this->mMaxPPNodeCount
; }
69 function getMaxPPExpandDepth() { return $this->mMaxPPExpandDepth
; }
70 function getMaxTemplateDepth() { return $this->mMaxTemplateDepth
; }
71 function getRemoveComments() { return $this->mRemoveComments
; }
72 function getTemplateCallback() { return $this->mTemplateCallback
; }
73 function getEnableLimitReport() { return $this->mEnableLimitReport
; }
74 function getCleanSignatures() { return $this->mCleanSignatures
; }
75 function getExternalLinkTarget() { return $this->mExternalLinkTarget
; }
76 function getMath() { $this->accessedOptions
['math'] = true;
77 return $this->mMath
; }
78 function getThumbSize() { $this->accessedOptions
['thumbsize'] = true;
79 return $this->mThumbSize
; }
81 function getIsPreview() { return $this->mIsPreview
; }
82 function getIsSectionPreview() { return $this->mIsSectionPreview
; }
83 function getIsPrintable() { $this->accessedOptions
['printable'] = true;
84 return $this->mIsPrintable
; }
85 function getUser() { return $this->mUser
; }
87 function getSkin( $title = null ) {
88 if ( !isset( $this->mSkin
) ) {
89 $this->mSkin
= $this->mUser
->getSkin( $title );
94 function getDateFormat() {
95 $this->accessedOptions
['dateformat'] = true;
96 if ( !isset( $this->mDateFormat
) ) {
97 $this->mDateFormat
= $this->mUser
->getDatePreference();
99 return $this->mDateFormat
;
102 function getTimestamp() {
103 if ( !isset( $this->mTimestamp
) ) {
104 $this->mTimestamp
= wfTimestampNow();
106 return $this->mTimestamp
;
110 * You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
111 * Using this fragments the cache and is discouraged. Yes, {{int: }} uses this,
112 * producing inconsistent tables (Bug 14404).
114 function getUserLang() {
115 $this->accessedOptions
['userlang'] = true;
116 return $this->mUserLang
;
119 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
120 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
121 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
122 function setAllowExternalImagesFrom( $x ) { return wfSetVar( $this->mAllowExternalImagesFrom
, $x ); }
123 function setEnableImageWhitelist( $x ) { return wfSetVar( $this->mEnableImageWhitelist
, $x ); }
124 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
125 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
126 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
127 function setAllowSpecialInclusion( $x ) { return wfSetVar( $this->mAllowSpecialInclusion
, $x ); }
128 function setTidy( $x ) { return wfSetVar( $this->mTidy
, $x); }
129 function setSkin( $x ) { $this->mSkin
= $x; }
130 function setInterfaceMessage( $x ) { return wfSetVar( $this->mInterfaceMessage
, $x); }
131 function setTargetLanguage( $x ) { return wfSetVar( $this->mTargetLanguage
, $x); }
132 function setMaxIncludeSize( $x ) { return wfSetVar( $this->mMaxIncludeSize
, $x ); }
133 function setMaxPPNodeCount( $x ) { return wfSetVar( $this->mMaxPPNodeCount
, $x ); }
134 function setMaxTemplateDepth( $x ) { return wfSetVar( $this->mMaxTemplateDepth
, $x ); }
135 function setRemoveComments( $x ) { return wfSetVar( $this->mRemoveComments
, $x ); }
136 function setTemplateCallback( $x ) { return wfSetVar( $this->mTemplateCallback
, $x ); }
137 function enableLimitReport( $x = true ) { return wfSetVar( $this->mEnableLimitReport
, $x ); }
138 function setTimestamp( $x ) { return wfSetVar( $this->mTimestamp
, $x ); }
139 function setCleanSignatures( $x ) { return wfSetVar( $this->mCleanSignatures
, $x ); }
140 function setExternalLinkTarget( $x ) { return wfSetVar( $this->mExternalLinkTarget
, $x ); }
141 function setMath( $x ) { return wfSetVar( $this->mMath
, $x ); }
142 function setUserLang( $x ) { return wfSetVar( $this->mUserLang
, $x ); }
143 function setThumbSize( $x ) { return wfSetVar( $this->mThumbSize
, $x ); }
145 function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview
, $x ); }
146 function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview
, $x ); }
147 function setIsPrintable( $x ) { return wfSetVar( $this->mIsPrintable
, $x ); }
150 * Extra key that should be present in the parser cache key.
152 function addExtraKey( $key ) {
153 $this->mExtraKey
.= '!' . $key;
156 function __construct( $user = null ) {
157 $this->initialiseFromUser( $user );
163 * @param $user User object
164 * @return ParserOptions object
166 static function newFromUser( $user ) {
167 return new ParserOptions( $user );
170 /** Get user options */
171 function initialiseFromUser( $userInput ) {
172 global $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
173 global $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion, $wgMaxArticleSize;
174 global $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth, $wgCleanSignatures;
175 global $wgExternalLinkTarget, $wgLang;
177 wfProfileIn( __METHOD__
);
181 if ( isset( $wgUser ) ) {
190 $this->mUser
= $user;
192 $this->mUseDynamicDates
= $wgUseDynamicDates;
193 $this->mInterwikiMagic
= $wgInterwikiMagic;
194 $this->mAllowExternalImages
= $wgAllowExternalImages;
195 $this->mAllowExternalImagesFrom
= $wgAllowExternalImagesFrom;
196 $this->mEnableImageWhitelist
= $wgEnableImageWhitelist;
197 $this->mAllowSpecialInclusion
= $wgAllowSpecialInclusion;
198 $this->mMaxIncludeSize
= $wgMaxArticleSize * 1024;
199 $this->mMaxPPNodeCount
= $wgMaxPPNodeCount;
200 $this->mMaxPPExpandDepth
= $wgMaxPPExpandDepth;
201 $this->mMaxTemplateDepth
= $wgMaxTemplateDepth;
202 $this->mCleanSignatures
= $wgCleanSignatures;
203 $this->mExternalLinkTarget
= $wgExternalLinkTarget;
205 $this->mNumberHeadings
= $user->getOption( 'numberheadings' );
206 $this->mMath
= $user->getOption( 'math' );
207 $this->mThumbSize
= $user->getOption( 'thumbsize' );
208 $this->mUserLang
= $wgLang->getCode();
210 wfProfileOut( __METHOD__
);
214 * Returns the options from this ParserOptions which have been used.
216 public function usedOptions() {
217 return array_keys( $this->accessedOptions
);
221 * Resets the memory of options usage.
223 public function resetUsage() {
224 $this->accessedOptions
= array();
228 * Returns the full array of options that would have been used by
230 * Used to get the old parser cache entries when available.
232 public static function legacyOptions() {
233 global $wgUseDynamicDates;
234 $legacyOpts = array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
235 if ( $wgUseDynamicDates ) {
236 $legacyOpts[] = 'dateformat';
242 * Generate a hash string with the values set on these ParserOptions
243 * for the keys given in the array.
244 * This will be used as part of the hash key for the parser cache,
245 * so users sharign the options with vary for the same page share
246 * the same cached data safely.
248 * Replaces User::getPageRenderingHash()
250 * Extensions which require it should install 'PageRenderingHash' hook,
251 * which will give them a chance to modify this key based on their own
255 * @return \string Page rendering hash
257 public function optionsHash( $forOptions ) {
258 global $wgContLang, $wgRenderHashAppend;
262 if ( in_array( 'math', $forOptions ) )
263 $confstr .= $this->mMath
;
268 // Space assigned for the stubthreshold but unused
269 // since it disables the parser cache, its value will always
270 // be 0 when this function is called by parsercache.
271 // The conditional is here to avoid a confusing 0
272 if ( in_array( 'stubthreshold', $forOptions ) )
277 if ( in_array( 'dateformat', $forOptions ) )
278 $confstr .= '!' . $this->getDateFormat();
280 if ( in_array( 'numberheadings', $forOptions ) )
281 $confstr .= '!' . ( $this->mNumberHeadings ?
'1' : '' );
285 if ( in_array( 'userlang', $forOptions ) )
286 $confstr .= '!' . $this->mUserLang
;
290 if ( in_array( 'thumbsize', $forOptions ) )
291 $confstr .= '!' . $this->mThumbSize
;
295 // add in language specific options, if any
296 // FIXME: This is just a way of retrieving the url/user preferred variant
297 $confstr .= $wgContLang->getExtraHashOptions();
299 // Since the skin could be overloading link(), it should be
300 // included here but in practice, none of our skins do that.
301 // $confstr .= "!" . $this->mSkin->getSkinName();
303 $confstr .= $wgRenderHashAppend;
305 if ( !$this->mEditSection
&& in_array( 'editsection', $forOptions ) )
306 $confstr .= '!edit=0';
307 if ( $this->mIsPrintable
&& in_array( 'printable', $forOptions ) )
308 $confstr .= '!printable=1';
310 if ( $this->mExtraKey
!= '' )
311 $confstr .= $this->mExtraKey
;
313 // Give a chance for extensions to modify the hash, if they have
314 // extra options or other effects on the parser cache.
315 wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
317 // Make it a valid memcached key fragment
318 $confstr = str_replace( ' ', '_', $confstr );