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 $onAccessCallback = null;
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->optionUsed('editsection');
58 return $this->mEditSection; }
59 function getNumberHeadings() { $this->optionUsed('numberheadings');
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->optionUsed('math');
75 return $this->mMath; }
76 function getThumbSize() { $this->optionUsed('thumbsize');
77 return $this->mThumbSize; }
79 function getIsPreview() { return $this->mIsPreview; }
80 function getIsSectionPreview() { return $this->mIsSectionPreview; }
81 function getIsPrintable() { $this->optionUsed('printable');
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->optionUsed('dateformat');
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->optionUsed('userlang');
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 * Registers a callback for tracking which ParserOptions which are used.
224 * This is a private API with the parser.
226 function registerWatcher( $callback ) {
227 $this->onAccessCallback = $callback;
231 * Called when an option is accessed.
233 protected function optionUsed( $optionName ) {
234 if ( $this->onAccessCallback ) {
235 call_user_func( $this->onAccessCallback, $optionName );
240 * Returns the full array of options that would have been used by
242 * Used to get the old parser cache entries when available.
244 public static function legacyOptions() {
245 global $wgUseDynamicDates;
246 $legacyOpts = array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
247 if ( $wgUseDynamicDates ) {
248 $legacyOpts[] = 'dateformat';
254 * Generate a hash string with the values set on these ParserOptions
255 * for the keys given in the array.
256 * This will be used as part of the hash key for the parser cache,
257 * so users sharign the options with vary for the same page share
258 * the same cached data safely.
260 * Replaces User::getPageRenderingHash()
262 * Extensions which require it should install 'PageRenderingHash' hook,
263 * which will give them a chance to modify this key based on their own
267 * @return \string Page rendering hash
269 public function optionsHash( $forOptions ) {
270 global $wgContLang, $wgRenderHashAppend;
274 if ( in_array( 'math', $forOptions ) )
275 $confstr .= $this->mMath;
280 // Space assigned for the stubthreshold but unused
281 // since it disables the parser cache, its value will always
282 // be 0 when this function is called by parsercache.
283 // The conditional is here to avoid a confusing 0
284 if ( true || in_array( 'stubthreshold', $forOptions ) )
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 );