MediaWiki  REL1_19
ParserOutput.php
Go to the documentation of this file.
00001 <?php
00014 class CacheTime {
00015         var     $mVersion = Parser::VERSION,  # Compatibility check
00016                 $mCacheTime = '',             # Time when this object was generated, or -1 for uncacheable. Used in ParserCache.
00017                 $mCacheExpiry = null,         # Seconds after which the object should expire, use 0 for uncachable. Used in ParserCache.
00018                 $mContainsOldMagic;           # Boolean variable indicating if the input contained variables like {{CURRENTDAY}}
00019 
00020         function getCacheTime()              { return $this->mCacheTime; }
00021 
00022         function containsOldMagic()          { return $this->mContainsOldMagic; }
00023         function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
00024 
00031         function setCacheTime( $t )          { return wfSetVar( $this->mCacheTime, $t ); }
00032 
00043         function updateCacheExpiry( $seconds ) {
00044                 $seconds = (int)$seconds;
00045 
00046                 if ( $this->mCacheExpiry === null || $this->mCacheExpiry > $seconds ) {
00047                         $this->mCacheExpiry = $seconds;
00048                 }
00049 
00050                 // hack: set old-style marker for uncacheable entries.
00051                 if ( $this->mCacheExpiry !== null && $this->mCacheExpiry <= 0 ) {
00052                         $this->mCacheTime = -1;
00053                 }
00054         }
00055 
00064         function getCacheExpiry() {
00065                 global $wgParserCacheExpireTime;
00066 
00067                 if ( $this->mCacheTime < 0 ) {
00068                         return 0;
00069                 } // old-style marker for "not cachable"
00070 
00071                 $expire = $this->mCacheExpiry;
00072 
00073                 if ( $expire === null ) {
00074                         $expire = $wgParserCacheExpireTime;
00075                 } else {
00076                         $expire = min( $expire, $wgParserCacheExpireTime );
00077                 }
00078 
00079                 if( $this->containsOldMagic() ) { //compatibility hack
00080                         $expire = min( $expire, 3600 ); # 1 hour
00081                 }
00082 
00083                 if ( $expire <= 0 ) {
00084                         return 0; // not cachable
00085                 } else {
00086                         return $expire;
00087                 }
00088         }
00089 
00093         function isCacheable() {
00094                 return $this->getCacheExpiry() > 0;
00095         }
00096 
00105         public function expired( $touched ) {
00106                 global $wgCacheEpoch;
00107                 return !$this->isCacheable() || // parser says it's uncacheable
00108                            $this->getCacheTime() < $touched ||
00109                            $this->getCacheTime() <= $wgCacheEpoch ||
00110                            $this->getCacheTime() < wfTimestamp( TS_MW, time() - $this->getCacheExpiry() ) || // expiry period has passed
00111                            !isset( $this->mVersion ) ||
00112                            version_compare( $this->mVersion, Parser::VERSION, "lt" );
00113         }
00114 }
00115 
00116 class ParserOutput extends CacheTime {
00117         var $mText,                       # The output text
00118                 $mLanguageLinks,              # List of the full text of language links, in the order they appear
00119                 $mCategories,                 # Map of category names to sort keys
00120                 $mTitleText,                  # title text of the chosen language variant
00121                 $mLinks = array(),            # 2-D map of NS/DBK to ID for the links in the document. ID=zero for broken.
00122                 $mTemplates = array(),        # 2-D map of NS/DBK to ID for the template references. ID=zero for broken.
00123                 $mTemplateIds = array(),      # 2-D map of NS/DBK to rev ID for the template references. ID=zero for broken.
00124                 $mImages = array(),           # DB keys of the images used, in the array key only
00125                 $mFileSearchOptions = array(), # DB keys of the images used mapped to sha1 and MW timestamp
00126                 $mExternalLinks = array(),    # External link URLs, in the key only
00127                 $mInterwikiLinks = array(),   # 2-D map of prefix/DBK (in keys only) for the inline interwiki links in the document.
00128                 $mNewSection = false,         # Show a new section link?
00129                 $mHideNewSection = false,     # Hide the new section link?
00130                 $mNoGallery = false,          # No gallery on category page? (__NOGALLERY__)
00131                 $mHeadItems = array(),        # Items to put in the <head> section
00132                 $mModules = array(),          # Modules to be loaded by the resource loader
00133                 $mModuleScripts = array(),    # Modules of which only the JS will be loaded by the resource loader
00134                 $mModuleStyles = array(),     # Modules of which only the CSSS will be loaded by the resource loader
00135                 $mModuleMessages = array(),   # Modules of which only the messages will be loaded by the resource loader
00136                 $mOutputHooks = array(),      # Hook tags as per $wgParserOutputHooks
00137                 $mWarnings = array(),         # Warning text to be returned to the user. Wikitext formatted, in the key only
00138                 $mSections = array(),         # Table of contents
00139                 $mEditSectionTokens = false,  # prefix/suffix markers if edit sections were output as tokens
00140                 $mProperties = array(),       # Name/value pairs to be cached in the DB
00141                 $mTOCHTML = '',               # HTML of the TOC
00142                 $mTimestamp;                  # Timestamp of the revision
00143         private $mIndexPolicy = '';       # 'index' or 'noindex'?  Any other value will result in no change.
00144         private $mAccessedOptions = array(); # List of ParserOptions (stored in the keys)
00145         private $mPreventClickjacking = false; # Whether to emit X-Frame-Options: DENY
00146 
00147         const EDITSECTION_REGEX = '#<(?:mw:)?editsection page="(.*?)" section="(.*?)"(?:/>|>(.*?)(</(?:mw:)?editsection>))#';
00148 
00149         function __construct( $text = '', $languageLinks = array(), $categoryLinks = array(),
00150                 $containsOldMagic = false, $titletext = '' )
00151         {
00152                 $this->mText = $text;
00153                 $this->mLanguageLinks = $languageLinks;
00154                 $this->mCategories = $categoryLinks;
00155                 $this->mContainsOldMagic = $containsOldMagic;
00156                 $this->mTitleText = $titletext;
00157         }
00158 
00159         function getText() {
00160                 if ( $this->mEditSectionTokens ) {
00161                         return preg_replace_callback( ParserOutput::EDITSECTION_REGEX,
00162                                 array( &$this, 'replaceEditSectionLinksCallback' ), $this->mText );
00163                 }
00164                 return preg_replace( ParserOutput::EDITSECTION_REGEX, '', $this->mText );
00165         }
00166 
00171         function replaceEditSectionLinksCallback( $m ) {
00172                 global $wgOut, $wgLang;
00173                 $args = array(
00174                         htmlspecialchars_decode($m[1]),
00175                         htmlspecialchars_decode($m[2]),
00176                         isset($m[4]) ? $m[3] : null,
00177                 );
00178                 $args[0] = Title::newFromText( $args[0] );
00179                 if ( !is_object($args[0]) ) {
00180                         throw new MWException("Bad parser output text.");
00181                 }
00182                 $args[] = $wgLang->getCode();
00183                 $skin = $wgOut->getSkin();
00184                 return call_user_func_array( array( $skin, 'doEditSectionLink' ), $args );
00185         }
00186 
00187         function &getLanguageLinks()         { return $this->mLanguageLinks; }
00188         function getInterwikiLinks()         { return $this->mInterwikiLinks; }
00189         function getCategoryLinks()          { return array_keys( $this->mCategories ); }
00190         function &getCategories()            { return $this->mCategories; }
00191         function getTitleText()              { return $this->mTitleText; }
00192         function getSections()               { return $this->mSections; }
00193         function getEditSectionTokens()      { return $this->mEditSectionTokens; }
00194         function &getLinks()                 { return $this->mLinks; }
00195         function &getTemplates()             { return $this->mTemplates; }
00196         function &getTemplateIds()           { return $this->mTemplateIds; }
00197         function &getImages()                { return $this->mImages; }
00198         function &getFileSearchOptions()     { return $this->mFileSearchOptions; }
00199         function &getExternalLinks()         { return $this->mExternalLinks; }
00200         function getNoGallery()              { return $this->mNoGallery; }
00201         function getHeadItems()              { return $this->mHeadItems; }
00202         function getModules()                { return $this->mModules; }
00203         function getModuleScripts()          { return $this->mModuleScripts; }
00204         function getModuleStyles()           { return $this->mModuleStyles; }
00205         function getModuleMessages()         { return $this->mModuleMessages; }
00206         function getOutputHooks()            { return (array)$this->mOutputHooks; }
00207         function getWarnings()               { return array_keys( $this->mWarnings ); }
00208         function getIndexPolicy()            { return $this->mIndexPolicy; }
00209         function getTOCHTML()                { return $this->mTOCHTML; }
00210         function getTimestamp()              { return $this->mTimestamp; }
00211 
00212         function setText( $text )            { return wfSetVar( $this->mText, $text ); }
00213         function setLanguageLinks( $ll )     { return wfSetVar( $this->mLanguageLinks, $ll ); }
00214         function setCategoryLinks( $cl )     { return wfSetVar( $this->mCategories, $cl ); }
00215 
00216         function setTitleText( $t )          { return wfSetVar( $this->mTitleText, $t ); }
00217         function setSections( $toc )         { return wfSetVar( $this->mSections, $toc ); }
00218         function setEditSectionTokens( $t )  { return wfSetVar( $this->mEditSectionTokens, $t ); }
00219         function setIndexPolicy( $policy )   { return wfSetVar( $this->mIndexPolicy, $policy ); }
00220         function setTOCHTML( $tochtml )      { return wfSetVar( $this->mTOCHTML, $tochtml ); }
00221         function setTimestamp( $timestamp )  { return wfSetVar( $this->mTimestamp, $timestamp ); }
00222 
00223         function addCategory( $c, $sort )    { $this->mCategories[$c] = $sort; }
00224         function addLanguageLink( $t )       { $this->mLanguageLinks[] = $t; }
00225         function addWarning( $s )            { $this->mWarnings[$s] = 1; }
00226 
00227         function addOutputHook( $hook, $data = false ) {
00228                 $this->mOutputHooks[] = array( $hook, $data );
00229         }
00230 
00231         function setNewSection( $value ) {
00232                 $this->mNewSection = (bool)$value;
00233         }
00234         function hideNewSection ( $value ) {
00235                 $this->mHideNewSection = (bool)$value;
00236         }
00237         function getHideNewSection () {
00238                 return (bool)$this->mHideNewSection;
00239         }
00240         function getNewSection() {
00241                 return (bool)$this->mNewSection;
00242         }
00243 
00244         function addExternalLink( $url ) {
00245                 # We don't register links pointing to our own server, unless... :-)
00246                 global $wgServer, $wgRegisterInternalExternals;
00247                 if( $wgRegisterInternalExternals or stripos($url,$wgServer.'/')!==0)
00248                         $this->mExternalLinks[$url] = 1;
00249         }
00250 
00257         function addLink( $title, $id = null ) {
00258                 if ( $title->isExternal() ) {
00259                         // Don't record interwikis in pagelinks
00260                         $this->addInterwikiLink( $title );
00261                         return;
00262                 }
00263                 $ns = $title->getNamespace();
00264                 $dbk = $title->getDBkey();
00265                 if ( $ns == NS_MEDIA ) {
00266                         // Normalize this pseudo-alias if it makes it down here...
00267                         $ns = NS_FILE;
00268                 } elseif( $ns == NS_SPECIAL ) {
00269                         // We don't record Special: links currently
00270                         // It might actually be wise to, but we'd need to do some normalization.
00271                         return;
00272                 } elseif( $dbk === '' ) {
00273                         // Don't record self links -  [[#Foo]]
00274                         return;
00275                 }
00276                 if ( !isset( $this->mLinks[$ns] ) ) {
00277                         $this->mLinks[$ns] = array();
00278                 }
00279                 if ( is_null( $id ) ) {
00280                         $id = $title->getArticleID();
00281                 }
00282                 $this->mLinks[$ns][$dbk] = $id;
00283         }
00284 
00292         function addImage( $name, $timestamp = null, $sha1 = null ) {
00293                 $this->mImages[$name] = 1;
00294                 if ( $timestamp !== null && $sha1 !== null ) {
00295                         $this->mFileSearchOptions[$name] = array( 'time' => $timestamp, 'sha1' => $sha1 );
00296                 }
00297         }
00298 
00306         function addTemplate( $title, $page_id, $rev_id ) {
00307                 $ns = $title->getNamespace();
00308                 $dbk = $title->getDBkey();
00309                 if ( !isset( $this->mTemplates[$ns] ) ) {
00310                         $this->mTemplates[$ns] = array();
00311                 }
00312                 $this->mTemplates[$ns][$dbk] = $page_id;
00313                 if ( !isset( $this->mTemplateIds[$ns] ) ) {
00314                         $this->mTemplateIds[$ns] = array();
00315                 }
00316                 $this->mTemplateIds[$ns][$dbk] = $rev_id; // For versioning
00317         }
00318 
00323         function addInterwikiLink( $title ) {
00324                 $prefix = $title->getInterwiki();
00325                 if( $prefix == '' ) {
00326                         throw new MWException( 'Non-interwiki link passed, internal parser error.' );
00327                 }
00328                 if (!isset($this->mInterwikiLinks[$prefix])) {
00329                         $this->mInterwikiLinks[$prefix] = array();
00330                 }
00331                 $this->mInterwikiLinks[$prefix][$title->getDBkey()] = 1;
00332         }
00333 
00339         function addHeadItem( $section, $tag = false ) {
00340                 if ( $tag !== false ) {
00341                         $this->mHeadItems[$tag] = $section;
00342                 } else {
00343                         $this->mHeadItems[] = $section;
00344                 }
00345         }
00346 
00347         public function addModules( $modules ) {
00348                 $this->mModules = array_merge( $this->mModules, (array) $modules );
00349         }
00350 
00351         public function addModuleScripts( $modules ) {
00352                 $this->mModuleScripts = array_merge( $this->mModuleScripts, (array)$modules );
00353         }
00354 
00355         public function addModuleStyles( $modules ) {
00356                 $this->mModuleStyles = array_merge( $this->mModuleStyles, (array)$modules );
00357         }
00358 
00359         public function addModuleMessages( $modules ) {
00360                 $this->mModuleMessages = array_merge( $this->mModuleMessages, (array)$modules );
00361         }
00362 
00368         public function addOutputPageMetadata( OutputPage $out ) {
00369                 $this->addModules( $out->getModules() );
00370                 $this->addModuleScripts( $out->getModuleScripts() );
00371                 $this->addModuleStyles( $out->getModuleStyles() );
00372                 $this->addModuleMessages( $out->getModuleMessages() );
00373 
00374                 $this->mHeadItems = array_merge( $this->mHeadItems, $out->getHeadItemsArray() );
00375                 $this->mPreventClickjacking = $this->mPreventClickjacking || $out->getPreventClickjacking();
00376         }
00377 
00385         public function setDisplayTitle( $text ) {
00386                 $this->setTitleText( $text );
00387                 $this->setProperty( 'displaytitle', $text );
00388         }
00389 
00395         public function getDisplayTitle() {
00396                 $t = $this->getTitleText();
00397                 if( $t === '' ) {
00398                         return false;
00399                 }
00400                 return $t;
00401         }
00402 
00406         public function setFlag( $flag ) {
00407                 $this->mFlags[$flag] = true;
00408         }
00409 
00410         public function getFlag( $flag ) {
00411                 return isset( $this->mFlags[$flag] );
00412         }
00413 
00417         public function setProperty( $name, $value ) {
00418                 $this->mProperties[$name] = $value;
00419         }
00420 
00421         public function getProperty( $name ){
00422                 return isset( $this->mProperties[$name] ) ? $this->mProperties[$name] : false;
00423         }
00424 
00425         public function getProperties() {
00426                 if ( !isset( $this->mProperties ) ) {
00427                         $this->mProperties = array();
00428                 }
00429                 return $this->mProperties;
00430         }
00431 
00432 
00438          public function getUsedOptions() {
00439                 if ( !isset( $this->mAccessedOptions ) ) {
00440                         return array();
00441                 }
00442                 return array_keys( $this->mAccessedOptions );
00443          }
00444 
00449          function recordOption( $option ) {
00450                  $this->mAccessedOptions[$option] = true;
00451          }
00452 
00460         public function preventClickjacking( $flag = null ) {
00461                 return wfSetVar( $this->mPreventClickjacking, $flag );
00462         }
00463 }