Tweak for r29561: don't grab a database object until we need it
[lhc/web/wiklou.git] / includes / ParserOutput.php
1 <?php
2 /**
3 * @todo document
4 * @addtogroup 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.
25 $mSections; # Table of contents
26
27 /**
28 * Overridden title for display
29 */
30 private $displayTitle = false;
31
32 function ParserOutput( $text = '', $languageLinks = array(), $categoryLinks = array(),
33 $containsOldMagic = false, $titletext = '' )
34 {
35 $this->mText = $text;
36 $this->mLanguageLinks = $languageLinks;
37 $this->mCategories = $categoryLinks;
38 $this->mContainsOldMagic = $containsOldMagic;
39 $this->mCacheTime = '';
40 $this->mVersion = Parser::VERSION;
41 $this->mTitleText = $titletext;
42 $this->mSections = array();
43 $this->mLinks = array();
44 $this->mTemplates = array();
45 $this->mImages = array();
46 $this->mExternalLinks = array();
47 $this->mNewSection = false;
48 $this->mNoGallery = false;
49 $this->mHeadItems = array();
50 $this->mTemplateIds = array();
51 $this->mOutputHooks = array();
52 $this->mWarnings = array();
53 }
54
55 function getText() { return $this->mText; }
56 function &getLanguageLinks() { return $this->mLanguageLinks; }
57 function getCategoryLinks() { return array_keys( $this->mCategories ); }
58 function &getCategories() { return $this->mCategories; }
59 function getCacheTime() { return $this->mCacheTime; }
60 function getTitleText() { return $this->mTitleText; }
61 function getSections() { return $this->mSections; }
62 function &getLinks() { return $this->mLinks; }
63 function &getTemplates() { return $this->mTemplates; }
64 function &getImages() { return $this->mImages; }
65 function &getExternalLinks() { return $this->mExternalLinks; }
66 function getNoGallery() { return $this->mNoGallery; }
67 function getSubtitle() { return $this->mSubtitle; }
68 function getOutputHooks() { return (array)$this->mOutputHooks; }
69 function getWarnings() { return isset( $this->mWarnings ) ? $this->mWarnings : array(); }
70
71 function containsOldMagic() { return $this->mContainsOldMagic; }
72 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
73 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
74 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategories, $cl ); }
75 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
76 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
77 function setTitleText( $t ) { return wfSetVar( $this->mTitleText, $t ); }
78 function setSections( $toc ) { return wfSetVar( $this->mSections, $toc ); }
79
80 function addCategory( $c, $sort ) { $this->mCategories[$c] = $sort; }
81 function addLanguageLink( $t ) { $this->mLanguageLinks[] = $t; }
82 function addExternalLink( $url ) { $this->mExternalLinks[$url] = 1; }
83 function addWarning( $s ) { $this->mWarnings[] = $s; }
84
85 function addOutputHook( $hook, $data = false ) {
86 $this->mOutputHooks[] = array( $hook, $data );
87 }
88
89 function setNewSection( $value ) {
90 $this->mNewSection = (bool)$value;
91 }
92 function getNewSection() {
93 return (bool)$this->mNewSection;
94 }
95
96 function addLink( $title, $id = null ) {
97 $ns = $title->getNamespace();
98 $dbk = $title->getDBkey();
99 if ( !isset( $this->mLinks[$ns] ) ) {
100 $this->mLinks[$ns] = array();
101 }
102 if ( is_null( $id ) ) {
103 $id = $title->getArticleID();
104 }
105 $this->mLinks[$ns][$dbk] = $id;
106 }
107
108 function addImage( $name ) {
109 $this->mImages[$name] = 1;
110 }
111
112 function addTemplate( $title, $page_id, $rev_id ) {
113 $ns = $title->getNamespace();
114 $dbk = $title->getDBkey();
115 if ( !isset( $this->mTemplates[$ns] ) ) {
116 $this->mTemplates[$ns] = array();
117 }
118 $this->mTemplates[$ns][$dbk] = $page_id;
119 if ( !isset( $this->mTemplateIds[$ns] ) ) {
120 $this->mTemplateIds[$ns] = array();
121 }
122 $this->mTemplateIds[$ns][$dbk] = $rev_id; // For versioning
123 }
124
125 /**
126 * Return true if this cached output object predates the global or
127 * per-article cache invalidation timestamps, or if it comes from
128 * an incompatible older version.
129 *
130 * @param string $touched the affected article's last touched timestamp
131 * @return bool
132 * @public
133 */
134 function expired( $touched ) {
135 global $wgCacheEpoch;
136 return $this->getCacheTime() == -1 || // parser says it's uncacheable
137 $this->getCacheTime() < $touched ||
138 $this->getCacheTime() <= $wgCacheEpoch ||
139 !isset( $this->mVersion ) ||
140 version_compare( $this->mVersion, Parser::VERSION, "lt" );
141 }
142
143 /**
144 * Add some text to the <head>.
145 * If $tag is set, the section with that tag will only be included once
146 * in a given page.
147 */
148 function addHeadItem( $section, $tag = false ) {
149 if ( $tag !== false ) {
150 $this->mHeadItems[$tag] = $section;
151 } else {
152 $this->mHeadItems[] = $section;
153 }
154 }
155
156 /**
157 * Override the title to be used for display
158 * -- this is assumed to have been validated
159 * (check equal normalisation, etc.)
160 *
161 * @param string $text Desired title text
162 */
163 public function setDisplayTitle( $text ) {
164 $this->displayTitle = $text;
165 }
166
167 /**
168 * Get the title to be used for display
169 *
170 * @return string
171 */
172 public function getDisplayTitle() {
173 return $this->displayTitle;
174 }
175
176 /**
177 * Fairly generic flag setter thingy.
178 */
179 public function setFlag( $flag ) {
180 $this->mFlags[$flag] = true;
181 }
182
183 public function getFlag( $flag ) {
184 return isset( $this->mFlags[$flag] );
185 }
186
187 }
188
189