4 * Set options of the Parser
9 # All variables are supposed to be private in theory, although in practise this is not the case.
10 var $mUseDynamicDates; # Use DateFormatter to format dates
11 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
12 var $mAllowExternalImages; # Allow external images inline
13 var $mAllowExternalImagesFrom; # If not, any exception?
14 var $mEnableImageWhitelist; # If not or it doesn't match, should we check an on-wiki whitelist?
15 var $mSkin; # Reference to the preferred skin
16 var $mDateFormat; # Date format index
17 var $mEditSection; # Create "edit section" links
18 var $mNumberHeadings; # Automatically number headings
19 var $mAllowSpecialInclusion; # Allow inclusion of special pages
20 var $mTidy; # Ask for tidy cleanup
21 var $mInterfaceMessage; # Which lang to call for PLURAL and GRAMMAR
22 var $mTargetLanguage; # Overrides above setting with arbitrary language
23 var $mMaxIncludeSize; # Maximum size of template expansions, in bytes
24 var $mMaxPPNodeCount; # Maximum number of nodes touched by PPFrame::expand()
25 var $mMaxPPExpandDepth; # Maximum recursion depth in PPFrame::expand()
26 var $mMaxTemplateDepth; # Maximum recursion depth for templates within templates
27 var $mRemoveComments; # Remove HTML comments. ONLY APPLIES TO PREPROCESS OPERATIONS
28 var $mTemplateCallback; # Callback for template fetching
29 var $mEnableLimitReport; # Enable limit report in an HTML comment on output
30 var $mTimestamp; # Timestamp used for {{CURRENTDAY}} etc.
31 var $mExternalLinkTarget; # Target attribute for external links
32 var $mMath; # User math preference (as integer)
33 var $mUserLang; # Language code of the User language.
34 var $mThumbSize; # Thumb size preferred by the user.
36 var $mUser; # Stored user object, just used to initialise the skin
37 var $mIsPreview; # Parsing the page for a "preview" operation
38 var $mIsSectionPreview; # Parsing the page for a "preview" operation on a single section
39 var $mIsPrintable; # Parsing the printable version of the page
41 var $mExtraKey = ''; # Extra key that should be present in the caching key.
43 protected $accessedOptions;
45 function getUseDynamicDates() { return $this->mUseDynamicDates
; }
46 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
47 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
48 function getAllowExternalImagesFrom() { return $this->mAllowExternalImagesFrom
; }
49 function getEnableImageWhitelist() { return $this->mEnableImageWhitelist
; }
50 function getEditSection() { $this->accessedOptions
['editsection'] = true;
51 return $this->mEditSection
; }
52 function getNumberHeadings() { $this->accessedOptions
['numberheadings'] = true;
53 return $this->mNumberHeadings
; }
54 function getAllowSpecialInclusion() { return $this->mAllowSpecialInclusion
; }
55 function getTidy() { return $this->mTidy
; }
56 function getInterfaceMessage() { return $this->mInterfaceMessage
; }
57 function getTargetLanguage() { return $this->mTargetLanguage
; }
58 function getMaxIncludeSize() { return $this->mMaxIncludeSize
; }
59 function getMaxPPNodeCount() { return $this->mMaxPPNodeCount
; }
60 function getMaxPPExpandDepth() { return $this->mMaxPPExpandDepth
; }
61 function getMaxTemplateDepth() { return $this->mMaxTemplateDepth
; }
62 function getRemoveComments() { return $this->mRemoveComments
; }
63 function getTemplateCallback() { return $this->mTemplateCallback
; }
64 function getEnableLimitReport() { return $this->mEnableLimitReport
; }
65 function getCleanSignatures() { return $this->mCleanSignatures
; }
66 function getExternalLinkTarget() { return $this->mExternalLinkTarget
; }
67 function getMath() { $this->accessedOptions
['math'] = true;
68 return $this->mMath
; }
69 function getThumbSize() { $this->accessedOptions
['thumbsize'] = true;
70 return $this->mThumbSize
; }
72 function getIsPreview() { return $this->mIsPreview
; }
73 function getIsSectionPreview() { return $this->mIsSectionPreview
; }
74 function getIsPrintable() { $this->accessedOptions
['printable'] = true;
75 return $this->mIsPrintable
; }
77 function getSkin( $title = null ) {
78 if ( !isset( $this->mSkin
) ) {
79 $this->mSkin
= $this->mUser
->getSkin( $title );
84 function getDateFormat() {
85 $this->accessedOptions
['dateformat'] = true;
86 if ( !isset( $this->mDateFormat
) ) {
87 $this->mDateFormat
= $this->mUser
->getDatePreference();
89 return $this->mDateFormat
;
92 function getTimestamp() {
93 if ( !isset( $this->mTimestamp
) ) {
94 $this->mTimestamp
= wfTimestampNow();
96 return $this->mTimestamp
;
99 # You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
100 # Using this fragments the cache and is discouraged. Yes, {{int: }} uses this,
101 # producing inconsistent tables (Bug 14404).
102 function getUserLang() {
103 $this->accessedOptions
['userlang'] = true;
104 return $this->mUserLang
;
107 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
108 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
109 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
110 function setAllowExternalImagesFrom( $x ) { return wfSetVar( $this->mAllowExternalImagesFrom
, $x ); }
111 function setEnableImageWhitelist( $x ) { return wfSetVar( $this->mEnableImageWhitelist
, $x ); }
112 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
113 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
114 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
115 function setAllowSpecialInclusion( $x ) { return wfSetVar( $this->mAllowSpecialInclusion
, $x ); }
116 function setTidy( $x ) { return wfSetVar( $this->mTidy
, $x); }
117 function setSkin( $x ) { $this->mSkin
= $x; }
118 function setInterfaceMessage( $x ) { return wfSetVar( $this->mInterfaceMessage
, $x); }
119 function setTargetLanguage( $x ) { return wfSetVar( $this->mTargetLanguage
, $x); }
120 function setMaxIncludeSize( $x ) { return wfSetVar( $this->mMaxIncludeSize
, $x ); }
121 function setMaxPPNodeCount( $x ) { return wfSetVar( $this->mMaxPPNodeCount
, $x ); }
122 function setMaxTemplateDepth( $x ) { return wfSetVar( $this->mMaxTemplateDepth
, $x ); }
123 function setRemoveComments( $x ) { return wfSetVar( $this->mRemoveComments
, $x ); }
124 function setTemplateCallback( $x ) { return wfSetVar( $this->mTemplateCallback
, $x ); }
125 function enableLimitReport( $x = true ) { return wfSetVar( $this->mEnableLimitReport
, $x ); }
126 function setTimestamp( $x ) { return wfSetVar( $this->mTimestamp
, $x ); }
127 function setCleanSignatures( $x ) { return wfSetVar( $this->mCleanSignatures
, $x ); }
128 function setExternalLinkTarget( $x ) { return wfSetVar( $this->mExternalLinkTarget
, $x ); }
129 function setMath( $x ) { return wfSetVar( $this->mMath
, $x ); }
130 function setUserLang( $x ) { return wfSetVar( $this->mUserLang
, $x ); }
131 function setThumbSize() { return wfSetVar( $this->mThumbSize
, $x ); }
133 function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview
, $x ); }
134 function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview
, $x ); }
135 function setIsPrintable( $x ) { return wfSetVar( $this->mIsPrintable
, $x ); }
138 * Extra key that should be present in the parser cache key.
140 function addExtraKey( $key ) {
141 $this->mExtraKey
.= '!' . $key;
144 function __construct( $user = null ) {
145 $this->initialiseFromUser( $user );
151 * @param $user User object
152 * @return ParserOptions object
154 static function newFromUser( $user ) {
155 return new ParserOptions( $user );
158 /** Get user options */
159 function initialiseFromUser( $userInput ) {
160 global $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
161 global $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion, $wgMaxArticleSize;
162 global $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth, $wgCleanSignatures;
163 global $wgExternalLinkTarget, $wgLang;
165 wfProfileIn( __METHOD__
);
169 if ( isset( $wgUser ) ) {
178 $this->mUser
= $user;
180 $this->mUseDynamicDates
= $wgUseDynamicDates;
181 $this->mInterwikiMagic
= $wgInterwikiMagic;
182 $this->mAllowExternalImages
= $wgAllowExternalImages;
183 $this->mAllowExternalImagesFrom
= $wgAllowExternalImagesFrom;
184 $this->mEnableImageWhitelist
= $wgEnableImageWhitelist;
185 $this->mSkin
= null; # Deferred
186 $this->mDateFormat
= null; # Deferred
187 $this->mEditSection
= true;
188 $this->mNumberHeadings
= $user->getOption( 'numberheadings' );
189 $this->mAllowSpecialInclusion
= $wgAllowSpecialInclusion;
190 $this->mTidy
= false;
191 $this->mInterfaceMessage
= false;
192 $this->mTargetLanguage
= null; // default depends on InterfaceMessage setting
193 $this->mMaxIncludeSize
= $wgMaxArticleSize * 1024;
194 $this->mMaxPPNodeCount
= $wgMaxPPNodeCount;
195 $this->mMaxPPExpandDepth
= $wgMaxPPExpandDepth;
196 $this->mMaxTemplateDepth
= $wgMaxTemplateDepth;
197 $this->mRemoveComments
= true;
198 $this->mTemplateCallback
= array( 'Parser', 'statelessFetchTemplate' );
199 $this->mEnableLimitReport
= false;
200 $this->mCleanSignatures
= $wgCleanSignatures;
201 $this->mExternalLinkTarget
= $wgExternalLinkTarget;
202 $this->mMath
= $user->getOption( 'math' );
203 $this->mUserLang
= $wgLang->getCode();
204 $this->mThumbSize
= $user->getOption( 'thumbsize' );
206 $this->mIsPreview
= false;
207 $this->mIsSectionPreview
= false;
208 $this->mIsPrintable
= false;
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->mDateFormat
;
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 );