follow up r60832 and follow up r60763
[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 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 getSubtitle() { return $this->mSubtitle; }
54 function getOutputHooks() { return (array)$this->mOutputHooks; }
55 function getWarnings() { return array_keys( $this->mWarnings ); }
56 function getIndexPolicy() { return $this->mIndexPolicy; }
57 function getTOCHTML() { return $this->mTOCHTML; }
58
59 function containsOldMagic() { return $this->mContainsOldMagic; }
60 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
61 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
62 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategories, $cl ); }
63 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
64 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
65 function setTitleText( $t ) { return wfSetVar( $this->mTitleText, $t ); }
66 function setSections( $toc ) { return wfSetVar( $this->mSections, $toc ); }
67 function setIndexPolicy( $policy ) { return wfSetVar( $this->mIndexPolicy, $policy ); }
68 function setTOCHTML( $tochtml ) { return wfSetVar( $this->mTOCHTML, $tochtml ); }
69
70 function addCategory( $c, $sort ) { $this->mCategories[$c] = $sort; }
71 function addLanguageLink( $t ) { $this->mLanguageLinks[] = $t; }
72 function addWarning( $s ) { $this->mWarnings[$s] = 1; }
73
74 function addOutputHook( $hook, $data = false ) {
75 $this->mOutputHooks[] = array( $hook, $data );
76 }
77
78 function setNewSection( $value ) {
79 $this->mNewSection = (bool)$value;
80 }
81 function hideNewSection ( $value ) {
82 $this->mHideNewSection = (bool)$value;
83 }
84 function getHideNewSection () {
85 return (bool)$this->mHideNewSection;
86 }
87 function getNewSection() {
88 return (bool)$this->mNewSection;
89 }
90
91 function addExternalLink( $url ) {
92 # We don't register links pointing to our own server, unless... :-)
93 global $wgServer, $wgRegisterInternalExternals;
94 if( $wgRegisterInternalExternals or stripos($url,$wgServer.'/')!==0)
95 $this->mExternalLinks[$url] = 1;
96 }
97
98 function addLink( $title, $id = null ) {
99 if ( $title->isExternal() ) {
100 // Don't record interwikis in pagelinks
101 return;
102 }
103 $ns = $title->getNamespace();
104 $dbk = $title->getDBkey();
105 if ( $ns == NS_MEDIA ) {
106 // Normalize this pseudo-alias if it makes it down here...
107 $ns = NS_FILE;
108 } elseif( $ns == NS_SPECIAL ) {
109 // We don't record Special: links currently
110 // It might actually be wise to, but we'd need to do some normalization.
111 return;
112 } elseif( $dbk === '' ) {
113 // Don't record self links - [[#Foo]]
114 return;
115 }
116 if ( !isset( $this->mLinks[$ns] ) ) {
117 $this->mLinks[$ns] = array();
118 }
119 if ( is_null( $id ) ) {
120 $id = $title->getArticleID();
121 }
122 $this->mLinks[$ns][$dbk] = $id;
123 }
124
125 function addImage( $name ) {
126 $this->mImages[$name] = 1;
127 }
128
129 function addTemplate( $title, $page_id, $rev_id ) {
130 $ns = $title->getNamespace();
131 $dbk = $title->getDBkey();
132 if ( !isset( $this->mTemplates[$ns] ) ) {
133 $this->mTemplates[$ns] = array();
134 }
135 $this->mTemplates[$ns][$dbk] = $page_id;
136 if ( !isset( $this->mTemplateIds[$ns] ) ) {
137 $this->mTemplateIds[$ns] = array();
138 }
139 $this->mTemplateIds[$ns][$dbk] = $rev_id; // For versioning
140 }
141
142 /**
143 * Return true if this cached output object predates the global or
144 * per-article cache invalidation timestamps, or if it comes from
145 * an incompatible older version.
146 *
147 * @param string $touched the affected article's last touched timestamp
148 * @return bool
149 * @public
150 */
151 function expired( $touched ) {
152 global $wgCacheEpoch;
153 return $this->getCacheTime() == -1 || // parser says it's uncacheable
154 $this->getCacheTime() < $touched ||
155 $this->getCacheTime() <= $wgCacheEpoch ||
156 !isset( $this->mVersion ) ||
157 version_compare( $this->mVersion, Parser::VERSION, "lt" );
158 }
159
160 /**
161 * Add some text to the <head>.
162 * If $tag is set, the section with that tag will only be included once
163 * in a given page.
164 */
165 function addHeadItem( $section, $tag = false ) {
166 if ( $tag !== false ) {
167 $this->mHeadItems[$tag] = $section;
168 } else {
169 $this->mHeadItems[] = $section;
170 }
171 }
172
173 /**
174 * Override the title to be used for display
175 * -- this is assumed to have been validated
176 * (check equal normalisation, etc.)
177 *
178 * @param string $text Desired title text
179 */
180 public function setDisplayTitle( $text ) {
181 $this->setTitleText( $text );
182 }
183
184 /**
185 * Get the title to be used for display
186 *
187 * @return string
188 */
189 public function getDisplayTitle() {
190 $t = $this->getTitleText( $text );
191 if( $t === '' ) {
192 return false;
193 }
194 }
195
196 /**
197 * Fairly generic flag setter thingy.
198 */
199 public function setFlag( $flag ) {
200 $this->mFlags[$flag] = true;
201 }
202
203 public function getFlag( $flag ) {
204 return isset( $this->mFlags[$flag] );
205 }
206
207 /**
208 * Set a property to be cached in the DB
209 */
210 public function setProperty( $name, $value ) {
211 $this->mProperties[$name] = $value;
212 }
213
214 public function getProperty( $name ){
215 return isset( $this->mProperties[$name] ) ? $this->mProperties[$name] : false;
216 }
217
218 public function getProperties() {
219 if ( !isset( $this->mProperties ) ) {
220 $this->mProperties = array();
221 }
222 return $this->mProperties;
223 }
224 }