0b48032d2957fac0ac52a52774e19245be21120b
[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 $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.
39
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 object of the User language.
45
46 /**
47 * @var User
48 */
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
53
54 var $mExtraKey = ''; # Extra key that should be present in the caching key.
55
56 protected $onAccessCallback = null;
57
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() { return $this->mTargetLanguage; }
70 function getMaxIncludeSize() { return $this->mMaxIncludeSize; }
71 function getMaxPPNodeCount() { return $this->mMaxPPNodeCount; }
72 function getMaxPPExpandDepth() { return $this->mMaxPPExpandDepth; }
73 function getMaxTemplateDepth() { return $this->mMaxTemplateDepth; }
74 function getRemoveComments() { return $this->mRemoveComments; }
75 function getTemplateCallback() { return $this->mTemplateCallback; }
76 function getEnableLimitReport() { return $this->mEnableLimitReport; }
77 function getCleanSignatures() { return $this->mCleanSignatures; }
78 function getExternalLinkTarget() { return $this->mExternalLinkTarget; }
79 function getMath() { $this->optionUsed( 'math' );
80 return $this->mMath; }
81 function getThumbSize() { $this->optionUsed( 'thumbsize' );
82 return $this->mThumbSize; }
83 function getStubThreshold() { $this->optionUsed( 'stubthreshold' );
84 return $this->mStubThreshold; }
85
86 function getIsPreview() { return $this->mIsPreview; }
87 function getIsSectionPreview() { return $this->mIsSectionPreview; }
88 function getIsPrintable() { $this->optionUsed( 'printable' );
89 return $this->mIsPrintable; }
90 function getUser() { return $this->mUser; }
91 function getPreSaveTransform() { return $this->mPreSaveTransform; }
92
93 /**
94 * @param $title Title
95 * @return Skin
96 * @deprecated since 1.18 Use Linker::* instead
97 */
98 function getSkin( $title = null ) {
99 wfDeprecated( __METHOD__, '1.18' );
100 return new DummyLinker;
101 }
102
103 function getDateFormat() {
104 $this->optionUsed( 'dateformat' );
105 if ( !isset( $this->mDateFormat ) ) {
106 $this->mDateFormat = $this->mUser->getDatePreference();
107 }
108 return $this->mDateFormat;
109 }
110
111 function getTimestamp() {
112 if ( !isset( $this->mTimestamp ) ) {
113 $this->mTimestamp = wfTimestampNow();
114 }
115 return $this->mTimestamp;
116 }
117
118 /**
119 * You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
120 * Using this fragments the cache and is discouraged. Yes, {{int: }} uses this,
121 * producing inconsistent tables (Bug 14404).
122 *
123 * @return Language object
124 * @since 1.19
125 */
126 function getUserLangObj() {
127 $this->optionUsed( 'userlang' );
128 return $this->mUserLang;
129 }
130
131 /**
132 * Same as getUserLangObj() but returns a string instead.
133 *
134 * @return String Language code
135 * @since 1.17
136 */
137 function getUserLang() {
138 return $this->getUserLangObj()->getCode();
139 }
140
141 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
142 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
143 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
144 function setAllowExternalImagesFrom( $x ) { return wfSetVar( $this->mAllowExternalImagesFrom, $x ); }
145 function setEnableImageWhitelist( $x ) { return wfSetVar( $this->mEnableImageWhitelist, $x ); }
146 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
147 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
148 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
149 function setAllowSpecialInclusion( $x ) { return wfSetVar( $this->mAllowSpecialInclusion, $x ); }
150 function setTidy( $x ) { return wfSetVar( $this->mTidy, $x ); }
151
152 /** @deprecated in 1.19; will be removed in 1.20 */
153 function setSkin( $x ) { wfDeprecated( __METHOD__, '1.19' ); }
154 function setInterfaceMessage( $x ) { return wfSetVar( $this->mInterfaceMessage, $x ); }
155 function setTargetLanguage( $x ) { return wfSetVar( $this->mTargetLanguage, $x, true ); }
156 function setMaxIncludeSize( $x ) { return wfSetVar( $this->mMaxIncludeSize, $x ); }
157 function setMaxPPNodeCount( $x ) { return wfSetVar( $this->mMaxPPNodeCount, $x ); }
158 function setMaxTemplateDepth( $x ) { return wfSetVar( $this->mMaxTemplateDepth, $x ); }
159 function setRemoveComments( $x ) { return wfSetVar( $this->mRemoveComments, $x ); }
160 function setTemplateCallback( $x ) { return wfSetVar( $this->mTemplateCallback, $x ); }
161 function enableLimitReport( $x = true ) { return wfSetVar( $this->mEnableLimitReport, $x ); }
162 function setTimestamp( $x ) { return wfSetVar( $this->mTimestamp, $x ); }
163 function setCleanSignatures( $x ) { return wfSetVar( $this->mCleanSignatures, $x ); }
164 function setExternalLinkTarget( $x ) { return wfSetVar( $this->mExternalLinkTarget, $x ); }
165 function setMath( $x ) { return wfSetVar( $this->mMath, $x ); }
166 function setUserLang( $x ) {
167 if ( is_string( $x ) ) {
168 $x = Language::factory( $x );
169 }
170 return wfSetVar( $this->mUserLang, $x );
171 }
172 function setThumbSize( $x ) { return wfSetVar( $this->mThumbSize, $x ); }
173 function setStubThreshold( $x ) { return wfSetVar( $this->mStubThreshold, $x ); }
174 function setPreSaveTransform( $x ) { return wfSetVar( $this->mPreSaveTransform, $x ); }
175
176 function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview, $x ); }
177 function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview, $x ); }
178 function setIsPrintable( $x ) { return wfSetVar( $this->mIsPrintable, $x ); }
179
180 /**
181 * Extra key that should be present in the parser cache key.
182 */
183 function addExtraKey( $key ) {
184 $this->mExtraKey .= '!' . $key;
185 }
186
187 function __construct( $user = null, $lang = null ) {
188 if ( $user === null ) {
189 global $wgUser;
190 if ( $wgUser === null ) {
191 $user = new User;
192 } else {
193 $user = $wgUser;
194 }
195 }
196 if ( $lang === null ) {
197 global $wgLang;
198 if ( !StubObject::isRealObject( $wgLang ) ) {
199 $wgLang->_unstub();
200 }
201 $lang = $wgLang;
202 }
203 $this->initialiseFromUser( $user, $lang );
204 }
205
206 /**
207 * Get a ParserOptions object from a given user.
208 * Language will be taken from $wgLang.
209 *
210 * @param $user User object
211 * @return ParserOptions object
212 */
213 public static function newFromUser( $user ) {
214 return new ParserOptions( $user );
215 }
216
217 /**
218 * Get a ParserOptions object from a given user and language
219 *
220 * @param $user User object
221 * @param $lang Language object
222 * @return ParserOptions object
223 */
224 public static function newFromUserAndLang( User $user, Language $lang ) {
225 return new ParserOptions( $user, $lang );
226 }
227
228 /**
229 * Get a ParserOptions object from a IContextSource object
230 *
231 * @param $context IContextSource object
232 * @return ParserOptions object
233 */
234 public static function newFromContext( IContextSource $context ) {
235 return new ParserOptions( $context->getUser(), $context->getLanguage() );
236 }
237
238 /** Get user options */
239 private function initialiseFromUser( $user, $lang ) {
240 global $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages,
241 $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion,
242 $wgMaxArticleSize, $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth,
243 $wgCleanSignatures, $wgExternalLinkTarget;
244
245 wfProfileIn( __METHOD__ );
246
247 $this->mUseDynamicDates = $wgUseDynamicDates;
248 $this->mInterwikiMagic = $wgInterwikiMagic;
249 $this->mAllowExternalImages = $wgAllowExternalImages;
250 $this->mAllowExternalImagesFrom = $wgAllowExternalImagesFrom;
251 $this->mEnableImageWhitelist = $wgEnableImageWhitelist;
252 $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion;
253 $this->mMaxIncludeSize = $wgMaxArticleSize * 1024;
254 $this->mMaxPPNodeCount = $wgMaxPPNodeCount;
255 $this->mMaxPPExpandDepth = $wgMaxPPExpandDepth;
256 $this->mMaxTemplateDepth = $wgMaxTemplateDepth;
257 $this->mCleanSignatures = $wgCleanSignatures;
258 $this->mExternalLinkTarget = $wgExternalLinkTarget;
259
260 $this->mUser = $user;
261 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
262 $this->mMath = $user->getOption( 'math' );
263 $this->mThumbSize = $user->getOption( 'thumbsize' );
264 $this->mStubThreshold = $user->getStubThreshold();
265 $this->mUserLang = $lang;
266
267 wfProfileOut( __METHOD__ );
268 }
269
270 /**
271 * Registers a callback for tracking which ParserOptions which are used.
272 * This is a private API with the parser.
273 */
274 function registerWatcher( $callback ) {
275 $this->onAccessCallback = $callback;
276 }
277
278 /**
279 * Called when an option is accessed.
280 */
281 protected function optionUsed( $optionName ) {
282 if ( $this->onAccessCallback ) {
283 call_user_func( $this->onAccessCallback, $optionName );
284 }
285 }
286
287 /**
288 * Returns the full array of options that would have been used by
289 * in 1.16.
290 * Used to get the old parser cache entries when available.
291 */
292 public static function legacyOptions() {
293 global $wgUseDynamicDates;
294 $legacyOpts = array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
295 if ( $wgUseDynamicDates ) {
296 $legacyOpts[] = 'dateformat';
297 }
298 return $legacyOpts;
299 }
300
301 /**
302 * Generate a hash string with the values set on these ParserOptions
303 * for the keys given in the array.
304 * This will be used as part of the hash key for the parser cache,
305 * so users sharign the options with vary for the same page share
306 * the same cached data safely.
307 *
308 * Replaces User::getPageRenderingHash()
309 *
310 * Extensions which require it should install 'PageRenderingHash' hook,
311 * which will give them a chance to modify this key based on their own
312 * settings.
313 *
314 * @since 1.17
315 * @param $forOptions Array
316 * @param $title Title: used to get the content language of the page (since r97636)
317 * @return \string Page rendering hash
318 */
319 public function optionsHash( $forOptions, $title = null ) {
320 global $wgRenderHashAppend;
321
322 $confstr = '';
323
324 if ( in_array( 'math', $forOptions ) ) {
325 $confstr .= $this->mMath;
326 } else {
327 $confstr .= '*';
328 }
329
330
331 // Space assigned for the stubthreshold but unused
332 // since it disables the parser cache, its value will always
333 // be 0 when this function is called by parsercache.
334 if ( in_array( 'stubthreshold', $forOptions ) ) {
335 $confstr .= '!' . $this->mStubThreshold;
336 } else {
337 $confstr .= '!*' ;
338 }
339
340 if ( in_array( 'dateformat', $forOptions ) ) {
341 $confstr .= '!' . $this->getDateFormat();
342 }
343
344 if ( in_array( 'numberheadings', $forOptions ) ) {
345 $confstr .= '!' . ( $this->mNumberHeadings ? '1' : '' );
346 } else {
347 $confstr .= '!*';
348 }
349
350 if ( in_array( 'userlang', $forOptions ) ) {
351 $confstr .= '!' . $this->mUserLang->getCode();
352 } else {
353 $confstr .= '!*';
354 }
355
356 if ( in_array( 'thumbsize', $forOptions ) ) {
357 $confstr .= '!' . $this->mThumbSize;
358 } else {
359 $confstr .= '!*';
360 }
361
362 // add in language specific options, if any
363 // @todo FIXME: This is just a way of retrieving the url/user preferred variant
364 if( !is_null( $title ) ) {
365 $confstr .= $title->getPageLanguage()->getExtraHashOptions();
366 } else {
367 global $wgContLang;
368 $confstr .= $wgContLang->getExtraHashOptions();
369 }
370
371 $confstr .= $wgRenderHashAppend;
372
373 if ( !in_array( 'editsection', $forOptions ) ) {
374 $confstr .= '!*';
375 } elseif ( !$this->mEditSection ) {
376 $confstr .= '!edit=0';
377 }
378
379 if ( $this->mIsPrintable && in_array( 'printable', $forOptions ) ) {
380 $confstr .= '!printable=1';
381 }
382
383 if ( $this->mExtraKey != '' )
384 $confstr .= $this->mExtraKey;
385
386 // Give a chance for extensions to modify the hash, if they have
387 // extra options or other effects on the parser cache.
388 wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
389
390 // Make it a valid memcached key fragment
391 $confstr = str_replace( ' ', '_', $confstr );
392
393 return $confstr;
394 }
395 }