Localisation updates from http://translatewiki.net.
[mediawiki.git] / includes / parser / ParserOptions.php
blobbf9611f365edb1e057c7d605660c89078d103575
1 <?php
2 /**
3 * \brief Options for the PHP parser
5 * @file
6 * @ingroup Parser
7 */
9 /**
10 * \brief Set options of the Parser
12 * All member variables are supposed to be private in theory, although in practise this is not the case.
14 * @ingroup Parser
16 class ParserOptions {
18 /**
19 * Use DateFormatter to format dates
21 var $mUseDynamicDates;
23 /**
24 * Interlanguage links are removed and returned in an array
26 var $mInterwikiMagic;
28 /**
29 * Allow external images inline?
31 var $mAllowExternalImages;
33 /**
34 * If not, any exception?
36 var $mAllowExternalImagesFrom;
38 /**
39 * If not or it doesn't match, should we check an on-wiki whitelist?
41 var $mEnableImageWhitelist;
43 /**
44 * Date format index
46 var $mDateFormat = null;
48 /**
49 * Create "edit section" links?
51 var $mEditSection = true;
53 /**
54 * Allow inclusion of special pages?
56 var $mAllowSpecialInclusion;
58 /**
59 * Use tidy to cleanup output HTML?
61 var $mTidy = false;
63 /**
64 * Which lang to call for PLURAL and GRAMMAR
66 var $mInterfaceMessage = false;
68 /**
69 * Overrides $mInterfaceMessage with arbitrary language
71 var $mTargetLanguage = null;
73 /**
74 * Maximum size of template expansions, in bytes
76 var $mMaxIncludeSize;
78 /**
79 * Maximum number of nodes touched by PPFrame::expand()
81 var $mMaxPPNodeCount;
83 /**
84 * Maximum recursion depth in PPFrame::expand()
86 var $mMaxPPExpandDepth;
88 /**
89 * Maximum recursion depth for templates within templates
91 var $mMaxTemplateDepth;
93 /**
94 * Remove HTML comments. ONLY APPLIES TO PREPROCESS OPERATIONS
96 var $mRemoveComments = true;
98 /**
99 * Callback for template fetching. Used as first argument to call_user_func().
101 var $mTemplateCallback =
102 array( 'Parser', 'statelessFetchTemplate' );
105 * Enable limit report in an HTML comment on output
107 var $mEnableLimitReport = false;
110 * Timestamp used for {{CURRENTDAY}} etc.
112 var $mTimestamp;
115 * Target attribute for external links
117 var $mExternalLinkTarget;
120 * Clean up signature texts?
122 * 1) Strip ~~~, ~~~~ and ~~~~~ out of signatures
123 * 2) Substitute all transclusions
125 var $mCleanSignatures;
128 * Transform wiki markup when saving the page?
130 var $mPreSaveTransform = true;
133 * Automatically number headings?
135 var $mNumberHeadings;
138 * User math preference (as integer). Not used (1.19)
140 var $mMath;
143 * Thumb size preferred by the user.
145 var $mThumbSize;
148 * Maximum article size of an article to be marked as "stub"
150 private $mStubThreshold;
153 * Language object of the User language.
155 var $mUserLang;
158 * @var User
159 * Stored user object
161 var $mUser;
164 * Parsing the page for a "preview" operation?
166 var $mIsPreview = false;
169 * Parsing the page for a "preview" operation on a single section?
171 var $mIsSectionPreview = false;
174 * Parsing the printable version of the page?
176 var $mIsPrintable = false;
179 * Extra key that should be present in the caching key.
181 var $mExtraKey = '';
184 * Function to be called when an option is accessed.
186 protected $onAccessCallback = null;
188 function getUseDynamicDates() { return $this->mUseDynamicDates; }
189 function getInterwikiMagic() { return $this->mInterwikiMagic; }
190 function getAllowExternalImages() { return $this->mAllowExternalImages; }
191 function getAllowExternalImagesFrom() { return $this->mAllowExternalImagesFrom; }
192 function getEnableImageWhitelist() { return $this->mEnableImageWhitelist; }
193 function getEditSection() { return $this->mEditSection; }
194 function getNumberHeadings() { $this->optionUsed( 'numberheadings' );
195 return $this->mNumberHeadings; }
196 function getAllowSpecialInclusion() { return $this->mAllowSpecialInclusion; }
197 function getTidy() { return $this->mTidy; }
198 function getInterfaceMessage() { return $this->mInterfaceMessage; }
199 function getTargetLanguage() { return $this->mTargetLanguage; }
200 function getMaxIncludeSize() { return $this->mMaxIncludeSize; }
201 function getMaxPPNodeCount() { return $this->mMaxPPNodeCount; }
202 function getMaxPPExpandDepth() { return $this->mMaxPPExpandDepth; }
203 function getMaxTemplateDepth() { return $this->mMaxTemplateDepth; }
204 function getRemoveComments() { return $this->mRemoveComments; }
205 function getTemplateCallback() { return $this->mTemplateCallback; }
206 function getEnableLimitReport() { return $this->mEnableLimitReport; }
207 function getCleanSignatures() { return $this->mCleanSignatures; }
208 function getExternalLinkTarget() { return $this->mExternalLinkTarget; }
209 function getMath() { $this->optionUsed( 'math' );
210 return $this->mMath; }
211 function getThumbSize() { $this->optionUsed( 'thumbsize' );
212 return $this->mThumbSize; }
213 function getStubThreshold() { $this->optionUsed( 'stubthreshold' );
214 return $this->mStubThreshold; }
216 function getIsPreview() { return $this->mIsPreview; }
217 function getIsSectionPreview() { return $this->mIsSectionPreview; }
218 function getIsPrintable() { $this->optionUsed( 'printable' );
219 return $this->mIsPrintable; }
220 function getUser() { return $this->mUser; }
221 function getPreSaveTransform() { return $this->mPreSaveTransform; }
224 * @param $title Title
225 * @return Skin
226 * @deprecated since 1.18 Use Linker::* instead
228 function getSkin( $title = null ) {
229 wfDeprecated( __METHOD__, '1.18' );
230 return new DummyLinker;
233 function getDateFormat() {
234 $this->optionUsed( 'dateformat' );
235 if ( !isset( $this->mDateFormat ) ) {
236 $this->mDateFormat = $this->mUser->getDatePreference();
238 return $this->mDateFormat;
241 function getTimestamp() {
242 if ( !isset( $this->mTimestamp ) ) {
243 $this->mTimestamp = wfTimestampNow();
245 return $this->mTimestamp;
249 * You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
250 * Using this fragments the cache and is discouraged. Yes, {{int: }} uses this,
251 * producing inconsistent tables (Bug 14404).
253 * @return Language object
254 * @since 1.19
256 function getUserLangObj() {
257 $this->optionUsed( 'userlang' );
258 return $this->mUserLang;
262 * Same as getUserLangObj() but returns a string instead.
264 * @return String Language code
265 * @since 1.17
267 function getUserLang() {
268 return $this->getUserLangObj()->getCode();
271 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); }
272 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); }
273 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); }
274 function setAllowExternalImagesFrom( $x ) { return wfSetVar( $this->mAllowExternalImagesFrom, $x ); }
275 function setEnableImageWhitelist( $x ) { return wfSetVar( $this->mEnableImageWhitelist, $x ); }
276 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); }
277 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); }
278 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); }
279 function setAllowSpecialInclusion( $x ) { return wfSetVar( $this->mAllowSpecialInclusion, $x ); }
280 function setTidy( $x ) { return wfSetVar( $this->mTidy, $x ); }
282 /** @deprecated in 1.19; will be removed in 1.20 */
283 function setSkin( $x ) { wfDeprecated( __METHOD__, '1.19' ); }
284 function setInterfaceMessage( $x ) { return wfSetVar( $this->mInterfaceMessage, $x ); }
285 function setTargetLanguage( $x ) { return wfSetVar( $this->mTargetLanguage, $x, true ); }
286 function setMaxIncludeSize( $x ) { return wfSetVar( $this->mMaxIncludeSize, $x ); }
287 function setMaxPPNodeCount( $x ) { return wfSetVar( $this->mMaxPPNodeCount, $x ); }
288 function setMaxTemplateDepth( $x ) { return wfSetVar( $this->mMaxTemplateDepth, $x ); }
289 function setRemoveComments( $x ) { return wfSetVar( $this->mRemoveComments, $x ); }
290 function setTemplateCallback( $x ) { return wfSetVar( $this->mTemplateCallback, $x ); }
291 function enableLimitReport( $x = true ) { return wfSetVar( $this->mEnableLimitReport, $x ); }
292 function setTimestamp( $x ) { return wfSetVar( $this->mTimestamp, $x ); }
293 function setCleanSignatures( $x ) { return wfSetVar( $this->mCleanSignatures, $x ); }
294 function setExternalLinkTarget( $x ) { return wfSetVar( $this->mExternalLinkTarget, $x ); }
295 function setMath( $x ) { return wfSetVar( $this->mMath, $x ); }
296 function setUserLang( $x ) {
297 if ( is_string( $x ) ) {
298 $x = Language::factory( $x );
300 return wfSetVar( $this->mUserLang, $x );
302 function setThumbSize( $x ) { return wfSetVar( $this->mThumbSize, $x ); }
303 function setStubThreshold( $x ) { return wfSetVar( $this->mStubThreshold, $x ); }
304 function setPreSaveTransform( $x ) { return wfSetVar( $this->mPreSaveTransform, $x ); }
306 function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview, $x ); }
307 function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview, $x ); }
308 function setIsPrintable( $x ) { return wfSetVar( $this->mIsPrintable, $x ); }
311 * Extra key that should be present in the parser cache key.
313 function addExtraKey( $key ) {
314 $this->mExtraKey .= '!' . $key;
318 * Constructor
319 * @param $user User object
320 * @param $lang Language object
322 function __construct( $user = null, $lang = null ) {
323 if ( $user === null ) {
324 global $wgUser;
325 if ( $wgUser === null ) {
326 $user = new User;
327 } else {
328 $user = $wgUser;
331 if ( $lang === null ) {
332 global $wgLang;
333 if ( !StubObject::isRealObject( $wgLang ) ) {
334 $wgLang->_unstub();
336 $lang = $wgLang;
338 $this->initialiseFromUser( $user, $lang );
342 * Get a ParserOptions object from a given user.
343 * Language will be taken from $wgLang.
345 * @param $user User object
346 * @return ParserOptions object
348 public static function newFromUser( $user ) {
349 return new ParserOptions( $user );
353 * Get a ParserOptions object from a given user and language
355 * @param $user User object
356 * @param $lang Language object
357 * @return ParserOptions object
359 public static function newFromUserAndLang( User $user, Language $lang ) {
360 return new ParserOptions( $user, $lang );
364 * Get a ParserOptions object from a IContextSource object
366 * @param $context IContextSource object
367 * @return ParserOptions object
369 public static function newFromContext( IContextSource $context ) {
370 return new ParserOptions( $context->getUser(), $context->getLanguage() );
373 /**
374 * Get user options
376 * @param $user User object
377 * @param $lang Language object
379 private function initialiseFromUser( $user, $lang ) {
380 global $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages,
381 $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion,
382 $wgMaxArticleSize, $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth,
383 $wgCleanSignatures, $wgExternalLinkTarget;
385 wfProfileIn( __METHOD__ );
387 $this->mUseDynamicDates = $wgUseDynamicDates;
388 $this->mInterwikiMagic = $wgInterwikiMagic;
389 $this->mAllowExternalImages = $wgAllowExternalImages;
390 $this->mAllowExternalImagesFrom = $wgAllowExternalImagesFrom;
391 $this->mEnableImageWhitelist = $wgEnableImageWhitelist;
392 $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion;
393 $this->mMaxIncludeSize = $wgMaxArticleSize * 1024;
394 $this->mMaxPPNodeCount = $wgMaxPPNodeCount;
395 $this->mMaxPPExpandDepth = $wgMaxPPExpandDepth;
396 $this->mMaxTemplateDepth = $wgMaxTemplateDepth;
397 $this->mCleanSignatures = $wgCleanSignatures;
398 $this->mExternalLinkTarget = $wgExternalLinkTarget;
400 $this->mUser = $user;
401 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
402 $this->mMath = $user->getOption( 'math' );
403 $this->mThumbSize = $user->getOption( 'thumbsize' );
404 $this->mStubThreshold = $user->getStubThreshold();
405 $this->mUserLang = $lang;
407 wfProfileOut( __METHOD__ );
411 * Registers a callback for tracking which ParserOptions which are used.
412 * This is a private API with the parser.
414 function registerWatcher( $callback ) {
415 $this->onAccessCallback = $callback;
419 * Called when an option is accessed.
421 protected function optionUsed( $optionName ) {
422 if ( $this->onAccessCallback ) {
423 call_user_func( $this->onAccessCallback, $optionName );
428 * Returns the full array of options that would have been used by
429 * in 1.16.
430 * Used to get the old parser cache entries when available.
431 * @return array
433 public static function legacyOptions() {
434 global $wgUseDynamicDates;
435 $legacyOpts = array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
436 if ( $wgUseDynamicDates ) {
437 $legacyOpts[] = 'dateformat';
439 return $legacyOpts;
443 * Generate a hash string with the values set on these ParserOptions
444 * for the keys given in the array.
445 * This will be used as part of the hash key for the parser cache,
446 * so users sharign the options with vary for the same page share
447 * the same cached data safely.
449 * Replaces User::getPageRenderingHash()
451 * Extensions which require it should install 'PageRenderingHash' hook,
452 * which will give them a chance to modify this key based on their own
453 * settings.
455 * @since 1.17
456 * @param $forOptions Array
457 * @param $title Title: used to get the content language of the page (since r97636)
458 * @return string Page rendering hash
460 public function optionsHash( $forOptions, $title = null ) {
461 global $wgRenderHashAppend;
463 $confstr = '';
465 if ( in_array( 'math', $forOptions ) ) {
466 $confstr .= $this->mMath;
467 } else {
468 $confstr .= '*';
472 // Space assigned for the stubthreshold but unused
473 // since it disables the parser cache, its value will always
474 // be 0 when this function is called by parsercache.
475 if ( in_array( 'stubthreshold', $forOptions ) ) {
476 $confstr .= '!' . $this->mStubThreshold;
477 } else {
478 $confstr .= '!*' ;
481 if ( in_array( 'dateformat', $forOptions ) ) {
482 $confstr .= '!' . $this->getDateFormat();
485 if ( in_array( 'numberheadings', $forOptions ) ) {
486 $confstr .= '!' . ( $this->mNumberHeadings ? '1' : '' );
487 } else {
488 $confstr .= '!*';
491 if ( in_array( 'userlang', $forOptions ) ) {
492 $confstr .= '!' . $this->mUserLang->getCode();
493 } else {
494 $confstr .= '!*';
497 if ( in_array( 'thumbsize', $forOptions ) ) {
498 $confstr .= '!' . $this->mThumbSize;
499 } else {
500 $confstr .= '!*';
503 // add in language specific options, if any
504 // @todo FIXME: This is just a way of retrieving the url/user preferred variant
505 if( !is_null( $title ) ) {
506 $confstr .= $title->getPageLanguage()->getExtraHashOptions();
507 } else {
508 global $wgContLang;
509 $confstr .= $wgContLang->getExtraHashOptions();
512 $confstr .= $wgRenderHashAppend;
514 if ( !in_array( 'editsection', $forOptions ) ) {
515 $confstr .= '!*';
516 } elseif ( !$this->mEditSection ) {
517 $confstr .= '!edit=0';
520 if ( $this->mIsPrintable && in_array( 'printable', $forOptions ) ) {
521 $confstr .= '!printable=1';
524 if ( $this->mExtraKey != '' )
525 $confstr .= $this->mExtraKey;
527 // Give a chance for extensions to modify the hash, if they have
528 // extra options or other effects on the parser cache.
529 wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
531 // Make it a valid memcached key fragment
532 $confstr = str_replace( ' ', '_', $confstr );
534 return $confstr;