6f6e2dfadd2cbd7217a868caf791e13ee3c0001c
[lhc/web/wiklou.git] / includes / parser / ParserOptions.php
1 <?php
2
3 /**
4 * Set options of the Parser
5 * @todo document
6 * @ingroup Parser
7 */
8 class ParserOptions {
9 # All variables are supposed to be private in theory, although in practise this is not the case.
10 var $mUseDynamicDates; # Use DateFormatter to format dates
11 var $mInterwikiMagic; # Interlanguage links are removed and returned in an array
12 var $mAllowExternalImages; # Allow external images inline
13 var $mAllowExternalImagesFrom; # If not, any exception?
14 var $mEnableImageWhitelist; # If not or it doesn't match, should we check an on-wiki whitelist?
15 var $mSkin; # Reference to the preferred skin
16 var $mDateFormat; # Date format index
17 var $mEditSection; # Create "edit section" links
18 var $mNumberHeadings; # Automatically number headings
19 var $mAllowSpecialInclusion; # Allow inclusion of special pages
20 var $mTidy; # Ask for tidy cleanup
21 var $mInterfaceMessage; # Which lang to call for PLURAL and GRAMMAR
22 var $mTargetLanguage; # Overrides above setting with arbitrary language
23 var $mMaxIncludeSize; # Maximum size of template expansions, in bytes
24 var $mMaxPPNodeCount; # Maximum number of nodes touched by PPFrame::expand()
25 var $mMaxPPExpandDepth; # Maximum recursion depth in PPFrame::expand()
26 var $mMaxTemplateDepth; # Maximum recursion depth for templates within templates
27 var $mRemoveComments; # Remove HTML comments. ONLY APPLIES TO PREPROCESS OPERATIONS
28 var $mTemplateCallback; # Callback for template fetching
29 var $mEnableLimitReport; # Enable limit report in an HTML comment on output
30 var $mTimestamp; # Timestamp used for {{CURRENTDAY}} etc.
31 var $mExternalLinkTarget; # Target attribute for external links
32 var $mMath; # User math preference (as integer)
33 var $mUserLang; # Language code of the User language.
34 var $mThumbSize; # Thumb size preferred by the user.
35
36 var $mUser; # Stored user object, just used to initialise the skin
37 var $mIsPreview; # Parsing the page for a "preview" operation
38 var $mIsSectionPreview; # Parsing the page for a "preview" operation on a single section
39 var $mIsPrintable; # Parsing the printable version of the page
40
41 var $mExtraKey = ''; # Extra key that should be present in the caching key.
42
43 protected $accessedOptions;
44
45 function getUseDynamicDates() { return $this->mUseDynamicDates; }
46 function getInterwikiMagic() { return $this->mInterwikiMagic; }
47 function getAllowExternalImages() { return $this->mAllowExternalImages; }
48 function getAllowExternalImagesFrom() { return $this->mAllowExternalImagesFrom; }
49 function getEnableImageWhitelist() { return $this->mEnableImageWhitelist; }
50 function getEditSection() { $this->accessedOptions['editsection'] = true;
51 return $this->mEditSection; }
52 function getNumberHeadings() { $this->accessedOptions['numberheadings'] = true;
53 return $this->mNumberHeadings; }
54 function getAllowSpecialInclusion() { return $this->mAllowSpecialInclusion; }
55 function getTidy() { return $this->mTidy; }
56 function getInterfaceMessage() { return $this->mInterfaceMessage; }
57 function getTargetLanguage() { return $this->mTargetLanguage; }
58 function getMaxIncludeSize() { return $this->mMaxIncludeSize; }
59 function getMaxPPNodeCount() { return $this->mMaxPPNodeCount; }
60 function getMaxPPExpandDepth() { return $this->mMaxPPExpandDepth; }
61 function getMaxTemplateDepth() { return $this->mMaxTemplateDepth; }
62 function getRemoveComments() { return $this->mRemoveComments; }
63 function getTemplateCallback() { return $this->mTemplateCallback; }
64 function getEnableLimitReport() { return $this->mEnableLimitReport; }
65 function getCleanSignatures() { return $this->mCleanSignatures; }
66 function getExternalLinkTarget() { return $this->mExternalLinkTarget; }
67 function getMath() { $this->accessedOptions['math'] = true;
68 return $this->mMath; }
69 function getThumbSize() { $this->accessedOptions['thumbsize'] = true;
70 return $this->mThumbSize; }
71
72 function getIsPreview() { return $this->mIsPreview; }
73 function getIsSectionPreview() { return $this->mIsSectionPreview; }
74 function getIsPrintable() { $this->accessedOptions['printable'] = true;
75 return $this->mIsPrintable; }
76
77 function getSkin( $title = null ) {
78 if ( !isset( $this->mSkin ) ) {
79 $this->mSkin = $this->mUser->getSkin( $title );
80 }
81 return $this->mSkin;
82 }
83
84 function getDateFormat() {
85 $this->accessedOptions['dateformat'] = true;
86 if ( !isset( $this->mDateFormat ) ) {
87 $this->mDateFormat = $this->mUser->getDatePreference();
88 }
89 return $this->mDateFormat;
90 }
91
92 function getTimestamp() {
93 if ( !isset( $this->mTimestamp ) ) {
94 $this->mTimestamp = wfTimestampNow();
95 }
96 return $this->mTimestamp;
97 }
98
99 # You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
100 # Using this fragments the cache and is discouraged. Yes, {{int: }} uses this,
101 # producing inconsistent tables (Bug 14404).
102 function getUserLang() {
103 $this->accessedOptions['userlang'] = true;
104 return $this->mUserLang;
105 }
106
107 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
108 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
109 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
110 function setAllowExternalImagesFrom( $x ) { return wfSetVar( $this->mAllowExternalImagesFrom, $x ); }
111 function setEnableImageWhitelist( $x ) { return wfSetVar( $this->mEnableImageWhitelist, $x ); }
112 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
113 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
114 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
115 function setAllowSpecialInclusion( $x ) { return wfSetVar( $this->mAllowSpecialInclusion, $x ); }
116 function setTidy( $x ) { return wfSetVar( $this->mTidy, $x); }
117 function setSkin( $x ) { $this->mSkin = $x; }
118 function setInterfaceMessage( $x ) { return wfSetVar( $this->mInterfaceMessage, $x); }
119 function setTargetLanguage( $x ) { return wfSetVar( $this->mTargetLanguage, $x); }
120 function setMaxIncludeSize( $x ) { return wfSetVar( $this->mMaxIncludeSize, $x ); }
121 function setMaxPPNodeCount( $x ) { return wfSetVar( $this->mMaxPPNodeCount, $x ); }
122 function setMaxTemplateDepth( $x ) { return wfSetVar( $this->mMaxTemplateDepth, $x ); }
123 function setRemoveComments( $x ) { return wfSetVar( $this->mRemoveComments, $x ); }
124 function setTemplateCallback( $x ) { return wfSetVar( $this->mTemplateCallback, $x ); }
125 function enableLimitReport( $x = true ) { return wfSetVar( $this->mEnableLimitReport, $x ); }
126 function setTimestamp( $x ) { return wfSetVar( $this->mTimestamp, $x ); }
127 function setCleanSignatures( $x ) { return wfSetVar( $this->mCleanSignatures, $x ); }
128 function setExternalLinkTarget( $x ) { return wfSetVar( $this->mExternalLinkTarget, $x ); }
129 function setMath( $x ) { return wfSetVar( $this->mMath, $x ); }
130 function setUserLang( $x ) { return wfSetVar( $this->mUserLang, $x ); }
131 function setThumbSize() { return wfSetVar( $this->mThumbSize, $x ); }
132
133 function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview, $x ); }
134 function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview, $x ); }
135 function setIsPrintable( $x ) { return wfSetVar( $this->mIsPrintable, $x ); }
136
137 /**
138 * Extra key that should be present in the parser cache key.
139 */
140 function addExtraKey( $key ) {
141 $this->mExtraKey .= '!' . $key;
142 }
143
144 function __construct( $user = null ) {
145 $this->initialiseFromUser( $user );
146 }
147
148 /**
149 * Get parser options
150 *
151 * @param $user User object
152 * @return ParserOptions object
153 */
154 static function newFromUser( $user ) {
155 return new ParserOptions( $user );
156 }
157
158 /** Get user options */
159 function initialiseFromUser( $userInput ) {
160 global $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages;
161 global $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion, $wgMaxArticleSize;
162 global $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth, $wgCleanSignatures;
163 global $wgExternalLinkTarget, $wgLang;
164
165 wfProfileIn( __METHOD__ );
166
167 if ( !$userInput ) {
168 global $wgUser;
169 if ( isset( $wgUser ) ) {
170 $user = $wgUser;
171 } else {
172 $user = new User;
173 }
174 } else {
175 $user =& $userInput;
176 }
177
178 $this->mUser = $user;
179
180 $this->mUseDynamicDates = $wgUseDynamicDates;
181 $this->mInterwikiMagic = $wgInterwikiMagic;
182 $this->mAllowExternalImages = $wgAllowExternalImages;
183 $this->mAllowExternalImagesFrom = $wgAllowExternalImagesFrom;
184 $this->mEnableImageWhitelist = $wgEnableImageWhitelist;
185 $this->mSkin = null; # Deferred
186 $this->mDateFormat = null; # Deferred
187 $this->mEditSection = true;
188 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
189 $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion;
190 $this->mTidy = false;
191 $this->mInterfaceMessage = false;
192 $this->mTargetLanguage = null; // default depends on InterfaceMessage setting
193 $this->mMaxIncludeSize = $wgMaxArticleSize * 1024;
194 $this->mMaxPPNodeCount = $wgMaxPPNodeCount;
195 $this->mMaxPPExpandDepth = $wgMaxPPExpandDepth;
196 $this->mMaxTemplateDepth = $wgMaxTemplateDepth;
197 $this->mRemoveComments = true;
198 $this->mTemplateCallback = array( 'Parser', 'statelessFetchTemplate' );
199 $this->mEnableLimitReport = false;
200 $this->mCleanSignatures = $wgCleanSignatures;
201 $this->mExternalLinkTarget = $wgExternalLinkTarget;
202 $this->mMath = $user->getOption( 'math' );
203 $this->mUserLang = $wgLang->getCode();
204 $this->mThumbSize = $user->getOption( 'thumbsize' );
205
206 $this->mIsPreview = false;
207 $this->mIsSectionPreview = false;
208 $this->mIsPrintable = false;
209
210 wfProfileOut( __METHOD__ );
211 }
212
213 /**
214 * Returns the options from this ParserOptions which have been used.
215 */
216 public function usedOptions() {
217 return array_keys( $this->accessedOptions );
218 }
219
220 /**
221 * Resets the memory of options usage.
222 */
223 public function resetUsage() {
224 $this->accessedOptions = array();
225 }
226
227 /**
228 * Returns the full array of options that would have been used by
229 * in 1.16.
230 * Used to get the old parser cache entries when available.
231 */
232 public static function legacyOptions() {
233 global $wgUseDynamicDates;
234 $legacyOpts = array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
235 if ( $wgUseDynamicDates ) {
236 $legacyOpts[] = 'dateformat';
237 }
238 return $legacyOpts;
239 }
240
241 /**
242 * Generate a hash string with the values set on these ParserOptions
243 * for the keys given in the array.
244 * This will be used as part of the hash key for the parser cache,
245 * so users sharign the options with vary for the same page share
246 * the same cached data safely.
247 *
248 * Replaces User::getPageRenderingHash()
249 *
250 * Extensions which require it should install 'PageRenderingHash' hook,
251 * which will give them a chance to modify this key based on their own
252 * settings.
253 *
254 * @since 1.17
255 * @return \string Page rendering hash
256 */
257 public function optionsHash( $forOptions ) {
258 global $wgContLang, $wgRenderHashAppend;
259
260 $confstr = '';
261
262 if ( in_array( 'math', $forOptions ) )
263 $confstr .= $this->mMath;
264 else
265 $confstr .= '*';
266
267
268 // Space assigned for the stubthreshold but unused
269 // since it disables the parser cache, its value will always
270 // be 0 when this function is called by parsercache.
271 // The conditional is here to avoid a confusing 0
272 if ( in_array( 'stubthreshold', $forOptions ) )
273 $confstr .= '!0' ;
274 else
275 $confstr .= '!*' ;
276
277 if ( in_array( 'dateformat', $forOptions ) )
278 $confstr .= '!' . $this->mDateFormat;
279
280 if ( in_array( 'numberheadings', $forOptions ) )
281 $confstr .= '!' . ( $this->mNumberHeadings ? '1' : '' );
282 else
283 $confstr .= '!*';
284
285 if ( in_array( 'userlang', $forOptions ) )
286 $confstr .= '!' . $this->mUserLang;
287 else
288 $confstr .= '!*';
289
290 if ( in_array( 'thumbsize', $forOptions ) )
291 $confstr .= '!' . $this->mThumbSize;
292 else
293 $confstr .= '!*';
294
295 // add in language specific options, if any
296 // FIXME: This is just a way of retrieving the url/user preferred variant
297 $confstr .= $wgContLang->getExtraHashOptions();
298
299 // Since the skin could be overloading link(), it should be
300 // included here but in practice, none of our skins do that.
301 // $confstr .= "!" . $this->mSkin->getSkinName();
302
303 $confstr .= $wgRenderHashAppend;
304
305 if ( !$this->mEditSection && in_array( 'editsection', $forOptions ) )
306 $confstr .= '!edit=0';
307 if ( $this->mIsPrintable && in_array( 'printable', $forOptions ) )
308 $confstr .= '!printable=1';
309
310 if ( $this->mExtraKey != '' )
311 $confstr .= $this->mExtraKey;
312
313 // Give a chance for extensions to modify the hash, if they have
314 // extra options or other effects on the parser cache.
315 wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
316
317 // Make it a valid memcached key fragment
318 $confstr = str_replace( ' ', '_', $confstr );
319
320 return $confstr;
321 }
322 }