3 * Options for the PHP parser
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * \brief Set options of the Parser
27 * All member variables are supposed to be private in theory, although in practise this is not the case.
34 * Use DateFormatter to format dates
36 var $mUseDynamicDates;
39 * Interlanguage links are removed and returned in an array
44 * Allow external images inline?
46 var $mAllowExternalImages;
49 * If not, any exception?
51 var $mAllowExternalImagesFrom;
54 * If not or it doesn't match, should we check an on-wiki whitelist?
56 var $mEnableImageWhitelist;
61 var $mDateFormat = null;
64 * Create "edit section" links?
66 var $mEditSection = true;
69 * Allow inclusion of special pages?
71 var $mAllowSpecialInclusion;
74 * Use tidy to cleanup output HTML?
79 * Which lang to call for PLURAL and GRAMMAR
81 var $mInterfaceMessage = false;
84 * Overrides $mInterfaceMessage with arbitrary language
86 var $mTargetLanguage = null;
89 * Maximum size of template expansions, in bytes
94 * Maximum number of nodes touched by PPFrame::expand()
99 * Maximum number of nodes generated by Preprocessor::preprocessToObj()
101 var $mMaxGeneratedPPNodeCount;
104 * Maximum recursion depth in PPFrame::expand()
106 var $mMaxPPExpandDepth;
109 * Maximum recursion depth for templates within templates
111 var $mMaxTemplateDepth;
114 * Maximum number of calls per parse to expensive parser functions
116 var $mExpensiveParserFunctionLimit;
119 * Remove HTML comments. ONLY APPLIES TO PREPROCESS OPERATIONS
121 var $mRemoveComments = true;
124 * Callback for template fetching. Used as first argument to call_user_func().
126 var $mTemplateCallback =
127 array( 'Parser', 'statelessFetchTemplate' );
130 * Enable limit report in an HTML comment on output
132 var $mEnableLimitReport = false;
135 * Timestamp used for {{CURRENTDAY}} etc.
140 * Target attribute for external links
142 var $mExternalLinkTarget;
145 * Clean up signature texts?
147 * 1) Strip ~~~, ~~~~ and ~~~~~ out of signatures
148 * 2) Substitute all transclusions
150 var $mCleanSignatures;
153 * Transform wiki markup when saving the page?
155 var $mPreSaveTransform = true;
158 * Whether content conversion should be disabled
160 var $mDisableContentConversion;
163 * Whether title conversion should be disabled
165 var $mDisableTitleConversion;
168 * Automatically number headings?
170 var $mNumberHeadings;
173 * User math preference (as integer). Not used (1.19)
178 * Thumb size preferred by the user.
183 * Maximum article size of an article to be marked as "stub"
185 private $mStubThreshold;
188 * Language object of the User language.
199 * Parsing the page for a "preview" operation?
201 var $mIsPreview = false;
204 * Parsing the page for a "preview" operation on a single section?
206 var $mIsSectionPreview = false;
209 * Parsing the printable version of the page?
211 var $mIsPrintable = false;
214 * Extra key that should be present in the caching key.
219 * Function to be called when an option is accessed.
221 protected $onAccessCallback = null;
223 function getUseDynamicDates() { return $this->mUseDynamicDates
; }
224 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
225 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
226 function getAllowExternalImagesFrom() { return $this->mAllowExternalImagesFrom
; }
227 function getEnableImageWhitelist() { return $this->mEnableImageWhitelist
; }
228 function getEditSection() { return $this->mEditSection
; }
229 function getNumberHeadings() { $this->optionUsed( 'numberheadings' );
230 return $this->mNumberHeadings
; }
231 function getAllowSpecialInclusion() { return $this->mAllowSpecialInclusion
; }
232 function getTidy() { return $this->mTidy
; }
233 function getInterfaceMessage() { return $this->mInterfaceMessage
; }
234 function getTargetLanguage() { return $this->mTargetLanguage
; }
235 function getMaxIncludeSize() { return $this->mMaxIncludeSize
; }
236 function getMaxPPNodeCount() { return $this->mMaxPPNodeCount
; }
237 function getMaxGeneratedPPNodeCount() { return $this->mMaxGeneratedPPNodeCount
; }
238 function getMaxPPExpandDepth() { return $this->mMaxPPExpandDepth
; }
239 function getMaxTemplateDepth() { return $this->mMaxTemplateDepth
; }
241 function getExpensiveParserFunctionLimit() { return $this->mExpensiveParserFunctionLimit
; }
242 function getRemoveComments() { return $this->mRemoveComments
; }
243 function getTemplateCallback() { return $this->mTemplateCallback
; }
244 function getEnableLimitReport() { return $this->mEnableLimitReport
; }
245 function getCleanSignatures() { return $this->mCleanSignatures
; }
246 function getExternalLinkTarget() { return $this->mExternalLinkTarget
; }
247 function getDisableContentConversion() { return $this->mDisableContentConversion
; }
248 function getDisableTitleConversion() { return $this->mDisableTitleConversion
; }
249 function getMath() { $this->optionUsed( 'math' );
250 return $this->mMath
; }
251 function getThumbSize() { $this->optionUsed( 'thumbsize' );
252 return $this->mThumbSize
; }
253 function getStubThreshold() { $this->optionUsed( 'stubthreshold' );
254 return $this->mStubThreshold
; }
256 function getIsPreview() { return $this->mIsPreview
; }
257 function getIsSectionPreview() { return $this->mIsSectionPreview
; }
258 function getIsPrintable() { $this->optionUsed( 'printable' );
259 return $this->mIsPrintable
; }
260 function getUser() { return $this->mUser
; }
261 function getPreSaveTransform() { return $this->mPreSaveTransform
; }
264 * @param $title Title
266 * @deprecated since 1.18 Use Linker::* instead
268 function getSkin( $title = null ) {
269 wfDeprecated( __METHOD__
, '1.18' );
270 return new DummyLinker
;
273 function getDateFormat() {
274 $this->optionUsed( 'dateformat' );
275 if ( !isset( $this->mDateFormat
) ) {
276 $this->mDateFormat
= $this->mUser
->getDatePreference();
278 return $this->mDateFormat
;
281 function getTimestamp() {
282 if ( !isset( $this->mTimestamp
) ) {
283 $this->mTimestamp
= wfTimestampNow();
285 return $this->mTimestamp
;
289 * You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
290 * Using this fragments the cache and is discouraged. Yes, {{int: }} uses this,
291 * producing inconsistent tables (Bug 14404).
293 * @return Language object
296 function getUserLangObj() {
297 $this->optionUsed( 'userlang' );
298 return $this->mUserLang
;
302 * Same as getUserLangObj() but returns a string instead.
304 * @return String Language code
307 function getUserLang() {
308 return $this->getUserLangObj()->getCode();
311 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
312 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
313 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
314 function setAllowExternalImagesFrom( $x ) { return wfSetVar( $this->mAllowExternalImagesFrom
, $x ); }
315 function setEnableImageWhitelist( $x ) { return wfSetVar( $this->mEnableImageWhitelist
, $x ); }
316 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
317 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
318 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
319 function setAllowSpecialInclusion( $x ) { return wfSetVar( $this->mAllowSpecialInclusion
, $x ); }
320 function setTidy( $x ) { return wfSetVar( $this->mTidy
, $x ); }
322 /** @deprecated in 1.19; will be removed in 1.20 */
323 function setSkin( $x ) { wfDeprecated( __METHOD__
, '1.19' ); }
324 function setInterfaceMessage( $x ) { return wfSetVar( $this->mInterfaceMessage
, $x ); }
325 function setTargetLanguage( $x ) { return wfSetVar( $this->mTargetLanguage
, $x, true ); }
326 function setMaxIncludeSize( $x ) { return wfSetVar( $this->mMaxIncludeSize
, $x ); }
327 function setMaxPPNodeCount( $x ) { return wfSetVar( $this->mMaxPPNodeCount
, $x ); }
328 function setMaxGeneratedPPNodeCount( $x ) { return wfSetVar( $this->mMaxGeneratedPPNodeCount
, $x ); }
329 function setMaxTemplateDepth( $x ) { return wfSetVar( $this->mMaxTemplateDepth
, $x ); }
331 function setExpensiveParserFunctionLimit( $x ) { return wfSetVar( $this->mExpensiveParserFunctionLimit
, $x ); }
332 function setRemoveComments( $x ) { return wfSetVar( $this->mRemoveComments
, $x ); }
333 function setTemplateCallback( $x ) { return wfSetVar( $this->mTemplateCallback
, $x ); }
334 function enableLimitReport( $x = true ) { return wfSetVar( $this->mEnableLimitReport
, $x ); }
335 function setTimestamp( $x ) { return wfSetVar( $this->mTimestamp
, $x ); }
336 function setCleanSignatures( $x ) { return wfSetVar( $this->mCleanSignatures
, $x ); }
337 function setExternalLinkTarget( $x ) { return wfSetVar( $this->mExternalLinkTarget
, $x ); }
338 function disableContentConversion( $x = true ) { return wfSetVar( $this->mDisableContentConversion
, $x ); }
339 function disableTitleConversion( $x = true ) { return wfSetVar( $this->mDisableTitleConversion
, $x ); }
340 function setMath( $x ) { return wfSetVar( $this->mMath
, $x ); }
341 function setUserLang( $x ) {
342 if ( is_string( $x ) ) {
343 $x = Language
::factory( $x );
345 return wfSetVar( $this->mUserLang
, $x );
347 function setThumbSize( $x ) { return wfSetVar( $this->mThumbSize
, $x ); }
348 function setStubThreshold( $x ) { return wfSetVar( $this->mStubThreshold
, $x ); }
349 function setPreSaveTransform( $x ) { return wfSetVar( $this->mPreSaveTransform
, $x ); }
351 function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview
, $x ); }
352 function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview
, $x ); }
353 function setIsPrintable( $x ) { return wfSetVar( $this->mIsPrintable
, $x ); }
356 * Extra key that should be present in the parser cache key.
358 function addExtraKey( $key ) {
359 $this->mExtraKey
.= '!' . $key;
364 * @param $user User object
365 * @param $lang Language object
367 function __construct( $user = null, $lang = null ) {
368 if ( $user === null ) {
370 if ( $wgUser === null ) {
376 if ( $lang === null ) {
378 if ( !StubObject
::isRealObject( $wgLang ) ) {
383 $this->initialiseFromUser( $user, $lang );
387 * Get a ParserOptions object from a given user.
388 * Language will be taken from $wgLang.
390 * @param $user User object
391 * @return ParserOptions object
393 public static function newFromUser( $user ) {
394 return new ParserOptions( $user );
398 * Get a ParserOptions object from a given user and language
400 * @param $user User object
401 * @param $lang Language object
402 * @return ParserOptions object
404 public static function newFromUserAndLang( User
$user, Language
$lang ) {
405 return new ParserOptions( $user, $lang );
409 * Get a ParserOptions object from a IContextSource object
411 * @param $context IContextSource object
412 * @return ParserOptions object
414 public static function newFromContext( IContextSource
$context ) {
415 return new ParserOptions( $context->getUser(), $context->getLanguage() );
421 * @param $user User object
422 * @param $lang Language object
424 private function initialiseFromUser( $user, $lang ) {
425 global $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages,
426 $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion,
427 $wgMaxArticleSize, $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth,
428 $wgCleanSignatures, $wgExternalLinkTarget, $wgExpensiveParserFunctionLimit,
429 $wgMaxGeneratedPPNodeCount, $wgDisableLangConversion, $wgDisableTitleConversion;
431 wfProfileIn( __METHOD__
);
433 $this->mUseDynamicDates
= $wgUseDynamicDates;
434 $this->mInterwikiMagic
= $wgInterwikiMagic;
435 $this->mAllowExternalImages
= $wgAllowExternalImages;
436 $this->mAllowExternalImagesFrom
= $wgAllowExternalImagesFrom;
437 $this->mEnableImageWhitelist
= $wgEnableImageWhitelist;
438 $this->mAllowSpecialInclusion
= $wgAllowSpecialInclusion;
439 $this->mMaxIncludeSize
= $wgMaxArticleSize * 1024;
440 $this->mMaxPPNodeCount
= $wgMaxPPNodeCount;
441 $this->mMaxGeneratedPPNodeCount
= $wgMaxGeneratedPPNodeCount;
442 $this->mMaxPPExpandDepth
= $wgMaxPPExpandDepth;
443 $this->mMaxTemplateDepth
= $wgMaxTemplateDepth;
444 $this->mExpensiveParserFunctionLimit
= $wgExpensiveParserFunctionLimit;
445 $this->mCleanSignatures
= $wgCleanSignatures;
446 $this->mExternalLinkTarget
= $wgExternalLinkTarget;
447 $this->mDisableContentConversion
= $wgDisableLangConversion;
448 $this->mDisableTitleConversion
= $wgDisableLangConversion ||
$wgDisableTitleConversion;
450 $this->mUser
= $user;
451 $this->mNumberHeadings
= $user->getOption( 'numberheadings' );
452 $this->mMath
= $user->getOption( 'math' );
453 $this->mThumbSize
= $user->getOption( 'thumbsize' );
454 $this->mStubThreshold
= $user->getStubThreshold();
455 $this->mUserLang
= $lang;
457 wfProfileOut( __METHOD__
);
461 * Registers a callback for tracking which ParserOptions which are used.
462 * This is a private API with the parser.
464 function registerWatcher( $callback ) {
465 $this->onAccessCallback
= $callback;
469 * Called when an option is accessed.
471 protected function optionUsed( $optionName ) {
472 if ( $this->onAccessCallback
) {
473 call_user_func( $this->onAccessCallback
, $optionName );
478 * Returns the full array of options that would have been used by
480 * Used to get the old parser cache entries when available.
483 public static function legacyOptions() {
484 global $wgUseDynamicDates;
485 $legacyOpts = array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
486 if ( $wgUseDynamicDates ) {
487 $legacyOpts[] = 'dateformat';
493 * Generate a hash string with the values set on these ParserOptions
494 * for the keys given in the array.
495 * This will be used as part of the hash key for the parser cache,
496 * so users sharign the options with vary for the same page share
497 * the same cached data safely.
499 * Replaces User::getPageRenderingHash()
501 * Extensions which require it should install 'PageRenderingHash' hook,
502 * which will give them a chance to modify this key based on their own
506 * @param $forOptions Array
507 * @param $title Title: used to get the content language of the page (since r97636)
508 * @return string Page rendering hash
510 public function optionsHash( $forOptions, $title = null ) {
511 global $wgRenderHashAppend;
515 if ( in_array( 'math', $forOptions ) ) {
516 $confstr .= $this->mMath
;
522 // Space assigned for the stubthreshold but unused
523 // since it disables the parser cache, its value will always
524 // be 0 when this function is called by parsercache.
525 if ( in_array( 'stubthreshold', $forOptions ) ) {
526 $confstr .= '!' . $this->mStubThreshold
;
531 if ( in_array( 'dateformat', $forOptions ) ) {
532 $confstr .= '!' . $this->getDateFormat();
535 if ( in_array( 'numberheadings', $forOptions ) ) {
536 $confstr .= '!' . ( $this->mNumberHeadings ?
'1' : '' );
541 if ( in_array( 'userlang', $forOptions ) ) {
542 $confstr .= '!' . $this->mUserLang
->getCode();
547 if ( in_array( 'thumbsize', $forOptions ) ) {
548 $confstr .= '!' . $this->mThumbSize
;
553 // add in language specific options, if any
554 // @todo FIXME: This is just a way of retrieving the url/user preferred variant
555 if( !is_null( $title ) ) {
556 $confstr .= $title->getPageLanguage()->getExtraHashOptions();
559 $confstr .= $wgContLang->getExtraHashOptions();
562 $confstr .= $wgRenderHashAppend;
564 if ( !in_array( 'editsection', $forOptions ) ) {
566 } elseif ( !$this->mEditSection
) {
567 $confstr .= '!edit=0';
570 if ( $this->mIsPrintable
&& in_array( 'printable', $forOptions ) ) {
571 $confstr .= '!printable=1';
574 if ( $this->mExtraKey
!= '' )
575 $confstr .= $this->mExtraKey
;
577 // Give a chance for extensions to modify the hash, if they have
578 // extra options or other effects on the parser cache.
579 wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
581 // Make it a valid memcached key fragment
582 $confstr = str_replace( ' ', '_', $confstr );