MediaWiki  REL1_22
ParserOptions.php
Go to the documentation of this file.
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 $mMath;
00171 
00175     var $mThumbSize;
00176 
00180     private $mStubThreshold;
00181 
00185     var $mUserLang;
00186 
00191     var $mUser;
00192 
00196     var $mIsPreview = false;
00197 
00201     var $mIsSectionPreview = false;
00202 
00206     var $mIsPrintable = false;
00207 
00211     var $mExtraKey = '';
00212 
00216     protected $onAccessCallback = null;
00217 
00218     function getInterwikiMagic()                { return $this->mInterwikiMagic; }
00219     function getAllowExternalImages()           { return $this->mAllowExternalImages; }
00220     function getAllowExternalImagesFrom()       { return $this->mAllowExternalImagesFrom; }
00221     function getEnableImageWhitelist()          { return $this->mEnableImageWhitelist; }
00222     function getEditSection()                   { return $this->mEditSection; }
00223     function getNumberHeadings()                { $this->optionUsed( 'numberheadings' );
00224                                                   return $this->mNumberHeadings; }
00225     function getAllowSpecialInclusion()         { return $this->mAllowSpecialInclusion; }
00226     function getTidy()                          { return $this->mTidy; }
00227     function getInterfaceMessage()              { return $this->mInterfaceMessage; }
00228     function getTargetLanguage()                { return $this->mTargetLanguage; }
00229     function getMaxIncludeSize()                { return $this->mMaxIncludeSize; }
00230     function getMaxPPNodeCount()                { return $this->mMaxPPNodeCount; }
00231     function getMaxGeneratedPPNodeCount()       { return $this->mMaxGeneratedPPNodeCount; }
00232     function getMaxPPExpandDepth()              { return $this->mMaxPPExpandDepth; }
00233     function getMaxTemplateDepth()              { return $this->mMaxTemplateDepth; }
00234     /* @since 1.20 */
00235     function getExpensiveParserFunctionLimit()  { return $this->mExpensiveParserFunctionLimit; }
00236     function getRemoveComments()                { return $this->mRemoveComments; }
00237     function getTemplateCallback()              { return $this->mTemplateCallback; }
00238     function getEnableLimitReport()             { return $this->mEnableLimitReport; }
00239     function getCleanSignatures()               { return $this->mCleanSignatures; }
00240     function getExternalLinkTarget()            { return $this->mExternalLinkTarget; }
00241     function getDisableContentConversion()      { return $this->mDisableContentConversion; }
00242     function getDisableTitleConversion()        { return $this->mDisableTitleConversion; }
00244     function getMath()                          { $this->optionUsed( 'math' );
00245                                                   return $this->mMath; }
00246     function getThumbSize()                     { $this->optionUsed( 'thumbsize' );
00247                                                   return $this->mThumbSize; }
00248     function getStubThreshold()                 { $this->optionUsed( 'stubthreshold' );
00249                                                   return $this->mStubThreshold; }
00250 
00251     function getIsPreview()                     { return $this->mIsPreview; }
00252     function getIsSectionPreview()              { return $this->mIsSectionPreview; }
00253     function getIsPrintable()                   { $this->optionUsed( 'printable' );
00254                                                   return $this->mIsPrintable; }
00255     function getUser()                          { return $this->mUser; }
00256     function getPreSaveTransform()              { return $this->mPreSaveTransform; }
00257 
00263     function getSkin( $title = null ) {
00264         wfDeprecated( __METHOD__, '1.18' );
00265         return new DummyLinker;
00266     }
00267 
00268     function getDateFormat() {
00269         $this->optionUsed( 'dateformat' );
00270         if ( !isset( $this->mDateFormat ) ) {
00271             $this->mDateFormat = $this->mUser->getDatePreference();
00272         }
00273         return $this->mDateFormat;
00274     }
00275 
00276     function getTimestamp() {
00277         if ( !isset( $this->mTimestamp ) ) {
00278             $this->mTimestamp = wfTimestampNow();
00279         }
00280         return $this->mTimestamp;
00281     }
00282 
00299     function getUserLangObj() {
00300         $this->optionUsed( 'userlang' );
00301         return $this->mUserLang;
00302     }
00303 
00310     function getUserLang() {
00311         return $this->getUserLangObj()->getCode();
00312     }
00313 
00314     function setInterwikiMagic( $x )            { return wfSetVar( $this->mInterwikiMagic, $x ); }
00315     function setAllowExternalImages( $x )       { return wfSetVar( $this->mAllowExternalImages, $x ); }
00316     function setAllowExternalImagesFrom( $x )   { return wfSetVar( $this->mAllowExternalImagesFrom, $x ); }
00317     function setEnableImageWhitelist( $x )      { return wfSetVar( $this->mEnableImageWhitelist, $x ); }
00318     function setDateFormat( $x )                { return wfSetVar( $this->mDateFormat, $x ); }
00319     function setEditSection( $x )               { return wfSetVar( $this->mEditSection, $x ); }
00320     function setNumberHeadings( $x )            { return wfSetVar( $this->mNumberHeadings, $x ); }
00321     function setAllowSpecialInclusion( $x )     { return wfSetVar( $this->mAllowSpecialInclusion, $x ); }
00322     function setTidy( $x )                      { return wfSetVar( $this->mTidy, $x ); }
00323 
00325     function setSkin( $x )                      { wfDeprecated( __METHOD__, '1.19' ); }
00326     function setInterfaceMessage( $x )          { return wfSetVar( $this->mInterfaceMessage, $x ); }
00327     function setTargetLanguage( $x )            { return wfSetVar( $this->mTargetLanguage, $x, true ); }
00328     function setMaxIncludeSize( $x )            { return wfSetVar( $this->mMaxIncludeSize, $x ); }
00329     function setMaxPPNodeCount( $x )            { return wfSetVar( $this->mMaxPPNodeCount, $x ); }
00330     function setMaxGeneratedPPNodeCount( $x )   { return wfSetVar( $this->mMaxGeneratedPPNodeCount, $x ); }
00331     function setMaxTemplateDepth( $x )          { return wfSetVar( $this->mMaxTemplateDepth, $x ); }
00332     /* @since 1.20 */
00333     function setExpensiveParserFunctionLimit( $x ) { return wfSetVar( $this->mExpensiveParserFunctionLimit, $x ); }
00334     function setRemoveComments( $x )            { return wfSetVar( $this->mRemoveComments, $x ); }
00335     function setTemplateCallback( $x )          { return wfSetVar( $this->mTemplateCallback, $x ); }
00336     function enableLimitReport( $x = true )     { return wfSetVar( $this->mEnableLimitReport, $x ); }
00337     function setTimestamp( $x )                 { return wfSetVar( $this->mTimestamp, $x ); }
00338     function setCleanSignatures( $x )           { return wfSetVar( $this->mCleanSignatures, $x ); }
00339     function setExternalLinkTarget( $x )        { return wfSetVar( $this->mExternalLinkTarget, $x ); }
00340     function disableContentConversion( $x = true ) { return wfSetVar( $this->mDisableContentConversion, $x ); }
00341     function disableTitleConversion( $x = true ) { return wfSetVar( $this->mDisableTitleConversion, $x ); }
00343     function setMath( $x )                      { return wfSetVar( $this->mMath, $x ); }
00344     function setUserLang( $x )                  {
00345         if ( is_string( $x ) ) {
00346             $x = Language::factory( $x );
00347         }
00348         return wfSetVar( $this->mUserLang, $x );
00349     }
00350     function setThumbSize( $x )                 { return wfSetVar( $this->mThumbSize, $x ); }
00351     function setStubThreshold( $x )             { return wfSetVar( $this->mStubThreshold, $x ); }
00352     function setPreSaveTransform( $x )          { return wfSetVar( $this->mPreSaveTransform, $x ); }
00353 
00354     function setIsPreview( $x )                 { return wfSetVar( $this->mIsPreview, $x ); }
00355     function setIsSectionPreview( $x )          { return wfSetVar( $this->mIsSectionPreview, $x ); }
00356     function setIsPrintable( $x )               { return wfSetVar( $this->mIsPrintable, $x ); }
00357 
00361     function addExtraKey( $key ) {
00362         $this->mExtraKey .= '!' . $key;
00363     }
00364 
00370     function __construct( $user = null, $lang = null ) {
00371         if ( $user === null ) {
00372             global $wgUser;
00373             if ( $wgUser === null ) {
00374                 $user = new User;
00375             } else {
00376                 $user = $wgUser;
00377             }
00378         }
00379         if ( $lang === null ) {
00380             global $wgLang;
00381             if ( !StubObject::isRealObject( $wgLang ) ) {
00382                 $wgLang->_unstub();
00383             }
00384             $lang = $wgLang;
00385         }
00386         $this->initialiseFromUser( $user, $lang );
00387     }
00388 
00396     public static function newFromUser( $user ) {
00397         return new ParserOptions( $user );
00398     }
00399 
00407     public static function newFromUserAndLang( User $user, Language $lang ) {
00408         return new ParserOptions( $user, $lang );
00409     }
00410 
00417     public static function newFromContext( IContextSource $context ) {
00418         return new ParserOptions( $context->getUser(), $context->getLanguage() );
00419     }
00420 
00427     private function initialiseFromUser( $user, $lang ) {
00428         global $wgInterwikiMagic, $wgAllowExternalImages,
00429             $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion,
00430             $wgMaxArticleSize, $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth,
00431             $wgCleanSignatures, $wgExternalLinkTarget, $wgExpensiveParserFunctionLimit,
00432             $wgMaxGeneratedPPNodeCount, $wgDisableLangConversion, $wgDisableTitleConversion;
00433 
00434         wfProfileIn( __METHOD__ );
00435 
00436         $this->mInterwikiMagic = $wgInterwikiMagic;
00437         $this->mAllowExternalImages = $wgAllowExternalImages;
00438         $this->mAllowExternalImagesFrom = $wgAllowExternalImagesFrom;
00439         $this->mEnableImageWhitelist = $wgEnableImageWhitelist;
00440         $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion;
00441         $this->mMaxIncludeSize = $wgMaxArticleSize * 1024;
00442         $this->mMaxPPNodeCount = $wgMaxPPNodeCount;
00443         $this->mMaxGeneratedPPNodeCount = $wgMaxGeneratedPPNodeCount;
00444         $this->mMaxPPExpandDepth = $wgMaxPPExpandDepth;
00445         $this->mMaxTemplateDepth = $wgMaxTemplateDepth;
00446         $this->mExpensiveParserFunctionLimit = $wgExpensiveParserFunctionLimit;
00447         $this->mCleanSignatures = $wgCleanSignatures;
00448         $this->mExternalLinkTarget = $wgExternalLinkTarget;
00449         $this->mDisableContentConversion = $wgDisableLangConversion;
00450         $this->mDisableTitleConversion = $wgDisableLangConversion || $wgDisableTitleConversion;
00451 
00452         $this->mUser = $user;
00453         $this->mNumberHeadings = $user->getOption( 'numberheadings' );
00454         $this->mMath = $user->getOption( 'math' );
00455         $this->mThumbSize = $user->getOption( 'thumbsize' );
00456         $this->mStubThreshold = $user->getStubThreshold();
00457         $this->mUserLang = $lang;
00458 
00459         wfProfileOut( __METHOD__ );
00460     }
00461 
00466     function registerWatcher( $callback ) {
00467         $this->onAccessCallback = $callback;
00468     }
00469 
00473     protected function optionUsed( $optionName ) {
00474         if ( $this->onAccessCallback ) {
00475             call_user_func( $this->onAccessCallback, $optionName );
00476         }
00477     }
00478 
00485     public static function legacyOptions() {
00486         return array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
00487     }
00488 
00507     public function optionsHash( $forOptions, $title = null ) {
00508         global $wgRenderHashAppend;
00509 
00510         $confstr = '';
00511 
00512         if ( in_array( 'math', $forOptions ) ) {
00513             $confstr .= $this->mMath;
00514         } else {
00515             $confstr .= '*';
00516         }
00517 
00518         // Space assigned for the stubthreshold but unused
00519         // since it disables the parser cache, its value will always
00520         // be 0 when this function is called by parsercache.
00521         if ( in_array( 'stubthreshold', $forOptions ) ) {
00522             $confstr .= '!' . $this->mStubThreshold;
00523         } else {
00524             $confstr .= '!*';
00525         }
00526 
00527         if ( in_array( 'dateformat', $forOptions ) ) {
00528             $confstr .= '!' . $this->getDateFormat();
00529         }
00530 
00531         if ( in_array( 'numberheadings', $forOptions ) ) {
00532             $confstr .= '!' . ( $this->mNumberHeadings ? '1' : '' );
00533         } else {
00534             $confstr .= '!*';
00535         }
00536 
00537         if ( in_array( 'userlang', $forOptions ) ) {
00538             $confstr .= '!' . $this->mUserLang->getCode();
00539         } else {
00540             $confstr .= '!*';
00541         }
00542 
00543         if ( in_array( 'thumbsize', $forOptions ) ) {
00544             $confstr .= '!' . $this->mThumbSize;
00545         } else {
00546             $confstr .= '!*';
00547         }
00548 
00549         // add in language specific options, if any
00550         // @todo FIXME: This is just a way of retrieving the url/user preferred variant
00551         if ( !is_null( $title ) ) {
00552             $confstr .= $title->getPageLanguage()->getExtraHashOptions();
00553         } else {
00554             global $wgContLang;
00555             $confstr .= $wgContLang->getExtraHashOptions();
00556         }
00557 
00558         $confstr .= $wgRenderHashAppend;
00559 
00560         if ( !in_array( 'editsection', $forOptions ) ) {
00561             $confstr .= '!*';
00562         } elseif ( !$this->mEditSection ) {
00563             $confstr .= '!edit=0';
00564         }
00565 
00566         if ( $this->mIsPrintable && in_array( 'printable', $forOptions ) ) {
00567             $confstr .= '!printable=1';
00568         }
00569 
00570         if ( $this->mExtraKey != '' ) {
00571             $confstr .= $this->mExtraKey;
00572         }
00573 
00574         // Give a chance for extensions to modify the hash, if they have
00575         // extra options or other effects on the parser cache.
00576         wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
00577 
00578         // Make it a valid memcached key fragment
00579         $confstr = str_replace( ' ', '_', $confstr );
00580 
00581         return $confstr;
00582     }
00583 }