Follow-up to r45174: (bug 16806) Fix regression from r44524 that caused links to...
[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 $mNoGallery = false, # No gallery on category page? (__NOGALLERY__)
22 $mHeadItems = array(), # Items to put in the <head> section
23 $mOutputHooks = array(), # Hook tags as per $wgParserOutputHooks
24 $mWarnings = array(), # Warning text to be returned to the user. Wikitext formatted, in the key only
25 $mSections = array(), # Table of contents
26 $mProperties = array(); # Name/value pairs to be cached in the DB
27 private $mIndexPolicy = ''; # 'index' or 'noindex'? Any other value will result in no change.
28
29 /**
30 * Overridden title for display
31 */
32 private $displayTitle = false;
33
34 function ParserOutput( $text = '', $languageLinks = array(), $categoryLinks = array(),
35 $containsOldMagic = false, $titletext = '' )
36 {
37 $this->mText = $text;
38 $this->mLanguageLinks = $languageLinks;
39 $this->mCategories = $categoryLinks;
40 $this->mContainsOldMagic = $containsOldMagic;
41 $this->mTitleText = $titletext;
42 }
43
44 function getText() { return $this->mText; }
45 function &getLanguageLinks() { return $this->mLanguageLinks; }
46 function getCategoryLinks() { return array_keys( $this->mCategories ); }
47 function &getCategories() { return $this->mCategories; }
48 function getCacheTime() { return $this->mCacheTime; }
49 function getTitleText() { return $this->mTitleText; }
50 function getSections() { return $this->mSections; }
51 function &getLinks() { return $this->mLinks; }
52 function &getTemplates() { return $this->mTemplates; }
53 function &getImages() { return $this->mImages; }
54 function &getExternalLinks() { return $this->mExternalLinks; }
55 function getNoGallery() { return $this->mNoGallery; }
56 function getSubtitle() { return $this->mSubtitle; }
57 function getOutputHooks() { return (array)$this->mOutputHooks; }
58 function getWarnings() { return array_keys( $this->mWarnings ); }
59 function getIndexPolicy() { return $this->mIndexPolicy; }
60
61 function containsOldMagic() { return $this->mContainsOldMagic; }
62 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
63 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
64 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategories, $cl ); }
65 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
66 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
67 function setTitleText( $t ) { return wfSetVar( $this->mTitleText, $t ); }
68 function setSections( $toc ) { return wfSetVar( $this->mSections, $toc ); }
69 function setIndexPolicy( $policy ) { return wfSetVar( $this->mIndexPolicy, $policy ); }
70
71 function addCategory( $c, $sort ) { $this->mCategories[$c] = $sort; }
72 function addLanguageLink( $t ) { $this->mLanguageLinks[] = $t; }
73 function addExternalLink( $url ) { $this->mExternalLinks[$url] = 1; }
74 function addWarning( $s ) { $this->mWarnings[$s] = 1; }
75
76 function addOutputHook( $hook, $data = false ) {
77 $this->mOutputHooks[] = array( $hook, $data );
78 }
79
80 function setNewSection( $value ) {
81 $this->mNewSection = (bool)$value;
82 }
83 function getNewSection() {
84 return (bool)$this->mNewSection;
85 }
86
87 function addLink( $title, $id = null ) {
88 $ns = $title->getNamespace();
89 $dbk = $title->getDBkey();
90 if ( $ns == NS_MEDIA ) {
91 // Normalize this pseudo-alias if it makes it down here...
92 $ns = NS_FILE;
93 } elseif( $ns == NS_SPECIAL ) {
94 // We don't record Special: links currently
95 // It might actually be wise to, but we'd need to do some normalization.
96 return;
97 }
98 if ( !isset( $this->mLinks[$ns] ) ) {
99 $this->mLinks[$ns] = array();
100 }
101 if ( is_null( $id ) ) {
102 $id = $title->getArticleID();
103 }
104 $this->mLinks[$ns][$dbk] = $id;
105 }
106
107 function addImage( $name ) {
108 $this->mImages[$name] = 1;
109 }
110
111 function addTemplate( $title, $page_id, $rev_id ) {
112 $ns = $title->getNamespace();
113 $dbk = $title->getDBkey();
114 if ( !isset( $this->mTemplates[$ns] ) ) {
115 $this->mTemplates[$ns] = array();
116 }
117 $this->mTemplates[$ns][$dbk] = $page_id;
118 if ( !isset( $this->mTemplateIds[$ns] ) ) {
119 $this->mTemplateIds[$ns] = array();
120 }
121 $this->mTemplateIds[$ns][$dbk] = $rev_id; // For versioning
122 }
123
124 /**
125 * Return true if this cached output object predates the global or
126 * per-article cache invalidation timestamps, or if it comes from
127 * an incompatible older version.
128 *
129 * @param string $touched the affected article's last touched timestamp
130 * @return bool
131 * @public
132 */
133 function expired( $touched ) {
134 global $wgCacheEpoch;
135 return $this->getCacheTime() == -1 || // parser says it's uncacheable
136 $this->getCacheTime() < $touched ||
137 $this->getCacheTime() <= $wgCacheEpoch ||
138 !isset( $this->mVersion ) ||
139 version_compare( $this->mVersion, Parser::VERSION, "lt" );
140 }
141
142 /**
143 * Add some text to the <head>.
144 * If $tag is set, the section with that tag will only be included once
145 * in a given page.
146 */
147 function addHeadItem( $section, $tag = false ) {
148 if ( $tag !== false ) {
149 $this->mHeadItems[$tag] = $section;
150 } else {
151 $this->mHeadItems[] = $section;
152 }
153 }
154
155 /**
156 * Override the title to be used for display
157 * -- this is assumed to have been validated
158 * (check equal normalisation, etc.)
159 *
160 * @param string $text Desired title text
161 */
162 public function setDisplayTitle( $text ) {
163 $this->displayTitle = $text;
164 }
165
166 /**
167 * Get the title to be used for display
168 *
169 * @return string
170 */
171 public function getDisplayTitle() {
172 return $this->displayTitle;
173 }
174
175 /**
176 * Fairly generic flag setter thingy.
177 */
178 public function setFlag( $flag ) {
179 $this->mFlags[$flag] = true;
180 }
181
182 public function getFlag( $flag ) {
183 return isset( $this->mFlags[$flag] );
184 }
185
186 /**
187 * Set a property to be cached in the DB
188 */
189 public function setProperty( $name, $value ) {
190 $this->mProperties[$name] = $value;
191 }
192
193 public function getProperty( $name ){
194 return isset( $this->mProperties[$name] ) ? $this->mProperties[$name] : false;
195 }
196
197 public function getProperties() {
198 if ( !isset( $this->mProperties ) ) {
199 $this->mProperties = array();
200 }
201 return $this->mProperties;
202 }
203 }