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; #
39 var $mPreSaveTransform = true; # Transform wiki markup when saving the page.
41 var $mNumberHeadings; # Automatically number headings
42 var $mMath; # User math preference (as integer)
43 var $mThumbSize; # Thumb size preferred by the user.
44 var $mUserLang; # Language code of the User language.
49 var $mUser; # Stored user object
50 var $mIsPreview = false; # Parsing the page for a "preview" operation
51 var $mIsSectionPreview = false; # Parsing the page for a "preview" operation on a single section
52 var $mIsPrintable = false; # Parsing the printable version of the page
54 var $mExtraKey = ''; # Extra key that should be present in the caching key.
56 protected $onAccessCallback = null;
58 function getUseDynamicDates() { return $this->mUseDynamicDates
; }
59 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
60 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
61 function getAllowExternalImagesFrom() { return $this->mAllowExternalImagesFrom
; }
62 function getEnableImageWhitelist() { return $this->mEnableImageWhitelist
; }
63 function getEditSection() { return $this->mEditSection
; }
64 function getNumberHeadings() { $this->optionUsed('numberheadings');
65 return $this->mNumberHeadings
; }
66 function getAllowSpecialInclusion() { return $this->mAllowSpecialInclusion
; }
67 function getTidy() { return $this->mTidy
; }
68 function getInterfaceMessage() { return $this->mInterfaceMessage
; }
69 function getTargetLanguage() { return $this->mTargetLanguage
; }
70 function getMaxIncludeSize() { return $this->mMaxIncludeSize
; }
71 function getMaxPPNodeCount() { return $this->mMaxPPNodeCount
; }
72 function getMaxPPExpandDepth() { return $this->mMaxPPExpandDepth
; }
73 function getMaxTemplateDepth() { return $this->mMaxTemplateDepth
; }
74 function getRemoveComments() { return $this->mRemoveComments
; }
75 function getTemplateCallback() { return $this->mTemplateCallback
; }
76 function getEnableLimitReport() { return $this->mEnableLimitReport
; }
77 function getCleanSignatures() { return $this->mCleanSignatures
; }
78 function getExternalLinkTarget() { return $this->mExternalLinkTarget
; }
79 function getMath() { $this->optionUsed('math');
80 return $this->mMath
; }
81 function getThumbSize() { $this->optionUsed('thumbsize');
82 return $this->mThumbSize
; }
84 function getIsPreview() { return $this->mIsPreview
; }
85 function getIsSectionPreview() { return $this->mIsSectionPreview
; }
86 function getIsPrintable() { $this->optionUsed('printable');
87 return $this->mIsPrintable
; }
88 function getUser() { return $this->mUser
; }
89 function getPreSaveTransform() { return $this->mPreSaveTransform
; }
91 function getSkin( $title = null ) {
92 if ( !isset( $this->mSkin
) ) {
93 $this->mSkin
= $this->mUser
->getSkin( $title );
98 function getDateFormat() {
99 $this->optionUsed('dateformat');
100 if ( !isset( $this->mDateFormat
) ) {
101 $this->mDateFormat
= $this->mUser
->getDatePreference();
103 return $this->mDateFormat
;
106 function getTimestamp() {
107 if ( !isset( $this->mTimestamp
) ) {
108 $this->mTimestamp
= wfTimestampNow();
110 return $this->mTimestamp
;
114 * You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
115 * Using this fragments the cache and is discouraged. Yes, {{int: }} uses this,
116 * producing inconsistent tables (Bug 14404).
118 function getUserLang() {
119 $this->optionUsed('userlang');
120 return $this->mUserLang
;
123 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
124 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
125 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
126 function setAllowExternalImagesFrom( $x ) { return wfSetVar( $this->mAllowExternalImagesFrom
, $x ); }
127 function setEnableImageWhitelist( $x ) { return wfSetVar( $this->mEnableImageWhitelist
, $x ); }
128 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
129 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
130 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
131 function setAllowSpecialInclusion( $x ) { return wfSetVar( $this->mAllowSpecialInclusion
, $x ); }
132 function setTidy( $x ) { return wfSetVar( $this->mTidy
, $x); }
133 function setSkin( $x ) { $this->mSkin
= $x; }
134 function setInterfaceMessage( $x ) { return wfSetVar( $this->mInterfaceMessage
, $x); }
135 function setTargetLanguage( $x ) { return wfSetVar( $this->mTargetLanguage
, $x); }
136 function setMaxIncludeSize( $x ) { return wfSetVar( $this->mMaxIncludeSize
, $x ); }
137 function setMaxPPNodeCount( $x ) { return wfSetVar( $this->mMaxPPNodeCount
, $x ); }
138 function setMaxTemplateDepth( $x ) { return wfSetVar( $this->mMaxTemplateDepth
, $x ); }
139 function setRemoveComments( $x ) { return wfSetVar( $this->mRemoveComments
, $x ); }
140 function setTemplateCallback( $x ) { return wfSetVar( $this->mTemplateCallback
, $x ); }
141 function enableLimitReport( $x = true ) { return wfSetVar( $this->mEnableLimitReport
, $x ); }
142 function setTimestamp( $x ) { return wfSetVar( $this->mTimestamp
, $x ); }
143 function setCleanSignatures( $x ) { return wfSetVar( $this->mCleanSignatures
, $x ); }
144 function setExternalLinkTarget( $x ) { return wfSetVar( $this->mExternalLinkTarget
, $x ); }
145 function setMath( $x ) { return wfSetVar( $this->mMath
, $x ); }
146 function setUserLang( $x ) { return wfSetVar( $this->mUserLang
, $x ); }
147 function setThumbSize( $x ) { return wfSetVar( $this->mThumbSize
, $x ); }
148 function setPreSaveTransform( $x ) { return wfSetVar( $this->mPreSaveTransform
, $x ); }
150 function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview
, $x ); }
151 function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview
, $x ); }
152 function setIsPrintable( $x ) { return wfSetVar( $this->mIsPrintable
, $x ); }
155 * Extra key that should be present in the parser cache key.
157 function addExtraKey( $key ) {
158 $this->mExtraKey
.= '!' . $key;
161 function __construct( $user = null ) {
162 $this->initialiseFromUser( $user );
168 * @param $user User object
169 * @return ParserOptions object
171 static function newFromUser( $user ) {
172 return new ParserOptions( $user );
175 /** Get user options */
176 function initialiseFromUser( $userInput ) {
177 global $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
178 global $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion, $wgMaxArticleSize;
179 global $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth, $wgCleanSignatures;
180 global $wgExternalLinkTarget, $wgLang;
182 wfProfileIn( __METHOD__
);
186 if ( isset( $wgUser ) ) {
195 $this->mUser
= $user;
197 $this->mUseDynamicDates
= $wgUseDynamicDates;
198 $this->mInterwikiMagic
= $wgInterwikiMagic;
199 $this->mAllowExternalImages
= $wgAllowExternalImages;
200 $this->mAllowExternalImagesFrom
= $wgAllowExternalImagesFrom;
201 $this->mEnableImageWhitelist
= $wgEnableImageWhitelist;
202 $this->mAllowSpecialInclusion
= $wgAllowSpecialInclusion;
203 $this->mMaxIncludeSize
= $wgMaxArticleSize * 1024;
204 $this->mMaxPPNodeCount
= $wgMaxPPNodeCount;
205 $this->mMaxPPExpandDepth
= $wgMaxPPExpandDepth;
206 $this->mMaxTemplateDepth
= $wgMaxTemplateDepth;
207 $this->mCleanSignatures
= $wgCleanSignatures;
208 $this->mExternalLinkTarget
= $wgExternalLinkTarget;
210 $this->mNumberHeadings
= $user->getOption( 'numberheadings' );
211 $this->mMath
= $user->getOption( 'math' );
212 $this->mThumbSize
= $user->getOption( 'thumbsize' );
213 $this->mUserLang
= $wgLang->getCode();
215 wfProfileOut( __METHOD__
);
219 * Registers a callback for tracking which ParserOptions which are used.
220 * This is a private API with the parser.
222 function registerWatcher( $callback ) {
223 $this->onAccessCallback
= $callback;
227 * Called when an option is accessed.
229 protected function optionUsed( $optionName ) {
230 if ( $this->onAccessCallback
) {
231 call_user_func( $this->onAccessCallback
, $optionName );
236 * Returns the full array of options that would have been used by
238 * Used to get the old parser cache entries when available.
240 public static function legacyOptions() {
241 global $wgUseDynamicDates;
242 $legacyOpts = array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
243 if ( $wgUseDynamicDates ) {
244 $legacyOpts[] = 'dateformat';
250 * Generate a hash string with the values set on these ParserOptions
251 * for the keys given in the array.
252 * This will be used as part of the hash key for the parser cache,
253 * so users sharign the options with vary for the same page share
254 * the same cached data safely.
256 * Replaces User::getPageRenderingHash()
258 * Extensions which require it should install 'PageRenderingHash' hook,
259 * which will give them a chance to modify this key based on their own
263 * @return \string Page rendering hash
265 public function optionsHash( $forOptions ) {
266 global $wgContLang, $wgRenderHashAppend;
270 if ( in_array( 'math', $forOptions ) )
271 $confstr .= $this->mMath
;
276 // Space assigned for the stubthreshold but unused
277 // since it disables the parser cache, its value will always
278 // be 0 when this function is called by parsercache.
279 // The conditional is here to avoid a confusing 0
280 if ( true ||
in_array( 'stubthreshold', $forOptions ) )
285 if ( in_array( 'dateformat', $forOptions ) )
286 $confstr .= '!' . $this->getDateFormat();
288 if ( in_array( 'numberheadings', $forOptions ) )
289 $confstr .= '!' . ( $this->mNumberHeadings ?
'1' : '' );
293 if ( in_array( 'userlang', $forOptions ) )
294 $confstr .= '!' . $this->mUserLang
;
298 if ( in_array( 'thumbsize', $forOptions ) )
299 $confstr .= '!' . $this->mThumbSize
;
303 // add in language specific options, if any
304 // FIXME: This is just a way of retrieving the url/user preferred variant
305 $confstr .= $wgContLang->getExtraHashOptions();
307 // Since the skin could be overloading link(), it should be
308 // included here but in practice, none of our skins do that.
309 // $confstr .= "!" . $this->mSkin->getSkinName();
311 $confstr .= $wgRenderHashAppend;
313 if ( !in_array( 'editsection', $forOptions ) ) {
315 } elseif ( !$this->mEditSection
) {
316 $confstr .= '!edit=0';
319 if ( $this->mIsPrintable
&& in_array( 'printable', $forOptions ) )
320 $confstr .= '!printable=1';
322 if ( $this->mExtraKey
!= '' )
323 $confstr .= $this->mExtraKey
;
325 // Give a chance for extensions to modify the hash, if they have
326 // extra options or other effects on the parser cache.
327 wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
329 // Make it a valid memcached key fragment
330 $confstr = str_replace( ' ', '_', $confstr );