* (bug 10181) Support the XCache object caching mechanism [patch from Kurt Radwanski]
[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 $mImageTimestamps, # Map of DBK to rev ID for the template references. ID=zero for broken.
20 $mExternalLinks, # External link URLs, in the key only
21 $mHTMLtitle, # Display HTML title
22 $mSubtitle, # Additional subtitle
23 $mNewSection, # Show a new section link?
24 $mNoGallery, # No gallery on category page? (__NOGALLERY__)
25 $mHeadItems; # Items to put in the <head> section
26
27 function ParserOutput( $text = '', $languageLinks = array(), $categoryLinks = array(),
28 $containsOldMagic = false, $titletext = '' )
29 {
30 $this->mText = $text;
31 $this->mLanguageLinks = $languageLinks;
32 $this->mCategories = $categoryLinks;
33 $this->mContainsOldMagic = $containsOldMagic;
34 $this->mCacheTime = '';
35 $this->mVersion = Parser::VERSION;
36 $this->mTitleText = $titletext;
37 $this->mLinks = array();
38 $this->mTemplates = array();
39 $this->mImages = array();
40 $this->mExternalLinks = array();
41 $this->mHTMLtitle = "" ;
42 $this->mSubtitle = "" ;
43 $this->mNewSection = false;
44 $this->mNoGallery = false;
45 $this->mHeadItems = array();
46 $this->mTemplateIds = array();
47 $this->mImageTimestamps = array();
48 }
49
50 function getText() { return $this->mText; }
51 function &getLanguageLinks() { return $this->mLanguageLinks; }
52 function getCategoryLinks() { return array_keys( $this->mCategories ); }
53 function &getCategories() { return $this->mCategories; }
54 function getCacheTime() { return $this->mCacheTime; }
55 function getTitleText() { return $this->mTitleText; }
56 function &getLinks() { return $this->mLinks; }
57 function &getTemplates() { return $this->mTemplates; }
58 function &getImages() { return $this->mImages; }
59 function &getExternalLinks() { return $this->mExternalLinks; }
60 function getNoGallery() { return $this->mNoGallery; }
61 function getSubtitle() { return $this->mSubtitle; }
62
63 function containsOldMagic() { return $this->mContainsOldMagic; }
64 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
65 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
66 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategories, $cl ); }
67 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
68 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
69 function setTitleText( $t ) { return wfSetVar($this->mTitleText, $t); }
70 function setSubtitle( $st ) { return wfSetVar( $this->mSubtitle, $st ); }
71
72 function addCategory( $c, $sort ) { $this->mCategories[$c] = $sort; }
73 function addLanguageLink( $t ) { $this->mLanguageLinks[] = $t; }
74 function addExternalLink( $url ) { $this->mExternalLinks[$url] = 1; }
75
76 function setNewSection( $value ) {
77 $this->mNewSection = (bool)$value;
78 }
79 function getNewSection() {
80 return (bool)$this->mNewSection;
81 }
82
83 function addLink( $title, $id = null ) {
84 $ns = $title->getNamespace();
85 $dbk = $title->getDBkey();
86 if ( !isset( $this->mLinks[$ns] ) ) {
87 $this->mLinks[$ns] = array();
88 }
89 if ( is_null( $id ) ) {
90 $id = $title->getArticleID();
91 }
92 $this->mLinks[$ns][$dbk] = $id;
93 }
94
95 function addImage( $name, $timestamp=NULL ) {
96 if( isset($this->mImages[$name]) )
97 return; // No repeated pointless DB calls!
98 $this->mImages[$name] = 1;
99 if( is_null($timestamp) ) {
100 wfProfileIn( __METHOD__ );
101 $dbr = wfGetDB(DB_SLAVE);
102 $timestamp = $dbr->selectField('image', 'img_timestamp',
103 array('img_name' => $name),
104 __METHOD__ );
105 }
106 $timestamp = $timestamp ? $timestamp : null;
107 $this->mImageTimestamps[$name] = $timestamp; // For versioning
108 }
109
110 function addTemplate( $title, $page_id, $rev_id ) {
111 $ns = $title->getNamespace();
112 $dbk = $title->getDBkey();
113 if ( !isset( $this->mTemplates[$ns] ) ) {
114 $this->mTemplates[$ns] = array();
115 }
116 $this->mTemplates[$ns][$dbk] = $page_id;
117 if ( !isset( $this->mTemplateIds[$ns] ) ) {
118 $this->mTemplateIds[$ns] = array();
119 }
120 $this->mTemplateIds[$ns][$dbk] = $rev_id; // For versioning
121 }
122
123 /**
124 * Return true if this cached output object predates the global or
125 * per-article cache invalidation timestamps, or if it comes from
126 * an incompatible older version.
127 *
128 * @param string $touched the affected article's last touched timestamp
129 * @return bool
130 * @public
131 */
132 function expired( $touched ) {
133 global $wgCacheEpoch;
134 return $this->getCacheTime() == -1 || // parser says it's uncacheable
135 $this->getCacheTime() < $touched ||
136 $this->getCacheTime() <= $wgCacheEpoch ||
137 !isset( $this->mVersion ) ||
138 version_compare( $this->mVersion, Parser::VERSION, "lt" );
139 }
140
141 /**
142 * Add some text to the <head>.
143 * If $tag is set, the section with that tag will only be included once
144 * in a given page.
145 */
146 function addHeadItem( $section, $tag = false ) {
147 if ( $tag !== false ) {
148 $this->mHeadItems[$tag] = $section;
149 } else {
150 $this->mHeadItems[] = $section;
151 }
152 }
153 }
154
155 ?>