MediaWiki
REL1_24
|
00001 <?php 00032 class ParserOptions { 00033 00037 public $mInterwikiMagic; 00038 00042 public $mAllowExternalImages; 00043 00047 public $mAllowExternalImagesFrom; 00048 00052 public $mEnableImageWhitelist; 00053 00057 public $mDateFormat = null; 00058 00062 public $mEditSection = true; 00063 00067 public $mAllowSpecialInclusion; 00068 00072 public $mTidy = false; 00073 00077 public $mInterfaceMessage = false; 00078 00082 public $mTargetLanguage = null; 00083 00087 public $mMaxIncludeSize; 00088 00092 public $mMaxPPNodeCount; 00093 00097 public $mMaxGeneratedPPNodeCount; 00098 00102 public $mMaxPPExpandDepth; 00103 00107 public $mMaxTemplateDepth; 00108 00112 public $mExpensiveParserFunctionLimit; 00113 00117 public $mRemoveComments = true; 00118 00122 public $mTemplateCallback = 00123 array( 'Parser', 'statelessFetchTemplate' ); 00124 00128 public $mEnableLimitReport = false; 00129 00133 public $mTimestamp; 00134 00138 public $mExternalLinkTarget; 00139 00146 public $mCleanSignatures; 00147 00151 public $mPreSaveTransform = true; 00152 00156 public $mDisableContentConversion; 00157 00161 public $mDisableTitleConversion; 00162 00166 public $mNumberHeadings; 00167 00171 public $mThumbSize; 00172 00176 private $mStubThreshold; 00177 00181 public $mUserLang; 00182 00187 public $mUser; 00188 00192 public $mIsPreview = false; 00193 00197 public $mIsSectionPreview = false; 00198 00202 public $mIsPrintable = false; 00203 00207 public $mExtraKey = ''; 00208 00212 protected $onAccessCallback = null; 00213 00219 private $redirectTarget = null; 00220 00221 public function getInterwikiMagic() { 00222 return $this->mInterwikiMagic; 00223 } 00224 00225 public function getAllowExternalImages() { 00226 return $this->mAllowExternalImages; 00227 } 00228 00229 public function getAllowExternalImagesFrom() { 00230 return $this->mAllowExternalImagesFrom; 00231 } 00232 00233 public function getEnableImageWhitelist() { 00234 return $this->mEnableImageWhitelist; 00235 } 00236 00237 public function getEditSection() { 00238 return $this->mEditSection; 00239 } 00240 00241 public function getNumberHeadings() { 00242 $this->optionUsed( 'numberheadings' ); 00243 00244 return $this->mNumberHeadings; 00245 } 00246 00247 public function getAllowSpecialInclusion() { 00248 return $this->mAllowSpecialInclusion; 00249 } 00250 00251 public function getTidy() { 00252 return $this->mTidy; 00253 } 00254 00255 public function getInterfaceMessage() { 00256 return $this->mInterfaceMessage; 00257 } 00258 00259 public function getTargetLanguage() { 00260 return $this->mTargetLanguage; 00261 } 00262 00263 public function getMaxIncludeSize() { 00264 return $this->mMaxIncludeSize; 00265 } 00266 00267 public function getMaxPPNodeCount() { 00268 return $this->mMaxPPNodeCount; 00269 } 00270 00271 public function getMaxGeneratedPPNodeCount() { 00272 return $this->mMaxGeneratedPPNodeCount; 00273 } 00274 00275 public function getMaxPPExpandDepth() { 00276 return $this->mMaxPPExpandDepth; 00277 } 00278 00279 public function getMaxTemplateDepth() { 00280 return $this->mMaxTemplateDepth; 00281 } 00282 00283 /* @since 1.20 */ 00284 public function getExpensiveParserFunctionLimit() { 00285 return $this->mExpensiveParserFunctionLimit; 00286 } 00287 00288 public function getRemoveComments() { 00289 return $this->mRemoveComments; 00290 } 00291 00292 public function getTemplateCallback() { 00293 return $this->mTemplateCallback; 00294 } 00295 00296 public function getEnableLimitReport() { 00297 return $this->mEnableLimitReport; 00298 } 00299 00300 public function getCleanSignatures() { 00301 return $this->mCleanSignatures; 00302 } 00303 00304 public function getExternalLinkTarget() { 00305 return $this->mExternalLinkTarget; 00306 } 00307 00308 public function getDisableContentConversion() { 00309 return $this->mDisableContentConversion; 00310 } 00311 00312 public function getDisableTitleConversion() { 00313 return $this->mDisableTitleConversion; 00314 } 00315 00316 public function getThumbSize() { 00317 $this->optionUsed( 'thumbsize' ); 00318 00319 return $this->mThumbSize; 00320 } 00321 00322 public function getStubThreshold() { 00323 $this->optionUsed( 'stubthreshold' ); 00324 00325 return $this->mStubThreshold; 00326 } 00327 00328 public function getIsPreview() { 00329 return $this->mIsPreview; 00330 } 00331 00332 public function getIsSectionPreview() { 00333 return $this->mIsSectionPreview; 00334 } 00335 00336 public function getIsPrintable() { 00337 $this->optionUsed( 'printable' ); 00338 00339 return $this->mIsPrintable; 00340 } 00341 00342 public function getUser() { 00343 return $this->mUser; 00344 } 00345 00346 public function getPreSaveTransform() { 00347 return $this->mPreSaveTransform; 00348 } 00349 00350 public function getDateFormat() { 00351 $this->optionUsed( 'dateformat' ); 00352 if ( !isset( $this->mDateFormat ) ) { 00353 $this->mDateFormat = $this->mUser->getDatePreference(); 00354 } 00355 return $this->mDateFormat; 00356 } 00357 00358 public function getTimestamp() { 00359 if ( !isset( $this->mTimestamp ) ) { 00360 $this->mTimestamp = wfTimestampNow(); 00361 } 00362 return $this->mTimestamp; 00363 } 00364 00381 public function getUserLangObj() { 00382 $this->optionUsed( 'userlang' ); 00383 return $this->mUserLang; 00384 } 00385 00392 public function getUserLang() { 00393 return $this->getUserLangObj()->getCode(); 00394 } 00395 00396 public function setInterwikiMagic( $x ) { 00397 return wfSetVar( $this->mInterwikiMagic, $x ); 00398 } 00399 00400 public function setAllowExternalImages( $x ) { 00401 return wfSetVar( $this->mAllowExternalImages, $x ); 00402 } 00403 00404 public function setAllowExternalImagesFrom( $x ) { 00405 return wfSetVar( $this->mAllowExternalImagesFrom, $x ); 00406 } 00407 00408 public function setEnableImageWhitelist( $x ) { 00409 return wfSetVar( $this->mEnableImageWhitelist, $x ); 00410 } 00411 00412 public function setDateFormat( $x ) { 00413 return wfSetVar( $this->mDateFormat, $x ); 00414 } 00415 00416 public function setEditSection( $x ) { 00417 return wfSetVar( $this->mEditSection, $x ); 00418 } 00419 00420 public function setNumberHeadings( $x ) { 00421 return wfSetVar( $this->mNumberHeadings, $x ); 00422 } 00423 00424 public function setAllowSpecialInclusion( $x ) { 00425 return wfSetVar( $this->mAllowSpecialInclusion, $x ); 00426 } 00427 00428 public function setTidy( $x ) { 00429 return wfSetVar( $this->mTidy, $x ); 00430 } 00431 00432 public function setInterfaceMessage( $x ) { 00433 return wfSetVar( $this->mInterfaceMessage, $x ); 00434 } 00435 00436 public function setTargetLanguage( $x ) { 00437 return wfSetVar( $this->mTargetLanguage, $x, true ); 00438 } 00439 00440 public function setMaxIncludeSize( $x ) { 00441 return wfSetVar( $this->mMaxIncludeSize, $x ); 00442 } 00443 00444 public function setMaxPPNodeCount( $x ) { 00445 return wfSetVar( $this->mMaxPPNodeCount, $x ); 00446 } 00447 00448 public function setMaxGeneratedPPNodeCount( $x ) { 00449 return wfSetVar( $this->mMaxGeneratedPPNodeCount, $x ); 00450 } 00451 00452 public function setMaxTemplateDepth( $x ) { 00453 return wfSetVar( $this->mMaxTemplateDepth, $x ); 00454 } 00455 00456 /* @since 1.20 */ 00457 public function setExpensiveParserFunctionLimit( $x ) { 00458 return wfSetVar( $this->mExpensiveParserFunctionLimit, $x ); 00459 } 00460 00461 public function setRemoveComments( $x ) { 00462 return wfSetVar( $this->mRemoveComments, $x ); 00463 } 00464 00465 public function setTemplateCallback( $x ) { 00466 return wfSetVar( $this->mTemplateCallback, $x ); 00467 } 00468 00469 public function enableLimitReport( $x = true ) { 00470 return wfSetVar( $this->mEnableLimitReport, $x ); 00471 } 00472 00473 public function setTimestamp( $x ) { 00474 return wfSetVar( $this->mTimestamp, $x ); 00475 } 00476 00477 public function setCleanSignatures( $x ) { 00478 return wfSetVar( $this->mCleanSignatures, $x ); 00479 } 00480 00481 public function setExternalLinkTarget( $x ) { 00482 return wfSetVar( $this->mExternalLinkTarget, $x ); 00483 } 00484 00485 public function disableContentConversion( $x = true ) { 00486 return wfSetVar( $this->mDisableContentConversion, $x ); 00487 } 00488 00489 public function disableTitleConversion( $x = true ) { 00490 return wfSetVar( $this->mDisableTitleConversion, $x ); 00491 } 00492 00493 public function setUserLang( $x ) { 00494 if ( is_string( $x ) ) { 00495 $x = Language::factory( $x ); 00496 } 00497 00498 return wfSetVar( $this->mUserLang, $x ); 00499 } 00500 00501 public function setThumbSize( $x ) { 00502 return wfSetVar( $this->mThumbSize, $x ); 00503 } 00504 00505 public function setStubThreshold( $x ) { 00506 return wfSetVar( $this->mStubThreshold, $x ); 00507 } 00508 00509 public function setPreSaveTransform( $x ) { 00510 return wfSetVar( $this->mPreSaveTransform, $x ); 00511 } 00512 00513 public function setIsPreview( $x ) { 00514 return wfSetVar( $this->mIsPreview, $x ); 00515 } 00516 00517 public function setIsSectionPreview( $x ) { 00518 return wfSetVar( $this->mIsSectionPreview, $x ); 00519 } 00520 00521 public function setIsPrintable( $x ) { 00522 return wfSetVar( $this->mIsPrintable, $x ); 00523 } 00524 00535 function setRedirectTarget( $title ) { 00536 $this->redirectTarget = $title; 00537 } 00538 00545 function getRedirectTarget() { 00546 return $this->redirectTarget; 00547 } 00548 00553 public function addExtraKey( $key ) { 00554 $this->mExtraKey .= '!' . $key; 00555 } 00556 00562 public function __construct( $user = null, $lang = null ) { 00563 if ( $user === null ) { 00564 global $wgUser; 00565 if ( $wgUser === null ) { 00566 $user = new User; 00567 } else { 00568 $user = $wgUser; 00569 } 00570 } 00571 if ( $lang === null ) { 00572 global $wgLang; 00573 if ( !StubObject::isRealObject( $wgLang ) ) { 00574 $wgLang->_unstub(); 00575 } 00576 $lang = $wgLang; 00577 } 00578 $this->initialiseFromUser( $user, $lang ); 00579 } 00580 00588 public static function newFromUser( $user ) { 00589 return new ParserOptions( $user ); 00590 } 00591 00599 public static function newFromUserAndLang( User $user, Language $lang ) { 00600 return new ParserOptions( $user, $lang ); 00601 } 00602 00609 public static function newFromContext( IContextSource $context ) { 00610 return new ParserOptions( $context->getUser(), $context->getLanguage() ); 00611 } 00612 00619 private function initialiseFromUser( $user, $lang ) { 00620 global $wgInterwikiMagic, $wgAllowExternalImages, 00621 $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion, 00622 $wgMaxArticleSize, $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth, 00623 $wgCleanSignatures, $wgExternalLinkTarget, $wgExpensiveParserFunctionLimit, 00624 $wgMaxGeneratedPPNodeCount, $wgDisableLangConversion, $wgDisableTitleConversion; 00625 00626 wfProfileIn( __METHOD__ ); 00627 00628 $this->mInterwikiMagic = $wgInterwikiMagic; 00629 $this->mAllowExternalImages = $wgAllowExternalImages; 00630 $this->mAllowExternalImagesFrom = $wgAllowExternalImagesFrom; 00631 $this->mEnableImageWhitelist = $wgEnableImageWhitelist; 00632 $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion; 00633 $this->mMaxIncludeSize = $wgMaxArticleSize * 1024; 00634 $this->mMaxPPNodeCount = $wgMaxPPNodeCount; 00635 $this->mMaxGeneratedPPNodeCount = $wgMaxGeneratedPPNodeCount; 00636 $this->mMaxPPExpandDepth = $wgMaxPPExpandDepth; 00637 $this->mMaxTemplateDepth = $wgMaxTemplateDepth; 00638 $this->mExpensiveParserFunctionLimit = $wgExpensiveParserFunctionLimit; 00639 $this->mCleanSignatures = $wgCleanSignatures; 00640 $this->mExternalLinkTarget = $wgExternalLinkTarget; 00641 $this->mDisableContentConversion = $wgDisableLangConversion; 00642 $this->mDisableTitleConversion = $wgDisableLangConversion || $wgDisableTitleConversion; 00643 00644 $this->mUser = $user; 00645 $this->mNumberHeadings = $user->getOption( 'numberheadings' ); 00646 $this->mThumbSize = $user->getOption( 'thumbsize' ); 00647 $this->mStubThreshold = $user->getStubThreshold(); 00648 $this->mUserLang = $lang; 00649 00650 wfProfileOut( __METHOD__ ); 00651 } 00652 00658 public function registerWatcher( $callback ) { 00659 $this->onAccessCallback = $callback; 00660 } 00661 00666 public function optionUsed( $optionName ) { 00667 if ( $this->onAccessCallback ) { 00668 call_user_func( $this->onAccessCallback, $optionName ); 00669 } 00670 } 00671 00678 public static function legacyOptions() { 00679 return array( 00680 'stubthreshold', 00681 'numberheadings', 00682 'userlang', 00683 'thumbsize', 00684 'editsection', 00685 'printable' 00686 ); 00687 } 00688 00705 public function optionsHash( $forOptions, $title = null ) { 00706 global $wgRenderHashAppend; 00707 00708 // FIXME: Once the cache key is reorganized this argument 00709 // can be dropped. It was used when the math extension was 00710 // part of core. 00711 $confstr = '*'; 00712 00713 // Space assigned for the stubthreshold but unused 00714 // since it disables the parser cache, its value will always 00715 // be 0 when this function is called by parsercache. 00716 if ( in_array( 'stubthreshold', $forOptions ) ) { 00717 $confstr .= '!' . $this->mStubThreshold; 00718 } else { 00719 $confstr .= '!*'; 00720 } 00721 00722 if ( in_array( 'dateformat', $forOptions ) ) { 00723 $confstr .= '!' . $this->getDateFormat(); 00724 } 00725 00726 if ( in_array( 'numberheadings', $forOptions ) ) { 00727 $confstr .= '!' . ( $this->mNumberHeadings ? '1' : '' ); 00728 } else { 00729 $confstr .= '!*'; 00730 } 00731 00732 if ( in_array( 'userlang', $forOptions ) ) { 00733 $confstr .= '!' . $this->mUserLang->getCode(); 00734 } else { 00735 $confstr .= '!*'; 00736 } 00737 00738 if ( in_array( 'thumbsize', $forOptions ) ) { 00739 $confstr .= '!' . $this->mThumbSize; 00740 } else { 00741 $confstr .= '!*'; 00742 } 00743 00744 // add in language specific options, if any 00745 // @todo FIXME: This is just a way of retrieving the url/user preferred variant 00746 if ( !is_null( $title ) ) { 00747 $confstr .= $title->getPageLanguage()->getExtraHashOptions(); 00748 } else { 00749 global $wgContLang; 00750 $confstr .= $wgContLang->getExtraHashOptions(); 00751 } 00752 00753 $confstr .= $wgRenderHashAppend; 00754 00755 if ( !in_array( 'editsection', $forOptions ) ) { 00756 $confstr .= '!*'; 00757 } elseif ( !$this->mEditSection ) { 00758 $confstr .= '!edit=0'; 00759 } 00760 00761 if ( $this->mIsPrintable && in_array( 'printable', $forOptions ) ) { 00762 $confstr .= '!printable=1'; 00763 } 00764 00765 if ( $this->mExtraKey != '' ) { 00766 $confstr .= $this->mExtraKey; 00767 } 00768 00769 // Give a chance for extensions to modify the hash, if they have 00770 // extra options or other effects on the parser cache. 00771 wfRunHooks( 'PageRenderingHash', array( &$confstr, $this->getUser(), &$forOptions ) ); 00772 00773 // Make it a valid memcached key fragment 00774 $confstr = str_replace( ' ', '_', $confstr ); 00775 00776 return $confstr; 00777 } 00778 }