49473cabeca5f8f9338b1f6333db9fbcbf7b3aed
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( $title = null ) {
70 if ( $title && $title->isCssOrJsPage() ) {
71 return Language
::factory( 'en' ); // css/js should always be LTR
72 } elseif ( $title && $title->getNamespace() == NS_MEDIAWIKI
) {
73 // Parse mediawiki messages with correct target language
74 list( /* $unused */, $lang ) = MessageCache
::singleton()->figureMessage( $title->getText() );
75 $obj = wfGetLangObj( $lang );
78 return $this->mTargetLanguage
;
80 function getMaxIncludeSize() { return $this->mMaxIncludeSize
; }
81 function getMaxPPNodeCount() { return $this->mMaxPPNodeCount
; }
82 function getMaxPPExpandDepth() { return $this->mMaxPPExpandDepth
; }
83 function getMaxTemplateDepth() { return $this->mMaxTemplateDepth
; }
84 function getRemoveComments() { return $this->mRemoveComments
; }
85 function getTemplateCallback() { return $this->mTemplateCallback
; }
86 function getEnableLimitReport() { return $this->mEnableLimitReport
; }
87 function getCleanSignatures() { return $this->mCleanSignatures
; }
88 function getExternalLinkTarget() { return $this->mExternalLinkTarget
; }
89 function getMath() { $this->optionUsed('math');
90 return $this->mMath
; }
91 function getThumbSize() { $this->optionUsed('thumbsize');
92 return $this->mThumbSize
; }
93 function getStubThreshold() { $this->optionUsed('stubthreshold');
94 return $this->mStubThreshold
; }
96 function getIsPreview() { return $this->mIsPreview
; }
97 function getIsSectionPreview() { return $this->mIsSectionPreview
; }
98 function getIsPrintable() { $this->optionUsed('printable');
99 return $this->mIsPrintable
; }
100 function getUser() { return $this->mUser
; }
101 function getPreSaveTransform() { return $this->mPreSaveTransform
; }
104 * @param $title Title
106 * @deprecated since 1.18 Use Linker::* instead
108 function getSkin( $title = null ) {
109 wfDeprecated( __METHOD__
);
110 return new DummyLinker
;
113 function getDateFormat() {
114 $this->optionUsed('dateformat');
115 if ( !isset( $this->mDateFormat
) ) {
116 $this->mDateFormat
= $this->mUser
->getDatePreference();
118 return $this->mDateFormat
;
121 function getTimestamp() {
122 if ( !isset( $this->mTimestamp
) ) {
123 $this->mTimestamp
= wfTimestampNow();
125 return $this->mTimestamp
;
129 * You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
130 * Using this fragments the cache and is discouraged. Yes, {{int: }} uses this,
131 * producing inconsistent tables (Bug 14404).
132 * @return String Language code
135 function getUserLang() {
136 $this->optionUsed('userlang');
137 return $this->mUserLang
;
140 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
141 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
142 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
143 function setAllowExternalImagesFrom( $x ) { return wfSetVar( $this->mAllowExternalImagesFrom
, $x ); }
144 function setEnableImageWhitelist( $x ) { return wfSetVar( $this->mEnableImageWhitelist
, $x ); }
145 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
146 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
147 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
148 function setAllowSpecialInclusion( $x ) { return wfSetVar( $this->mAllowSpecialInclusion
, $x ); }
149 function setTidy( $x ) { return wfSetVar( $this->mTidy
, $x); }
150 function setSkin( $x ) { $this->mSkin
= $x; }
151 function setInterfaceMessage( $x ) { return wfSetVar( $this->mInterfaceMessage
, $x); }
152 function setTargetLanguage( $x ) { return wfSetVar( $this->mTargetLanguage
, $x, true ); }
153 function setMaxIncludeSize( $x ) { return wfSetVar( $this->mMaxIncludeSize
, $x ); }
154 function setMaxPPNodeCount( $x ) { return wfSetVar( $this->mMaxPPNodeCount
, $x ); }
155 function setMaxTemplateDepth( $x ) { return wfSetVar( $this->mMaxTemplateDepth
, $x ); }
156 function setRemoveComments( $x ) { return wfSetVar( $this->mRemoveComments
, $x ); }
157 function setTemplateCallback( $x ) { return wfSetVar( $this->mTemplateCallback
, $x ); }
158 function enableLimitReport( $x = true ) { return wfSetVar( $this->mEnableLimitReport
, $x ); }
159 function setTimestamp( $x ) { return wfSetVar( $this->mTimestamp
, $x ); }
160 function setCleanSignatures( $x ) { return wfSetVar( $this->mCleanSignatures
, $x ); }
161 function setExternalLinkTarget( $x ) { return wfSetVar( $this->mExternalLinkTarget
, $x ); }
162 function setMath( $x ) { return wfSetVar( $this->mMath
, $x ); }
163 function setUserLang( $x ) {
164 if ( $x instanceof Language
) {
167 return wfSetVar( $this->mUserLang
, $x );
169 function setThumbSize( $x ) { return wfSetVar( $this->mThumbSize
, $x ); }
170 function setStubThreshold( $x ) { return wfSetVar( $this->mStubThreshold
, $x ); }
171 function setPreSaveTransform( $x ) { return wfSetVar( $this->mPreSaveTransform
, $x ); }
173 function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview
, $x ); }
174 function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview
, $x ); }
175 function setIsPrintable( $x ) { return wfSetVar( $this->mIsPrintable
, $x ); }
178 * Extra key that should be present in the parser cache key.
180 function addExtraKey( $key ) {
181 $this->mExtraKey
.= '!' . $key;
184 function __construct( $user = null ) {
185 $this->initialiseFromUser( $user );
191 * @param $user User object
192 * @return ParserOptions object
194 static function newFromUser( $user ) {
195 return new ParserOptions( $user );
198 /** Get user options */
199 function initialiseFromUser( $userInput ) {
200 global $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
201 global $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion, $wgMaxArticleSize;
202 global $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth, $wgCleanSignatures;
203 global $wgExternalLinkTarget, $wgLang;
205 wfProfileIn( __METHOD__
);
209 if ( isset( $wgUser ) ) {
218 $this->mUser
= $user;
220 $this->mUseDynamicDates
= $wgUseDynamicDates;
221 $this->mInterwikiMagic
= $wgInterwikiMagic;
222 $this->mAllowExternalImages
= $wgAllowExternalImages;
223 $this->mAllowExternalImagesFrom
= $wgAllowExternalImagesFrom;
224 $this->mEnableImageWhitelist
= $wgEnableImageWhitelist;
225 $this->mAllowSpecialInclusion
= $wgAllowSpecialInclusion;
226 $this->mMaxIncludeSize
= $wgMaxArticleSize * 1024;
227 $this->mMaxPPNodeCount
= $wgMaxPPNodeCount;
228 $this->mMaxPPExpandDepth
= $wgMaxPPExpandDepth;
229 $this->mMaxTemplateDepth
= $wgMaxTemplateDepth;
230 $this->mCleanSignatures
= $wgCleanSignatures;
231 $this->mExternalLinkTarget
= $wgExternalLinkTarget;
233 $this->mNumberHeadings
= $user->getOption( 'numberheadings' );
234 $this->mMath
= $user->getOption( 'math' );
235 $this->mThumbSize
= $user->getOption( 'thumbsize' );
236 $this->mStubThreshold
= $user->getStubThreshold();
237 $this->mUserLang
= $wgLang->getCode();
239 wfProfileOut( __METHOD__
);
243 * Registers a callback for tracking which ParserOptions which are used.
244 * This is a private API with the parser.
246 function registerWatcher( $callback ) {
247 $this->onAccessCallback
= $callback;
251 * Called when an option is accessed.
253 protected function optionUsed( $optionName ) {
254 if ( $this->onAccessCallback
) {
255 call_user_func( $this->onAccessCallback
, $optionName );
260 * Returns the full array of options that would have been used by
262 * Used to get the old parser cache entries when available.
264 public static function legacyOptions() {
265 global $wgUseDynamicDates;
266 $legacyOpts = array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
267 if ( $wgUseDynamicDates ) {
268 $legacyOpts[] = 'dateformat';
274 * Generate a hash string with the values set on these ParserOptions
275 * for the keys given in the array.
276 * This will be used as part of the hash key for the parser cache,
277 * so users sharign the options with vary for the same page share
278 * the same cached data safely.
280 * Replaces User::getPageRenderingHash()
282 * Extensions which require it should install 'PageRenderingHash' hook,
283 * which will give them a chance to modify this key based on their own
287 * @return \string Page rendering hash
289 public function optionsHash( $forOptions ) {
290 global $wgContLang, $wgRenderHashAppend;
294 if ( in_array( 'math', $forOptions ) )
295 $confstr .= $this->mMath
;
300 // Space assigned for the stubthreshold but unused
301 // since it disables the parser cache, its value will always
302 // be 0 when this function is called by parsercache.
303 if ( in_array( 'stubthreshold', $forOptions ) )
304 $confstr .= '!' . $this->mStubThreshold
;
308 if ( in_array( 'dateformat', $forOptions ) )
309 $confstr .= '!' . $this->getDateFormat();
311 if ( in_array( 'numberheadings', $forOptions ) )
312 $confstr .= '!' . ( $this->mNumberHeadings ?
'1' : '' );
316 if ( in_array( 'userlang', $forOptions ) )
317 $confstr .= '!' . $this->mUserLang
;
321 if ( in_array( 'thumbsize', $forOptions ) )
322 $confstr .= '!' . $this->mThumbSize
;
326 // add in language specific options, if any
327 // @todo FIXME: This is just a way of retrieving the url/user preferred variant
328 $confstr .= $wgContLang->getExtraHashOptions();
330 $confstr .= $wgRenderHashAppend;
332 if ( !in_array( 'editsection', $forOptions ) ) {
334 } elseif ( !$this->mEditSection
) {
335 $confstr .= '!edit=0';
338 if ( $this->mIsPrintable
&& in_array( 'printable', $forOptions ) )
339 $confstr .= '!printable=1';
341 if ( $this->mExtraKey
!= '' )
342 $confstr .= $this->mExtraKey
;
344 // Give a chance for extensions to modify the hash, if they have
345 // extra options or other effects on the parser cache.
346 wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
348 // Make it a valid memcached key fragment
349 $confstr = str_replace( ' ', '_', $confstr );