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