e0b0f776044534733e73306d978e57faa192abf2
[lhc/web/wiklou.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 /**
32 * Overridden title for display
33 */
34 private $displayTitle = false;
35
36 function ParserOutput( $text = '', $languageLinks = array(), $categoryLinks = array(),
37 $containsOldMagic = false, $titletext = '' )
38 {
39 $this->mText = $text;
40 $this->mLanguageLinks = $languageLinks;
41 $this->mCategories = $categoryLinks;
42 $this->mContainsOldMagic = $containsOldMagic;
43 $this->mTitleText = $titletext;
44 }
45
46 function getText() { return $this->mText; }
47 function &getLanguageLinks() { return $this->mLanguageLinks; }
48 function getCategoryLinks() { return array_keys( $this->mCategories ); }
49 function &getCategories() { return $this->mCategories; }
50 function getCacheTime() { return $this->mCacheTime; }
51 function getTitleText() { return $this->mTitleText; }
52 function getSections() { return $this->mSections; }
53 function &getLinks() { return $this->mLinks; }
54 function &getTemplates() { return $this->mTemplates; }
55 function &getImages() { return $this->mImages; }
56 function &getExternalLinks() { return $this->mExternalLinks; }
57 function getNoGallery() { return $this->mNoGallery; }
58 function getSubtitle() { return $this->mSubtitle; }
59 function getOutputHooks() { return (array)$this->mOutputHooks; }
60 function getWarnings() { return array_keys( $this->mWarnings ); }
61 function getIndexPolicy() { return $this->mIndexPolicy; }
62 function getTOCHTML() { return $this->mTOCHTML; }
63
64 function containsOldMagic() { return $this->mContainsOldMagic; }
65 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
66 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
67 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategories, $cl ); }
68 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
69 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
70 function setTitleText( $t ) { return wfSetVar( $this->mTitleText, $t ); }
71 function setSections( $toc ) { return wfSetVar( $this->mSections, $toc ); }
72 function setIndexPolicy( $policy ) { return wfSetVar( $this->mIndexPolicy, $policy ); }
73 function setTOCHTML( $tochtml ) { return wfSetVar( $this->mTOCHTML, $tochtml ); }
74
75 function addCategory( $c, $sort ) { $this->mCategories[$c] = $sort; }
76 function addLanguageLink( $t ) { $this->mLanguageLinks[] = $t; }
77 function addWarning( $s ) { $this->mWarnings[$s] = 1; }
78
79 function addOutputHook( $hook, $data = false ) {
80 $this->mOutputHooks[] = array( $hook, $data );
81 }
82
83 function setNewSection( $value ) {
84 $this->mNewSection = (bool)$value;
85 }
86 function hideNewSection ( $value ) {
87 $this->mHideNewSection = (bool)$value;
88 }
89 function getHideNewSection () {
90 return (bool)$this->mHideNewSection;
91 }
92 function getNewSection() {
93 return (bool)$this->mNewSection;
94 }
95
96 function addExternalLink( $url ) {
97 # We don't register links pointing to our own server, unless... :-)
98 global $wgServer, $wgRegisterInternalExternals;
99 if( $wgRegisterInternalExternals or stripos($url,$wgServer)!==0)
100 $this->mExternalLinks[$url] = 1;
101 }
102
103 function addLink( $title, $id = null ) {
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->displayTitle = $text;
183 }
184
185 /**
186 * Get the title to be used for display
187 *
188 * @return string
189 */
190 public function getDisplayTitle() {
191 return $this->displayTitle;
192 }
193
194 /**
195 * Fairly generic flag setter thingy.
196 */
197 public function setFlag( $flag ) {
198 $this->mFlags[$flag] = true;
199 }
200
201 public function getFlag( $flag ) {
202 return isset( $this->mFlags[$flag] );
203 }
204
205 /**
206 * Set a property to be cached in the DB
207 */
208 public function setProperty( $name, $value ) {
209 $this->mProperties[$name] = $value;
210 }
211
212 public function getProperty( $name ){
213 return isset( $this->mProperties[$name] ) ? $this->mProperties[$name] : false;
214 }
215
216 public function getProperties() {
217 if ( !isset( $this->mProperties ) ) {
218 $this->mProperties = array();
219 }
220 return $this->mProperties;
221 }
222 }