]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/parser/ParserOutput.php
MediaWiki 1.16.0
[autoinstallsdev/mediawiki.git] / includes / parser / ParserOutput.php
1 <?php
2 /**
3  * @todo document
4  * @ingroup Parser
5  */
6 class ParserOutput
7 {
8         var $mText,                       # The output text
9                 $mLanguageLinks,              # List of the full text of language links, in the order they appear
10                 $mCategories,                 # Map of category names to sort keys
11                 $mContainsOldMagic,           # Boolean variable indicating if the input contained variables like {{CURRENTDAY}}
12                 $mTitleText,                  # title text of the chosen language variant
13                 $mCacheTime = '',             # Time when this object was generated, or -1 for uncacheable. Used in ParserCache.
14                 $mVersion = Parser::VERSION,  # Compatibility check
15                 $mLinks = array(),            # 2-D map of NS/DBK to ID for the links in the document. ID=zero for broken.
16                 $mTemplates = array(),        # 2-D map of NS/DBK to ID for the template references. ID=zero for broken.
17                 $mTemplateIds = array(),      # 2-D map of NS/DBK to rev ID for the template references. ID=zero for broken.
18                 $mImages = array(),           # DB keys of the images used, in the array key only
19                 $mExternalLinks = array(),    # External link URLs, in the key only
20                 $mNewSection = false,         # Show a new section link?
21                 $mHideNewSection = false,     # Hide the new section link?
22                 $mNoGallery = false,          # No gallery on category page? (__NOGALLERY__)
23                 $mHeadItems = array(),        # Items to put in the <head> section
24                 $mOutputHooks = array(),      # Hook tags as per $wgParserOutputHooks
25                 $mWarnings = array(),         # Warning text to be returned to the user. Wikitext formatted, in the key only
26                 $mSections = array(),         # Table of contents
27                 $mProperties = array(),       # Name/value pairs to be cached in the DB
28                 $mTOCHTML = '';               # HTML of the TOC
29         private $mIndexPolicy = '';           # 'index' or 'noindex'?  Any other value will result in no change.
30
31         function ParserOutput( $text = '', $languageLinks = array(), $categoryLinks = array(),
32                 $containsOldMagic = false, $titletext = '' )
33         {
34                 $this->mText = $text;
35                 $this->mLanguageLinks = $languageLinks;
36                 $this->mCategories = $categoryLinks;
37                 $this->mContainsOldMagic = $containsOldMagic;
38                 $this->mTitleText = $titletext;
39         }
40
41         function getText()                   { return $this->mText; }
42         function &getLanguageLinks()         { return $this->mLanguageLinks; }
43         function getCategoryLinks()          { return array_keys( $this->mCategories ); }
44         function &getCategories()            { return $this->mCategories; }
45         function getCacheTime()              { return $this->mCacheTime; }
46         function getTitleText()              { return $this->mTitleText; }
47         function getSections()               { return $this->mSections; }
48         function &getLinks()                 { return $this->mLinks; }
49         function &getTemplates()             { return $this->mTemplates; }
50         function &getImages()                { return $this->mImages; }
51         function &getExternalLinks()         { return $this->mExternalLinks; }
52         function getNoGallery()              { return $this->mNoGallery; }
53         function getHeadItems()              { return $this->mHeadItems; }
54         function getSubtitle()               { return $this->mSubtitle; }
55         function getOutputHooks()            { return (array)$this->mOutputHooks; }
56         function getWarnings()               { return array_keys( $this->mWarnings ); }
57         function getIndexPolicy()            { return $this->mIndexPolicy; }
58         function getTOCHTML()                { return $this->mTOCHTML; }
59
60         function containsOldMagic()          { return $this->mContainsOldMagic; }
61         function setText( $text )            { return wfSetVar( $this->mText, $text ); }
62         function setLanguageLinks( $ll )     { return wfSetVar( $this->mLanguageLinks, $ll ); }
63         function setCategoryLinks( $cl )     { return wfSetVar( $this->mCategories, $cl ); }
64         function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
65         function setCacheTime( $t )          { return wfSetVar( $this->mCacheTime, $t ); }
66         function setTitleText( $t )          { return wfSetVar( $this->mTitleText, $t ); }
67         function setSections( $toc )         { return wfSetVar( $this->mSections, $toc ); }
68         function setIndexPolicy( $policy )   { return wfSetVar( $this->mIndexPolicy, $policy ); }
69         function setTOCHTML( $tochtml )      { return wfSetVar( $this->mTOCHTML, $tochtml ); }
70
71         function addCategory( $c, $sort )    { $this->mCategories[$c] = $sort; }
72         function addLanguageLink( $t )       { $this->mLanguageLinks[] = $t; }
73         function addWarning( $s )            { $this->mWarnings[$s] = 1; }
74
75         function addOutputHook( $hook, $data = false ) {
76                 $this->mOutputHooks[] = array( $hook, $data );
77         }
78
79         function setNewSection( $value ) {
80                 $this->mNewSection = (bool)$value;
81         }
82         function hideNewSection ( $value ) {
83                 $this->mHideNewSection = (bool)$value;
84         }
85         function getHideNewSection () {
86                 return (bool)$this->mHideNewSection;
87         }
88         function getNewSection() {
89                 return (bool)$this->mNewSection;
90         }
91
92         function addExternalLink( $url ) {
93                 # We don't register links pointing to our own server, unless... :-)
94                 global $wgServer, $wgRegisterInternalExternals;
95                 if( $wgRegisterInternalExternals or stripos($url,$wgServer.'/')!==0)
96                         $this->mExternalLinks[$url] = 1; 
97         }
98
99         function addLink( $title, $id = null ) {
100                 if ( $title->isExternal() ) {
101                         // Don't record interwikis in pagelinks
102                         return;
103                 }
104                 $ns = $title->getNamespace();
105                 $dbk = $title->getDBkey();
106                 if ( $ns == NS_MEDIA ) {
107                         // Normalize this pseudo-alias if it makes it down here...
108                         $ns = NS_FILE;
109                 } elseif( $ns == NS_SPECIAL ) {
110                         // We don't record Special: links currently
111                         // It might actually be wise to, but we'd need to do some normalization.
112                         return;
113                 } elseif( $dbk === '' ) {
114                         // Don't record self links -  [[#Foo]]
115                         return;
116                 }
117                 if ( !isset( $this->mLinks[$ns] ) ) {
118                         $this->mLinks[$ns] = array();
119                 }
120                 if ( is_null( $id ) ) {
121                         $id = $title->getArticleID();
122                 }
123                 $this->mLinks[$ns][$dbk] = $id;
124         }
125
126         function addImage( $name ) {
127                 $this->mImages[$name] = 1;
128         }
129
130         function addTemplate( $title, $page_id, $rev_id ) {
131                 $ns = $title->getNamespace();
132                 $dbk = $title->getDBkey();
133                 if ( !isset( $this->mTemplates[$ns] ) ) {
134                         $this->mTemplates[$ns] = array();
135                 }
136                 $this->mTemplates[$ns][$dbk] = $page_id;
137                 if ( !isset( $this->mTemplateIds[$ns] ) ) {
138                         $this->mTemplateIds[$ns] = array();
139                 }
140                 $this->mTemplateIds[$ns][$dbk] = $rev_id; // For versioning
141         }
142
143         /**
144          * Return true if this cached output object predates the global or
145          * per-article cache invalidation timestamps, or if it comes from
146          * an incompatible older version.
147          *
148          * @param string $touched the affected article's last touched timestamp
149          * @return bool
150          * @public
151          */
152         function expired( $touched ) {
153                 global $wgCacheEpoch;
154                 return $this->getCacheTime() == -1 || // parser says it's uncacheable
155                        $this->getCacheTime() < $touched ||
156                        $this->getCacheTime() <= $wgCacheEpoch ||
157                        !isset( $this->mVersion ) ||
158                        version_compare( $this->mVersion, Parser::VERSION, "lt" );
159         }
160
161         /**
162          * Add some text to the <head>.
163          * If $tag is set, the section with that tag will only be included once
164          * in a given page.
165          */
166         function addHeadItem( $section, $tag = false ) {
167                 if ( $tag !== false ) {
168                         $this->mHeadItems[$tag] = $section;
169                 } else {
170                         $this->mHeadItems[] = $section;
171                 }
172         }
173
174         /**
175          * Override the title to be used for display
176          * -- this is assumed to have been validated
177          * (check equal normalisation, etc.)
178          *
179          * @param string $text Desired title text
180          */
181         public function setDisplayTitle( $text ) {
182                 $this->setTitleText( $text );
183         }
184
185         /**
186          * Get the title to be used for display
187          *
188          * @return string
189          */
190         public function getDisplayTitle() {
191                 $t = $this->getTitleText( );
192                 if( $t === '' ) {
193                         return false;
194                 }
195                 return $t;
196         }
197
198         /**
199          * Fairly generic flag setter thingy.
200          */
201         public function setFlag( $flag ) {
202                 $this->mFlags[$flag] = true;
203         }
204
205         public function getFlag( $flag ) {
206                 return isset( $this->mFlags[$flag] );
207         }
208
209         /**
210          * Set a property to be cached in the DB
211          */
212         public function setProperty( $name, $value ) {
213                 $this->mProperties[$name] = $value;
214         }
215
216         public function getProperty( $name ){
217                 return isset( $this->mProperties[$name] ) ? $this->mProperties[$name] : false;
218         }
219
220         public function getProperties() {
221                 if ( !isset( $this->mProperties ) ) {
222                         $this->mProperties = array();
223                 }
224                 return $this->mProperties;
225         }
226 }