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 ); }
32 * Sets the number of seconds after which this object should expire.
33 * This value is used with the ParserCache.
34 * If called with a value greater than the value provided at any previous call,
35 * the new call has no effect. The value returned by getCacheExpiry is smaller
36 * or equal to the smallest number that was provided as an argument to
37 * updateCacheExpiry().
39 function updateCacheExpiry( $seconds ) {
40 $seconds = (int)$seconds;
42 if ( $this->mCacheExpiry
=== null ||
$this->mCacheExpiry
> $seconds ) {
43 $this->mCacheExpiry
= $seconds;
46 // hack: set old-style marker for uncacheable entries.
47 if ( $this->mCacheExpiry
!== null && $this->mCacheExpiry
<= 0 ) {
48 $this->mCacheTime
= -1;
53 * Returns the number of seconds after which this object should expire.
54 * This method is used by ParserCache to determine how long the ParserOutput can be cached.
55 * The timestamp of expiry can be calculated by adding getCacheExpiry() to getCacheTime().
56 * The value returned by getCacheExpiry is smaller or equal to the smallest number
57 * that was provided to a call of updateCacheExpiry(), and smaller or equal to the
58 * value of $wgParserCacheExpireTime.
60 function getCacheExpiry() {
61 global $wgParserCacheExpireTime;
63 if ( $this->mCacheTime
< 0 ) {
65 } // old-style marker for "not cachable"
67 $expire = $this->mCacheExpiry
;
69 if ( $expire === null ) {
70 $expire = $wgParserCacheExpireTime;
72 $expire = min( $expire, $wgParserCacheExpireTime );
75 if( $this->containsOldMagic() ) { //compatibility hack
76 $expire = min( $expire, 3600 ); # 1 hour
80 return 0; // not cachable
89 function isCacheable() {
90 return $this->getCacheExpiry() > 0;
94 * Return true if this cached output object predates the global or
95 * per-article cache invalidation timestamps, or if it comes from
96 * an incompatible older version.
98 * @param $touched String: the affected article's last touched timestamp
101 public function expired( $touched ) {
102 global $wgCacheEpoch;
103 return !$this->isCacheable() ||
// parser says it's uncacheable
104 $this->getCacheTime() < $touched ||
105 $this->getCacheTime() <= $wgCacheEpoch ||
106 $this->getCacheTime() < wfTimestamp( TS_MW
, time() - $this->getCacheExpiry() ) ||
// expiry period has passed
107 !isset( $this->mVersion
) ||
108 version_compare( $this->mVersion
, Parser
::VERSION
, "lt" );
112 class ParserOutput
extends CacheTime
{
113 var $mText, # The output text
114 $mLanguageLinks, # List of the full text of language links, in the order they appear
115 $mCategories, # Map of category names to sort keys
116 $mTitleText, # title text of the chosen language variant
117 $mLinks = array(), # 2-D map of NS/DBK to ID for the links in the document. ID=zero for broken.
118 $mTemplates = array(), # 2-D map of NS/DBK to ID for the template references. ID=zero for broken.
119 $mTemplateIds = array(), # 2-D map of NS/DBK to rev ID for the template references. ID=zero for broken.
120 $mImages = array(), # DB keys of the images used, in the array key only
121 $mImageTimeKeys = array(), # DB keys of the images used mapped to sha1 and MW timestamp
122 $mExternalLinks = array(), # External link URLs, in the key only
123 $mInterwikiLinks = array(), # 2-D map of prefix/DBK (in keys only) for the inline interwiki links in the document.
124 $mNewSection = false, # Show a new section link?
125 $mHideNewSection = false, # Hide the new section link?
126 $mNoGallery = false, # No gallery on category page? (__NOGALLERY__)
127 $mHeadItems = array(), # Items to put in the <head> section
128 $mModules = array(), # Modules to be loaded by the resource loader
129 $mOutputHooks = array(), # Hook tags as per $wgParserOutputHooks
130 $mWarnings = array(), # Warning text to be returned to the user. Wikitext formatted, in the key only
131 $mSections = array(), # Table of contents
132 $mEditSectionTokens = false, # prefix/suffix markers if edit sections were output as tokens
133 $mProperties = array(), # Name/value pairs to be cached in the DB
134 $mTOCHTML = ''; # HTML of the TOC
135 private $mIndexPolicy = ''; # 'index' or 'noindex'? Any other value will result in no change.
136 private $mAccessedOptions = array(); # List of ParserOptions (stored in the keys)
138 const EDITSECTION_REGEX
= '#<(?:mw:)?editsection page="(.*?)" section="(.*?)"(?:/>|>(.*?)(</(?:mw:)?editsection>))#';
140 function __construct( $text = '', $languageLinks = array(), $categoryLinks = array(),
141 $containsOldMagic = false, $titletext = '' )
143 $this->mText
= $text;
144 $this->mLanguageLinks
= $languageLinks;
145 $this->mCategories
= $categoryLinks;
146 $this->mContainsOldMagic
= $containsOldMagic;
147 $this->mTitleText
= $titletext;
151 if ( $this->mEditSectionTokens
) {
152 return preg_replace_callback( ParserOutput
::EDITSECTION_REGEX
,
153 array( &$this, 'replaceEditSectionLinksCallback' ), $this->mText
);
159 * callback used by getText to replace editsection tokens
162 function replaceEditSectionLinksCallback( $m ) {
163 global $wgOut, $wgLang;
165 htmlspecialchars_decode($m[1]),
166 htmlspecialchars_decode($m[2]),
167 isset($m[4]) ?
$m[3] : null,
169 $args[0] = Title
::newFromText( $args[0] );
170 if ( !is_object($args[0]) ) {
171 throw new MWException("Bad parser output text.");
173 $args[] = $wgLang->getCode();
174 $skin = $wgOut->getSkin();
175 return call_user_func_array( array( $skin, 'doEditSectionLink' ), $args );
178 function &getLanguageLinks() { return $this->mLanguageLinks
; }
179 function getInterwikiLinks() { return $this->mInterwikiLinks
; }
180 function getCategoryLinks() { return array_keys( $this->mCategories
); }
181 function &getCategories() { return $this->mCategories
; }
182 function getTitleText() { return $this->mTitleText
; }
183 function getSections() { return $this->mSections
; }
184 function getEditSectionTokens() { return $this->mEditSectionTokens
; }
185 function &getLinks() { return $this->mLinks
; }
186 function &getTemplates() { return $this->mTemplates
; }
187 function &getTemplateIds() { return $this->mTemplateIds
; }
188 function &getImages() { return $this->mImages
; }
189 function &getImageTimeKeys() { return $this->mImageTimeKeys
; }
190 function &getExternalLinks() { return $this->mExternalLinks
; }
191 function getNoGallery() { return $this->mNoGallery
; }
192 function getHeadItems() { return $this->mHeadItems
; }
193 function getModules() { return $this->mModules
; }
194 function getOutputHooks() { return (array)$this->mOutputHooks
; }
195 function getWarnings() { return array_keys( $this->mWarnings
); }
196 function getIndexPolicy() { return $this->mIndexPolicy
; }
197 function getTOCHTML() { return $this->mTOCHTML
; }
199 function setText( $text ) { return wfSetVar( $this->mText
, $text ); }
200 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks
, $ll ); }
201 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategories
, $cl ); }
203 function setTitleText( $t ) { return wfSetVar( $this->mTitleText
, $t ); }
204 function setSections( $toc ) { return wfSetVar( $this->mSections
, $toc ); }
205 function setEditSectionTokens( $t ) { return wfSetVar( $this->mEditSectionTokens
, $t ); }
206 function setIndexPolicy( $policy ) { return wfSetVar( $this->mIndexPolicy
, $policy ); }
207 function setTOCHTML( $tochtml ) { return wfSetVar( $this->mTOCHTML
, $tochtml ); }
209 function addCategory( $c, $sort ) { $this->mCategories
[$c] = $sort; }
210 function addLanguageLink( $t ) { $this->mLanguageLinks
[] = $t; }
211 function addWarning( $s ) { $this->mWarnings
[$s] = 1; }
213 function addOutputHook( $hook, $data = false ) {
214 $this->mOutputHooks
[] = array( $hook, $data );
217 function setNewSection( $value ) {
218 $this->mNewSection
= (bool)$value;
220 function hideNewSection ( $value ) {
221 $this->mHideNewSection
= (bool)$value;
223 function getHideNewSection () {
224 return (bool)$this->mHideNewSection
;
226 function getNewSection() {
227 return (bool)$this->mNewSection
;
230 function addExternalLink( $url ) {
231 # We don't register links pointing to our own server, unless... :-)
232 global $wgServer, $wgRegisterInternalExternals;
233 if( $wgRegisterInternalExternals or stripos($url,$wgServer.'/')!==0)
234 $this->mExternalLinks
[$url] = 1;
238 * Record a local or interwiki inline link for saving in future link tables.
240 * @param $title Title object
241 * @param $id Mixed: optional known page_id so we can skip the lookup
243 function addLink( $title, $id = null ) {
244 if ( $title->isExternal() ) {
245 // Don't record interwikis in pagelinks
246 $this->addInterwikiLink( $title );
249 $ns = $title->getNamespace();
250 $dbk = $title->getDBkey();
251 if ( $ns == NS_MEDIA
) {
252 // Normalize this pseudo-alias if it makes it down here...
254 } elseif( $ns == NS_SPECIAL
) {
255 // We don't record Special: links currently
256 // It might actually be wise to, but we'd need to do some normalization.
258 } elseif( $dbk === '' ) {
259 // Don't record self links - [[#Foo]]
262 if ( !isset( $this->mLinks
[$ns] ) ) {
263 $this->mLinks
[$ns] = array();
265 if ( is_null( $id ) ) {
266 $id = $title->getArticleID();
268 $this->mLinks
[$ns][$dbk] = $id;
272 * Register a file dependency for this output
273 * @param $name string Title dbKey
274 * @param $timestamp string MW timestamp of file creation (or false if non-existing)
275 * @param $sha string base 36 SHA-1 of file (or false if non-existing)
278 function addImage( $name, $timestamp = null, $sha1 = null ) {
279 $this->mImages
[$name] = 1;
280 if ( $timestamp !== null && $sha1 !== null ) {
281 $this->mImageTimeKeys
[$name] = array( 'time' => $timestamp, 'sha1' => $sha1 );
286 * Register a template dependency for this output
287 * @param $title Title
292 function addTemplate( $title, $page_id, $rev_id ) {
293 $ns = $title->getNamespace();
294 $dbk = $title->getDBkey();
295 if ( !isset( $this->mTemplates
[$ns] ) ) {
296 $this->mTemplates
[$ns] = array();
298 $this->mTemplates
[$ns][$dbk] = $page_id;
299 if ( !isset( $this->mTemplateIds
[$ns] ) ) {
300 $this->mTemplateIds
[$ns] = array();
302 $this->mTemplateIds
[$ns][$dbk] = $rev_id; // For versioning
306 * @param $title Title object, must be an interwiki link
307 * @throws MWException if given invalid input
309 function addInterwikiLink( $title ) {
310 $prefix = $title->getInterwiki();
311 if( $prefix == '' ) {
312 throw new MWException( 'Non-interwiki link passed, internal parser error.' );
314 if (!isset($this->mInterwikiLinks
[$prefix])) {
315 $this->mInterwikiLinks
[$prefix] = array();
317 $this->mInterwikiLinks
[$prefix][$title->getDBkey()] = 1;
321 * Add some text to the <head>.
322 * If $tag is set, the section with that tag will only be included once
325 function addHeadItem( $section, $tag = false ) {
326 if ( $tag !== false ) {
327 $this->mHeadItems
[$tag] = $section;
329 $this->mHeadItems
[] = $section;
333 function addModules( $modules ) {
334 $this->mModules
= array_merge( $this->mModules
, (array) $modules );
338 * Override the title to be used for display
339 * -- this is assumed to have been validated
340 * (check equal normalisation, etc.)
342 * @param $text String: desired title text
344 public function setDisplayTitle( $text ) {
345 $this->setTitleText( $text );
346 $this->setProperty( 'displaytitle', $text );
350 * Get the title to be used for display
354 public function getDisplayTitle() {
355 $t = $this->getTitleText();
363 * Fairly generic flag setter thingy.
365 public function setFlag( $flag ) {
366 $this->mFlags
[$flag] = true;
369 public function getFlag( $flag ) {
370 return isset( $this->mFlags
[$flag] );
374 * Set a property to be cached in the DB
376 public function setProperty( $name, $value ) {
377 $this->mProperties
[$name] = $value;
380 public function getProperty( $name ){
381 return isset( $this->mProperties
[$name] ) ?
$this->mProperties
[$name] : false;
384 public function getProperties() {
385 if ( !isset( $this->mProperties
) ) {
386 $this->mProperties
= array();
388 return $this->mProperties
;
393 * Returns the options from its ParserOptions which have been taken
394 * into account to produce this output or false if not available.
395 * @return mixed Array/false
397 public function getUsedOptions() {
398 if ( !isset( $this->mAccessedOptions
) ) {
401 return array_keys( $this->mAccessedOptions
);
405 * Callback passed by the Parser to the ParserOptions to keep track of which options are used.
408 function recordOption( $option ) {
409 $this->mAccessedOptions
[$option] = true;