MediaWiki
REL1_20
|
00001 <?php 00031 class ParserOptions { 00032 00036 var $mUseDynamicDates; 00037 00041 var $mInterwikiMagic; 00042 00046 var $mAllowExternalImages; 00047 00051 var $mAllowExternalImagesFrom; 00052 00056 var $mEnableImageWhitelist; 00057 00061 var $mDateFormat = null; 00062 00066 var $mEditSection = true; 00067 00071 var $mAllowSpecialInclusion; 00072 00076 var $mTidy = false; 00077 00081 var $mInterfaceMessage = false; 00082 00086 var $mTargetLanguage = null; 00087 00091 var $mMaxIncludeSize; 00092 00096 var $mMaxPPNodeCount; 00097 00101 var $mMaxGeneratedPPNodeCount; 00102 00106 var $mMaxPPExpandDepth; 00107 00111 var $mMaxTemplateDepth; 00112 00116 var $mExpensiveParserFunctionLimit; 00117 00121 var $mRemoveComments = true; 00122 00126 var $mTemplateCallback = 00127 array( 'Parser', 'statelessFetchTemplate' ); 00128 00132 var $mEnableLimitReport = false; 00133 00137 var $mTimestamp; 00138 00142 var $mExternalLinkTarget; 00143 00150 var $mCleanSignatures; 00151 00155 var $mPreSaveTransform = true; 00156 00160 var $mDisableContentConversion; 00161 00165 var $mDisableTitleConversion; 00166 00170 var $mNumberHeadings; 00171 00175 var $mMath; 00176 00180 var $mThumbSize; 00181 00185 private $mStubThreshold; 00186 00190 var $mUserLang; 00191 00196 var $mUser; 00197 00201 var $mIsPreview = false; 00202 00206 var $mIsSectionPreview = false; 00207 00211 var $mIsPrintable = false; 00212 00216 var $mExtraKey = ''; 00217 00221 protected $onAccessCallback = null; 00222 00223 function getUseDynamicDates() { return $this->mUseDynamicDates; } 00224 function getInterwikiMagic() { return $this->mInterwikiMagic; } 00225 function getAllowExternalImages() { return $this->mAllowExternalImages; } 00226 function getAllowExternalImagesFrom() { return $this->mAllowExternalImagesFrom; } 00227 function getEnableImageWhitelist() { return $this->mEnableImageWhitelist; } 00228 function getEditSection() { return $this->mEditSection; } 00229 function getNumberHeadings() { $this->optionUsed( 'numberheadings' ); 00230 return $this->mNumberHeadings; } 00231 function getAllowSpecialInclusion() { return $this->mAllowSpecialInclusion; } 00232 function getTidy() { return $this->mTidy; } 00233 function getInterfaceMessage() { return $this->mInterfaceMessage; } 00234 function getTargetLanguage() { return $this->mTargetLanguage; } 00235 function getMaxIncludeSize() { return $this->mMaxIncludeSize; } 00236 function getMaxPPNodeCount() { return $this->mMaxPPNodeCount; } 00237 function getMaxGeneratedPPNodeCount() { return $this->mMaxGeneratedPPNodeCount; } 00238 function getMaxPPExpandDepth() { return $this->mMaxPPExpandDepth; } 00239 function getMaxTemplateDepth() { return $this->mMaxTemplateDepth; } 00240 /* @since 1.20 */ 00241 function getExpensiveParserFunctionLimit() { return $this->mExpensiveParserFunctionLimit; } 00242 function getRemoveComments() { return $this->mRemoveComments; } 00243 function getTemplateCallback() { return $this->mTemplateCallback; } 00244 function getEnableLimitReport() { return $this->mEnableLimitReport; } 00245 function getCleanSignatures() { return $this->mCleanSignatures; } 00246 function getExternalLinkTarget() { return $this->mExternalLinkTarget; } 00247 function getDisableContentConversion() { return $this->mDisableContentConversion; } 00248 function getDisableTitleConversion() { return $this->mDisableTitleConversion; } 00249 function getMath() { $this->optionUsed( 'math' ); 00250 return $this->mMath; } 00251 function getThumbSize() { $this->optionUsed( 'thumbsize' ); 00252 return $this->mThumbSize; } 00253 function getStubThreshold() { $this->optionUsed( 'stubthreshold' ); 00254 return $this->mStubThreshold; } 00255 00256 function getIsPreview() { return $this->mIsPreview; } 00257 function getIsSectionPreview() { return $this->mIsSectionPreview; } 00258 function getIsPrintable() { $this->optionUsed( 'printable' ); 00259 return $this->mIsPrintable; } 00260 function getUser() { return $this->mUser; } 00261 function getPreSaveTransform() { return $this->mPreSaveTransform; } 00262 00268 function getSkin( $title = null ) { 00269 wfDeprecated( __METHOD__, '1.18' ); 00270 return new DummyLinker; 00271 } 00272 00273 function getDateFormat() { 00274 $this->optionUsed( 'dateformat' ); 00275 if ( !isset( $this->mDateFormat ) ) { 00276 $this->mDateFormat = $this->mUser->getDatePreference(); 00277 } 00278 return $this->mDateFormat; 00279 } 00280 00281 function getTimestamp() { 00282 if ( !isset( $this->mTimestamp ) ) { 00283 $this->mTimestamp = wfTimestampNow(); 00284 } 00285 return $this->mTimestamp; 00286 } 00287 00296 function getUserLangObj() { 00297 $this->optionUsed( 'userlang' ); 00298 return $this->mUserLang; 00299 } 00300 00307 function getUserLang() { 00308 return $this->getUserLangObj()->getCode(); 00309 } 00310 00311 function setUseDynamicDates( $x ) { return wfSetVar( $this->mUseDynamicDates, $x ); } 00312 function setInterwikiMagic( $x ) { return wfSetVar( $this->mInterwikiMagic, $x ); } 00313 function setAllowExternalImages( $x ) { return wfSetVar( $this->mAllowExternalImages, $x ); } 00314 function setAllowExternalImagesFrom( $x ) { return wfSetVar( $this->mAllowExternalImagesFrom, $x ); } 00315 function setEnableImageWhitelist( $x ) { return wfSetVar( $this->mEnableImageWhitelist, $x ); } 00316 function setDateFormat( $x ) { return wfSetVar( $this->mDateFormat, $x ); } 00317 function setEditSection( $x ) { return wfSetVar( $this->mEditSection, $x ); } 00318 function setNumberHeadings( $x ) { return wfSetVar( $this->mNumberHeadings, $x ); } 00319 function setAllowSpecialInclusion( $x ) { return wfSetVar( $this->mAllowSpecialInclusion, $x ); } 00320 function setTidy( $x ) { return wfSetVar( $this->mTidy, $x ); } 00321 00323 function setSkin( $x ) { wfDeprecated( __METHOD__, '1.19' ); } 00324 function setInterfaceMessage( $x ) { return wfSetVar( $this->mInterfaceMessage, $x ); } 00325 function setTargetLanguage( $x ) { return wfSetVar( $this->mTargetLanguage, $x, true ); } 00326 function setMaxIncludeSize( $x ) { return wfSetVar( $this->mMaxIncludeSize, $x ); } 00327 function setMaxPPNodeCount( $x ) { return wfSetVar( $this->mMaxPPNodeCount, $x ); } 00328 function setMaxGeneratedPPNodeCount( $x ) { return wfSetVar( $this->mMaxGeneratedPPNodeCount, $x ); } 00329 function setMaxTemplateDepth( $x ) { return wfSetVar( $this->mMaxTemplateDepth, $x ); } 00330 /* @since 1.20 */ 00331 function setExpensiveParserFunctionLimit( $x ) { return wfSetVar( $this->mExpensiveParserFunctionLimit, $x ); } 00332 function setRemoveComments( $x ) { return wfSetVar( $this->mRemoveComments, $x ); } 00333 function setTemplateCallback( $x ) { return wfSetVar( $this->mTemplateCallback, $x ); } 00334 function enableLimitReport( $x = true ) { return wfSetVar( $this->mEnableLimitReport, $x ); } 00335 function setTimestamp( $x ) { return wfSetVar( $this->mTimestamp, $x ); } 00336 function setCleanSignatures( $x ) { return wfSetVar( $this->mCleanSignatures, $x ); } 00337 function setExternalLinkTarget( $x ) { return wfSetVar( $this->mExternalLinkTarget, $x ); } 00338 function disableContentConversion( $x = true ) { return wfSetVar( $this->mDisableContentConversion, $x ); } 00339 function disableTitleConversion( $x = true ) { return wfSetVar( $this->mDisableTitleConversion, $x ); } 00340 function setMath( $x ) { return wfSetVar( $this->mMath, $x ); } 00341 function setUserLang( $x ) { 00342 if ( is_string( $x ) ) { 00343 $x = Language::factory( $x ); 00344 } 00345 return wfSetVar( $this->mUserLang, $x ); 00346 } 00347 function setThumbSize( $x ) { return wfSetVar( $this->mThumbSize, $x ); } 00348 function setStubThreshold( $x ) { return wfSetVar( $this->mStubThreshold, $x ); } 00349 function setPreSaveTransform( $x ) { return wfSetVar( $this->mPreSaveTransform, $x ); } 00350 00351 function setIsPreview( $x ) { return wfSetVar( $this->mIsPreview, $x ); } 00352 function setIsSectionPreview( $x ) { return wfSetVar( $this->mIsSectionPreview, $x ); } 00353 function setIsPrintable( $x ) { return wfSetVar( $this->mIsPrintable, $x ); } 00354 00358 function addExtraKey( $key ) { 00359 $this->mExtraKey .= '!' . $key; 00360 } 00361 00367 function __construct( $user = null, $lang = null ) { 00368 if ( $user === null ) { 00369 global $wgUser; 00370 if ( $wgUser === null ) { 00371 $user = new User; 00372 } else { 00373 $user = $wgUser; 00374 } 00375 } 00376 if ( $lang === null ) { 00377 global $wgLang; 00378 if ( !StubObject::isRealObject( $wgLang ) ) { 00379 $wgLang->_unstub(); 00380 } 00381 $lang = $wgLang; 00382 } 00383 $this->initialiseFromUser( $user, $lang ); 00384 } 00385 00393 public static function newFromUser( $user ) { 00394 return new ParserOptions( $user ); 00395 } 00396 00404 public static function newFromUserAndLang( User $user, Language $lang ) { 00405 return new ParserOptions( $user, $lang ); 00406 } 00407 00414 public static function newFromContext( IContextSource $context ) { 00415 return new ParserOptions( $context->getUser(), $context->getLanguage() ); 00416 } 00417 00424 private function initialiseFromUser( $user, $lang ) { 00425 global $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages, 00426 $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion, 00427 $wgMaxArticleSize, $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth, 00428 $wgCleanSignatures, $wgExternalLinkTarget, $wgExpensiveParserFunctionLimit, 00429 $wgMaxGeneratedPPNodeCount, $wgDisableLangConversion, $wgDisableTitleConversion; 00430 00431 wfProfileIn( __METHOD__ ); 00432 00433 $this->mUseDynamicDates = $wgUseDynamicDates; 00434 $this->mInterwikiMagic = $wgInterwikiMagic; 00435 $this->mAllowExternalImages = $wgAllowExternalImages; 00436 $this->mAllowExternalImagesFrom = $wgAllowExternalImagesFrom; 00437 $this->mEnableImageWhitelist = $wgEnableImageWhitelist; 00438 $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion; 00439 $this->mMaxIncludeSize = $wgMaxArticleSize * 1024; 00440 $this->mMaxPPNodeCount = $wgMaxPPNodeCount; 00441 $this->mMaxGeneratedPPNodeCount = $wgMaxGeneratedPPNodeCount; 00442 $this->mMaxPPExpandDepth = $wgMaxPPExpandDepth; 00443 $this->mMaxTemplateDepth = $wgMaxTemplateDepth; 00444 $this->mExpensiveParserFunctionLimit = $wgExpensiveParserFunctionLimit; 00445 $this->mCleanSignatures = $wgCleanSignatures; 00446 $this->mExternalLinkTarget = $wgExternalLinkTarget; 00447 $this->mDisableContentConversion = $wgDisableLangConversion; 00448 $this->mDisableTitleConversion = $wgDisableLangConversion || $wgDisableTitleConversion; 00449 00450 $this->mUser = $user; 00451 $this->mNumberHeadings = $user->getOption( 'numberheadings' ); 00452 $this->mMath = $user->getOption( 'math' ); 00453 $this->mThumbSize = $user->getOption( 'thumbsize' ); 00454 $this->mStubThreshold = $user->getStubThreshold(); 00455 $this->mUserLang = $lang; 00456 00457 wfProfileOut( __METHOD__ ); 00458 } 00459 00464 function registerWatcher( $callback ) { 00465 $this->onAccessCallback = $callback; 00466 } 00467 00471 protected function optionUsed( $optionName ) { 00472 if ( $this->onAccessCallback ) { 00473 call_user_func( $this->onAccessCallback, $optionName ); 00474 } 00475 } 00476 00483 public static function legacyOptions() { 00484 global $wgUseDynamicDates; 00485 $legacyOpts = array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' ); 00486 if ( $wgUseDynamicDates ) { 00487 $legacyOpts[] = 'dateformat'; 00488 } 00489 return $legacyOpts; 00490 } 00491 00510 public function optionsHash( $forOptions, $title = null ) { 00511 global $wgRenderHashAppend; 00512 00513 $confstr = ''; 00514 00515 if ( in_array( 'math', $forOptions ) ) { 00516 $confstr .= $this->mMath; 00517 } else { 00518 $confstr .= '*'; 00519 } 00520 00521 00522 // Space assigned for the stubthreshold but unused 00523 // since it disables the parser cache, its value will always 00524 // be 0 when this function is called by parsercache. 00525 if ( in_array( 'stubthreshold', $forOptions ) ) { 00526 $confstr .= '!' . $this->mStubThreshold; 00527 } else { 00528 $confstr .= '!*' ; 00529 } 00530 00531 if ( in_array( 'dateformat', $forOptions ) ) { 00532 $confstr .= '!' . $this->getDateFormat(); 00533 } 00534 00535 if ( in_array( 'numberheadings', $forOptions ) ) { 00536 $confstr .= '!' . ( $this->mNumberHeadings ? '1' : '' ); 00537 } else { 00538 $confstr .= '!*'; 00539 } 00540 00541 if ( in_array( 'userlang', $forOptions ) ) { 00542 $confstr .= '!' . $this->mUserLang->getCode(); 00543 } else { 00544 $confstr .= '!*'; 00545 } 00546 00547 if ( in_array( 'thumbsize', $forOptions ) ) { 00548 $confstr .= '!' . $this->mThumbSize; 00549 } else { 00550 $confstr .= '!*'; 00551 } 00552 00553 // add in language specific options, if any 00554 // @todo FIXME: This is just a way of retrieving the url/user preferred variant 00555 if( !is_null( $title ) ) { 00556 $confstr .= $title->getPageLanguage()->getExtraHashOptions(); 00557 } else { 00558 global $wgContLang; 00559 $confstr .= $wgContLang->getExtraHashOptions(); 00560 } 00561 00562 $confstr .= $wgRenderHashAppend; 00563 00564 if ( !in_array( 'editsection', $forOptions ) ) { 00565 $confstr .= '!*'; 00566 } elseif ( !$this->mEditSection ) { 00567 $confstr .= '!edit=0'; 00568 } 00569 00570 if ( $this->mIsPrintable && in_array( 'printable', $forOptions ) ) { 00571 $confstr .= '!printable=1'; 00572 } 00573 00574 if ( $this->mExtraKey != '' ) 00575 $confstr .= $this->mExtraKey; 00576 00577 // Give a chance for extensions to modify the hash, if they have 00578 // extra options or other effects on the parser cache. 00579 wfRunHooks( 'PageRenderingHash', array( &$confstr ) ); 00580 00581 // Make it a valid memcached key fragment 00582 $confstr = str_replace( ' ', '_', $confstr ); 00583 00584 return $confstr; 00585 } 00586 }