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 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 * Automatically number headings?
155 var $mNumberHeadings;
158 * User math preference (as integer). Not used (1.19)
163 * Thumb size preferred by the user.
168 * Maximum article size of an article to be marked as "stub"
170 private $mStubThreshold;
173 * Language object of the User language.
184 * Parsing the page for a "preview" operation?
186 var $mIsPreview = false;
189 * Parsing the page for a "preview" operation on a single section?
191 var $mIsSectionPreview = false;
194 * Parsing the printable version of the page?
196 var $mIsPrintable = false;
199 * Extra key that should be present in the caching key.
204 * Function to be called when an option is accessed.
206 protected $onAccessCallback = null;
208 function getUseDynamicDates() { return $this->mUseDynamicDates
; }
209 function getInterwikiMagic() { return $this->mInterwikiMagic
; }
210 function getAllowExternalImages() { return $this->mAllowExternalImages
; }
211 function getAllowExternalImagesFrom() { return $this->mAllowExternalImagesFrom
; }
212 function getEnableImageWhitelist() { return $this->mEnableImageWhitelist
; }
213 function getEditSection() { return $this->mEditSection
; }
214 function getNumberHeadings() { $this->optionUsed( 'numberheadings' );
215 return $this->mNumberHeadings
; }
216 function getAllowSpecialInclusion() { return $this->mAllowSpecialInclusion
; }
217 function getTidy() { return $this->mTidy
; }
218 function getInterfaceMessage() { return $this->mInterfaceMessage
; }
219 function getTargetLanguage() { return $this->mTargetLanguage
; }
220 function getMaxIncludeSize() { return $this->mMaxIncludeSize
; }
221 function getMaxPPNodeCount() { return $this->mMaxPPNodeCount
; }
222 function getMaxPPExpandDepth() { return $this->mMaxPPExpandDepth
; }
223 function getMaxTemplateDepth() { return $this->mMaxTemplateDepth
; }
225 function getExpensiveParserFunctionLimit() { return $this->mExpensiveParserFunctionLimit
; }
226 function getRemoveComments() { return $this->mRemoveComments
; }
227 function getTemplateCallback() { return $this->mTemplateCallback
; }
228 function getEnableLimitReport() { return $this->mEnableLimitReport
; }
229 function getCleanSignatures() { return $this->mCleanSignatures
; }
230 function getExternalLinkTarget() { return $this->mExternalLinkTarget
; }
231 function getMath() { $this->optionUsed( 'math' );
232 return $this->mMath
; }
233 function getThumbSize() { $this->optionUsed( 'thumbsize' );
234 return $this->mThumbSize
; }
235 function getStubThreshold() { $this->optionUsed( 'stubthreshold' );
236 return $this->mStubThreshold
; }
238 function getIsPreview() { return $this->mIsPreview
; }
239 function getIsSectionPreview() { return $this->mIsSectionPreview
; }
240 function getIsPrintable() { $this->optionUsed( 'printable' );
241 return $this->mIsPrintable
; }
242 function getUser() { return $this->mUser
; }
243 function getPreSaveTransform() { return $this->mPreSaveTransform
; }
246 * @param $title Title
248 * @deprecated since 1.18 Use Linker::* instead
250 function getSkin( $title = null ) {
251 wfDeprecated( __METHOD__
, '1.18' );
252 return new DummyLinker
;
255 function getDateFormat() {
256 $this->optionUsed( 'dateformat' );
257 if ( !isset( $this->mDateFormat
) ) {
258 $this->mDateFormat
= $this->mUser
->getDatePreference();
260 return $this->mDateFormat
;
263 function getTimestamp() {
264 if ( !isset( $this->mTimestamp
) ) {
265 $this->mTimestamp
= wfTimestampNow();
267 return $this->mTimestamp
;
271 * You shouldn't use this. Really. $parser->getFunctionLang() is all you need.
272 * Using this fragments the cache and is discouraged. Yes, {{int: }} uses this,
273 * producing inconsistent tables (Bug 14404).
275 * @return Language object
278 function getUserLangObj() {
279 $this->optionUsed( 'userlang' );
280 return $this->mUserLang
;
284 * Same as getUserLangObj() but returns a string instead.
286 * @return String Language code
289 function getUserLang() {
290 return $this->getUserLangObj()->getCode();
293 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates
, $x ); }
294 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic
, $x ); }
295 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages
, $x ); }
296 function setAllowExternalImagesFrom( $x ) { return wfSetVar( $this->mAllowExternalImagesFrom
, $x ); }
297 function setEnableImageWhitelist( $x ) { return wfSetVar( $this->mEnableImageWhitelist
, $x ); }
298 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat
, $x ); }
299 function setEditSection( $x ) { return wfSetVar( $this->mEditSection
, $x ); }
300 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings
, $x ); }
301 function setAllowSpecialInclusion( $x ) { return wfSetVar( $this->mAllowSpecialInclusion
, $x ); }
302 function setTidy( $x ) { return wfSetVar( $this->mTidy
, $x ); }
304 /** @deprecated in 1.19; will be removed in 1.20 */
305 function setSkin( $x ) { wfDeprecated( __METHOD__
, '1.19' ); }
306 function setInterfaceMessage( $x ) { return wfSetVar( $this->mInterfaceMessage
, $x ); }
307 function setTargetLanguage( $x ) { return wfSetVar( $this->mTargetLanguage
, $x, true ); }
308 function setMaxIncludeSize( $x ) { return wfSetVar( $this->mMaxIncludeSize
, $x ); }
309 function setMaxPPNodeCount( $x ) { return wfSetVar( $this->mMaxPPNodeCount
, $x ); }
310 function setMaxTemplateDepth( $x ) { return wfSetVar( $this->mMaxTemplateDepth
, $x ); }
312 function setExpensiveParserFunctionLimit( $x ) { return wfSetVar( $this->mExpensiveParserFunctionLimit
, $x ); }
313 function setRemoveComments( $x ) { return wfSetVar( $this->mRemoveComments
, $x ); }
314 function setTemplateCallback( $x ) { return wfSetVar( $this->mTemplateCallback
, $x ); }
315 function enableLimitReport( $x = true ) { return wfSetVar( $this->mEnableLimitReport
, $x ); }
316 function setTimestamp( $x ) { return wfSetVar( $this->mTimestamp
, $x ); }
317 function setCleanSignatures( $x ) { return wfSetVar( $this->mCleanSignatures
, $x ); }
318 function setExternalLinkTarget( $x ) { return wfSetVar( $this->mExternalLinkTarget
, $x ); }
319 function setMath( $x ) { return wfSetVar( $this->mMath
, $x ); }
320 function setUserLang( $x ) {
321 if ( is_string( $x ) ) {
322 $x = Language
::factory( $x );
324 return wfSetVar( $this->mUserLang
, $x );
326 function setThumbSize( $x ) { return wfSetVar( $this->mThumbSize
, $x ); }
327 function setStubThreshold( $x ) { return wfSetVar( $this->mStubThreshold
, $x ); }
328 function setPreSaveTransform( $x ) { return wfSetVar( $this->mPreSaveTransform
, $x ); }
330 function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview
, $x ); }
331 function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview
, $x ); }
332 function setIsPrintable( $x ) { return wfSetVar( $this->mIsPrintable
, $x ); }
335 * Extra key that should be present in the parser cache key.
337 function addExtraKey( $key ) {
338 $this->mExtraKey
.= '!' . $key;
343 * @param $user User object
344 * @param $lang Language object
346 function __construct( $user = null, $lang = null ) {
347 if ( $user === null ) {
349 if ( $wgUser === null ) {
355 if ( $lang === null ) {
357 if ( !StubObject
::isRealObject( $wgLang ) ) {
362 $this->initialiseFromUser( $user, $lang );
366 * Get a ParserOptions object from a given user.
367 * Language will be taken from $wgLang.
369 * @param $user User object
370 * @return ParserOptions object
372 public static function newFromUser( $user ) {
373 return new ParserOptions( $user );
377 * Get a ParserOptions object from a given user and language
379 * @param $user User object
380 * @param $lang Language object
381 * @return ParserOptions object
383 public static function newFromUserAndLang( User
$user, Language
$lang ) {
384 return new ParserOptions( $user, $lang );
388 * Get a ParserOptions object from a IContextSource object
390 * @param $context IContextSource object
391 * @return ParserOptions object
393 public static function newFromContext( IContextSource
$context ) {
394 return new ParserOptions( $context->getUser(), $context->getLanguage() );
400 * @param $user User object
401 * @param $lang Language object
403 private function initialiseFromUser( $user, $lang ) {
404 global $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages,
405 $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion,
406 $wgMaxArticleSize, $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth,
407 $wgCleanSignatures, $wgExternalLinkTarget, $wgExpensiveParserFunctionLimit;
409 wfProfileIn( __METHOD__
);
411 $this->mUseDynamicDates
= $wgUseDynamicDates;
412 $this->mInterwikiMagic
= $wgInterwikiMagic;
413 $this->mAllowExternalImages
= $wgAllowExternalImages;
414 $this->mAllowExternalImagesFrom
= $wgAllowExternalImagesFrom;
415 $this->mEnableImageWhitelist
= $wgEnableImageWhitelist;
416 $this->mAllowSpecialInclusion
= $wgAllowSpecialInclusion;
417 $this->mMaxIncludeSize
= $wgMaxArticleSize * 1024;
418 $this->mMaxPPNodeCount
= $wgMaxPPNodeCount;
419 $this->mMaxPPExpandDepth
= $wgMaxPPExpandDepth;
420 $this->mMaxTemplateDepth
= $wgMaxTemplateDepth;
421 $this->mExpensiveParserFunctionLimit
= $wgExpensiveParserFunctionLimit;
422 $this->mCleanSignatures
= $wgCleanSignatures;
423 $this->mExternalLinkTarget
= $wgExternalLinkTarget;
425 $this->mUser
= $user;
426 $this->mNumberHeadings
= $user->getOption( 'numberheadings' );
427 $this->mMath
= $user->getOption( 'math' );
428 $this->mThumbSize
= $user->getOption( 'thumbsize' );
429 $this->mStubThreshold
= $user->getStubThreshold();
430 $this->mUserLang
= $lang;
432 wfProfileOut( __METHOD__
);
436 * Registers a callback for tracking which ParserOptions which are used.
437 * This is a private API with the parser.
439 function registerWatcher( $callback ) {
440 $this->onAccessCallback
= $callback;
444 * Called when an option is accessed.
446 protected function optionUsed( $optionName ) {
447 if ( $this->onAccessCallback
) {
448 call_user_func( $this->onAccessCallback
, $optionName );
453 * Returns the full array of options that would have been used by
455 * Used to get the old parser cache entries when available.
458 public static function legacyOptions() {
459 global $wgUseDynamicDates;
460 $legacyOpts = array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
461 if ( $wgUseDynamicDates ) {
462 $legacyOpts[] = 'dateformat';
468 * Generate a hash string with the values set on these ParserOptions
469 * for the keys given in the array.
470 * This will be used as part of the hash key for the parser cache,
471 * so users sharign the options with vary for the same page share
472 * the same cached data safely.
474 * Replaces User::getPageRenderingHash()
476 * Extensions which require it should install 'PageRenderingHash' hook,
477 * which will give them a chance to modify this key based on their own
481 * @param $forOptions Array
482 * @param $title Title: used to get the content language of the page (since r97636)
483 * @return string Page rendering hash
485 public function optionsHash( $forOptions, $title = null ) {
486 global $wgRenderHashAppend;
490 if ( in_array( 'math', $forOptions ) ) {
491 $confstr .= $this->mMath
;
497 // Space assigned for the stubthreshold but unused
498 // since it disables the parser cache, its value will always
499 // be 0 when this function is called by parsercache.
500 if ( in_array( 'stubthreshold', $forOptions ) ) {
501 $confstr .= '!' . $this->mStubThreshold
;
506 if ( in_array( 'dateformat', $forOptions ) ) {
507 $confstr .= '!' . $this->getDateFormat();
510 if ( in_array( 'numberheadings', $forOptions ) ) {
511 $confstr .= '!' . ( $this->mNumberHeadings ?
'1' : '' );
516 if ( in_array( 'userlang', $forOptions ) ) {
517 $confstr .= '!' . $this->mUserLang
->getCode();
522 if ( in_array( 'thumbsize', $forOptions ) ) {
523 $confstr .= '!' . $this->mThumbSize
;
528 // add in language specific options, if any
529 // @todo FIXME: This is just a way of retrieving the url/user preferred variant
530 if( !is_null( $title ) ) {
531 $confstr .= $title->getPageLanguage()->getExtraHashOptions();
534 $confstr .= $wgContLang->getExtraHashOptions();
537 $confstr .= $wgRenderHashAppend;
539 if ( !in_array( 'editsection', $forOptions ) ) {
541 } elseif ( !$this->mEditSection
) {
542 $confstr .= '!edit=0';
545 if ( $this->mIsPrintable
&& in_array( 'printable', $forOptions ) ) {
546 $confstr .= '!printable=1';
549 if ( $this->mExtraKey
!= '' )
550 $confstr .= $this->mExtraKey
;
552 // Give a chance for extensions to modify the hash, if they have
553 // extra options or other effects on the parser cache.
554 wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
556 // Make it a valid memcached key fragment
557 $confstr = str_replace( ' ', '_', $confstr );