3 * Output of the PHP parser
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}}
20 function getCacheTime() { return $this->mCacheTime
; }
22 function containsOldMagic() { return $this->mContainsOldMagic
; }
23 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic
, $com ); }
26 * setCacheTime() sets the timestamp expressing when the page has been rendered.
27 * This doesn not control expiry, see updateCacheExpiry() for that!
29 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime
, $t ); }
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().
40 function updateCacheExpiry( $seconds ) {
41 $seconds = (int)$seconds;
43 if ( $this->mCacheExpiry
=== null ||
$this->mCacheExpiry
> $seconds )
44 $this->mCacheExpiry
= $seconds;
46 // hack: set old-style marker for uncacheable entries.
47 if ( $this->mCacheExpiry
!== null && $this->mCacheExpiry
<= 0 )
48 $this->mCacheTime
= -1;
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.
59 function getCacheExpiry() {
60 global $wgParserCacheExpireTime;
62 if ( $this->mCacheTime
< 0 ) return 0; // old-style marker for "not cachable"
64 $expire = $this->mCacheExpiry
;
66 if ( $expire === null )
67 $expire = $wgParserCacheExpireTime;
69 $expire = min( $expire, $wgParserCacheExpireTime );
71 if( $this->containsOldMagic() ) { //compatibility hack
72 $expire = min( $expire, 3600 ); # 1 hour
75 if ( $expire <= 0 ) return 0; // not cachable
80 function isCacheable() {
81 return $this->getCacheExpiry() > 0;
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.
89 * @param $touched String: the affected article's last touched timestamp
92 public function expired( $touched ) {
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" );
103 class ParserOutput
extends CacheTime
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 $mModules = array(), # Modules to be loaded by the resource loader
120 $mOutputHooks = array(), # Hook tags as per $wgParserOutputHooks
121 $mWarnings = array(), # Warning text to be returned to the user. Wikitext formatted, in the key only
122 $mSections = array(), # Table of contents
123 $mProperties = array(), # Name/value pairs to be cached in the DB
124 $mTOCHTML = ''; # HTML of the TOC
125 private $mIndexPolicy = ''; # 'index' or 'noindex'? Any other value will result in no change.
127 function __construct( $text = '', $languageLinks = array(), $categoryLinks = array(),
128 $containsOldMagic = false, $titletext = '' )
130 $this->mText
= $text;
131 $this->mLanguageLinks
= $languageLinks;
132 $this->mCategories
= $categoryLinks;
133 $this->mContainsOldMagic
= $containsOldMagic;
134 $this->mTitleText
= $titletext;
137 function getText() { return $this->mText
; }
138 function &getLanguageLinks() { return $this->mLanguageLinks
; }
139 function getInterwikiLinks() { return $this->mInterwikiLinks
; }
140 function getCategoryLinks() { return array_keys( $this->mCategories
); }
141 function &getCategories() { return $this->mCategories
; }
142 function getTitleText() { return $this->mTitleText
; }
143 function getSections() { return $this->mSections
; }
144 function &getLinks() { return $this->mLinks
; }
145 function &getTemplates() { return $this->mTemplates
; }
146 function &getImages() { return $this->mImages
; }
147 function &getExternalLinks() { return $this->mExternalLinks
; }
148 function getNoGallery() { return $this->mNoGallery
; }
149 function getHeadItems() { return $this->mHeadItems
; }
150 function getModules() { return $this->mModules
; }
151 function getSubtitle() { return $this->mSubtitle
; }
152 function getOutputHooks() { return (array)$this->mOutputHooks
; }
153 function getWarnings() { return array_keys( $this->mWarnings
); }
154 function getIndexPolicy() { return $this->mIndexPolicy
; }
155 function getTOCHTML() { return $this->mTOCHTML
; }
157 function setText( $text ) { return wfSetVar( $this->mText
, $text ); }
158 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks
, $ll ); }
159 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategories
, $cl ); }
161 function setTitleText( $t ) { return wfSetVar( $this->mTitleText
, $t ); }
162 function setSections( $toc ) { return wfSetVar( $this->mSections
, $toc ); }
163 function setIndexPolicy( $policy ) { return wfSetVar( $this->mIndexPolicy
, $policy ); }
164 function setTOCHTML( $tochtml ) { return wfSetVar( $this->mTOCHTML
, $tochtml ); }
166 function addCategory( $c, $sort ) { $this->mCategories
[$c] = $sort; }
167 function addLanguageLink( $t ) { $this->mLanguageLinks
[] = $t; }
168 function addWarning( $s ) { $this->mWarnings
[$s] = 1; }
170 function addOutputHook( $hook, $data = false ) {
171 $this->mOutputHooks
[] = array( $hook, $data );
174 function setNewSection( $value ) {
175 $this->mNewSection
= (bool)$value;
177 function hideNewSection ( $value ) {
178 $this->mHideNewSection
= (bool)$value;
180 function getHideNewSection () {
181 return (bool)$this->mHideNewSection
;
183 function getNewSection() {
184 return (bool)$this->mNewSection
;
187 function addExternalLink( $url ) {
188 # We don't register links pointing to our own server, unless... :-)
189 global $wgServer, $wgRegisterInternalExternals;
190 if( $wgRegisterInternalExternals or stripos($url,$wgServer.'/')!==0)
191 $this->mExternalLinks
[$url] = 1;
195 * Record a local or interwiki inline link for saving in future link tables.
197 * @param $title Title object
198 * @param $id Mixed: optional known page_id so we can skip the lookup
200 function addLink( $title, $id = null ) {
201 if ( $title->isExternal() ) {
202 // Don't record interwikis in pagelinks
203 $this->addInterwikiLink( $title );
206 $ns = $title->getNamespace();
207 $dbk = $title->getDBkey();
208 if ( $ns == NS_MEDIA
) {
209 // Normalize this pseudo-alias if it makes it down here...
211 } elseif( $ns == NS_SPECIAL
) {
212 // We don't record Special: links currently
213 // It might actually be wise to, but we'd need to do some normalization.
215 } elseif( $dbk === '' ) {
216 // Don't record self links - [[#Foo]]
219 if ( !isset( $this->mLinks
[$ns] ) ) {
220 $this->mLinks
[$ns] = array();
222 if ( is_null( $id ) ) {
223 $id = $title->getArticleID();
225 $this->mLinks
[$ns][$dbk] = $id;
228 function addImage( $name ) {
229 $this->mImages
[$name] = 1;
232 function addTemplate( $title, $page_id, $rev_id ) {
233 $ns = $title->getNamespace();
234 $dbk = $title->getDBkey();
235 if ( !isset( $this->mTemplates
[$ns] ) ) {
236 $this->mTemplates
[$ns] = array();
238 $this->mTemplates
[$ns][$dbk] = $page_id;
239 if ( !isset( $this->mTemplateIds
[$ns] ) ) {
240 $this->mTemplateIds
[$ns] = array();
242 $this->mTemplateIds
[$ns][$dbk] = $rev_id; // For versioning
246 * @param $title Title object, must be an interwiki link
247 * @throws MWException if given invalid input
249 function addInterwikiLink( $title ) {
250 $prefix = $title->getInterwiki();
251 if( $prefix == '' ) {
252 throw new MWException( 'Non-interwiki link passed, internal parser error.' );
254 if (!isset($this->mInterwikiLinks
[$prefix])) {
255 $this->mInterwikiLinks
[$prefix] = array();
257 $this->mInterwikiLinks
[$prefix][$title->getDBkey()] = 1;
261 * Add some text to the <head>.
262 * If $tag is set, the section with that tag will only be included once
265 function addHeadItem( $section, $tag = false ) {
266 if ( $tag !== false ) {
267 $this->mHeadItems
[$tag] = $section;
269 $this->mHeadItems
[] = $section;
273 function addModules( $modules ) {
274 $this->mModules
= array_merge( $this->mModules
, (array) $modules );
278 * Override the title to be used for display
279 * -- this is assumed to have been validated
280 * (check equal normalisation, etc.)
282 * @param $text String: desired title text
284 public function setDisplayTitle( $text ) {
285 $this->setTitleText( $text );
286 $this->setProperty( 'displaytitle', $text );
290 * Get the title to be used for display
294 public function getDisplayTitle() {
295 $t = $this->getTitleText( );
303 * Fairly generic flag setter thingy.
305 public function setFlag( $flag ) {
306 $this->mFlags
[$flag] = true;
309 public function getFlag( $flag ) {
310 return isset( $this->mFlags
[$flag] );
314 * Set a property to be cached in the DB
316 public function setProperty( $name, $value ) {
317 $this->mProperties
[$name] = $value;
320 public function getProperty( $name ){
321 return isset( $this->mProperties
[$name] ) ?
$this->mProperties
[$name] : false;
324 public function getProperties() {
325 if ( !isset( $this->mProperties
) ) {
326 $this->mProperties
= array();
328 return $this->mProperties
;