MediaWiki  REL1_20
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 instances of SecondaryDataObject(), used to cause some information extracted from the page in a custom place.
00054 
00055         const EDITSECTION_REGEX = '#<(?:mw:)?editsection page="(.*?)" section="(.*?)"(?:/>|>(.*?)(</(?:mw:)?editsection>))#';
00056 
00057         function __construct( $text = '', $languageLinks = array(), $categoryLinks = array(),
00058                 $containsOldMagic = false, $titletext = '' )
00059         {
00060                 $this->mText = $text;
00061                 $this->mLanguageLinks = $languageLinks;
00062                 $this->mCategories = $categoryLinks;
00063                 $this->mContainsOldMagic = $containsOldMagic;
00064                 $this->mTitleText = $titletext;
00065         }
00066 
00067         function getText() {
00068                 if ( $this->mEditSectionTokens ) {
00069                         return preg_replace_callback( ParserOutput::EDITSECTION_REGEX,
00070                                 array( &$this, 'replaceEditSectionLinksCallback' ), $this->mText );
00071                 }
00072                 return preg_replace( ParserOutput::EDITSECTION_REGEX, '', $this->mText );
00073         }
00074 
00080         function replaceEditSectionLinksCallback( $m ) {
00081                 global $wgOut, $wgLang;
00082                 $args = array(
00083                         htmlspecialchars_decode($m[1]),
00084                         htmlspecialchars_decode($m[2]),
00085                         isset($m[4]) ? $m[3] : null,
00086                 );
00087                 $args[0] = Title::newFromText( $args[0] );
00088                 if ( !is_object($args[0]) ) {
00089                         throw new MWException("Bad parser output text.");
00090                 }
00091                 $args[] = $wgLang->getCode();
00092                 $skin = $wgOut->getSkin();
00093                 return call_user_func_array( array( $skin, 'doEditSectionLink' ), $args );
00094         }
00095 
00096         function &getLanguageLinks()         { return $this->mLanguageLinks; }
00097         function getInterwikiLinks()         { return $this->mInterwikiLinks; }
00098         function getCategoryLinks()          { return array_keys( $this->mCategories ); }
00099         function &getCategories()            { return $this->mCategories; }
00100         function getTitleText()              { return $this->mTitleText; }
00101         function getSections()               { return $this->mSections; }
00102         function getEditSectionTokens()      { return $this->mEditSectionTokens; }
00103         function &getLinks()                 { return $this->mLinks; }
00104         function &getTemplates()             { return $this->mTemplates; }
00105         function &getTemplateIds()           { return $this->mTemplateIds; }
00106         function &getImages()                { return $this->mImages; }
00107         function &getFileSearchOptions()     { return $this->mFileSearchOptions; }
00108         function &getExternalLinks()         { return $this->mExternalLinks; }
00109         function getNoGallery()              { return $this->mNoGallery; }
00110         function getHeadItems()              { return $this->mHeadItems; }
00111         function getModules()                { return $this->mModules; }
00112         function getModuleScripts()          { return $this->mModuleScripts; }
00113         function getModuleStyles()           { return $this->mModuleStyles; }
00114         function getModuleMessages()         { return $this->mModuleMessages; }
00115         function getOutputHooks()            { return (array)$this->mOutputHooks; }
00116         function getWarnings()               { return array_keys( $this->mWarnings ); }
00117         function getIndexPolicy()            { return $this->mIndexPolicy; }
00118         function getTOCHTML()                { return $this->mTOCHTML; }
00119         function getTimestamp()              { return $this->mTimestamp; }
00120 
00121         function setText( $text )            { return wfSetVar( $this->mText, $text ); }
00122         function setLanguageLinks( $ll )     { return wfSetVar( $this->mLanguageLinks, $ll ); }
00123         function setCategoryLinks( $cl )     { return wfSetVar( $this->mCategories, $cl ); }
00124 
00125         function setTitleText( $t )          { return wfSetVar( $this->mTitleText, $t ); }
00126         function setSections( $toc )         { return wfSetVar( $this->mSections, $toc ); }
00127         function setEditSectionTokens( $t )  { return wfSetVar( $this->mEditSectionTokens, $t ); }
00128         function setIndexPolicy( $policy )   { return wfSetVar( $this->mIndexPolicy, $policy ); }
00129         function setTOCHTML( $tochtml )      { return wfSetVar( $this->mTOCHTML, $tochtml ); }
00130         function setTimestamp( $timestamp )  { return wfSetVar( $this->mTimestamp, $timestamp ); }
00131 
00132         function addCategory( $c, $sort )    { $this->mCategories[$c] = $sort; }
00133         function addLanguageLink( $t )       { $this->mLanguageLinks[] = $t; }
00134         function addWarning( $s )            { $this->mWarnings[$s] = 1; }
00135 
00136         function addOutputHook( $hook, $data = false ) {
00137                 $this->mOutputHooks[] = array( $hook, $data );
00138         }
00139 
00140         function setNewSection( $value ) {
00141                 $this->mNewSection = (bool)$value;
00142         }
00143         function hideNewSection ( $value ) {
00144                 $this->mHideNewSection = (bool)$value;
00145         }
00146         function getHideNewSection () {
00147                 return (bool)$this->mHideNewSection;
00148         }
00149         function getNewSection() {
00150                 return (bool)$this->mNewSection;
00151         }
00152 
00153         function addExternalLink( $url ) {
00154                 # We don't register links pointing to our own server, unless... :-)
00155                 global $wgServer, $wgRegisterInternalExternals;
00156                 if( $wgRegisterInternalExternals or stripos($url,$wgServer.'/')!==0)
00157                         $this->mExternalLinks[$url] = 1;
00158         }
00159 
00166         function addLink( $title, $id = null ) {
00167                 if ( $title->isExternal() ) {
00168                         // Don't record interwikis in pagelinks
00169                         $this->addInterwikiLink( $title );
00170                         return;
00171                 }
00172                 $ns = $title->getNamespace();
00173                 $dbk = $title->getDBkey();
00174                 if ( $ns == NS_MEDIA ) {
00175                         // Normalize this pseudo-alias if it makes it down here...
00176                         $ns = NS_FILE;
00177                 } elseif( $ns == NS_SPECIAL ) {
00178                         // We don't record Special: links currently
00179                         // It might actually be wise to, but we'd need to do some normalization.
00180                         return;
00181                 } elseif( $dbk === '' ) {
00182                         // Don't record self links -  [[#Foo]]
00183                         return;
00184                 }
00185                 if ( !isset( $this->mLinks[$ns] ) ) {
00186                         $this->mLinks[$ns] = array();
00187                 }
00188                 if ( is_null( $id ) ) {
00189                         $id = $title->getArticleID();
00190                 }
00191                 $this->mLinks[$ns][$dbk] = $id;
00192         }
00193 
00201         function addImage( $name, $timestamp = null, $sha1 = null ) {
00202                 $this->mImages[$name] = 1;
00203                 if ( $timestamp !== null && $sha1 !== null ) {
00204                         $this->mFileSearchOptions[$name] = array( 'time' => $timestamp, 'sha1' => $sha1 );
00205                 }
00206         }
00207 
00215         function addTemplate( $title, $page_id, $rev_id ) {
00216                 $ns = $title->getNamespace();
00217                 $dbk = $title->getDBkey();
00218                 if ( !isset( $this->mTemplates[$ns] ) ) {
00219                         $this->mTemplates[$ns] = array();
00220                 }
00221                 $this->mTemplates[$ns][$dbk] = $page_id;
00222                 if ( !isset( $this->mTemplateIds[$ns] ) ) {
00223                         $this->mTemplateIds[$ns] = array();
00224                 }
00225                 $this->mTemplateIds[$ns][$dbk] = $rev_id; // For versioning
00226         }
00227 
00232         function addInterwikiLink( $title ) {
00233                 $prefix = $title->getInterwiki();
00234                 if( $prefix == '' ) {
00235                         throw new MWException( 'Non-interwiki link passed, internal parser error.' );
00236                 }
00237                 if (!isset($this->mInterwikiLinks[$prefix])) {
00238                         $this->mInterwikiLinks[$prefix] = array();
00239                 }
00240                 $this->mInterwikiLinks[$prefix][$title->getDBkey()] = 1;
00241         }
00242 
00248         function addHeadItem( $section, $tag = false ) {
00249                 if ( $tag !== false ) {
00250                         $this->mHeadItems[$tag] = $section;
00251                 } else {
00252                         $this->mHeadItems[] = $section;
00253                 }
00254         }
00255 
00256         public function addModules( $modules ) {
00257                 $this->mModules = array_merge( $this->mModules, (array) $modules );
00258         }
00259 
00260         public function addModuleScripts( $modules ) {
00261                 $this->mModuleScripts = array_merge( $this->mModuleScripts, (array)$modules );
00262         }
00263 
00264         public function addModuleStyles( $modules ) {
00265                 $this->mModuleStyles = array_merge( $this->mModuleStyles, (array)$modules );
00266         }
00267 
00268         public function addModuleMessages( $modules ) {
00269                 $this->mModuleMessages = array_merge( $this->mModuleMessages, (array)$modules );
00270         }
00271 
00277         public function addOutputPageMetadata( OutputPage $out ) {
00278                 $this->addModules( $out->getModules() );
00279                 $this->addModuleScripts( $out->getModuleScripts() );
00280                 $this->addModuleStyles( $out->getModuleStyles() );
00281                 $this->addModuleMessages( $out->getModuleMessages() );
00282 
00283                 $this->mHeadItems = array_merge( $this->mHeadItems, $out->getHeadItemsArray() );
00284         }
00285 
00293         public function setDisplayTitle( $text ) {
00294                 $this->setTitleText( $text );
00295                 $this->setProperty( 'displaytitle', $text );
00296         }
00297 
00303         public function getDisplayTitle() {
00304                 $t = $this->getTitleText();
00305                 if( $t === '' ) {
00306                         return false;
00307                 }
00308                 return $t;
00309         }
00310 
00314         public function setFlag( $flag ) {
00315                 $this->mFlags[$flag] = true;
00316         }
00317 
00318         public function getFlag( $flag ) {
00319                 return isset( $this->mFlags[$flag] );
00320         }
00321 
00325         public function setProperty( $name, $value ) {
00326                 $this->mProperties[$name] = $value;
00327         }
00328 
00329         public function getProperty( $name ){
00330                 return isset( $this->mProperties[$name] ) ? $this->mProperties[$name] : false;
00331         }
00332 
00333         public function getProperties() {
00334                 if ( !isset( $this->mProperties ) ) {
00335                         $this->mProperties = array();
00336                 }
00337                 return $this->mProperties;
00338         }
00339 
00340 
00346          public function getUsedOptions() {
00347                 if ( !isset( $this->mAccessedOptions ) ) {
00348                         return array();
00349                 }
00350                 return array_keys( $this->mAccessedOptions );
00351          }
00352 
00357          function recordOption( $option ) {
00358                  $this->mAccessedOptions[$option] = true;
00359          }
00360 
00369         public function addSecondaryDataUpdate( DataUpdate $update ) {
00370                 $this->mSecondaryDataUpdates[] = $update;
00371         }
00372 
00385         public function getSecondaryDataUpdates( Title $title = null, $recursive = true ) {
00386                 if ( is_null( $title ) ) {
00387                         $title = Title::newFromText( $this->getTitleText() );
00388                 }
00389 
00390                 $linksUpdate = new LinksUpdate( $title, $this, $recursive );
00391 
00392                 if ( $this->mSecondaryDataUpdates === array() ) {
00393                         return array( $linksUpdate );
00394                 } else {
00395                         $updates = array_merge( $this->mSecondaryDataUpdates, array( $linksUpdate ) );
00396                 }
00397 
00398                 return $updates;
00399          }
00400 
00401 }