MediaWiki
REL1_23
|
00001 <?php 00031 class ParserOptions { 00032 00036 var $mInterwikiMagic; 00037 00041 var $mAllowExternalImages; 00042 00046 var $mAllowExternalImagesFrom; 00047 00051 var $mEnableImageWhitelist; 00052 00056 var $mDateFormat = null; 00057 00061 var $mEditSection = true; 00062 00066 var $mAllowSpecialInclusion; 00067 00071 var $mTidy = false; 00072 00076 var $mInterfaceMessage = false; 00077 00081 var $mTargetLanguage = null; 00082 00086 var $mMaxIncludeSize; 00087 00091 var $mMaxPPNodeCount; 00092 00096 var $mMaxGeneratedPPNodeCount; 00097 00101 var $mMaxPPExpandDepth; 00102 00106 var $mMaxTemplateDepth; 00107 00111 var $mExpensiveParserFunctionLimit; 00112 00116 var $mRemoveComments = true; 00117 00121 var $mTemplateCallback = 00122 array( 'Parser', 'statelessFetchTemplate' ); 00123 00127 var $mEnableLimitReport = false; 00128 00132 var $mTimestamp; 00133 00137 var $mExternalLinkTarget; 00138 00145 var $mCleanSignatures; 00146 00150 var $mPreSaveTransform = true; 00151 00155 var $mDisableContentConversion; 00156 00160 var $mDisableTitleConversion; 00161 00165 var $mNumberHeadings; 00166 00170 var $mThumbSize; 00171 00175 private $mStubThreshold; 00176 00180 var $mUserLang; 00181 00186 var $mUser; 00187 00191 var $mIsPreview = false; 00192 00196 var $mIsSectionPreview = false; 00197 00201 var $mIsPrintable = false; 00202 00206 var $mExtraKey = ''; 00207 00211 protected $onAccessCallback = null; 00212 00213 function getInterwikiMagic() { return $this->mInterwikiMagic; } 00214 function getAllowExternalImages() { return $this->mAllowExternalImages; } 00215 function getAllowExternalImagesFrom() { return $this->mAllowExternalImagesFrom; } 00216 function getEnableImageWhitelist() { return $this->mEnableImageWhitelist; } 00217 function getEditSection() { return $this->mEditSection; } 00218 function getNumberHeadings() { $this->optionUsed( 'numberheadings' ); 00219 return $this->mNumberHeadings; } 00220 function getAllowSpecialInclusion() { return $this->mAllowSpecialInclusion; } 00221 function getTidy() { return $this->mTidy; } 00222 function getInterfaceMessage() { return $this->mInterfaceMessage; } 00223 function getTargetLanguage() { return $this->mTargetLanguage; } 00224 function getMaxIncludeSize() { return $this->mMaxIncludeSize; } 00225 function getMaxPPNodeCount() { return $this->mMaxPPNodeCount; } 00226 function getMaxGeneratedPPNodeCount() { return $this->mMaxGeneratedPPNodeCount; } 00227 function getMaxPPExpandDepth() { return $this->mMaxPPExpandDepth; } 00228 function getMaxTemplateDepth() { return $this->mMaxTemplateDepth; } 00229 /* @since 1.20 */ 00230 function getExpensiveParserFunctionLimit() { return $this->mExpensiveParserFunctionLimit; } 00231 function getRemoveComments() { return $this->mRemoveComments; } 00232 function getTemplateCallback() { return $this->mTemplateCallback; } 00233 function getEnableLimitReport() { return $this->mEnableLimitReport; } 00234 function getCleanSignatures() { return $this->mCleanSignatures; } 00235 function getExternalLinkTarget() { return $this->mExternalLinkTarget; } 00236 function getDisableContentConversion() { return $this->mDisableContentConversion; } 00237 function getDisableTitleConversion() { return $this->mDisableTitleConversion; } 00238 function getThumbSize() { $this->optionUsed( 'thumbsize' ); 00239 return $this->mThumbSize; } 00240 function getStubThreshold() { $this->optionUsed( 'stubthreshold' ); 00241 return $this->mStubThreshold; } 00242 00243 function getIsPreview() { return $this->mIsPreview; } 00244 function getIsSectionPreview() { return $this->mIsSectionPreview; } 00245 function getIsPrintable() { $this->optionUsed( 'printable' ); 00246 return $this->mIsPrintable; } 00247 function getUser() { return $this->mUser; } 00248 function getPreSaveTransform() { return $this->mPreSaveTransform; } 00249 00250 function getDateFormat() { 00251 $this->optionUsed( 'dateformat' ); 00252 if ( !isset( $this->mDateFormat ) ) { 00253 $this->mDateFormat = $this->mUser->getDatePreference(); 00254 } 00255 return $this->mDateFormat; 00256 } 00257 00258 function getTimestamp() { 00259 if ( !isset( $this->mTimestamp ) ) { 00260 $this->mTimestamp = wfTimestampNow(); 00261 } 00262 return $this->mTimestamp; 00263 } 00264 00281 function getUserLangObj() { 00282 $this->optionUsed( 'userlang' ); 00283 return $this->mUserLang; 00284 } 00285 00292 function getUserLang() { 00293 return $this->getUserLangObj()->getCode(); 00294 } 00295 00296 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); } 00297 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); } 00298 function setAllowExternalImagesFrom( $x ) { return wfSetVar( $this->mAllowExternalImagesFrom, $x ); } 00299 function setEnableImageWhitelist( $x ) { return wfSetVar( $this->mEnableImageWhitelist, $x ); } 00300 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); } 00301 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); } 00302 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); } 00303 function setAllowSpecialInclusion( $x ) { return wfSetVar( $this->mAllowSpecialInclusion, $x ); } 00304 function setTidy( $x ) { return wfSetVar( $this->mTidy, $x ); } 00305 00307 function setSkin( $x ) { wfDeprecated( __METHOD__, '1.19' ); } 00308 function setInterfaceMessage( $x ) { return wfSetVar( $this->mInterfaceMessage, $x ); } 00309 function setTargetLanguage( $x ) { return wfSetVar( $this->mTargetLanguage, $x, true ); } 00310 function setMaxIncludeSize( $x ) { return wfSetVar( $this->mMaxIncludeSize, $x ); } 00311 function setMaxPPNodeCount( $x ) { return wfSetVar( $this->mMaxPPNodeCount, $x ); } 00312 function setMaxGeneratedPPNodeCount( $x ) { return wfSetVar( $this->mMaxGeneratedPPNodeCount, $x ); } 00313 function setMaxTemplateDepth( $x ) { return wfSetVar( $this->mMaxTemplateDepth, $x ); } 00314 /* @since 1.20 */ 00315 function setExpensiveParserFunctionLimit( $x ) { return wfSetVar( $this->mExpensiveParserFunctionLimit, $x ); } 00316 function setRemoveComments( $x ) { return wfSetVar( $this->mRemoveComments, $x ); } 00317 function setTemplateCallback( $x ) { return wfSetVar( $this->mTemplateCallback, $x ); } 00318 function enableLimitReport( $x = true ) { return wfSetVar( $this->mEnableLimitReport, $x ); } 00319 function setTimestamp( $x ) { return wfSetVar( $this->mTimestamp, $x ); } 00320 function setCleanSignatures( $x ) { return wfSetVar( $this->mCleanSignatures, $x ); } 00321 function setExternalLinkTarget( $x ) { return wfSetVar( $this->mExternalLinkTarget, $x ); } 00322 function disableContentConversion( $x = true ) { return wfSetVar( $this->mDisableContentConversion, $x ); } 00323 function disableTitleConversion( $x = true ) { return wfSetVar( $this->mDisableTitleConversion, $x ); } 00324 function setUserLang( $x ) { 00325 if ( is_string( $x ) ) { 00326 $x = Language::factory( $x ); 00327 } 00328 return wfSetVar( $this->mUserLang, $x ); 00329 } 00330 function setThumbSize( $x ) { return wfSetVar( $this->mThumbSize, $x ); } 00331 function setStubThreshold( $x ) { return wfSetVar( $this->mStubThreshold, $x ); } 00332 function setPreSaveTransform( $x ) { return wfSetVar( $this->mPreSaveTransform, $x ); } 00333 00334 function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview, $x ); } 00335 function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview, $x ); } 00336 function setIsPrintable( $x ) { return wfSetVar( $this->mIsPrintable, $x ); } 00337 00341 function addExtraKey( $key ) { 00342 $this->mExtraKey .= '!' . $key; 00343 } 00344 00350 function __construct( $user = null, $lang = null ) { 00351 if ( $user === null ) { 00352 global $wgUser; 00353 if ( $wgUser === null ) { 00354 $user = new User; 00355 } else { 00356 $user = $wgUser; 00357 } 00358 } 00359 if ( $lang === null ) { 00360 global $wgLang; 00361 if ( !StubObject::isRealObject( $wgLang ) ) { 00362 $wgLang->_unstub(); 00363 } 00364 $lang = $wgLang; 00365 } 00366 $this->initialiseFromUser( $user, $lang ); 00367 } 00368 00376 public static function newFromUser( $user ) { 00377 return new ParserOptions( $user ); 00378 } 00379 00387 public static function newFromUserAndLang( User $user, Language $lang ) { 00388 return new ParserOptions( $user, $lang ); 00389 } 00390 00397 public static function newFromContext( IContextSource $context ) { 00398 return new ParserOptions( $context->getUser(), $context->getLanguage() ); 00399 } 00400 00407 private function initialiseFromUser( $user, $lang ) { 00408 global $wgInterwikiMagic, $wgAllowExternalImages, 00409 $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion, 00410 $wgMaxArticleSize, $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth, 00411 $wgCleanSignatures, $wgExternalLinkTarget, $wgExpensiveParserFunctionLimit, 00412 $wgMaxGeneratedPPNodeCount, $wgDisableLangConversion, $wgDisableTitleConversion; 00413 00414 wfProfileIn( __METHOD__ ); 00415 00416 $this->mInterwikiMagic = $wgInterwikiMagic; 00417 $this->mAllowExternalImages = $wgAllowExternalImages; 00418 $this->mAllowExternalImagesFrom = $wgAllowExternalImagesFrom; 00419 $this->mEnableImageWhitelist = $wgEnableImageWhitelist; 00420 $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion; 00421 $this->mMaxIncludeSize = $wgMaxArticleSize * 1024; 00422 $this->mMaxPPNodeCount = $wgMaxPPNodeCount; 00423 $this->mMaxGeneratedPPNodeCount = $wgMaxGeneratedPPNodeCount; 00424 $this->mMaxPPExpandDepth = $wgMaxPPExpandDepth; 00425 $this->mMaxTemplateDepth = $wgMaxTemplateDepth; 00426 $this->mExpensiveParserFunctionLimit = $wgExpensiveParserFunctionLimit; 00427 $this->mCleanSignatures = $wgCleanSignatures; 00428 $this->mExternalLinkTarget = $wgExternalLinkTarget; 00429 $this->mDisableContentConversion = $wgDisableLangConversion; 00430 $this->mDisableTitleConversion = $wgDisableLangConversion || $wgDisableTitleConversion; 00431 00432 $this->mUser = $user; 00433 $this->mNumberHeadings = $user->getOption( 'numberheadings' ); 00434 $this->mThumbSize = $user->getOption( 'thumbsize' ); 00435 $this->mStubThreshold = $user->getStubThreshold(); 00436 $this->mUserLang = $lang; 00437 00438 wfProfileOut( __METHOD__ ); 00439 } 00440 00445 function registerWatcher( $callback ) { 00446 $this->onAccessCallback = $callback; 00447 } 00448 00453 public function optionUsed( $optionName ) { 00454 if ( $this->onAccessCallback ) { 00455 call_user_func( $this->onAccessCallback, $optionName ); 00456 } 00457 } 00458 00465 public static function legacyOptions() { 00466 return array( 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' ); 00467 } 00468 00485 public function optionsHash( $forOptions, $title = null ) { 00486 global $wgRenderHashAppend; 00487 00488 // FIXME: Once the cache key is reorganized this argument 00489 // can be dropped. It was used when the math extension was 00490 // part of core. 00491 $confstr = '*'; 00492 00493 // Space assigned for the stubthreshold but unused 00494 // since it disables the parser cache, its value will always 00495 // be 0 when this function is called by parsercache. 00496 if ( in_array( 'stubthreshold', $forOptions ) ) { 00497 $confstr .= '!' . $this->mStubThreshold; 00498 } else { 00499 $confstr .= '!*'; 00500 } 00501 00502 if ( in_array( 'dateformat', $forOptions ) ) { 00503 $confstr .= '!' . $this->getDateFormat(); 00504 } 00505 00506 if ( in_array( 'numberheadings', $forOptions ) ) { 00507 $confstr .= '!' . ( $this->mNumberHeadings ? '1' : '' ); 00508 } else { 00509 $confstr .= '!*'; 00510 } 00511 00512 if ( in_array( 'userlang', $forOptions ) ) { 00513 $confstr .= '!' . $this->mUserLang->getCode(); 00514 } else { 00515 $confstr .= '!*'; 00516 } 00517 00518 if ( in_array( 'thumbsize', $forOptions ) ) { 00519 $confstr .= '!' . $this->mThumbSize; 00520 } else { 00521 $confstr .= '!*'; 00522 } 00523 00524 // add in language specific options, if any 00525 // @todo FIXME: This is just a way of retrieving the url/user preferred variant 00526 if ( !is_null( $title ) ) { 00527 $confstr .= $title->getPageLanguage()->getExtraHashOptions(); 00528 } else { 00529 global $wgContLang; 00530 $confstr .= $wgContLang->getExtraHashOptions(); 00531 } 00532 00533 $confstr .= $wgRenderHashAppend; 00534 00535 if ( !in_array( 'editsection', $forOptions ) ) { 00536 $confstr .= '!*'; 00537 } elseif ( !$this->mEditSection ) { 00538 $confstr .= '!edit=0'; 00539 } 00540 00541 if ( $this->mIsPrintable && in_array( 'printable', $forOptions ) ) { 00542 $confstr .= '!printable=1'; 00543 } 00544 00545 if ( $this->mExtraKey != '' ) { 00546 $confstr .= $this->mExtraKey; 00547 } 00548 00549 // Give a chance for extensions to modify the hash, if they have 00550 // extra options or other effects on the parser cache. 00551 wfRunHooks( 'PageRenderingHash', array( &$confstr, $this->getUser(), &$forOptions ) ); 00552 00553 // Make it a valid memcached key fragment 00554 $confstr = str_replace( ' ', '_', $confstr ); 00555 00556 return $confstr; 00557 } 00558 }