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