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