Fix spelling of max-age in Cache-Control: header. This inconsistency in the HTTP...
[lhc/web/wiklou.git] / includes / parser / ParserOptions.php
1 <?php
2 /**
3 * Options for the PHP parser
4 *
5 * @file
6 * @ingroup Parser
7 */
8
9 /**
10 * Set options of the Parser
11 * @todo document
12 * @ingroup Parser
13 */
14 class ParserOptions {
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; #
42
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
47
48 var $mExtraKey = ''; # Extra key that should be present in the caching key.
49
50 protected $accessedOptions;
51
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->accessedOptions['editsection'] = true;
58 return $this->mEditSection; }
59 function getNumberHeadings() { $this->accessedOptions['numberheadings'] = true;
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->accessedOptions['math'] = true;
75 return $this->mMath; }
76 function getThumbSize() { $this->accessedOptions['thumbsize'] = true;
77 return $this->mThumbSize; }
78
79 function getIsPreview() { return $this->mIsPreview; }
80 function getIsSectionPreview() { return $this->mIsSectionPreview; }
81 function getIsPrintable() { $this->accessedOptions['printable'] = true;
82 return $this->mIsPrintable; }
83
84 function getSkin( $title = null ) {
85 if ( !isset( $this->mSkin ) ) {
86 $this->mSkin = $this->mUser->getSkin( $title );
87 }
88 return $this->mSkin;
89 }
90
91 function getDateFormat() {
92 $this->accessedOptions['dateformat'] = true;
93 if ( !isset( $this->mDateFormat ) ) {
94 $this->mDateFormat = $this->mUser->getDatePreference();
95 }
96 return $this->mDateFormat;
97 }
98
99 function getTimestamp() {
100 if ( !isset( $this->mTimestamp ) ) {
101 $this->mTimestamp = wfTimestampNow();
102 }
103 return $this->mTimestamp;
104 }
105
106 # You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
107 # Using this fragments the cache and is discouraged. Yes, {{int: }} uses this,
108 # producing inconsistent tables (Bug 14404).
109 function getUserLang() {
110 $this->accessedOptions['userlang'] = true;
111 return $this->mUserLang;
112 }
113
114 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
115 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
116 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
117 function setAllowExternalImagesFrom( $x ) { return wfSetVar( $this->mAllowExternalImagesFrom, $x ); }
118 function setEnableImageWhitelist( $x ) { return wfSetVar( $this->mEnableImageWhitelist, $x ); }
119 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
120 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
121 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
122 function setAllowSpecialInclusion( $x ) { return wfSetVar( $this->mAllowSpecialInclusion, $x ); }
123 function setTidy( $x ) { return wfSetVar( $this->mTidy, $x); }
124 function setSkin( $x ) { $this->mSkin = $x; }
125 function setInterfaceMessage( $x ) { return wfSetVar( $this->mInterfaceMessage, $x); }
126 function setTargetLanguage( $x ) { return wfSetVar( $this->mTargetLanguage, $x); }
127 function setMaxIncludeSize( $x ) { return wfSetVar( $this->mMaxIncludeSize, $x ); }
128 function setMaxPPNodeCount( $x ) { return wfSetVar( $this->mMaxPPNodeCount, $x ); }
129 function setMaxTemplateDepth( $x ) { return wfSetVar( $this->mMaxTemplateDepth, $x ); }
130 function setRemoveComments( $x ) { return wfSetVar( $this->mRemoveComments, $x ); }
131 function setTemplateCallback( $x ) { return wfSetVar( $this->mTemplateCallback, $x ); }
132 function enableLimitReport( $x = true ) { return wfSetVar( $this->mEnableLimitReport, $x ); }
133 function setTimestamp( $x ) { return wfSetVar( $this->mTimestamp, $x ); }
134 function setCleanSignatures( $x ) { return wfSetVar( $this->mCleanSignatures, $x ); }
135 function setExternalLinkTarget( $x ) { return wfSetVar( $this->mExternalLinkTarget, $x ); }
136 function setMath( $x ) { return wfSetVar( $this->mMath, $x ); }
137 function setUserLang( $x ) { return wfSetVar( $this->mUserLang, $x ); }
138 function setThumbSize( $x ) { return wfSetVar( $this->mThumbSize, $x ); }
139
140 function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview, $x ); }
141 function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview, $x ); }
142 function setIsPrintable( $x ) { return wfSetVar( $this->mIsPrintable, $x ); }
143
144 /**
145 * Extra key that should be present in the parser cache key.
146 */
147 function addExtraKey( $key ) {
148 $this->mExtraKey .= '!' . $key;
149 }
150
151 function __construct( $user = null ) {
152 $this->initialiseFromUser( $user );
153 }
154
155 /**
156 * Get parser options
157 *
158 * @param $user User object
159 * @return ParserOptions object
160 */
161 static function newFromUser( $user ) {
162 return new ParserOptions( $user );
163 }
164
165 /** Get user options */
166 function initialiseFromUser( $userInput ) {
167 global $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
168 global $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion, $wgMaxArticleSize;
169 global $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth, $wgCleanSignatures;
170 global $wgExternalLinkTarget, $wgLang;
171
172 wfProfileIn( __METHOD__ );
173
174 if ( !$userInput ) {
175 global $wgUser;
176 if ( isset( $wgUser ) ) {
177 $user = $wgUser;
178 } else {
179 $user = new User;
180 }
181 } else {
182 $user =& $userInput;
183 }
184
185 $this->mUser = $user;
186
187 $this->mUseDynamicDates = $wgUseDynamicDates;
188 $this->mInterwikiMagic = $wgInterwikiMagic;
189 $this->mAllowExternalImages = $wgAllowExternalImages;
190 $this->mAllowExternalImagesFrom = $wgAllowExternalImagesFrom;
191 $this->mEnableImageWhitelist = $wgEnableImageWhitelist;
192 $this->mSkin = null; # Deferred
193 $this->mDateFormat = null; # Deferred
194 $this->mEditSection = true;
195 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
196 $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion;
197 $this->mTidy = false;
198 $this->mInterfaceMessage = false;
199 $this->mTargetLanguage = null; // default depends on InterfaceMessage setting
200 $this->mMaxIncludeSize = $wgMaxArticleSize * 1024;
201 $this->mMaxPPNodeCount = $wgMaxPPNodeCount;
202 $this->mMaxPPExpandDepth = $wgMaxPPExpandDepth;
203 $this->mMaxTemplateDepth = $wgMaxTemplateDepth;
204 $this->mRemoveComments = true;
205 $this->mTemplateCallback = array( 'Parser', 'statelessFetchTemplate' );
206 $this->mEnableLimitReport = false;
207 $this->mCleanSignatures = $wgCleanSignatures;
208 $this->mExternalLinkTarget = $wgExternalLinkTarget;
209 $this->mMath = $user->getOption( 'math' );
210 $this->mUserLang = $wgLang->getCode();
211 $this->mThumbSize = $user->getOption( 'thumbsize' );
212
213 $this->mIsPreview = false;
214 $this->mIsSectionPreview = false;
215 $this->mIsPrintable = false;
216
217 wfProfileOut( __METHOD__ );
218 }
219
220 /**
221 * Returns the options from this ParserOptions which have been used.
222 */
223 public function usedOptions() {
224 return array_keys( $this->accessedOptions );
225 }
226
227 /**
228 * Resets the memory of options usage.
229 */
230 public function resetUsage() {
231 $this->accessedOptions = array();
232 }
233
234 /**
235 * Returns the full array of options that would have been used by
236 * in 1.16.
237 * Used to get the old parser cache entries when available.
238 */
239 public static function legacyOptions() {
240 global $wgUseDynamicDates;
241 $legacyOpts = array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
242 if ( $wgUseDynamicDates ) {
243 $legacyOpts[] = 'dateformat';
244 }
245 return $legacyOpts;
246 }
247
248 /**
249 * Generate a hash string with the values set on these ParserOptions
250 * for the keys given in the array.
251 * This will be used as part of the hash key for the parser cache,
252 * so users sharign the options with vary for the same page share
253 * the same cached data safely.
254 *
255 * Replaces User::getPageRenderingHash()
256 *
257 * Extensions which require it should install 'PageRenderingHash' hook,
258 * which will give them a chance to modify this key based on their own
259 * settings.
260 *
261 * @since 1.17
262 * @return \string Page rendering hash
263 */
264 public function optionsHash( $forOptions ) {
265 global $wgContLang, $wgRenderHashAppend;
266
267 $confstr = '';
268
269 if ( in_array( 'math', $forOptions ) )
270 $confstr .= $this->mMath;
271 else
272 $confstr .= '*';
273
274
275 // Space assigned for the stubthreshold but unused
276 // since it disables the parser cache, its value will always
277 // be 0 when this function is called by parsercache.
278 // The conditional is here to avoid a confusing 0
279 if ( in_array( 'stubthreshold', $forOptions ) )
280 $confstr .= '!0' ;
281 else
282 $confstr .= '!*' ;
283
284 if ( in_array( 'dateformat', $forOptions ) )
285 $confstr .= '!' . $this->mDateFormat;
286
287 if ( in_array( 'numberheadings', $forOptions ) )
288 $confstr .= '!' . ( $this->mNumberHeadings ? '1' : '' );
289 else
290 $confstr .= '!*';
291
292 if ( in_array( 'userlang', $forOptions ) )
293 $confstr .= '!' . $this->mUserLang;
294 else
295 $confstr .= '!*';
296
297 if ( in_array( 'thumbsize', $forOptions ) )
298 $confstr .= '!' . $this->mThumbSize;
299 else
300 $confstr .= '!*';
301
302 // add in language specific options, if any
303 // FIXME: This is just a way of retrieving the url/user preferred variant
304 $confstr .= $wgContLang->getExtraHashOptions();
305
306 // Since the skin could be overloading link(), it should be
307 // included here but in practice, none of our skins do that.
308 // $confstr .= "!" . $this->mSkin->getSkinName();
309
310 $confstr .= $wgRenderHashAppend;
311
312 if ( !$this->mEditSection && in_array( 'editsection', $forOptions ) )
313 $confstr .= '!edit=0';
314 if ( $this->mIsPrintable && in_array( 'printable', $forOptions ) )
315 $confstr .= '!printable=1';
316
317 if ( $this->mExtraKey != '' )
318 $confstr .= $this->mExtraKey;
319
320 // Give a chance for extensions to modify the hash, if they have
321 // extra options or other effects on the parser cache.
322 wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
323
324 // Make it a valid memcached key fragment
325 $confstr = str_replace( ' ', '_', $confstr );
326
327 return $confstr;
328 }
329 }