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; # Reference to the preferred skin
22 var $mDateFormat; # Date format index
23 var $mEditSection; # Create "edit section" links
24 var $mNumberHeadings; # Automatically number headings
25 var $mAllowSpecialInclusion; # Allow inclusion of special pages
26 var $mTidy; # Ask for tidy cleanup
27 var $mInterfaceMessage; # Which lang to call for PLURAL and GRAMMAR
28 var $mTargetLanguage; # Overrides above setting with arbitrary language
29 var $mMaxIncludeSize; # Maximum size of template expansions, in bytes
30 var $mMaxPPNodeCount; # Maximum number of nodes touched by PPFrame::expand()
31 var $mMaxPPExpandDepth; # Maximum recursion depth in PPFrame::expand()
32 var $mMaxTemplateDepth; # Maximum recursion depth for templates within templates
33 var $mRemoveComments; # Remove HTML comments. ONLY APPLIES TO PREPROCESS OPERATIONS
34 var $mTemplateCallback; # Callback for template fetching
35 var $mEnableLimitReport; # 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 $mMath; # User math preference (as integer)
39 var $mUserLang; # Language code of the User language.
40 var $mThumbSize; # Thumb size preferred by the user.
41 var $mCleanSignatures; #
43 var $mUser; # Stored user object, just used to initialise the skin
44 var $mIsPreview; # Parsing the page for a "preview" operation
45 var $mIsSectionPreview; # Parsing the page for a "preview" operation on a single section
46 var $mIsPrintable; # Parsing the printable version of the page
48 var $mExtraKey = ''; # Extra key that should be present in the caching key.
50 protected $accessedOptions;
52 function getUseDynamicDates() { return $this->mUseDynamicDates
; }
53 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
54 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
55 function getAllowExternalImagesFrom() { return $this->mAllowExternalImagesFrom
; }
56 function getEnableImageWhitelist() { return $this->mEnableImageWhitelist
; }
57 function getEditSection() { $this->accessedOptions
['editsection'] = true;
58 return $this->mEditSection
; }
59 function getNumberHeadings() { $this->accessedOptions
['numberheadings'] = true;
60 return $this->mNumberHeadings
; }
61 function getAllowSpecialInclusion() { return $this->mAllowSpecialInclusion
; }
62 function getTidy() { return $this->mTidy
; }
63 function getInterfaceMessage() { return $this->mInterfaceMessage
; }
64 function getTargetLanguage() { return $this->mTargetLanguage
; }
65 function getMaxIncludeSize() { return $this->mMaxIncludeSize
; }
66 function getMaxPPNodeCount() { return $this->mMaxPPNodeCount
; }
67 function getMaxPPExpandDepth() { return $this->mMaxPPExpandDepth
; }
68 function getMaxTemplateDepth() { return $this->mMaxTemplateDepth
; }
69 function getRemoveComments() { return $this->mRemoveComments
; }
70 function getTemplateCallback() { return $this->mTemplateCallback
; }
71 function getEnableLimitReport() { return $this->mEnableLimitReport
; }
72 function getCleanSignatures() { return $this->mCleanSignatures
; }
73 function getExternalLinkTarget() { return $this->mExternalLinkTarget
; }
74 function getMath() { $this->accessedOptions
['math'] = true;
75 return $this->mMath
; }
76 function getThumbSize() { $this->accessedOptions
['thumbsize'] = true;
77 return $this->mThumbSize
; }
79 function getIsPreview() { return $this->mIsPreview
; }
80 function getIsSectionPreview() { return $this->mIsSectionPreview
; }
81 function getIsPrintable() { $this->accessedOptions
['printable'] = true;
82 return $this->mIsPrintable
; }
84 function getSkin( $title = null ) {
85 if ( !isset( $this->mSkin
) ) {
86 $this->mSkin
= $this->mUser
->getSkin( $title );
91 function getDateFormat() {
92 $this->accessedOptions
['dateformat'] = true;
93 if ( !isset( $this->mDateFormat
) ) {
94 $this->mDateFormat
= $this->mUser
->getDatePreference();
96 return $this->mDateFormat
;
99 function getTimestamp() {
100 if ( !isset( $this->mTimestamp
) ) {
101 $this->mTimestamp
= wfTimestampNow();
103 return $this->mTimestamp
;
107 * You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
108 * Using this fragments the cache and is discouraged. Yes, {{int: }} uses this,
109 * producing inconsistent tables (Bug 14404).
111 function getUserLang() {
112 $this->accessedOptions
['userlang'] = true;
113 return $this->mUserLang
;
116 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
117 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
118 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
119 function setAllowExternalImagesFrom( $x ) { return wfSetVar( $this->mAllowExternalImagesFrom
, $x ); }
120 function setEnableImageWhitelist( $x ) { return wfSetVar( $this->mEnableImageWhitelist
, $x ); }
121 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
122 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
123 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
124 function setAllowSpecialInclusion( $x ) { return wfSetVar( $this->mAllowSpecialInclusion
, $x ); }
125 function setTidy( $x ) { return wfSetVar( $this->mTidy
, $x); }
126 function setSkin( $x ) { $this->mSkin
= $x; }
127 function setInterfaceMessage( $x ) { return wfSetVar( $this->mInterfaceMessage
, $x); }
128 function setTargetLanguage( $x ) { return wfSetVar( $this->mTargetLanguage
, $x); }
129 function setMaxIncludeSize( $x ) { return wfSetVar( $this->mMaxIncludeSize
, $x ); }
130 function setMaxPPNodeCount( $x ) { return wfSetVar( $this->mMaxPPNodeCount
, $x ); }
131 function setMaxTemplateDepth( $x ) { return wfSetVar( $this->mMaxTemplateDepth
, $x ); }
132 function setRemoveComments( $x ) { return wfSetVar( $this->mRemoveComments
, $x ); }
133 function setTemplateCallback( $x ) { return wfSetVar( $this->mTemplateCallback
, $x ); }
134 function enableLimitReport( $x = true ) { return wfSetVar( $this->mEnableLimitReport
, $x ); }
135 function setTimestamp( $x ) { return wfSetVar( $this->mTimestamp
, $x ); }
136 function setCleanSignatures( $x ) { return wfSetVar( $this->mCleanSignatures
, $x ); }
137 function setExternalLinkTarget( $x ) { return wfSetVar( $this->mExternalLinkTarget
, $x ); }
138 function setMath( $x ) { return wfSetVar( $this->mMath
, $x ); }
139 function setUserLang( $x ) { return wfSetVar( $this->mUserLang
, $x ); }
140 function setThumbSize( $x ) { return wfSetVar( $this->mThumbSize
, $x ); }
142 function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview
, $x ); }
143 function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview
, $x ); }
144 function setIsPrintable( $x ) { return wfSetVar( $this->mIsPrintable
, $x ); }
147 * Extra key that should be present in the parser cache key.
149 function addExtraKey( $key ) {
150 $this->mExtraKey
.= '!' . $key;
153 function __construct( $user = null ) {
154 $this->initialiseFromUser( $user );
160 * @param $user User object
161 * @return ParserOptions object
163 static function newFromUser( $user ) {
164 return new ParserOptions( $user );
167 /** Get user options */
168 function initialiseFromUser( $userInput ) {
169 global $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
170 global $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion, $wgMaxArticleSize;
171 global $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth, $wgCleanSignatures;
172 global $wgExternalLinkTarget, $wgLang;
174 wfProfileIn( __METHOD__
);
178 if ( isset( $wgUser ) ) {
187 $this->mUser
= $user;
189 $this->mUseDynamicDates
= $wgUseDynamicDates;
190 $this->mInterwikiMagic
= $wgInterwikiMagic;
191 $this->mAllowExternalImages
= $wgAllowExternalImages;
192 $this->mAllowExternalImagesFrom
= $wgAllowExternalImagesFrom;
193 $this->mEnableImageWhitelist
= $wgEnableImageWhitelist;
194 $this->mSkin
= null; # Deferred
195 $this->mDateFormat
= null; # Deferred
196 $this->mEditSection
= true;
197 $this->mNumberHeadings
= $user->getOption( 'numberheadings' );
198 $this->mAllowSpecialInclusion
= $wgAllowSpecialInclusion;
199 $this->mTidy
= false;
200 $this->mInterfaceMessage
= false;
201 $this->mTargetLanguage
= null; // default depends on InterfaceMessage setting
202 $this->mMaxIncludeSize
= $wgMaxArticleSize * 1024;
203 $this->mMaxPPNodeCount
= $wgMaxPPNodeCount;
204 $this->mMaxPPExpandDepth
= $wgMaxPPExpandDepth;
205 $this->mMaxTemplateDepth
= $wgMaxTemplateDepth;
206 $this->mRemoveComments
= true;
207 $this->mTemplateCallback
= array( 'Parser', 'statelessFetchTemplate' );
208 $this->mEnableLimitReport
= false;
209 $this->mCleanSignatures
= $wgCleanSignatures;
210 $this->mExternalLinkTarget
= $wgExternalLinkTarget;
211 $this->mMath
= $user->getOption( 'math' );
212 $this->mUserLang
= $wgLang->getCode();
213 $this->mThumbSize
= $user->getOption( 'thumbsize' );
215 $this->mIsPreview
= false;
216 $this->mIsSectionPreview
= false;
217 $this->mIsPrintable
= false;
219 wfProfileOut( __METHOD__
);
223 * Returns the options from this ParserOptions which have been used.
225 public function usedOptions() {
226 return array_keys( $this->accessedOptions
);
230 * Resets the memory of options usage.
232 public function resetUsage() {
233 $this->accessedOptions
= array();
237 * Returns the full array of options that would have been used by
239 * Used to get the old parser cache entries when available.
241 public static function legacyOptions() {
242 global $wgUseDynamicDates;
243 $legacyOpts = array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
244 if ( $wgUseDynamicDates ) {
245 $legacyOpts[] = 'dateformat';
251 * Generate a hash string with the values set on these ParserOptions
252 * for the keys given in the array.
253 * This will be used as part of the hash key for the parser cache,
254 * so users sharign the options with vary for the same page share
255 * the same cached data safely.
257 * Replaces User::getPageRenderingHash()
259 * Extensions which require it should install 'PageRenderingHash' hook,
260 * which will give them a chance to modify this key based on their own
264 * @return \string Page rendering hash
266 public function optionsHash( $forOptions ) {
267 global $wgContLang, $wgRenderHashAppend;
271 if ( in_array( 'math', $forOptions ) )
272 $confstr .= $this->mMath
;
277 // Space assigned for the stubthreshold but unused
278 // since it disables the parser cache, its value will always
279 // be 0 when this function is called by parsercache.
280 // The conditional is here to avoid a confusing 0
281 if ( in_array( 'stubthreshold', $forOptions ) )
286 if ( in_array( 'dateformat', $forOptions ) )
287 $confstr .= '!' . $this->getDateFormat();
289 if ( in_array( 'numberheadings', $forOptions ) )
290 $confstr .= '!' . ( $this->mNumberHeadings ?
'1' : '' );
294 if ( in_array( 'userlang', $forOptions ) )
295 $confstr .= '!' . $this->mUserLang
;
299 if ( in_array( 'thumbsize', $forOptions ) )
300 $confstr .= '!' . $this->mThumbSize
;
304 // add in language specific options, if any
305 // FIXME: This is just a way of retrieving the url/user preferred variant
306 $confstr .= $wgContLang->getExtraHashOptions();
308 // Since the skin could be overloading link(), it should be
309 // included here but in practice, none of our skins do that.
310 // $confstr .= "!" . $this->mSkin->getSkinName();
312 $confstr .= $wgRenderHashAppend;
314 if ( !$this->mEditSection
&& in_array( 'editsection', $forOptions ) )
315 $confstr .= '!edit=0';
316 if ( $this->mIsPrintable
&& in_array( 'printable', $forOptions ) )
317 $confstr .= '!printable=1';
319 if ( $this->mExtraKey
!= '' )
320 $confstr .= $this->mExtraKey
;
322 // Give a chance for extensions to modify the hash, if they have
323 // extra options or other effects on the parser cache.
324 wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
326 // Make it a valid memcached key fragment
327 $confstr = str_replace( ' ', '_', $confstr );