MediaWiki  REL1_19
ParserOptions.php
Go to the documentation of this file.
00001 <?php
00016 class ParserOptions {
00017         
00021         var $mUseDynamicDates;
00022         
00026         var $mInterwikiMagic;
00027         
00031         var $mAllowExternalImages;
00032         
00036         var $mAllowExternalImagesFrom;
00037         
00041         var $mEnableImageWhitelist;
00042         
00046         var $mDateFormat = null;
00047         
00051         var $mEditSection = true;
00052         
00056         var $mAllowSpecialInclusion;
00057         
00061         var $mTidy = false;
00062         
00066         var $mInterfaceMessage = false;
00067         
00071         var $mTargetLanguage = null;
00072         
00076         var $mMaxIncludeSize;
00077         
00081         var $mMaxPPNodeCount;
00082         
00086         var $mMaxPPExpandDepth;
00087         
00091         var $mMaxTemplateDepth;
00092         
00096         var $mRemoveComments = true;
00097         
00101         var $mTemplateCallback =
00102                 array( 'Parser', 'statelessFetchTemplate' );
00103                 
00107         var $mEnableLimitReport = false;
00108         
00112         var $mTimestamp;
00113         
00117         var $mExternalLinkTarget;
00118         
00125         var $mCleanSignatures;
00126         
00130         var $mPreSaveTransform = true;
00131 
00135         var $mNumberHeadings;
00136         
00140         var $mMath;
00141         
00145         var $mThumbSize;
00146         
00150         private $mStubThreshold;
00151         
00155         var $mUserLang;
00156 
00161         var $mUser;
00162         
00166         var $mIsPreview = false;
00167         
00171         var $mIsSectionPreview = false;
00172         
00176         var $mIsPrintable = false;
00177 
00181         var $mExtraKey = '';
00182 
00186         protected $onAccessCallback = null;
00187 
00188         function getUseDynamicDates()               { return $this->mUseDynamicDates; }
00189         function getInterwikiMagic()                { return $this->mInterwikiMagic; }
00190         function getAllowExternalImages()           { return $this->mAllowExternalImages; }
00191         function getAllowExternalImagesFrom()       { return $this->mAllowExternalImagesFrom; }
00192         function getEnableImageWhitelist()          { return $this->mEnableImageWhitelist; }
00193         function getEditSection()                   { return $this->mEditSection; }
00194         function getNumberHeadings()                { $this->optionUsed( 'numberheadings' );
00195                                                                                                   return $this->mNumberHeadings; }
00196         function getAllowSpecialInclusion()         { return $this->mAllowSpecialInclusion; }
00197         function getTidy()                          { return $this->mTidy; }
00198         function getInterfaceMessage()              { return $this->mInterfaceMessage; }
00199         function getTargetLanguage()                { return $this->mTargetLanguage; }
00200         function getMaxIncludeSize()                { return $this->mMaxIncludeSize; }
00201         function getMaxPPNodeCount()                { return $this->mMaxPPNodeCount; }
00202         function getMaxPPExpandDepth()              { return $this->mMaxPPExpandDepth; }
00203         function getMaxTemplateDepth()              { return $this->mMaxTemplateDepth; }
00204         function getRemoveComments()                { return $this->mRemoveComments; }
00205         function getTemplateCallback()              { return $this->mTemplateCallback; }
00206         function getEnableLimitReport()             { return $this->mEnableLimitReport; }
00207         function getCleanSignatures()               { return $this->mCleanSignatures; }
00208         function getExternalLinkTarget()            { return $this->mExternalLinkTarget; }
00209         function getMath()                          { $this->optionUsed( 'math' );
00210                                                                                                   return $this->mMath; }
00211         function getThumbSize()                     { $this->optionUsed( 'thumbsize' );
00212                                                                                                   return $this->mThumbSize; }
00213         function getStubThreshold()                 { $this->optionUsed( 'stubthreshold' );
00214                                                                                                   return $this->mStubThreshold; }
00215 
00216         function getIsPreview()                     { return $this->mIsPreview; }
00217         function getIsSectionPreview()              { return $this->mIsSectionPreview; }
00218         function getIsPrintable()                   { $this->optionUsed( 'printable' );
00219                                                                                                   return $this->mIsPrintable; }
00220         function getUser()                          { return $this->mUser; }
00221         function getPreSaveTransform()              { return $this->mPreSaveTransform; }
00222 
00228         function getSkin( $title = null ) {
00229                 wfDeprecated( __METHOD__, '1.18' );
00230                 return new DummyLinker;
00231         }
00232 
00233         function getDateFormat() {
00234                 $this->optionUsed( 'dateformat' );
00235                 if ( !isset( $this->mDateFormat ) ) {
00236                         $this->mDateFormat = $this->mUser->getDatePreference();
00237                 }
00238                 return $this->mDateFormat;
00239         }
00240 
00241         function getTimestamp() {
00242                 if ( !isset( $this->mTimestamp ) ) {
00243                         $this->mTimestamp = wfTimestampNow();
00244                 }
00245                 return $this->mTimestamp;
00246         }
00247 
00256         function getUserLangObj() {
00257                 $this->optionUsed( 'userlang' );
00258                 return $this->mUserLang;
00259         }
00260 
00267         function getUserLang() {
00268                 return $this->getUserLangObj()->getCode();
00269         }
00270 
00271         function setUseDynamicDates( $x )           { return wfSetVar( $this->mUseDynamicDates, $x ); }
00272         function setInterwikiMagic( $x )            { return wfSetVar( $this->mInterwikiMagic, $x ); }
00273         function setAllowExternalImages( $x )       { return wfSetVar( $this->mAllowExternalImages, $x ); }
00274         function setAllowExternalImagesFrom( $x )   { return wfSetVar( $this->mAllowExternalImagesFrom, $x ); }
00275         function setEnableImageWhitelist( $x )      { return wfSetVar( $this->mEnableImageWhitelist, $x ); }
00276         function setDateFormat( $x )                { return wfSetVar( $this->mDateFormat, $x ); }
00277         function setEditSection( $x )               { return wfSetVar( $this->mEditSection, $x ); }
00278         function setNumberHeadings( $x )            { return wfSetVar( $this->mNumberHeadings, $x ); }
00279         function setAllowSpecialInclusion( $x )     { return wfSetVar( $this->mAllowSpecialInclusion, $x ); }
00280         function setTidy( $x )                      { return wfSetVar( $this->mTidy, $x ); }
00281 
00283         function setSkin( $x )                      { wfDeprecated( __METHOD__, '1.19' ); }
00284         function setInterfaceMessage( $x )          { return wfSetVar( $this->mInterfaceMessage, $x ); }
00285         function setTargetLanguage( $x )            { return wfSetVar( $this->mTargetLanguage, $x, true ); }
00286         function setMaxIncludeSize( $x )            { return wfSetVar( $this->mMaxIncludeSize, $x ); }
00287         function setMaxPPNodeCount( $x )            { return wfSetVar( $this->mMaxPPNodeCount, $x ); }
00288         function setMaxTemplateDepth( $x )          { return wfSetVar( $this->mMaxTemplateDepth, $x ); }
00289         function setRemoveComments( $x )            { return wfSetVar( $this->mRemoveComments, $x ); }
00290         function setTemplateCallback( $x )          { return wfSetVar( $this->mTemplateCallback, $x ); }
00291         function enableLimitReport( $x = true )     { return wfSetVar( $this->mEnableLimitReport, $x ); }
00292         function setTimestamp( $x )                 { return wfSetVar( $this->mTimestamp, $x ); }
00293         function setCleanSignatures( $x )           { return wfSetVar( $this->mCleanSignatures, $x ); }
00294         function setExternalLinkTarget( $x )        { return wfSetVar( $this->mExternalLinkTarget, $x ); }
00295         function setMath( $x )                      { return wfSetVar( $this->mMath, $x ); }
00296         function setUserLang( $x )                  {
00297                 if ( is_string( $x ) ) {
00298                         $x = Language::factory( $x );
00299                 }
00300                 return wfSetVar( $this->mUserLang, $x );
00301         }
00302         function setThumbSize( $x )                 { return wfSetVar( $this->mThumbSize, $x ); }
00303         function setStubThreshold( $x )             { return wfSetVar( $this->mStubThreshold, $x ); }
00304         function setPreSaveTransform( $x )          { return wfSetVar( $this->mPreSaveTransform, $x ); }
00305 
00306         function setIsPreview( $x )                 { return wfSetVar( $this->mIsPreview, $x ); }
00307         function setIsSectionPreview( $x )          { return wfSetVar( $this->mIsSectionPreview, $x ); }
00308         function setIsPrintable( $x )               { return wfSetVar( $this->mIsPrintable, $x ); }
00309 
00313         function addExtraKey( $key ) {
00314                 $this->mExtraKey .= '!' . $key;
00315         }
00316 
00322         function __construct( $user = null, $lang = null ) {
00323                 if ( $user === null ) {
00324                         global $wgUser;
00325                         if ( $wgUser === null ) {
00326                                 $user = new User;
00327                         } else {
00328                                 $user = $wgUser;
00329                         }
00330                 }
00331                 if ( $lang === null ) {
00332                         global $wgLang;
00333                         if ( !StubObject::isRealObject( $wgLang ) ) {
00334                                 $wgLang->_unstub();
00335                         }
00336                         $lang = $wgLang;
00337                 }
00338                 $this->initialiseFromUser( $user, $lang );
00339         }
00340 
00348         public static function newFromUser( $user ) {
00349                 return new ParserOptions( $user );
00350         }
00351 
00359         public static function newFromUserAndLang( User $user, Language $lang ) {
00360                 return new ParserOptions( $user, $lang );
00361         }
00362 
00369         public static function newFromContext( IContextSource $context ) {
00370                 return new ParserOptions( $context->getUser(), $context->getLanguage() );
00371         }
00372 
00379         private function initialiseFromUser( $user, $lang ) {
00380                 global $wgUseDynamicDates, $wgInterwikiMagic, $wgAllowExternalImages,
00381                         $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion,
00382                         $wgMaxArticleSize, $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth,
00383                         $wgCleanSignatures, $wgExternalLinkTarget;
00384 
00385                 wfProfileIn( __METHOD__ );
00386 
00387                 $this->mUseDynamicDates = $wgUseDynamicDates;
00388                 $this->mInterwikiMagic = $wgInterwikiMagic;
00389                 $this->mAllowExternalImages = $wgAllowExternalImages;
00390                 $this->mAllowExternalImagesFrom = $wgAllowExternalImagesFrom;
00391                 $this->mEnableImageWhitelist = $wgEnableImageWhitelist;
00392                 $this->mAllowSpecialInclusion = $wgAllowSpecialInclusion;
00393                 $this->mMaxIncludeSize = $wgMaxArticleSize * 1024;
00394                 $this->mMaxPPNodeCount = $wgMaxPPNodeCount;
00395                 $this->mMaxPPExpandDepth = $wgMaxPPExpandDepth;
00396                 $this->mMaxTemplateDepth = $wgMaxTemplateDepth;
00397                 $this->mCleanSignatures = $wgCleanSignatures;
00398                 $this->mExternalLinkTarget = $wgExternalLinkTarget;
00399 
00400                 $this->mUser = $user;
00401                 $this->mNumberHeadings = $user->getOption( 'numberheadings' );
00402                 $this->mMath = $user->getOption( 'math' );
00403                 $this->mThumbSize = $user->getOption( 'thumbsize' );
00404                 $this->mStubThreshold = $user->getStubThreshold();
00405                 $this->mUserLang = $lang;
00406 
00407                 wfProfileOut( __METHOD__ );
00408         }
00409 
00414         function registerWatcher( $callback ) {
00415                 $this->onAccessCallback = $callback;
00416         }
00417 
00421         protected function optionUsed( $optionName ) {
00422                 if ( $this->onAccessCallback ) {
00423                         call_user_func( $this->onAccessCallback, $optionName );
00424                 }
00425         }
00426 
00432         public static function legacyOptions() {
00433                 global $wgUseDynamicDates;
00434                 $legacyOpts = array( 'math', 'stubthreshold', 'numberheadings', 'userlang', 'thumbsize', 'editsection', 'printable' );
00435                 if ( $wgUseDynamicDates ) {
00436                         $legacyOpts[] = 'dateformat';
00437                 }
00438                 return $legacyOpts;
00439         }
00440 
00459         public function optionsHash( $forOptions, $title = null ) {
00460                 global $wgRenderHashAppend;
00461 
00462                 $confstr = '';
00463 
00464                 if ( in_array( 'math', $forOptions ) ) {
00465                         $confstr .= $this->mMath;
00466                 } else {
00467                         $confstr .= '*';
00468                 }
00469 
00470 
00471                 // Space assigned for the stubthreshold but unused
00472                 // since it disables the parser cache, its value will always
00473                 // be 0 when this function is called by parsercache.
00474                 if ( in_array( 'stubthreshold', $forOptions ) ) {
00475                         $confstr .= '!' . $this->mStubThreshold;
00476                 } else {
00477                         $confstr .= '!*' ;
00478                 }
00479 
00480                 if ( in_array( 'dateformat', $forOptions ) ) {
00481                         $confstr .= '!' . $this->getDateFormat();
00482                 }
00483 
00484                 if ( in_array( 'numberheadings', $forOptions ) ) {
00485                         $confstr .= '!' . ( $this->mNumberHeadings ? '1' : '' );
00486                 } else {
00487                         $confstr .= '!*';
00488                 }
00489 
00490                 if ( in_array( 'userlang', $forOptions ) ) {
00491                         $confstr .= '!' . $this->mUserLang->getCode();
00492                 } else {
00493                         $confstr .= '!*';
00494                 }
00495 
00496                 if ( in_array( 'thumbsize', $forOptions ) ) {
00497                         $confstr .= '!' . $this->mThumbSize;
00498                 } else {
00499                         $confstr .= '!*';
00500                 }
00501 
00502                 // add in language specific options, if any
00503                 // @todo FIXME: This is just a way of retrieving the url/user preferred variant
00504                 if( !is_null( $title ) ) {
00505                         $confstr .= $title->getPageLanguage()->getExtraHashOptions();
00506                 } else {
00507                         global $wgContLang;
00508                         $confstr .= $wgContLang->getExtraHashOptions();
00509                 }
00510 
00511                 $confstr .= $wgRenderHashAppend;
00512 
00513                 if ( !in_array( 'editsection', $forOptions ) ) {
00514                         $confstr .= '!*';
00515                 } elseif ( !$this->mEditSection ) {
00516                         $confstr .= '!edit=0';
00517                 }
00518 
00519                 if ( $this->mIsPrintable && in_array( 'printable', $forOptions ) ) {
00520                         $confstr .= '!printable=1';
00521                 }
00522 
00523                 if ( $this->mExtraKey != '' )
00524                         $confstr .= $this->mExtraKey;
00525 
00526                 // Give a chance for extensions to modify the hash, if they have
00527                 // extra options or other effects on the parser cache.
00528                 wfRunHooks( 'PageRenderingHash', array( &$confstr ) );
00529 
00530                 // Make it a valid memcached key fragment
00531                 $confstr = str_replace( ' ', '_', $confstr );
00532 
00533                 return $confstr;
00534         }
00535 }