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 private $mStubThreshold; # Maximum article size of an article to be marked as "stub"
45 var $mUserLang; # Language code of the User language.
50 var $mUser; # Stored user object
51 var $mIsPreview = false; # Parsing the page for a "preview" operation
52 var $mIsSectionPreview = false; # Parsing the page for a "preview" operation on a single section
53 var $mIsPrintable = false; # Parsing the printable version of the page
55 var $mExtraKey = ''; # Extra key that should be present in the caching key.
57 protected $onAccessCallback = null;
59 function getUseDynamicDates() { return $this->mUseDynamicDates
; }
60 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
61 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
62 function getAllowExternalImagesFrom() { return $this->mAllowExternalImagesFrom
; }
63 function getEnableImageWhitelist() { return $this->mEnableImageWhitelist
; }
64 function getEditSection() { return $this->mEditSection
; }
65 function getNumberHeadings() { $this->optionUsed('numberheadings');
66 return $this->mNumberHeadings
; }
67 function getAllowSpecialInclusion() { return $this->mAllowSpecialInclusion
; }
68 function getTidy() { return $this->mTidy
; }
69 function getInterfaceMessage() { return $this->mInterfaceMessage
; }
70 function getTargetLanguage() { return $this->mTargetLanguage
; }
71 function getMaxIncludeSize() { return $this->mMaxIncludeSize
; }
72 function getMaxPPNodeCount() { return $this->mMaxPPNodeCount
; }
73 function getMaxPPExpandDepth() { return $this->mMaxPPExpandDepth
; }
74 function getMaxTemplateDepth() { return $this->mMaxTemplateDepth
; }
75 function getRemoveComments() { return $this->mRemoveComments
; }
76 function getTemplateCallback() { return $this->mTemplateCallback
; }
77 function getEnableLimitReport() { return $this->mEnableLimitReport
; }
78 function getCleanSignatures() { return $this->mCleanSignatures
; }
79 function getExternalLinkTarget() { return $this->mExternalLinkTarget
; }
80 function getMath() { $this->optionUsed('math');
81 return $this->mMath
; }
82 function getThumbSize() { $this->optionUsed('thumbsize');
83 return $this->mThumbSize
; }
84 function getStubThreshold() { $this->optionUsed('stubthreshold');
85 return $this->mStubThreshold
; }
87 function getIsPreview() { return $this->mIsPreview
; }
88 function getIsSectionPreview() { return $this->mIsSectionPreview
; }
89 function getIsPrintable() { $this->optionUsed('printable');
90 return $this->mIsPrintable
; }
91 function getUser() { return $this->mUser
; }
92 function getPreSaveTransform() { return $this->mPreSaveTransform
; }
94 function getSkin( $title = null ) {
95 if ( !isset( $this->mSkin
) ) {
96 $this->mSkin
= $this->mUser
->getSkin( $title );
101 function getDateFormat() {
102 $this->optionUsed('dateformat');
103 if ( !isset( $this->mDateFormat
) ) {
104 $this->mDateFormat
= $this->mUser
->getDatePreference();
106 return $this->mDateFormat
;
109 function getTimestamp() {
110 if ( !isset( $this->mTimestamp
) ) {
111 $this->mTimestamp
= wfTimestampNow();
113 return $this->mTimestamp
;
117 * You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
118 * Using this fragments the cache and is discouraged. Yes, {{int: }} uses this,
119 * producing inconsistent tables (Bug 14404).
121 function getUserLang() {
122 $this->optionUsed('userlang');
123 return $this->mUserLang
;
126 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
127 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
128 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
129 function setAllowExternalImagesFrom( $x ) { return wfSetVar( $this->mAllowExternalImagesFrom
, $x ); }
130 function setEnableImageWhitelist( $x ) { return wfSetVar( $this->mEnableImageWhitelist
, $x ); }
131 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
132 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
133 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
134 function setAllowSpecialInclusion( $x ) { return wfSetVar( $this->mAllowSpecialInclusion
, $x ); }
135 function setTidy( $x ) { return wfSetVar( $this->mTidy
, $x); }
136 function setSkin( $x ) { $this->mSkin
= $x; }
137 function setInterfaceMessage( $x ) { return wfSetVar( $this->mInterfaceMessage
, $x); }
138 function setTargetLanguage( $x ) { return wfSetVar( $this->mTargetLanguage
, $x); }
139 function setMaxIncludeSize( $x ) { return wfSetVar( $this->mMaxIncludeSize
, $x ); }
140 function setMaxPPNodeCount( $x ) { return wfSetVar( $this->mMaxPPNodeCount
, $x ); }
141 function setMaxTemplateDepth( $x ) { return wfSetVar( $this->mMaxTemplateDepth
, $x ); }
142 function setRemoveComments( $x ) { return wfSetVar( $this->mRemoveComments
, $x ); }
143 function setTemplateCallback( $x ) { return wfSetVar( $this->mTemplateCallback
, $x ); }
144 function enableLimitReport( $x = true ) { return wfSetVar( $this->mEnableLimitReport
, $x ); }
145 function setTimestamp( $x ) { return wfSetVar( $this->mTimestamp
, $x ); }
146 function setCleanSignatures( $x ) { return wfSetVar( $this->mCleanSignatures
, $x ); }
147 function setExternalLinkTarget( $x ) { return wfSetVar( $this->mExternalLinkTarget
, $x ); }
148 function setMath( $x ) { return wfSetVar( $this->mMath
, $x ); }
149 function setUserLang( $x ) { return wfSetVar( $this->mUserLang
, $x ); }
150 function setThumbSize( $x ) { return wfSetVar( $this->mThumbSize
, $x ); }
151 function setStubThreshold( $x ) { return wfSetVar( $this->mStubThreshold
, $x ); }
152 function setPreSaveTransform( $x ) { return wfSetVar( $this->mPreSaveTransform
, $x ); }
154 function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview
, $x ); }
155 function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview
, $x ); }
156 function setIsPrintable( $x ) { return wfSetVar( $this->mIsPrintable
, $x ); }
159 * Extra key that should be present in the parser cache key.
161 function addExtraKey( $key ) {
162 $this->mExtraKey
.= '!' . $key;
165 function __construct( $user = null ) {
166 $this->initialiseFromUser( $user );
172 * @param $user User object
173 * @return ParserOptions object
175 static function newFromUser( $user ) {
176 return new ParserOptions( $user );
179 /** Get user options */
180 function initialiseFromUser( $userInput ) {
181 global $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
182 global $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion, $wgMaxArticleSize;
183 global $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth, $wgCleanSignatures;
184 global $wgExternalLinkTarget, $wgLang;
186 wfProfileIn( __METHOD__
);
190 if ( isset( $wgUser ) ) {
199 $this->mUser
= $user;
201 $this->mUseDynamicDates
= $wgUseDynamicDates;
202 $this->mInterwikiMagic
= $wgInterwikiMagic;
203 $this->mAllowExternalImages
= $wgAllowExternalImages;
204 $this->mAllowExternalImagesFrom
= $wgAllowExternalImagesFrom;
205 $this->mEnableImageWhitelist
= $wgEnableImageWhitelist;
206 $this->mAllowSpecialInclusion
= $wgAllowSpecialInclusion;
207 $this->mMaxIncludeSize
= $wgMaxArticleSize * 1024;
208 $this->mMaxPPNodeCount
= $wgMaxPPNodeCount;
209 $this->mMaxPPExpandDepth
= $wgMaxPPExpandDepth;
210 $this->mMaxTemplateDepth
= $wgMaxTemplateDepth;
211 $this->mCleanSignatures
= $wgCleanSignatures;
212 $this->mExternalLinkTarget
= $wgExternalLinkTarget;
214 $this->mNumberHeadings
= $user->getOption( 'numberheadings' );
215 $this->mMath
= $user->getOption( 'math' );
216 $this->mThumbSize
= $user->getOption( 'thumbsize' );
217 $this->mStubThreshold
= $user->getStubThreshold();
218 $this->mUserLang
= $wgLang->getCode();
220 wfProfileOut( __METHOD__
);
224 * Registers a callback for tracking which ParserOptions which are used.
225 * This is a private API with the parser.
227 function registerWatcher( $callback ) {
228 $this->onAccessCallback
= $callback;
232 * Called when an option is accessed.
234 protected function optionUsed( $optionName ) {
235 if ( $this->onAccessCallback
) {
236 call_user_func( $this->onAccessCallback
, $optionName );
241 * Returns the full array of options that would have been used by
243 * Used to get the old parser cache entries when available.
245 public static function legacyOptions() {
246 global $wgUseDynamicDates;
247 $legacyOpts = array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
248 if ( $wgUseDynamicDates ) {
249 $legacyOpts[] = 'dateformat';
255 * Generate a hash string with the values set on these ParserOptions
256 * for the keys given in the array.
257 * This will be used as part of the hash key for the parser cache,
258 * so users sharign the options with vary for the same page share
259 * the same cached data safely.
261 * Replaces User::getPageRenderingHash()
263 * Extensions which require it should install 'PageRenderingHash' hook,
264 * which will give them a chance to modify this key based on their own
268 * @return \string Page rendering hash
270 public function optionsHash( $forOptions ) {
271 global $wgContLang, $wgRenderHashAppend;
275 if ( in_array( 'math', $forOptions ) )
276 $confstr .= $this->mMath
;
281 // Space assigned for the stubthreshold but unused
282 // since it disables the parser cache, its value will always
283 // be 0 when this function is called by parsercache.
284 if ( in_array( 'stubthreshold', $forOptions ) )
285 $confstr .= '!' . $this->mStubThreshold
;
289 if ( in_array( 'dateformat', $forOptions ) )
290 $confstr .= '!' . $this->getDateFormat();
292 if ( in_array( 'numberheadings', $forOptions ) )
293 $confstr .= '!' . ( $this->mNumberHeadings ?
'1' : '' );
297 if ( in_array( 'userlang', $forOptions ) )
298 $confstr .= '!' . $this->mUserLang
;
302 if ( in_array( 'thumbsize', $forOptions ) )
303 $confstr .= '!' . $this->mThumbSize
;
307 // add in language specific options, if any
308 // FIXME: This is just a way of retrieving the url/user preferred variant
309 $confstr .= $wgContLang->getExtraHashOptions();
311 // Since the skin could be overloading link(), it should be
312 // included here but in practice, none of our skins do that.
313 // $confstr .= "!" . $this->mSkin->getSkinName();
315 $confstr .= $wgRenderHashAppend;
317 if ( !in_array( 'editsection', $forOptions ) ) {
319 } elseif ( !$this->mEditSection
) {
320 $confstr .= '!edit=0';
323 if ( $this->mIsPrintable
&& in_array( 'printable', $forOptions ) )
324 $confstr .= '!printable=1';
326 if ( $this->mExtraKey
!= '' )
327 $confstr .= $this->mExtraKey
;
329 // Give a chance for extensions to modify the hash, if they have
330 // extra options or other effects on the parser cache.
331 wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
333 // Make it a valid memcached key fragment
334 $confstr = str_replace( ' ', '_', $confstr );