MediaWiki  REL1_21
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; }
00243         function getMath()                          { $this->optionUsed( 'math' );
00244                                                                                                   return $this->mMath; }
00245         function getThumbSize()                     { $this->optionUsed( 'thumbsize' );
00246                                                                                                   return $this->mThumbSize; }
00247         function getStubThreshold()                 { $this->optionUsed( 'stubthreshold' );
00248                                                                                                   return $this->mStubThreshold; }
00249 
00250         function getIsPreview()                     { return $this->mIsPreview; }
00251         function getIsSectionPreview()              { return $this->mIsSectionPreview; }
00252         function getIsPrintable()                   { $this->optionUsed( 'printable' );
00253                                                                                                   return $this->mIsPrintable; }
00254         function getUser()                          { return $this->mUser; }
00255         function getPreSaveTransform()              { return $this->mPreSaveTransform; }
00256 
00262         function getSkin( $title = null ) {
00263                 wfDeprecated( __METHOD__, '1.18' );
00264                 return new DummyLinker;
00265         }
00266 
00267         function getDateFormat() {
00268                 $this->optionUsed( 'dateformat' );
00269                 if ( !isset( $this->mDateFormat ) ) {
00270                         $this->mDateFormat = $this->mUser->getDatePreference();
00271                 }
00272                 return $this->mDateFormat;
00273         }
00274 
00275         function getTimestamp() {
00276                 if ( !isset( $this->mTimestamp ) ) {
00277                         $this->mTimestamp = wfTimestampNow();
00278                 }
00279                 return $this->mTimestamp;
00280         }
00281 
00290         function getUserLangObj() {
00291                 $this->optionUsed( 'userlang' );
00292                 return $this->mUserLang;
00293         }
00294 
00301         function getUserLang() {
00302                 return $this->getUserLangObj()->getCode();
00303         }
00304 
00305         function setInterwikiMagic( $x )            { return wfSetVar( $this->mInterwikiMagic, $x ); }
00306         function setAllowExternalImages( $x )       { return wfSetVar( $this->mAllowExternalImages, $x ); }
00307         function setAllowExternalImagesFrom( $x )   { return wfSetVar( $this->mAllowExternalImagesFrom, $x ); }
00308         function setEnableImageWhitelist( $x )      { return wfSetVar( $this->mEnableImageWhitelist, $x ); }
00309         function setDateFormat( $x )                { return wfSetVar( $this->mDateFormat, $x ); }
00310         function setEditSection( $x )               { return wfSetVar( $this->mEditSection, $x ); }
00311         function setNumberHeadings( $x )            { return wfSetVar( $this->mNumberHeadings, $x ); }
00312         function setAllowSpecialInclusion( $x )     { return wfSetVar( $this->mAllowSpecialInclusion, $x ); }
00313         function setTidy( $x )                      { return wfSetVar( $this->mTidy, $x ); }
00314 
00316         function setSkin( $x )                      { wfDeprecated( __METHOD__, '1.19' ); }
00317         function setInterfaceMessage( $x )          { return wfSetVar( $this->mInterfaceMessage, $x ); }
00318         function setTargetLanguage( $x )            { return wfSetVar( $this->mTargetLanguage, $x, true ); }
00319         function setMaxIncludeSize( $x )            { return wfSetVar( $this->mMaxIncludeSize, $x ); }
00320         function setMaxPPNodeCount( $x )            { return wfSetVar( $this->mMaxPPNodeCount, $x ); }
00321         function setMaxGeneratedPPNodeCount( $x )   { return wfSetVar( $this->mMaxGeneratedPPNodeCount, $x ); }
00322         function setMaxTemplateDepth( $x )          { return wfSetVar( $this->mMaxTemplateDepth, $x ); }
00323         /* @since 1.20 */
00324         function setExpensiveParserFunctionLimit( $x ) { return wfSetVar( $this->mExpensiveParserFunctionLimit, $x ); }
00325         function setRemoveComments( $x )            { return wfSetVar( $this->mRemoveComments, $x ); }
00326         function setTemplateCallback( $x )          { return wfSetVar( $this->mTemplateCallback, $x ); }
00327         function enableLimitReport( $x = true )     { return wfSetVar( $this->mEnableLimitReport, $x ); }
00328         function setTimestamp( $x )                 { return wfSetVar( $this->mTimestamp, $x ); }
00329         function setCleanSignatures( $x )           { return wfSetVar( $this->mCleanSignatures, $x ); }
00330         function setExternalLinkTarget( $x )        { return wfSetVar( $this->mExternalLinkTarget, $x ); }
00331         function disableContentConversion( $x = true ) { return wfSetVar( $this->mDisableContentConversion, $x ); }
00332         function disableTitleConversion( $x = true ) { return wfSetVar( $this->mDisableTitleConversion, $x ); }
00333         function setMath( $x )                      { return wfSetVar( $this->mMath, $x ); }
00334         function setUserLang( $x )                  {
00335                 if ( is_string( $x ) ) {
00336                         $x = Language::factory( $x );
00337                 }
00338                 return wfSetVar( $this->mUserLang, $x );
00339         }
00340         function setThumbSize( $x )                 { return wfSetVar( $this->mThumbSize, $x ); }
00341         function setStubThreshold( $x )             { return wfSetVar( $this->mStubThreshold, $x ); }
00342         function setPreSaveTransform( $x )          { return wfSetVar( $this->mPreSaveTransform, $x ); }
00343 
00344         function setIsPreview( $x )                 { return wfSetVar( $this->mIsPreview, $x ); }
00345         function setIsSectionPreview( $x )          { return wfSetVar( $this->mIsSectionPreview, $x ); }
00346         function setIsPrintable( $x )               { return wfSetVar( $this->mIsPrintable, $x ); }
00347 
00351         function addExtraKey( $key ) {
00352                 $this->mExtraKey .= '!' . $key;
00353         }
00354 
00360         function __construct( $user = null, $lang = null ) {
00361                 if ( $user === null ) {
00362                         global $wgUser;
00363                         if ( $wgUser === null ) {
00364                                 $user = new User;
00365                         } else {
00366                                 $user = $wgUser;
00367                         }
00368                 }
00369                 if ( $lang === null ) {
00370                         global $wgLang;
00371                         if ( !StubObject::isRealObject( $wgLang ) ) {
00372                                 $wgLang->_unstub();
00373                         }
00374                         $lang = $wgLang;
00375                 }
00376                 $this->initialiseFromUser( $user, $lang );
00377         }
00378 
00386         public static function newFromUser( $user ) {
00387                 return new ParserOptions( $user );
00388         }
00389 
00397         public static function newFromUserAndLang( User $user, Language $lang ) {
00398                 return new ParserOptions( $user, $lang );
00399         }
00400 
00407         public static function newFromContext( IContextSource $context ) {
00408                 return new ParserOptions( $context->getUser(), $context->getLanguage() );
00409         }
00410 
00417         private function initialiseFromUser( $user, $lang ) {
00418                 global $wgInterwikiMagic, $wgAllowExternalImages,
00419                         $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion,
00420                         $wgMaxArticleSize, $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth,
00421                         $wgCleanSignatures, $wgExternalLinkTarget, $wgExpensiveParserFunctionLimit,
00422                         $wgMaxGeneratedPPNodeCount, $wgDisableLangConversion, $wgDisableTitleConversion;
00423 
00424                 wfProfileIn( __METHOD__ );
00425 
00426                 $this->mInterwikiMagic = $wgInterwikiMagic;
00427                 $this->mAllowExternalImages = $wgAllowExternalImages;
00428                 $this->mAllowExternalImagesFrom = $wgAllowExternalImagesFrom;
00429                 $this->mEnableImageWhitelist = $wgEnableImageWhitelist;
00430                 $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion;
00431                 $this->mMaxIncludeSize = $wgMaxArticleSize * 1024;
00432                 $this->mMaxPPNodeCount = $wgMaxPPNodeCount;
00433                 $this->mMaxGeneratedPPNodeCount = $wgMaxGeneratedPPNodeCount;
00434                 $this->mMaxPPExpandDepth = $wgMaxPPExpandDepth;
00435                 $this->mMaxTemplateDepth = $wgMaxTemplateDepth;
00436                 $this->mExpensiveParserFunctionLimit = $wgExpensiveParserFunctionLimit;
00437                 $this->mCleanSignatures = $wgCleanSignatures;
00438                 $this->mExternalLinkTarget = $wgExternalLinkTarget;
00439                 $this->mDisableContentConversion = $wgDisableLangConversion;
00440                 $this->mDisableTitleConversion = $wgDisableLangConversion || $wgDisableTitleConversion;
00441 
00442                 $this->mUser = $user;
00443                 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
00444                 $this->mMath = $user->getOption( 'math' );
00445                 $this->mThumbSize = $user->getOption( 'thumbsize' );
00446                 $this->mStubThreshold = $user->getStubThreshold();
00447                 $this->mUserLang = $lang;
00448 
00449                 wfProfileOut( __METHOD__ );
00450         }
00451 
00456         function registerWatcher( $callback ) {
00457                 $this->onAccessCallback = $callback;
00458         }
00459 
00463         protected function optionUsed( $optionName ) {
00464                 if ( $this->onAccessCallback ) {
00465                         call_user_func( $this->onAccessCallback, $optionName );
00466                 }
00467         }
00468 
00475         public static function legacyOptions() {
00476                 return array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
00477         }
00478 
00497         public function optionsHash( $forOptions, $title = null ) {
00498                 global $wgRenderHashAppend;
00499 
00500                 $confstr = '';
00501 
00502                 if ( in_array( 'math', $forOptions ) ) {
00503                         $confstr .= $this->mMath;
00504                 } else {
00505                         $confstr .= '*';
00506                 }
00507 
00508                 // Space assigned for the stubthreshold but unused
00509                 // since it disables the parser cache, its value will always
00510                 // be 0 when this function is called by parsercache.
00511                 if ( in_array( 'stubthreshold', $forOptions ) ) {
00512                         $confstr .= '!' . $this->mStubThreshold;
00513                 } else {
00514                         $confstr .= '!*';
00515                 }
00516 
00517                 if ( in_array( 'dateformat', $forOptions ) ) {
00518                         $confstr .= '!' . $this->getDateFormat();
00519                 }
00520 
00521                 if ( in_array( 'numberheadings', $forOptions ) ) {
00522                         $confstr .= '!' . ( $this->mNumberHeadings ? '1' : '' );
00523                 } else {
00524                         $confstr .= '!*';
00525                 }
00526 
00527                 if ( in_array( 'userlang', $forOptions ) ) {
00528                         $confstr .= '!' . $this->mUserLang->getCode();
00529                 } else {
00530                         $confstr .= '!*';
00531                 }
00532 
00533                 if ( in_array( 'thumbsize', $forOptions ) ) {
00534                         $confstr .= '!' . $this->mThumbSize;
00535                 } else {
00536                         $confstr .= '!*';
00537                 }
00538 
00539                 // add in language specific options, if any
00540                 // @todo FIXME: This is just a way of retrieving the url/user preferred variant
00541                 if( !is_null( $title ) ) {
00542                         $confstr .= $title->getPageLanguage()->getExtraHashOptions();
00543                 } else {
00544                         global $wgContLang;
00545                         $confstr .= $wgContLang->getExtraHashOptions();
00546                 }
00547 
00548                 $confstr .= $wgRenderHashAppend;
00549 
00550                 if ( !in_array( 'editsection', $forOptions ) ) {
00551                         $confstr .= '!*';
00552                 } elseif ( !$this->mEditSection ) {
00553                         $confstr .= '!edit=0';
00554                 }
00555 
00556                 if ( $this->mIsPrintable && in_array( 'printable', $forOptions ) ) {
00557                         $confstr .= '!printable=1';
00558                 }
00559 
00560                 if ( $this->mExtraKey != '' )
00561                         $confstr .= $this->mExtraKey;
00562 
00563                 // Give a chance for extensions to modify the hash, if they have
00564                 // extra options or other effects on the parser cache.
00565                 wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
00566 
00567                 // Make it a valid memcached key fragment
00568                 $confstr = str_replace( ' ', '_', $confstr );
00569 
00570                 return $confstr;
00571         }
00572 }