Get rid of PHP4-style constructors
[lhc/web/wiklou.git] / includes / parser / ParserOutput.php
1 <?php
2 /**
3 * Output of the PHP parser
4 *
5 * @file
6 * @ingroup Parser
7 */
8
9 /**
10 * @todo document
11 * @ingroup Parser
12 */
13
14 class CacheTime {
15 var $mVersion = Parser::VERSION, # Compatibility check
16 $mCacheTime = '', # Time when this object was generated, or -1 for uncacheable. Used in ParserCache.
17 $mCacheExpiry = null, # Seconds after which the object should expire, use 0 for uncachable. Used in ParserCache.
18 $mContainsOldMagic; # Boolean variable indicating if the input contained variables like {{CURRENTDAY}}
19
20 function getCacheTime() { return $this->mCacheTime; }
21
22 function containsOldMagic() { return $this->mContainsOldMagic; }
23 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
24
25 /**
26 * setCacheTime() sets the timestamp expressing when the page has been rendered.
27 * This doesn not control expiry, see updateCacheExpiry() for that!
28 */
29 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
30
31
32 /**
33 * Sets the number of seconds after which this object should expire.
34 * This value is used with the ParserCache.
35 * If called with a value greater than the value provided at any previous call,
36 * the new call has no effect. The value returned by getCacheExpiry is smaller
37 * or equal to the smallest number that was provided as an argument to
38 * updateCacheExpiry().
39 */
40 function updateCacheExpiry( $seconds ) {
41 $seconds = (int)$seconds;
42
43 if ( $this->mCacheExpiry === null || $this->mCacheExpiry > $seconds )
44 $this->mCacheExpiry = $seconds;
45
46 // hack: set old-style marker for uncacheable entries.
47 if ( $this->mCacheExpiry !== null && $this->mCacheExpiry <= 0 )
48 $this->mCacheTime = -1;
49 }
50
51 /**
52 * Returns the number of seconds after which this object should expire.
53 * This method is used by ParserCache to determine how long the ParserOutput can be cached.
54 * The timestamp of expiry can be calculated by adding getCacheExpiry() to getCacheTime().
55 * The value returned by getCacheExpiry is smaller or equal to the smallest number
56 * that was provided to a call of updateCacheExpiry(), and smaller or equal to the
57 * value of $wgParserCacheExpireTime.
58 */
59 function getCacheExpiry() {
60 global $wgParserCacheExpireTime;
61
62 if ( $this->mCacheTime < 0 ) return 0; // old-style marker for "not cachable"
63
64 $expire = $this->mCacheExpiry;
65
66 if ( $expire === null )
67 $expire = $wgParserCacheExpireTime;
68 else
69 $expire = min( $expire, $wgParserCacheExpireTime );
70
71 if( $this->containsOldMagic() ) { //compatibility hack
72 $expire = min( $expire, 3600 ); # 1 hour
73 }
74
75 if ( $expire <= 0 ) return 0; // not cachable
76 else return $expire;
77 }
78
79
80 function isCacheable() {
81 return $this->getCacheExpiry() > 0;
82 }
83
84 /**
85 * Return true if this cached output object predates the global or
86 * per-article cache invalidation timestamps, or if it comes from
87 * an incompatible older version.
88 *
89 * @param $touched String: the affected article's last touched timestamp
90 * @return Boolean
91 */
92 public function expired( $touched ) {
93 global $wgCacheEpoch;
94 return !$this->isCacheable() || // parser says it's uncacheable
95 $this->getCacheTime() < $touched ||
96 $this->getCacheTime() <= $wgCacheEpoch ||
97 $this->getCacheTime() < wfTimestamp( TS_MW, time() - $this->getCacheExpiry() ) || // expiry period has passed
98 !isset( $this->mVersion ) ||
99 version_compare( $this->mVersion, Parser::VERSION, "lt" );
100 }
101 }
102
103 class ParserOutput extends CacheTime
104 {
105 var $mText, # The output text
106 $mLanguageLinks, # List of the full text of language links, in the order they appear
107 $mCategories, # Map of category names to sort keys
108 $mTitleText, # title text of the chosen language variant
109 $mLinks = array(), # 2-D map of NS/DBK to ID for the links in the document. ID=zero for broken.
110 $mTemplates = array(), # 2-D map of NS/DBK to ID for the template references. ID=zero for broken.
111 $mTemplateIds = array(), # 2-D map of NS/DBK to rev ID for the template references. ID=zero for broken.
112 $mImages = array(), # DB keys of the images used, in the array key only
113 $mExternalLinks = array(), # External link URLs, in the key only
114 $mInterwikiLinks = array(), # 2-D map of prefix/DBK (in keys only) for the inline interwiki links in the document.
115 $mNewSection = false, # Show a new section link?
116 $mHideNewSection = false, # Hide the new section link?
117 $mNoGallery = false, # No gallery on category page? (__NOGALLERY__)
118 $mHeadItems = array(), # Items to put in the <head> section
119 $mOutputHooks = array(), # Hook tags as per $wgParserOutputHooks
120 $mWarnings = array(), # Warning text to be returned to the user. Wikitext formatted, in the key only
121 $mSections = array(), # Table of contents
122 $mProperties = array(), # Name/value pairs to be cached in the DB
123 $mTOCHTML = ''; # HTML of the TOC
124 private $mIndexPolicy = ''; # 'index' or 'noindex'? Any other value will result in no change.
125
126 function __construct( $text = '', $languageLinks = array(), $categoryLinks = array(),
127 $containsOldMagic = false, $titletext = '' )
128 {
129 $this->mText = $text;
130 $this->mLanguageLinks = $languageLinks;
131 $this->mCategories = $categoryLinks;
132 $this->mContainsOldMagic = $containsOldMagic;
133 $this->mTitleText = $titletext;
134 }
135
136 function getText() { return $this->mText; }
137 function &getLanguageLinks() { return $this->mLanguageLinks; }
138 function getInterwikiLinks() { return $this->mInterwikiLinks; }
139 function getCategoryLinks() { return array_keys( $this->mCategories ); }
140 function &getCategories() { return $this->mCategories; }
141 function getTitleText() { return $this->mTitleText; }
142 function getSections() { return $this->mSections; }
143 function &getLinks() { return $this->mLinks; }
144 function &getTemplates() { return $this->mTemplates; }
145 function &getImages() { return $this->mImages; }
146 function &getExternalLinks() { return $this->mExternalLinks; }
147 function getNoGallery() { return $this->mNoGallery; }
148 function getHeadItems() { return $this->mHeadItems; }
149 function getSubtitle() { return $this->mSubtitle; }
150 function getOutputHooks() { return (array)$this->mOutputHooks; }
151 function getWarnings() { return array_keys( $this->mWarnings ); }
152 function getIndexPolicy() { return $this->mIndexPolicy; }
153 function getTOCHTML() { return $this->mTOCHTML; }
154
155 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
156 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
157 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategories, $cl ); }
158
159 function setTitleText( $t ) { return wfSetVar( $this->mTitleText, $t ); }
160 function setSections( $toc ) { return wfSetVar( $this->mSections, $toc ); }
161 function setIndexPolicy( $policy ) { return wfSetVar( $this->mIndexPolicy, $policy ); }
162 function setTOCHTML( $tochtml ) { return wfSetVar( $this->mTOCHTML, $tochtml ); }
163
164 function addCategory( $c, $sort ) { $this->mCategories[$c] = $sort; }
165 function addLanguageLink( $t ) { $this->mLanguageLinks[] = $t; }
166 function addWarning( $s ) { $this->mWarnings[$s] = 1; }
167
168 function addOutputHook( $hook, $data = false ) {
169 $this->mOutputHooks[] = array( $hook, $data );
170 }
171
172 function setNewSection( $value ) {
173 $this->mNewSection = (bool)$value;
174 }
175 function hideNewSection ( $value ) {
176 $this->mHideNewSection = (bool)$value;
177 }
178 function getHideNewSection () {
179 return (bool)$this->mHideNewSection;
180 }
181 function getNewSection() {
182 return (bool)$this->mNewSection;
183 }
184
185 function addExternalLink( $url ) {
186 # We don't register links pointing to our own server, unless... :-)
187 global $wgServer, $wgRegisterInternalExternals;
188 if( $wgRegisterInternalExternals or stripos($url,$wgServer.'/')!==0)
189 $this->mExternalLinks[$url] = 1;
190 }
191
192 /**
193 * Record a local or interwiki inline link for saving in future link tables.
194 *
195 * @param $title Title object
196 * @param $id Mixed: optional known page_id so we can skip the lookup
197 */
198 function addLink( $title, $id = null ) {
199 if ( $title->isExternal() ) {
200 // Don't record interwikis in pagelinks
201 $this->addInterwikiLink( $title );
202 return;
203 }
204 $ns = $title->getNamespace();
205 $dbk = $title->getDBkey();
206 if ( $ns == NS_MEDIA ) {
207 // Normalize this pseudo-alias if it makes it down here...
208 $ns = NS_FILE;
209 } elseif( $ns == NS_SPECIAL ) {
210 // We don't record Special: links currently
211 // It might actually be wise to, but we'd need to do some normalization.
212 return;
213 } elseif( $dbk === '' ) {
214 // Don't record self links - [[#Foo]]
215 return;
216 }
217 if ( !isset( $this->mLinks[$ns] ) ) {
218 $this->mLinks[$ns] = array();
219 }
220 if ( is_null( $id ) ) {
221 $id = $title->getArticleID();
222 }
223 $this->mLinks[$ns][$dbk] = $id;
224 }
225
226 function addImage( $name ) {
227 $this->mImages[$name] = 1;
228 }
229
230 function addTemplate( $title, $page_id, $rev_id ) {
231 $ns = $title->getNamespace();
232 $dbk = $title->getDBkey();
233 if ( !isset( $this->mTemplates[$ns] ) ) {
234 $this->mTemplates[$ns] = array();
235 }
236 $this->mTemplates[$ns][$dbk] = $page_id;
237 if ( !isset( $this->mTemplateIds[$ns] ) ) {
238 $this->mTemplateIds[$ns] = array();
239 }
240 $this->mTemplateIds[$ns][$dbk] = $rev_id; // For versioning
241 }
242
243 /**
244 * @param $title Title object, must be an interwiki link
245 * @throws MWException if given invalid input
246 */
247 function addInterwikiLink( $title ) {
248 $prefix = $title->getInterwiki();
249 if( $prefix == '' ) {
250 throw new MWException( 'Non-interwiki link passed, internal parser error.' );
251 }
252 if (!isset($this->mInterwikiLinks[$prefix])) {
253 $this->mInterwikiLinks[$prefix] = array();
254 }
255 $this->mInterwikiLinks[$prefix][$title->getDBkey()] = 1;
256 }
257
258 /**
259 * Add some text to the <head>.
260 * If $tag is set, the section with that tag will only be included once
261 * in a given page.
262 */
263 function addHeadItem( $section, $tag = false ) {
264 if ( $tag !== false ) {
265 $this->mHeadItems[$tag] = $section;
266 } else {
267 $this->mHeadItems[] = $section;
268 }
269 }
270
271 /**
272 * Override the title to be used for display
273 * -- this is assumed to have been validated
274 * (check equal normalisation, etc.)
275 *
276 * @param $text String: desired title text
277 */
278 public function setDisplayTitle( $text ) {
279 $this->setTitleText( $text );
280 $this->setProperty( 'displaytitle', $text );
281 }
282
283 /**
284 * Get the title to be used for display
285 *
286 * @return String
287 */
288 public function getDisplayTitle() {
289 $t = $this->getTitleText( );
290 if( $t === '' ) {
291 return false;
292 }
293 return $t;
294 }
295
296 /**
297 * Fairly generic flag setter thingy.
298 */
299 public function setFlag( $flag ) {
300 $this->mFlags[$flag] = true;
301 }
302
303 public function getFlag( $flag ) {
304 return isset( $this->mFlags[$flag] );
305 }
306
307 /**
308 * Set a property to be cached in the DB
309 */
310 public function setProperty( $name, $value ) {
311 $this->mProperties[$name] = $value;
312 }
313
314 public function getProperty( $name ){
315 return isset( $this->mProperties[$name] ) ? $this->mProperties[$name] : false;
316 }
317
318 public function getProperties() {
319 if ( !isset( $this->mProperties ) ) {
320 $this->mProperties = array();
321 }
322 return $this->mProperties;
323 }
324 }