MediaWiki  REL1_21
ParserOutput.php
Go to the documentation of this file.
00001 <?php
00002 
00024 class ParserOutput extends CacheTime {
00025         var $mText,                       # The output text
00026                 $mLanguageLinks,              # List of the full text of language links, in the order they appear
00027                 $mCategories,                 # Map of category names to sort keys
00028                 $mTitleText,                  # title text of the chosen language variant
00029                 $mLinks = array(),            # 2-D map of NS/DBK to ID for the links in the document. ID=zero for broken.
00030                 $mTemplates = array(),        # 2-D map of NS/DBK to ID for the template references. ID=zero for broken.
00031                 $mTemplateIds = array(),      # 2-D map of NS/DBK to rev ID for the template references. ID=zero for broken.
00032                 $mImages = array(),           # DB keys of the images used, in the array key only
00033                 $mFileSearchOptions = array(), # DB keys of the images used mapped to sha1 and MW timestamp
00034                 $mExternalLinks = array(),    # External link URLs, in the key only
00035                 $mInterwikiLinks = array(),   # 2-D map of prefix/DBK (in keys only) for the inline interwiki links in the document.
00036                 $mNewSection = false,         # Show a new section link?
00037                 $mHideNewSection = false,     # Hide the new section link?
00038                 $mNoGallery = false,          # No gallery on category page? (__NOGALLERY__)
00039                 $mHeadItems = array(),        # Items to put in the <head> section
00040                 $mModules = array(),          # Modules to be loaded by the resource loader
00041                 $mModuleScripts = array(),    # Modules of which only the JS will be loaded by the resource loader
00042                 $mModuleStyles = array(),     # Modules of which only the CSSS will be loaded by the resource loader
00043                 $mModuleMessages = array(),   # Modules of which only the messages will be loaded by the resource loader
00044                 $mOutputHooks = array(),      # Hook tags as per $wgParserOutputHooks
00045                 $mWarnings = array(),         # Warning text to be returned to the user. Wikitext formatted, in the key only
00046                 $mSections = array(),         # Table of contents
00047                 $mEditSectionTokens = false,  # prefix/suffix markers if edit sections were output as tokens
00048                 $mProperties = array(),       # Name/value pairs to be cached in the DB
00049                 $mTOCHTML = '',               # HTML of the TOC
00050                 $mTimestamp;                  # Timestamp of the revision
00051                 private $mIndexPolicy = '';       # 'index' or 'noindex'?  Any other value will result in no change.
00052                 private $mAccessedOptions = array(); # List of ParserOptions (stored in the keys)
00053                 private $mSecondaryDataUpdates = array(); # List of DataUpdate, used to save info from the page somewhere else.
00054                 private $mExtensionData = array(); # extra data used by extensions
00055 
00056         const EDITSECTION_REGEX = '#<(?:mw:)?editsection page="(.*?)" section="(.*?)"(?:/>|>(.*?)(</(?:mw:)?editsection>))#';
00057 
00058         function __construct( $text = '', $languageLinks = array(), $categoryLinks = array(),
00059                 $containsOldMagic = false, $titletext = '' )
00060         {
00061                 $this->mText = $text;
00062                 $this->mLanguageLinks = $languageLinks;
00063                 $this->mCategories = $categoryLinks;
00064                 $this->mContainsOldMagic = $containsOldMagic;
00065                 $this->mTitleText = $titletext;
00066         }
00067 
00068         function getText() {
00069                 if ( $this->mEditSectionTokens ) {
00070                         return preg_replace_callback( ParserOutput::EDITSECTION_REGEX,
00071                                 array( &$this, 'replaceEditSectionLinksCallback' ), $this->mText );
00072                 }
00073                 return preg_replace( ParserOutput::EDITSECTION_REGEX, '', $this->mText );
00074         }
00075 
00083         function replaceEditSectionLinksCallback( $m ) {
00084                 global $wgOut, $wgLang;
00085                 $args = array(
00086                         htmlspecialchars_decode( $m[1] ),
00087                         htmlspecialchars_decode( $m[2] ),
00088                         isset( $m[4] ) ? $m[3] : null,
00089                 );
00090                 $args[0] = Title::newFromText( $args[0] );
00091                 if ( !is_object( $args[0] ) ) {
00092                         throw new MWException( "Bad parser output text." );
00093                 }
00094                 $args[] = $wgLang->getCode();
00095                 $skin = $wgOut->getSkin();
00096                 return call_user_func_array( array( $skin, 'doEditSectionLink' ), $args );
00097         }
00098 
00099         function &getLanguageLinks()         { return $this->mLanguageLinks; }
00100         function getInterwikiLinks()         { return $this->mInterwikiLinks; }
00101         function getCategoryLinks()          { return array_keys( $this->mCategories ); }
00102         function &getCategories()            { return $this->mCategories; }
00103         function getTitleText()              { return $this->mTitleText; }
00104         function getSections()               { return $this->mSections; }
00105         function getEditSectionTokens()      { return $this->mEditSectionTokens; }
00106         function &getLinks()                 { return $this->mLinks; }
00107         function &getTemplates()             { return $this->mTemplates; }
00108         function &getTemplateIds()           { return $this->mTemplateIds; }
00109         function &getImages()                { return $this->mImages; }
00110         function &getFileSearchOptions()     { return $this->mFileSearchOptions; }
00111         function &getExternalLinks()         { return $this->mExternalLinks; }
00112         function getNoGallery()              { return $this->mNoGallery; }
00113         function getHeadItems()              { return $this->mHeadItems; }
00114         function getModules()                { return $this->mModules; }
00115         function getModuleScripts()          { return $this->mModuleScripts; }
00116         function getModuleStyles()           { return $this->mModuleStyles; }
00117         function getModuleMessages()         { return $this->mModuleMessages; }
00118         function getOutputHooks()            { return (array)$this->mOutputHooks; }
00119         function getWarnings()               { return array_keys( $this->mWarnings ); }
00120         function getIndexPolicy()            { return $this->mIndexPolicy; }
00121         function getTOCHTML()                { return $this->mTOCHTML; }
00122         function getTimestamp()              { return $this->mTimestamp; }
00123 
00124         function setText( $text )            { return wfSetVar( $this->mText, $text ); }
00125         function setLanguageLinks( $ll )     { return wfSetVar( $this->mLanguageLinks, $ll ); }
00126         function setCategoryLinks( $cl )     { return wfSetVar( $this->mCategories, $cl ); }
00127 
00128         function setTitleText( $t )          { return wfSetVar( $this->mTitleText, $t ); }
00129         function setSections( $toc )         { return wfSetVar( $this->mSections, $toc ); }
00130         function setEditSectionTokens( $t )  { return wfSetVar( $this->mEditSectionTokens, $t ); }
00131         function setIndexPolicy( $policy )   { return wfSetVar( $this->mIndexPolicy, $policy ); }
00132         function setTOCHTML( $tochtml )      { return wfSetVar( $this->mTOCHTML, $tochtml ); }
00133         function setTimestamp( $timestamp )  { return wfSetVar( $this->mTimestamp, $timestamp ); }
00134 
00135         function addCategory( $c, $sort )    { $this->mCategories[$c] = $sort; }
00136         function addLanguageLink( $t )       { $this->mLanguageLinks[] = $t; }
00137         function addWarning( $s )            { $this->mWarnings[$s] = 1; }
00138 
00139         function addOutputHook( $hook, $data = false ) {
00140                 $this->mOutputHooks[] = array( $hook, $data );
00141         }
00142 
00143         function setNewSection( $value ) {
00144                 $this->mNewSection = (bool)$value;
00145         }
00146         function hideNewSection ( $value ) {
00147                 $this->mHideNewSection = (bool)$value;
00148         }
00149         function getHideNewSection () {
00150                 return (bool)$this->mHideNewSection;
00151         }
00152         function getNewSection() {
00153                 return (bool)$this->mNewSection;
00154         }
00155 
00163         static function isLinkInternal( $internal, $url ) {
00164                 return (bool)preg_match( '/^' .
00165                         # If server is proto relative, check also for http/https links
00166                         ( substr( $internal, 0, 2 ) === '//' ? '(?:https?:)?' : '' ) .
00167                         preg_quote( $internal, '/' ) .
00168                         # check for query/path/anchor or end of link in each case
00169                         '(?:[\?\/\#]|$)/i',
00170                         $url
00171                 );
00172         }
00173 
00174         function addExternalLink( $url ) {
00175                 # We don't register links pointing to our own server, unless... :-)
00176                 global $wgServer, $wgRegisterInternalExternals;
00177 
00178                 $registerExternalLink = true;
00179                 if( !$wgRegisterInternalExternals ) {
00180                         $registerExternalLink = !self::isLinkInternal( $wgServer, $url );
00181                 }
00182                 if( $registerExternalLink ) {
00183                         $this->mExternalLinks[$url] = 1;
00184                 }
00185         }
00186 
00193         function addLink( Title $title, $id = null ) {
00194                 if ( $title->isExternal() ) {
00195                         // Don't record interwikis in pagelinks
00196                         $this->addInterwikiLink( $title );
00197                         return;
00198                 }
00199                 $ns = $title->getNamespace();
00200                 $dbk = $title->getDBkey();
00201                 if ( $ns == NS_MEDIA ) {
00202                         // Normalize this pseudo-alias if it makes it down here...
00203                         $ns = NS_FILE;
00204                 } elseif( $ns == NS_SPECIAL ) {
00205                         // We don't record Special: links currently
00206                         // It might actually be wise to, but we'd need to do some normalization.
00207                         return;
00208                 } elseif( $dbk === '' ) {
00209                         // Don't record self links -  [[#Foo]]
00210                         return;
00211                 }
00212                 if ( !isset( $this->mLinks[$ns] ) ) {
00213                         $this->mLinks[$ns] = array();
00214                 }
00215                 if ( is_null( $id ) ) {
00216                         $id = $title->getArticleID();
00217                 }
00218                 $this->mLinks[$ns][$dbk] = $id;
00219         }
00220 
00228         function addImage( $name, $timestamp = null, $sha1 = null ) {
00229                 $this->mImages[$name] = 1;
00230                 if ( $timestamp !== null && $sha1 !== null ) {
00231                         $this->mFileSearchOptions[$name] = array( 'time' => $timestamp, 'sha1' => $sha1 );
00232                 }
00233         }
00234 
00242         function addTemplate( $title, $page_id, $rev_id ) {
00243                 $ns = $title->getNamespace();
00244                 $dbk = $title->getDBkey();
00245                 if ( !isset( $this->mTemplates[$ns] ) ) {
00246                         $this->mTemplates[$ns] = array();
00247                 }
00248                 $this->mTemplates[$ns][$dbk] = $page_id;
00249                 if ( !isset( $this->mTemplateIds[$ns] ) ) {
00250                         $this->mTemplateIds[$ns] = array();
00251                 }
00252                 $this->mTemplateIds[$ns][$dbk] = $rev_id; // For versioning
00253         }
00254 
00259         function addInterwikiLink( $title ) {
00260                 $prefix = $title->getInterwiki();
00261                 if( $prefix == '' ) {
00262                         throw new MWException( 'Non-interwiki link passed, internal parser error.' );
00263                 }
00264                 if ( !isset( $this->mInterwikiLinks[$prefix] ) ) {
00265                         $this->mInterwikiLinks[$prefix] = array();
00266                 }
00267                 $this->mInterwikiLinks[$prefix][$title->getDBkey()] = 1;
00268         }
00269 
00275         function addHeadItem( $section, $tag = false ) {
00276                 if ( $tag !== false ) {
00277                         $this->mHeadItems[$tag] = $section;
00278                 } else {
00279                         $this->mHeadItems[] = $section;
00280                 }
00281         }
00282 
00283         public function addModules( $modules ) {
00284                 $this->mModules = array_merge( $this->mModules, (array) $modules );
00285         }
00286 
00287         public function addModuleScripts( $modules ) {
00288                 $this->mModuleScripts = array_merge( $this->mModuleScripts, (array)$modules );
00289         }
00290 
00291         public function addModuleStyles( $modules ) {
00292                 $this->mModuleStyles = array_merge( $this->mModuleStyles, (array)$modules );
00293         }
00294 
00295         public function addModuleMessages( $modules ) {
00296                 $this->mModuleMessages = array_merge( $this->mModuleMessages, (array)$modules );
00297         }
00298 
00304         public function addOutputPageMetadata( OutputPage $out ) {
00305                 $this->addModules( $out->getModules() );
00306                 $this->addModuleScripts( $out->getModuleScripts() );
00307                 $this->addModuleStyles( $out->getModuleStyles() );
00308                 $this->addModuleMessages( $out->getModuleMessages() );
00309 
00310                 $this->mHeadItems = array_merge( $this->mHeadItems, $out->getHeadItemsArray() );
00311         }
00312 
00320         public function setDisplayTitle( $text ) {
00321                 $this->setTitleText( $text );
00322                 $this->setProperty( 'displaytitle', $text );
00323         }
00324 
00330         public function getDisplayTitle() {
00331                 $t = $this->getTitleText();
00332                 if( $t === '' ) {
00333                         return false;
00334                 }
00335                 return $t;
00336         }
00337 
00341         public function setFlag( $flag ) {
00342                 $this->mFlags[$flag] = true;
00343         }
00344 
00345         public function getFlag( $flag ) {
00346                 return isset( $this->mFlags[$flag] );
00347         }
00348 
00406         public function setProperty( $name, $value ) {
00407                 $this->mProperties[$name] = $value;
00408         }
00409 
00410         public function getProperty( $name ) {
00411                 return isset( $this->mProperties[$name] ) ? $this->mProperties[$name] : false;
00412         }
00413 
00414         public function getProperties() {
00415                 if ( !isset( $this->mProperties ) ) {
00416                         $this->mProperties = array();
00417                 }
00418                 return $this->mProperties;
00419         }
00420 
00426         public function getUsedOptions() {
00427                 if ( !isset( $this->mAccessedOptions ) ) {
00428                         return array();
00429                 }
00430                 return array_keys( $this->mAccessedOptions );
00431         }
00432 
00437         function recordOption( $option ) {
00438                 $this->mAccessedOptions[$option] = true;
00439         }
00440 
00449         public function addSecondaryDataUpdate( DataUpdate $update ) {
00450                 $this->mSecondaryDataUpdates[] = $update;
00451         }
00452 
00469         public function getSecondaryDataUpdates( Title $title = null, $recursive = true ) {
00470                 if ( is_null( $title ) ) {
00471                         $title = Title::newFromText( $this->getTitleText() );
00472                 }
00473 
00474                 $linksUpdate = new LinksUpdate( $title, $this, $recursive );
00475 
00476                 return array_merge( $this->mSecondaryDataUpdates, array( $linksUpdate ) );
00477         }
00478 
00520         public function setExtensionData( $key, $value ) {
00521                 if ( $value === null ) {
00522                         unset( $this->mExtensionData[$key] );
00523                 } else {
00524                         $this->mExtensionData[$key] = $value;
00525                 }
00526         }
00527 
00539         public function getExtensionData( $key ) {
00540                 if ( isset( $this->mExtensionData[$key] ) ) {
00541                         return $this->mExtensionData[$key];
00542                 }
00543 
00544                 return null;
00545         }
00546 
00547 }