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