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 * Automatically number headings?
160 var $mNumberHeadings;
163 * User math preference (as integer). Not used (1.19)
168 * Thumb size preferred by the user.
173 * Maximum article size of an article to be marked as "stub"
175 private $mStubThreshold;
178 * Language object of the User language.
189 * Parsing the page for a "preview" operation?
191 var $mIsPreview = false;
194 * Parsing the page for a "preview" operation on a single section?
196 var $mIsSectionPreview = false;
199 * Parsing the printable version of the page?
201 var $mIsPrintable = false;
204 * Extra key that should be present in the caching key.
209 * Function to be called when an option is accessed.
211 protected $onAccessCallback = null;
213 function getUseDynamicDates() { return $this->mUseDynamicDates
; }
214 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
215 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
216 function getAllowExternalImagesFrom() { return $this->mAllowExternalImagesFrom
; }
217 function getEnableImageWhitelist() { return $this->mEnableImageWhitelist
; }
218 function getEditSection() { return $this->mEditSection
; }
219 function getNumberHeadings() { $this->optionUsed( 'numberheadings' );
220 return $this->mNumberHeadings
; }
221 function getAllowSpecialInclusion() { return $this->mAllowSpecialInclusion
; }
222 function getTidy() { return $this->mTidy
; }
223 function getInterfaceMessage() { return $this->mInterfaceMessage
; }
224 function getTargetLanguage() { return $this->mTargetLanguage
; }
225 function getMaxIncludeSize() { return $this->mMaxIncludeSize
; }
226 function getMaxPPNodeCount() { return $this->mMaxPPNodeCount
; }
227 function getMaxGeneratedPPNodeCount() { return $this->mMaxGeneratedPPNodeCount
; }
228 function getMaxPPExpandDepth() { return $this->mMaxPPExpandDepth
; }
229 function getMaxTemplateDepth() { return $this->mMaxTemplateDepth
; }
231 function getExpensiveParserFunctionLimit() { return $this->mExpensiveParserFunctionLimit
; }
232 function getRemoveComments() { return $this->mRemoveComments
; }
233 function getTemplateCallback() { return $this->mTemplateCallback
; }
234 function getEnableLimitReport() { return $this->mEnableLimitReport
; }
235 function getCleanSignatures() { return $this->mCleanSignatures
; }
236 function getExternalLinkTarget() { return $this->mExternalLinkTarget
; }
237 function getMath() { $this->optionUsed( 'math' );
238 return $this->mMath
; }
239 function getThumbSize() { $this->optionUsed( 'thumbsize' );
240 return $this->mThumbSize
; }
241 function getStubThreshold() { $this->optionUsed( 'stubthreshold' );
242 return $this->mStubThreshold
; }
244 function getIsPreview() { return $this->mIsPreview
; }
245 function getIsSectionPreview() { return $this->mIsSectionPreview
; }
246 function getIsPrintable() { $this->optionUsed( 'printable' );
247 return $this->mIsPrintable
; }
248 function getUser() { return $this->mUser
; }
249 function getPreSaveTransform() { return $this->mPreSaveTransform
; }
252 * @param $title Title
254 * @deprecated since 1.18 Use Linker::* instead
256 function getSkin( $title = null ) {
257 wfDeprecated( __METHOD__
, '1.18' );
258 return new DummyLinker
;
261 function getDateFormat() {
262 $this->optionUsed( 'dateformat' );
263 if ( !isset( $this->mDateFormat
) ) {
264 $this->mDateFormat
= $this->mUser
->getDatePreference();
266 return $this->mDateFormat
;
269 function getTimestamp() {
270 if ( !isset( $this->mTimestamp
) ) {
271 $this->mTimestamp
= wfTimestampNow();
273 return $this->mTimestamp
;
277 * You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
278 * Using this fragments the cache and is discouraged. Yes, {{int: }} uses this,
279 * producing inconsistent tables (Bug 14404).
281 * @return Language object
284 function getUserLangObj() {
285 $this->optionUsed( 'userlang' );
286 return $this->mUserLang
;
290 * Same as getUserLangObj() but returns a string instead.
292 * @return String Language code
295 function getUserLang() {
296 return $this->getUserLangObj()->getCode();
299 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
300 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
301 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
302 function setAllowExternalImagesFrom( $x ) { return wfSetVar( $this->mAllowExternalImagesFrom
, $x ); }
303 function setEnableImageWhitelist( $x ) { return wfSetVar( $this->mEnableImageWhitelist
, $x ); }
304 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
305 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
306 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
307 function setAllowSpecialInclusion( $x ) { return wfSetVar( $this->mAllowSpecialInclusion
, $x ); }
308 function setTidy( $x ) { return wfSetVar( $this->mTidy
, $x ); }
310 /** @deprecated in 1.19; will be removed in 1.20 */
311 function setSkin( $x ) { wfDeprecated( __METHOD__
, '1.19' ); }
312 function setInterfaceMessage( $x ) { return wfSetVar( $this->mInterfaceMessage
, $x ); }
313 function setTargetLanguage( $x ) { return wfSetVar( $this->mTargetLanguage
, $x, true ); }
314 function setMaxIncludeSize( $x ) { return wfSetVar( $this->mMaxIncludeSize
, $x ); }
315 function setMaxPPNodeCount( $x ) { return wfSetVar( $this->mMaxPPNodeCount
, $x ); }
316 function setMaxGeneratedPPNodeCount( $x ) { return wfSetVar( $this->mMaxGeneratedPPNodeCount
, $x ); }
317 function setMaxTemplateDepth( $x ) { return wfSetVar( $this->mMaxTemplateDepth
, $x ); }
319 function setExpensiveParserFunctionLimit( $x ) { return wfSetVar( $this->mExpensiveParserFunctionLimit
, $x ); }
320 function setRemoveComments( $x ) { return wfSetVar( $this->mRemoveComments
, $x ); }
321 function setTemplateCallback( $x ) { return wfSetVar( $this->mTemplateCallback
, $x ); }
322 function enableLimitReport( $x = true ) { return wfSetVar( $this->mEnableLimitReport
, $x ); }
323 function setTimestamp( $x ) { return wfSetVar( $this->mTimestamp
, $x ); }
324 function setCleanSignatures( $x ) { return wfSetVar( $this->mCleanSignatures
, $x ); }
325 function setExternalLinkTarget( $x ) { return wfSetVar( $this->mExternalLinkTarget
, $x ); }
326 function setMath( $x ) { return wfSetVar( $this->mMath
, $x ); }
327 function setUserLang( $x ) {
328 if ( is_string( $x ) ) {
329 $x = Language
::factory( $x );
331 return wfSetVar( $this->mUserLang
, $x );
333 function setThumbSize( $x ) { return wfSetVar( $this->mThumbSize
, $x ); }
334 function setStubThreshold( $x ) { return wfSetVar( $this->mStubThreshold
, $x ); }
335 function setPreSaveTransform( $x ) { return wfSetVar( $this->mPreSaveTransform
, $x ); }
337 function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview
, $x ); }
338 function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview
, $x ); }
339 function setIsPrintable( $x ) { return wfSetVar( $this->mIsPrintable
, $x ); }
342 * Extra key that should be present in the parser cache key.
344 function addExtraKey( $key ) {
345 $this->mExtraKey
.= '!' . $key;
350 * @param $user User object
351 * @param $lang Language object
353 function __construct( $user = null, $lang = null ) {
354 if ( $user === null ) {
356 if ( $wgUser === null ) {
362 if ( $lang === null ) {
364 if ( !StubObject
::isRealObject( $wgLang ) ) {
369 $this->initialiseFromUser( $user, $lang );
373 * Get a ParserOptions object from a given user.
374 * Language will be taken from $wgLang.
376 * @param $user User object
377 * @return ParserOptions object
379 public static function newFromUser( $user ) {
380 return new ParserOptions( $user );
384 * Get a ParserOptions object from a given user and language
386 * @param $user User object
387 * @param $lang Language object
388 * @return ParserOptions object
390 public static function newFromUserAndLang( User
$user, Language
$lang ) {
391 return new ParserOptions( $user, $lang );
395 * Get a ParserOptions object from a IContextSource object
397 * @param $context IContextSource object
398 * @return ParserOptions object
400 public static function newFromContext( IContextSource
$context ) {
401 return new ParserOptions( $context->getUser(), $context->getLanguage() );
407 * @param $user User object
408 * @param $lang Language object
410 private function initialiseFromUser( $user, $lang ) {
411 global $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages,
412 $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion,
413 $wgMaxArticleSize, $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth,
414 $wgCleanSignatures, $wgExternalLinkTarget, $wgExpensiveParserFunctionLimit,
415 $wgMaxGeneratedPPNodeCount;
417 wfProfileIn( __METHOD__
);
419 $this->mUseDynamicDates
= $wgUseDynamicDates;
420 $this->mInterwikiMagic
= $wgInterwikiMagic;
421 $this->mAllowExternalImages
= $wgAllowExternalImages;
422 $this->mAllowExternalImagesFrom
= $wgAllowExternalImagesFrom;
423 $this->mEnableImageWhitelist
= $wgEnableImageWhitelist;
424 $this->mAllowSpecialInclusion
= $wgAllowSpecialInclusion;
425 $this->mMaxIncludeSize
= $wgMaxArticleSize * 1024;
426 $this->mMaxPPNodeCount
= $wgMaxPPNodeCount;
427 $this->mMaxGeneratedPPNodeCount
= $wgMaxGeneratedPPNodeCount;
428 $this->mMaxPPExpandDepth
= $wgMaxPPExpandDepth;
429 $this->mMaxTemplateDepth
= $wgMaxTemplateDepth;
430 $this->mExpensiveParserFunctionLimit
= $wgExpensiveParserFunctionLimit;
431 $this->mCleanSignatures
= $wgCleanSignatures;
432 $this->mExternalLinkTarget
= $wgExternalLinkTarget;
434 $this->mUser
= $user;
435 $this->mNumberHeadings
= $user->getOption( 'numberheadings' );
436 $this->mMath
= $user->getOption( 'math' );
437 $this->mThumbSize
= $user->getOption( 'thumbsize' );
438 $this->mStubThreshold
= $user->getStubThreshold();
439 $this->mUserLang
= $lang;
441 wfProfileOut( __METHOD__
);
445 * Registers a callback for tracking which ParserOptions which are used.
446 * This is a private API with the parser.
448 function registerWatcher( $callback ) {
449 $this->onAccessCallback
= $callback;
453 * Called when an option is accessed.
455 protected function optionUsed( $optionName ) {
456 if ( $this->onAccessCallback
) {
457 call_user_func( $this->onAccessCallback
, $optionName );
462 * Returns the full array of options that would have been used by
464 * Used to get the old parser cache entries when available.
467 public static function legacyOptions() {
468 global $wgUseDynamicDates;
469 $legacyOpts = array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
470 if ( $wgUseDynamicDates ) {
471 $legacyOpts[] = 'dateformat';
477 * Generate a hash string with the values set on these ParserOptions
478 * for the keys given in the array.
479 * This will be used as part of the hash key for the parser cache,
480 * so users sharign the options with vary for the same page share
481 * the same cached data safely.
483 * Replaces User::getPageRenderingHash()
485 * Extensions which require it should install 'PageRenderingHash' hook,
486 * which will give them a chance to modify this key based on their own
490 * @param $forOptions Array
491 * @param $title Title: used to get the content language of the page (since r97636)
492 * @return string Page rendering hash
494 public function optionsHash( $forOptions, $title = null ) {
495 global $wgRenderHashAppend;
499 if ( in_array( 'math', $forOptions ) ) {
500 $confstr .= $this->mMath
;
506 // Space assigned for the stubthreshold but unused
507 // since it disables the parser cache, its value will always
508 // be 0 when this function is called by parsercache.
509 if ( in_array( 'stubthreshold', $forOptions ) ) {
510 $confstr .= '!' . $this->mStubThreshold
;
515 if ( in_array( 'dateformat', $forOptions ) ) {
516 $confstr .= '!' . $this->getDateFormat();
519 if ( in_array( 'numberheadings', $forOptions ) ) {
520 $confstr .= '!' . ( $this->mNumberHeadings ?
'1' : '' );
525 if ( in_array( 'userlang', $forOptions ) ) {
526 $confstr .= '!' . $this->mUserLang
->getCode();
531 if ( in_array( 'thumbsize', $forOptions ) ) {
532 $confstr .= '!' . $this->mThumbSize
;
537 // add in language specific options, if any
538 // @todo FIXME: This is just a way of retrieving the url/user preferred variant
539 if( !is_null( $title ) ) {
540 $confstr .= $title->getPageLanguage()->getExtraHashOptions();
543 $confstr .= $wgContLang->getExtraHashOptions();
546 $confstr .= $wgRenderHashAppend;
548 if ( !in_array( 'editsection', $forOptions ) ) {
550 } elseif ( !$this->mEditSection
) {
551 $confstr .= '!edit=0';
554 if ( $this->mIsPrintable
&& in_array( 'printable', $forOptions ) ) {
555 $confstr .= '!printable=1';
558 if ( $this->mExtraKey
!= '' )
559 $confstr .= $this->mExtraKey
;
561 // Give a chance for extensions to modify the hash, if they have
562 // extra options or other effects on the parser cache.
563 wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
565 // Make it a valid memcached key fragment
566 $confstr = str_replace( ' ', '_', $confstr );