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 $mDateFormat = null; # Date format index
22 var $mEditSection = true; # Create "edit section" links
23 var $mAllowSpecialInclusion; # Allow inclusion of special pages
24 var $mTidy = false; # Ask for tidy cleanup
25 var $mInterfaceMessage = false; # Which lang to call for PLURAL and GRAMMAR
26 var $mTargetLanguage = null; # Overrides above setting with arbitrary language
27 var $mMaxIncludeSize; # Maximum size of template expansions, in bytes
28 var $mMaxPPNodeCount; # Maximum number of nodes touched by PPFrame::expand()
29 var $mMaxPPExpandDepth; # Maximum recursion depth in PPFrame::expand()
30 var $mMaxTemplateDepth; # Maximum recursion depth for templates within templates
31 var $mRemoveComments = true; # Remove HTML comments. ONLY APPLIES TO PREPROCESS OPERATIONS
32 var $mTemplateCallback = # Callback for template fetching
33 array( 'Parser', 'statelessFetchTemplate' );
34 var $mEnableLimitReport = false; # Enable limit report in an HTML comment on output
35 var $mTimestamp; # Timestamp used for {{CURRENTDAY}} etc.
36 var $mExternalLinkTarget; # Target attribute for external links
37 var $mCleanSignatures; #
38 var $mPreSaveTransform = true; # Transform wiki markup when saving the page.
40 var $mNumberHeadings; # Automatically number headings
41 var $mMath; # User math preference (as integer)
42 var $mThumbSize; # Thumb size preferred by the user.
43 private $mStubThreshold; # Maximum article size of an article to be marked as "stub"
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
; }
83 function getStubThreshold() { $this->optionUsed( 'stubthreshold' );
84 return $this->mStubThreshold
; }
86 function getIsPreview() { return $this->mIsPreview
; }
87 function getIsSectionPreview() { return $this->mIsSectionPreview
; }
88 function getIsPrintable() { $this->optionUsed( 'printable' );
89 return $this->mIsPrintable
; }
90 function getUser() { return $this->mUser
; }
91 function getPreSaveTransform() { return $this->mPreSaveTransform
; }
96 * @deprecated since 1.18 Use Linker::* instead
98 function getSkin( $title = null ) {
99 wfDeprecated( __METHOD__
);
100 return new DummyLinker
;
103 function getDateFormat() {
104 $this->optionUsed( 'dateformat' );
105 if ( !isset( $this->mDateFormat
) ) {
106 $this->mDateFormat
= $this->mUser
->getDatePreference();
108 return $this->mDateFormat
;
111 function getTimestamp() {
112 if ( !isset( $this->mTimestamp
) ) {
113 $this->mTimestamp
= wfTimestampNow();
115 return $this->mTimestamp
;
119 * You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
120 * Using this fragments the cache and is discouraged. Yes, {{int: }} uses this,
121 * producing inconsistent tables (Bug 14404).
122 * @return String Language code
125 function getUserLang() {
126 $this->optionUsed( 'userlang' );
127 return $this->mUserLang
;
130 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
131 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
132 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
133 function setAllowExternalImagesFrom( $x ) { return wfSetVar( $this->mAllowExternalImagesFrom
, $x ); }
134 function setEnableImageWhitelist( $x ) { return wfSetVar( $this->mEnableImageWhitelist
, $x ); }
135 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
136 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
137 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
138 function setAllowSpecialInclusion( $x ) { return wfSetVar( $this->mAllowSpecialInclusion
, $x ); }
139 function setTidy( $x ) { return wfSetVar( $this->mTidy
, $x ); }
140 function setSkin( $x ) {}
141 function setInterfaceMessage( $x ) { return wfSetVar( $this->mInterfaceMessage
, $x ); }
142 function setTargetLanguage( $x ) { return wfSetVar( $this->mTargetLanguage
, $x, true ); }
143 function setMaxIncludeSize( $x ) { return wfSetVar( $this->mMaxIncludeSize
, $x ); }
144 function setMaxPPNodeCount( $x ) { return wfSetVar( $this->mMaxPPNodeCount
, $x ); }
145 function setMaxTemplateDepth( $x ) { return wfSetVar( $this->mMaxTemplateDepth
, $x ); }
146 function setRemoveComments( $x ) { return wfSetVar( $this->mRemoveComments
, $x ); }
147 function setTemplateCallback( $x ) { return wfSetVar( $this->mTemplateCallback
, $x ); }
148 function enableLimitReport( $x = true ) { return wfSetVar( $this->mEnableLimitReport
, $x ); }
149 function setTimestamp( $x ) { return wfSetVar( $this->mTimestamp
, $x ); }
150 function setCleanSignatures( $x ) { return wfSetVar( $this->mCleanSignatures
, $x ); }
151 function setExternalLinkTarget( $x ) { return wfSetVar( $this->mExternalLinkTarget
, $x ); }
152 function setMath( $x ) { return wfSetVar( $this->mMath
, $x ); }
153 function setUserLang( $x ) {
154 if ( $x instanceof Language
) {
157 return wfSetVar( $this->mUserLang
, $x );
159 function setThumbSize( $x ) { return wfSetVar( $this->mThumbSize
, $x ); }
160 function setStubThreshold( $x ) { return wfSetVar( $this->mStubThreshold
, $x ); }
161 function setPreSaveTransform( $x ) { return wfSetVar( $this->mPreSaveTransform
, $x ); }
163 function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview
, $x ); }
164 function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview
, $x ); }
165 function setIsPrintable( $x ) { return wfSetVar( $this->mIsPrintable
, $x ); }
168 * Extra key that should be present in the parser cache key.
170 function addExtraKey( $key ) {
171 $this->mExtraKey
.= '!' . $key;
174 function __construct( $user = null ) {
175 $this->initialiseFromUser( $user );
181 * @param $user User object
182 * @return ParserOptions object
184 static function newFromUser( $user ) {
185 return new ParserOptions( $user );
188 /** Get user options */
189 function initialiseFromUser( $userInput ) {
190 global $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
191 global $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion, $wgMaxArticleSize;
192 global $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth, $wgCleanSignatures;
193 global $wgExternalLinkTarget, $wgLang;
195 wfProfileIn( __METHOD__
);
199 if ( isset( $wgUser ) ) {
208 $this->mUser
= $user;
210 $this->mUseDynamicDates
= $wgUseDynamicDates;
211 $this->mInterwikiMagic
= $wgInterwikiMagic;
212 $this->mAllowExternalImages
= $wgAllowExternalImages;
213 $this->mAllowExternalImagesFrom
= $wgAllowExternalImagesFrom;
214 $this->mEnableImageWhitelist
= $wgEnableImageWhitelist;
215 $this->mAllowSpecialInclusion
= $wgAllowSpecialInclusion;
216 $this->mMaxIncludeSize
= $wgMaxArticleSize * 1024;
217 $this->mMaxPPNodeCount
= $wgMaxPPNodeCount;
218 $this->mMaxPPExpandDepth
= $wgMaxPPExpandDepth;
219 $this->mMaxTemplateDepth
= $wgMaxTemplateDepth;
220 $this->mCleanSignatures
= $wgCleanSignatures;
221 $this->mExternalLinkTarget
= $wgExternalLinkTarget;
223 $this->mNumberHeadings
= $user->getOption( 'numberheadings' );
224 $this->mMath
= $user->getOption( 'math' );
225 $this->mThumbSize
= $user->getOption( 'thumbsize' );
226 $this->mStubThreshold
= $user->getStubThreshold();
227 $this->mUserLang
= $wgLang->getCode();
229 wfProfileOut( __METHOD__
);
233 * Registers a callback for tracking which ParserOptions which are used.
234 * This is a private API with the parser.
236 function registerWatcher( $callback ) {
237 $this->onAccessCallback
= $callback;
241 * Called when an option is accessed.
243 protected function optionUsed( $optionName ) {
244 if ( $this->onAccessCallback
) {
245 call_user_func( $this->onAccessCallback
, $optionName );
250 * Returns the full array of options that would have been used by
252 * Used to get the old parser cache entries when available.
254 public static function legacyOptions() {
255 global $wgUseDynamicDates;
256 $legacyOpts = array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
257 if ( $wgUseDynamicDates ) {
258 $legacyOpts[] = 'dateformat';
264 * Generate a hash string with the values set on these ParserOptions
265 * for the keys given in the array.
266 * This will be used as part of the hash key for the parser cache,
267 * so users sharign the options with vary for the same page share
268 * the same cached data safely.
270 * Replaces User::getPageRenderingHash()
272 * Extensions which require it should install 'PageRenderingHash' hook,
273 * which will give them a chance to modify this key based on their own
277 * @param $forOptions Array
278 * @param $title Title: will be used to get the page content language
279 * @return \string Page rendering hash
281 public function optionsHash( $forOptions, $title = null ) {
282 global $wgContLang, $wgRenderHashAppend;
286 if ( in_array( 'math', $forOptions ) ) {
287 $confstr .= $this->mMath
;
293 // Space assigned for the stubthreshold but unused
294 // since it disables the parser cache, its value will always
295 // be 0 when this function is called by parsercache.
296 if ( in_array( 'stubthreshold', $forOptions ) ) {
297 $confstr .= '!' . $this->mStubThreshold
;
302 if ( in_array( 'dateformat', $forOptions ) ) {
303 $confstr .= '!' . $this->getDateFormat();
306 if ( in_array( 'numberheadings', $forOptions ) ) {
307 $confstr .= '!' . ( $this->mNumberHeadings ?
'1' : '' );
312 if ( in_array( 'userlang', $forOptions ) ) {
313 $confstr .= '!' . $this->mUserLang
;
318 if ( in_array( 'thumbsize', $forOptions ) ) {
319 $confstr .= '!' . $this->mThumbSize
;
324 // add in language specific options, if any
325 // @todo FIXME: This is just a way of retrieving the url/user preferred variant
326 $confstr .= $wgContLang->getExtraHashOptions();
328 $confstr .= $wgRenderHashAppend;
330 if ( !in_array( 'editsection', $forOptions ) ) {
332 } elseif ( !$this->mEditSection
) {
333 $confstr .= '!edit=0';
336 if ( $this->mIsPrintable
&& in_array( 'printable', $forOptions ) ) {
337 $confstr .= '!printable=1';
340 if ( $this->mExtraKey
!= '' )
341 $confstr .= $this->mExtraKey
;
343 // Give a chance for extensions to modify the hash, if they have
344 // extra options or other effects on the parser cache.
345 wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
347 // Make it a valid memcached key fragment
348 $confstr = str_replace( ' ', '_', $confstr );