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 * Interlanguage links are removed and returned in an array
39 * Allow external images inline?
41 var $mAllowExternalImages;
44 * If not, any exception?
46 var $mAllowExternalImagesFrom;
49 * If not or it doesn't match, should we check an on-wiki whitelist?
51 var $mEnableImageWhitelist;
56 var $mDateFormat = null;
59 * Create "edit section" links?
61 var $mEditSection = true;
64 * Allow inclusion of special pages?
66 var $mAllowSpecialInclusion;
69 * Use tidy to cleanup output HTML?
74 * Which lang to call for PLURAL and GRAMMAR
76 var $mInterfaceMessage = false;
79 * Overrides $mInterfaceMessage with arbitrary language
81 var $mTargetLanguage = null;
84 * Maximum size of template expansions, in bytes
89 * Maximum number of nodes touched by PPFrame::expand()
94 * Maximum number of nodes generated by Preprocessor::preprocessToObj()
96 var $mMaxGeneratedPPNodeCount;
99 * Maximum recursion depth in PPFrame::expand()
101 var $mMaxPPExpandDepth;
104 * Maximum recursion depth for templates within templates
106 var $mMaxTemplateDepth;
109 * Maximum number of calls per parse to expensive parser functions
111 var $mExpensiveParserFunctionLimit;
114 * Remove HTML comments. ONLY APPLIES TO PREPROCESS OPERATIONS
116 var $mRemoveComments = true;
119 * Callback for template fetching. Used as first argument to call_user_func().
121 var $mTemplateCallback =
122 array( 'Parser', 'statelessFetchTemplate' );
125 * Enable limit report in an HTML comment on output
127 var $mEnableLimitReport = false;
130 * Timestamp used for {{CURRENTDAY}} etc.
135 * Target attribute for external links
137 var $mExternalLinkTarget;
140 * Clean up signature texts?
142 * 1) Strip ~~~, ~~~~ and ~~~~~ out of signatures
143 * 2) Substitute all transclusions
145 var $mCleanSignatures;
148 * Transform wiki markup when saving the page?
150 var $mPreSaveTransform = true;
153 * Whether content conversion should be disabled
155 var $mDisableContentConversion;
158 * Whether title conversion should be disabled
160 var $mDisableTitleConversion;
163 * Automatically number headings?
165 var $mNumberHeadings;
168 * User math preference (as integer). Not used (1.19)
173 * Thumb size preferred by the user.
178 * Maximum article size of an article to be marked as "stub"
180 private $mStubThreshold;
183 * Language object of the User language.
194 * Parsing the page for a "preview" operation?
196 var $mIsPreview = false;
199 * Parsing the page for a "preview" operation on a single section?
201 var $mIsSectionPreview = false;
204 * Parsing the printable version of the page?
206 var $mIsPrintable = false;
209 * Extra key that should be present in the caching key.
214 * Function to be called when an option is accessed.
216 protected $onAccessCallback = null;
218 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
219 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
220 function getAllowExternalImagesFrom() { return $this->mAllowExternalImagesFrom
; }
221 function getEnableImageWhitelist() { return $this->mEnableImageWhitelist
; }
222 function getEditSection() { return $this->mEditSection
; }
223 function getNumberHeadings() { $this->optionUsed( 'numberheadings' );
224 return $this->mNumberHeadings
; }
225 function getAllowSpecialInclusion() { return $this->mAllowSpecialInclusion
; }
226 function getTidy() { return $this->mTidy
; }
227 function getInterfaceMessage() { return $this->mInterfaceMessage
; }
228 function getTargetLanguage() { return $this->mTargetLanguage
; }
229 function getMaxIncludeSize() { return $this->mMaxIncludeSize
; }
230 function getMaxPPNodeCount() { return $this->mMaxPPNodeCount
; }
231 function getMaxGeneratedPPNodeCount() { return $this->mMaxGeneratedPPNodeCount
; }
232 function getMaxPPExpandDepth() { return $this->mMaxPPExpandDepth
; }
233 function getMaxTemplateDepth() { return $this->mMaxTemplateDepth
; }
235 function getExpensiveParserFunctionLimit() { return $this->mExpensiveParserFunctionLimit
; }
236 function getRemoveComments() { return $this->mRemoveComments
; }
237 function getTemplateCallback() { return $this->mTemplateCallback
; }
238 function getEnableLimitReport() { return $this->mEnableLimitReport
; }
239 function getCleanSignatures() { return $this->mCleanSignatures
; }
240 function getExternalLinkTarget() { return $this->mExternalLinkTarget
; }
241 function getDisableContentConversion() { return $this->mDisableContentConversion
; }
242 function getDisableTitleConversion() { return $this->mDisableTitleConversion
; }
243 /** @deprecated since 1.22 use User::getOption('math') instead */
244 function getMath() { $this->optionUsed( 'math' );
245 return $this->mMath
; }
246 function getThumbSize() { $this->optionUsed( 'thumbsize' );
247 return $this->mThumbSize
; }
248 function getStubThreshold() { $this->optionUsed( 'stubthreshold' );
249 return $this->mStubThreshold
; }
251 function getIsPreview() { return $this->mIsPreview
; }
252 function getIsSectionPreview() { return $this->mIsSectionPreview
; }
253 function getIsPrintable() { $this->optionUsed( 'printable' );
254 return $this->mIsPrintable
; }
255 function getUser() { return $this->mUser
; }
256 function getPreSaveTransform() { return $this->mPreSaveTransform
; }
259 * @param $title Title
261 * @deprecated since 1.18 Use Linker::* instead
263 function getSkin( $title = null ) {
264 wfDeprecated( __METHOD__
, '1.18' );
265 return new DummyLinker
;
268 function getDateFormat() {
269 $this->optionUsed( 'dateformat' );
270 if ( !isset( $this->mDateFormat
) ) {
271 $this->mDateFormat
= $this->mUser
->getDatePreference();
273 return $this->mDateFormat
;
276 function getTimestamp() {
277 if ( !isset( $this->mTimestamp
) ) {
278 $this->mTimestamp
= wfTimestampNow();
280 return $this->mTimestamp
;
284 * Get the user language used by the parser for this page.
286 * You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
288 * To avoid side-effects where the page will be rendered based on the language
289 * of the user who last saved, this function will triger a cache fragmentation.
290 * Usage of this method is discouraged for that reason.
292 * When saving, this will return the default language instead of the user's.
294 * {{int: }} uses this which used to produce inconsistent link tables (bug 14404).
296 * @return Language object
299 function getUserLangObj() {
300 $this->optionUsed( 'userlang' );
301 return $this->mUserLang
;
305 * Same as getUserLangObj() but returns a string instead.
307 * @return String Language code
310 function getUserLang() {
311 return $this->getUserLangObj()->getCode();
314 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
315 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
316 function setAllowExternalImagesFrom( $x ) { return wfSetVar( $this->mAllowExternalImagesFrom
, $x ); }
317 function setEnableImageWhitelist( $x ) { return wfSetVar( $this->mEnableImageWhitelist
, $x ); }
318 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
319 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
320 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
321 function setAllowSpecialInclusion( $x ) { return wfSetVar( $this->mAllowSpecialInclusion
, $x ); }
322 function setTidy( $x ) { return wfSetVar( $this->mTidy
, $x ); }
324 /** @deprecated in 1.19 */
325 function setSkin( $x ) { wfDeprecated( __METHOD__
, '1.19' ); }
326 function setInterfaceMessage( $x ) { return wfSetVar( $this->mInterfaceMessage
, $x ); }
327 function setTargetLanguage( $x ) { return wfSetVar( $this->mTargetLanguage
, $x, true ); }
328 function setMaxIncludeSize( $x ) { return wfSetVar( $this->mMaxIncludeSize
, $x ); }
329 function setMaxPPNodeCount( $x ) { return wfSetVar( $this->mMaxPPNodeCount
, $x ); }
330 function setMaxGeneratedPPNodeCount( $x ) { return wfSetVar( $this->mMaxGeneratedPPNodeCount
, $x ); }
331 function setMaxTemplateDepth( $x ) { return wfSetVar( $this->mMaxTemplateDepth
, $x ); }
333 function setExpensiveParserFunctionLimit( $x ) { return wfSetVar( $this->mExpensiveParserFunctionLimit
, $x ); }
334 function setRemoveComments( $x ) { return wfSetVar( $this->mRemoveComments
, $x ); }
335 function setTemplateCallback( $x ) { return wfSetVar( $this->mTemplateCallback
, $x ); }
336 function enableLimitReport( $x = true ) { return wfSetVar( $this->mEnableLimitReport
, $x ); }
337 function setTimestamp( $x ) { return wfSetVar( $this->mTimestamp
, $x ); }
338 function setCleanSignatures( $x ) { return wfSetVar( $this->mCleanSignatures
, $x ); }
339 function setExternalLinkTarget( $x ) { return wfSetVar( $this->mExternalLinkTarget
, $x ); }
340 function disableContentConversion( $x = true ) { return wfSetVar( $this->mDisableContentConversion
, $x ); }
341 function disableTitleConversion( $x = true ) { return wfSetVar( $this->mDisableTitleConversion
, $x ); }
342 /** @deprecated since 1.22 */
343 function setMath( $x ) { return wfSetVar( $this->mMath
, $x ); }
344 function setUserLang( $x ) {
345 if ( is_string( $x ) ) {
346 $x = Language
::factory( $x );
348 return wfSetVar( $this->mUserLang
, $x );
350 function setThumbSize( $x ) { return wfSetVar( $this->mThumbSize
, $x ); }
351 function setStubThreshold( $x ) { return wfSetVar( $this->mStubThreshold
, $x ); }
352 function setPreSaveTransform( $x ) { return wfSetVar( $this->mPreSaveTransform
, $x ); }
354 function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview
, $x ); }
355 function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview
, $x ); }
356 function setIsPrintable( $x ) { return wfSetVar( $this->mIsPrintable
, $x ); }
359 * Extra key that should be present in the parser cache key.
361 function addExtraKey( $key ) {
362 $this->mExtraKey
.= '!' . $key;
367 * @param $user User object
368 * @param $lang Language object
370 function __construct( $user = null, $lang = null ) {
371 if ( $user === null ) {
373 if ( $wgUser === null ) {
379 if ( $lang === null ) {
381 if ( !StubObject
::isRealObject( $wgLang ) ) {
386 $this->initialiseFromUser( $user, $lang );
390 * Get a ParserOptions object from a given user.
391 * Language will be taken from $wgLang.
393 * @param $user User object
394 * @return ParserOptions object
396 public static function newFromUser( $user ) {
397 return new ParserOptions( $user );
401 * Get a ParserOptions object from a given user and language
403 * @param $user User object
404 * @param $lang Language object
405 * @return ParserOptions object
407 public static function newFromUserAndLang( User
$user, Language
$lang ) {
408 return new ParserOptions( $user, $lang );
412 * Get a ParserOptions object from a IContextSource object
414 * @param $context IContextSource object
415 * @return ParserOptions object
417 public static function newFromContext( IContextSource
$context ) {
418 return new ParserOptions( $context->getUser(), $context->getLanguage() );
424 * @param $user User object
425 * @param $lang Language object
427 private function initialiseFromUser( $user, $lang ) {
428 global $wgInterwikiMagic, $wgAllowExternalImages,
429 $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion,
430 $wgMaxArticleSize, $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth,
431 $wgCleanSignatures, $wgExternalLinkTarget, $wgExpensiveParserFunctionLimit,
432 $wgMaxGeneratedPPNodeCount, $wgDisableLangConversion, $wgDisableTitleConversion;
434 wfProfileIn( __METHOD__
);
436 $this->mInterwikiMagic
= $wgInterwikiMagic;
437 $this->mAllowExternalImages
= $wgAllowExternalImages;
438 $this->mAllowExternalImagesFrom
= $wgAllowExternalImagesFrom;
439 $this->mEnableImageWhitelist
= $wgEnableImageWhitelist;
440 $this->mAllowSpecialInclusion
= $wgAllowSpecialInclusion;
441 $this->mMaxIncludeSize
= $wgMaxArticleSize * 1024;
442 $this->mMaxPPNodeCount
= $wgMaxPPNodeCount;
443 $this->mMaxGeneratedPPNodeCount
= $wgMaxGeneratedPPNodeCount;
444 $this->mMaxPPExpandDepth
= $wgMaxPPExpandDepth;
445 $this->mMaxTemplateDepth
= $wgMaxTemplateDepth;
446 $this->mExpensiveParserFunctionLimit
= $wgExpensiveParserFunctionLimit;
447 $this->mCleanSignatures
= $wgCleanSignatures;
448 $this->mExternalLinkTarget
= $wgExternalLinkTarget;
449 $this->mDisableContentConversion
= $wgDisableLangConversion;
450 $this->mDisableTitleConversion
= $wgDisableLangConversion ||
$wgDisableTitleConversion;
452 $this->mUser
= $user;
453 $this->mNumberHeadings
= $user->getOption( 'numberheadings' );
454 $this->mMath
= $user->getOption( 'math' );
455 $this->mThumbSize
= $user->getOption( 'thumbsize' );
456 $this->mStubThreshold
= $user->getStubThreshold();
457 $this->mUserLang
= $lang;
459 wfProfileOut( __METHOD__
);
463 * Registers a callback for tracking which ParserOptions which are used.
464 * This is a private API with the parser.
466 function registerWatcher( $callback ) {
467 $this->onAccessCallback
= $callback;
471 * Called when an option is accessed.
473 protected function optionUsed( $optionName ) {
474 if ( $this->onAccessCallback
) {
475 call_user_func( $this->onAccessCallback
, $optionName );
480 * Returns the full array of options that would have been used by
482 * Used to get the old parser cache entries when available.
485 public static function legacyOptions() {
486 return array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
490 * Generate a hash string with the values set on these ParserOptions
491 * for the keys given in the array.
492 * This will be used as part of the hash key for the parser cache,
493 * so users sharign the options with vary for the same page share
494 * the same cached data safely.
496 * Replaces User::getPageRenderingHash()
498 * Extensions which require it should install 'PageRenderingHash' hook,
499 * which will give them a chance to modify this key based on their own
503 * @param $forOptions Array
504 * @param $title Title: used to get the content language of the page (since r97636)
505 * @return string Page rendering hash
507 public function optionsHash( $forOptions, $title = null ) {
508 global $wgRenderHashAppend;
512 if ( in_array( 'math', $forOptions ) ) {
513 $confstr .= $this->mMath
;
518 // Space assigned for the stubthreshold but unused
519 // since it disables the parser cache, its value will always
520 // be 0 when this function is called by parsercache.
521 if ( in_array( 'stubthreshold', $forOptions ) ) {
522 $confstr .= '!' . $this->mStubThreshold
;
527 if ( in_array( 'dateformat', $forOptions ) ) {
528 $confstr .= '!' . $this->getDateFormat();
531 if ( in_array( 'numberheadings', $forOptions ) ) {
532 $confstr .= '!' . ( $this->mNumberHeadings ?
'1' : '' );
537 if ( in_array( 'userlang', $forOptions ) ) {
538 $confstr .= '!' . $this->mUserLang
->getCode();
543 if ( in_array( 'thumbsize', $forOptions ) ) {
544 $confstr .= '!' . $this->mThumbSize
;
549 // add in language specific options, if any
550 // @todo FIXME: This is just a way of retrieving the url/user preferred variant
551 if ( !is_null( $title ) ) {
552 $confstr .= $title->getPageLanguage()->getExtraHashOptions();
555 $confstr .= $wgContLang->getExtraHashOptions();
558 $confstr .= $wgRenderHashAppend;
560 if ( !in_array( 'editsection', $forOptions ) ) {
562 } elseif ( !$this->mEditSection
) {
563 $confstr .= '!edit=0';
566 if ( $this->mIsPrintable
&& in_array( 'printable', $forOptions ) ) {
567 $confstr .= '!printable=1';
570 if ( $this->mExtraKey
!= '' ) {
571 $confstr .= $this->mExtraKey
;
574 // Give a chance for extensions to modify the hash, if they have
575 // extra options or other effects on the parser cache.
576 wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
578 // Make it a valid memcached key fragment
579 $confstr = str_replace( ' ', '_', $confstr );