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!
31 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime
, $t ); }
34 * Sets the number of seconds after which this object should expire.
35 * This value is used with the ParserCache.
36 * If called with a value greater than the value provided at any previous call,
37 * the new call has no effect. The value returned by getCacheExpiry is smaller
38 * or equal to the smallest number that was provided as an argument to
39 * updateCacheExpiry().
41 * @param $seconds number
43 function updateCacheExpiry( $seconds ) {
44 $seconds = (int)$seconds;
46 if ( $this->mCacheExpiry
=== null ||
$this->mCacheExpiry
> $seconds ) {
47 $this->mCacheExpiry
= $seconds;
50 // hack: set old-style marker for uncacheable entries.
51 if ( $this->mCacheExpiry
!== null && $this->mCacheExpiry
<= 0 ) {
52 $this->mCacheTime
= -1;
57 * Returns the number of seconds after which this object should expire.
58 * This method is used by ParserCache to determine how long the ParserOutput can be cached.
59 * The timestamp of expiry can be calculated by adding getCacheExpiry() to getCacheTime().
60 * The value returned by getCacheExpiry is smaller or equal to the smallest number
61 * that was provided to a call of updateCacheExpiry(), and smaller or equal to the
62 * value of $wgParserCacheExpireTime.
64 function getCacheExpiry() {
65 global $wgParserCacheExpireTime;
67 if ( $this->mCacheTime
< 0 ) {
69 } // old-style marker for "not cachable"
71 $expire = $this->mCacheExpiry
;
73 if ( $expire === null ) {
74 $expire = $wgParserCacheExpireTime;
76 $expire = min( $expire, $wgParserCacheExpireTime );
79 if( $this->containsOldMagic() ) { //compatibility hack
80 $expire = min( $expire, 3600 ); # 1 hour
84 return 0; // not cachable
93 function isCacheable() {
94 return $this->getCacheExpiry() > 0;
98 * Return true if this cached output object predates the global or
99 * per-article cache invalidation timestamps, or if it comes from
100 * an incompatible older version.
102 * @param $touched String: the affected article's last touched timestamp
105 public function expired( $touched ) {
106 global $wgCacheEpoch;
107 return !$this->isCacheable() ||
// parser says it's uncacheable
108 $this->getCacheTime() < $touched ||
109 $this->getCacheTime() <= $wgCacheEpoch ||
110 $this->getCacheTime() < wfTimestamp( TS_MW
, time() - $this->getCacheExpiry() ) ||
// expiry period has passed
111 !isset( $this->mVersion
) ||
112 version_compare( $this->mVersion
, Parser
::VERSION
, "lt" );
116 class ParserOutput
extends CacheTime
{
117 var $mText, # The output text
118 $mLanguageLinks, # List of the full text of language links, in the order they appear
119 $mCategories, # Map of category names to sort keys
120 $mTitleText, # title text of the chosen language variant
121 $mLinks = array(), # 2-D map of NS/DBK to ID for the links in the document. ID=zero for broken.
122 $mTemplates = array(), # 2-D map of NS/DBK to ID for the template references. ID=zero for broken.
123 $mTemplateIds = array(), # 2-D map of NS/DBK to rev ID for the template references. ID=zero for broken.
124 $mImages = array(), # DB keys of the images used, in the array key only
125 $mFileSearchOptions = array(), # DB keys of the images used mapped to sha1 and MW timestamp
126 $mExternalLinks = array(), # External link URLs, in the key only
127 $mInterwikiLinks = array(), # 2-D map of prefix/DBK (in keys only) for the inline interwiki links in the document.
128 $mNewSection = false, # Show a new section link?
129 $mHideNewSection = false, # Hide the new section link?
130 $mNoGallery = false, # No gallery on category page? (__NOGALLERY__)
131 $mHeadItems = array(), # Items to put in the <head> section
132 $mModules = array(), # Modules to be loaded by the resource loader
133 $mModuleScripts = array(), # Modules of which only the JS will be loaded by the resource loader
134 $mModuleStyles = array(), # Modules of which only the CSSS will be loaded by the resource loader
135 $mModuleMessages = array(), # Modules of which only the messages will be loaded by the resource loader
136 $mOutputHooks = array(), # Hook tags as per $wgParserOutputHooks
137 $mWarnings = array(), # Warning text to be returned to the user. Wikitext formatted, in the key only
138 $mSections = array(), # Table of contents
139 $mEditSectionTokens = false, # prefix/suffix markers if edit sections were output as tokens
140 $mProperties = array(), # Name/value pairs to be cached in the DB
141 $mTOCHTML = ''; # HTML of the TOC
142 private $mIndexPolicy = ''; # 'index' or 'noindex'? Any other value will result in no change.
143 private $mAccessedOptions = array(); # List of ParserOptions (stored in the keys)
145 const EDITSECTION_REGEX
= '#<(?:mw:)?editsection page="(.*?)" section="(.*?)"(?:/>|>(.*?)(</(?:mw:)?editsection>))#';
147 function __construct( $text = '', $languageLinks = array(), $categoryLinks = array(),
148 $containsOldMagic = false, $titletext = '' )
150 $this->mText
= $text;
151 $this->mLanguageLinks
= $languageLinks;
152 $this->mCategories
= $categoryLinks;
153 $this->mContainsOldMagic
= $containsOldMagic;
154 $this->mTitleText
= $titletext;
158 if ( $this->mEditSectionTokens
) {
159 return preg_replace_callback( ParserOutput
::EDITSECTION_REGEX
,
160 array( &$this, 'replaceEditSectionLinksCallback' ), $this->mText
);
162 return preg_replace( ParserOutput
::EDITSECTION_REGEX
, '', $this->mText
);
168 * callback used by getText to replace editsection tokens
171 function replaceEditSectionLinksCallback( $m ) {
172 global $wgOut, $wgLang;
174 htmlspecialchars_decode($m[1]),
175 htmlspecialchars_decode($m[2]),
176 isset($m[4]) ?
$m[3] : null,
178 $args[0] = Title
::newFromText( $args[0] );
179 if ( !is_object($args[0]) ) {
180 throw new MWException("Bad parser output text.");
182 $args[] = $wgLang->getCode();
183 $skin = $wgOut->getSkin();
184 return call_user_func_array( array( $skin, 'doEditSectionLink' ), $args );
187 function &getLanguageLinks() { return $this->mLanguageLinks
; }
188 function getInterwikiLinks() { return $this->mInterwikiLinks
; }
189 function getCategoryLinks() { return array_keys( $this->mCategories
); }
190 function &getCategories() { return $this->mCategories
; }
191 function getTitleText() { return $this->mTitleText
; }
192 function getSections() { return $this->mSections
; }
193 function getEditSectionTokens() { return $this->mEditSectionTokens
; }
194 function &getLinks() { return $this->mLinks
; }
195 function &getTemplates() { return $this->mTemplates
; }
196 function &getTemplateIds() { return $this->mTemplateIds
; }
197 function &getImages() { return $this->mImages
; }
198 function &getFileSearchOptions() { return $this->mFileSearchOptions
; }
199 function &getExternalLinks() { return $this->mExternalLinks
; }
200 function getNoGallery() { return $this->mNoGallery
; }
201 function getHeadItems() { return $this->mHeadItems
; }
202 function getModules() { return $this->mModules
; }
203 function getModuleScripts() { return $this->mModuleScripts
; }
204 function getModuleStyles() { return $this->mModuleStyles
; }
205 function getModuleMessages() { return $this->mModuleMessages
; }
206 function getOutputHooks() { return (array)$this->mOutputHooks
; }
207 function getWarnings() { return array_keys( $this->mWarnings
); }
208 function getIndexPolicy() { return $this->mIndexPolicy
; }
209 function getTOCHTML() { return $this->mTOCHTML
; }
211 function setText( $text ) { return wfSetVar( $this->mText
, $text ); }
212 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks
, $ll ); }
213 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategories
, $cl ); }
215 function setTitleText( $t ) { return wfSetVar( $this->mTitleText
, $t ); }
216 function setSections( $toc ) { return wfSetVar( $this->mSections
, $toc ); }
217 function setEditSectionTokens( $t ) { return wfSetVar( $this->mEditSectionTokens
, $t ); }
218 function setIndexPolicy( $policy ) { return wfSetVar( $this->mIndexPolicy
, $policy ); }
219 function setTOCHTML( $tochtml ) { return wfSetVar( $this->mTOCHTML
, $tochtml ); }
221 function addCategory( $c, $sort ) { $this->mCategories
[$c] = $sort; }
222 function addLanguageLink( $t ) { $this->mLanguageLinks
[] = $t; }
223 function addWarning( $s ) { $this->mWarnings
[$s] = 1; }
225 function addOutputHook( $hook, $data = false ) {
226 $this->mOutputHooks
[] = array( $hook, $data );
229 function setNewSection( $value ) {
230 $this->mNewSection
= (bool)$value;
232 function hideNewSection ( $value ) {
233 $this->mHideNewSection
= (bool)$value;
235 function getHideNewSection () {
236 return (bool)$this->mHideNewSection
;
238 function getNewSection() {
239 return (bool)$this->mNewSection
;
242 function addExternalLink( $url ) {
243 # We don't register links pointing to our own server, unless... :-)
244 global $wgServer, $wgRegisterInternalExternals;
245 if( $wgRegisterInternalExternals or stripos($url,$wgServer.'/')!==0)
246 $this->mExternalLinks
[$url] = 1;
250 * Record a local or interwiki inline link for saving in future link tables.
252 * @param $title Title object
253 * @param $id Mixed: optional known page_id so we can skip the lookup
255 function addLink( $title, $id = null ) {
256 if ( $title->isExternal() ) {
257 // Don't record interwikis in pagelinks
258 $this->addInterwikiLink( $title );
261 $ns = $title->getNamespace();
262 $dbk = $title->getDBkey();
263 if ( $ns == NS_MEDIA
) {
264 // Normalize this pseudo-alias if it makes it down here...
266 } elseif( $ns == NS_SPECIAL
) {
267 // We don't record Special: links currently
268 // It might actually be wise to, but we'd need to do some normalization.
270 } elseif( $dbk === '' ) {
271 // Don't record self links - [[#Foo]]
274 if ( !isset( $this->mLinks
[$ns] ) ) {
275 $this->mLinks
[$ns] = array();
277 if ( is_null( $id ) ) {
278 $id = $title->getArticleID();
280 $this->mLinks
[$ns][$dbk] = $id;
284 * Register a file dependency for this output
285 * @param $name string Title dbKey
286 * @param $timestamp string MW timestamp of file creation (or false if non-existing)
287 * @param $sha string base 36 SHA-1 of file (or false if non-existing)
290 function addImage( $name, $timestamp = null, $sha1 = null ) {
291 $this->mImages
[$name] = 1;
292 if ( $timestamp !== null && $sha1 !== null ) {
293 $this->mFileSearchOptions
[$name] = array( 'time' => $timestamp, 'sha1' => $sha1 );
298 * Register a template dependency for this output
299 * @param $title Title
304 function addTemplate( $title, $page_id, $rev_id ) {
305 $ns = $title->getNamespace();
306 $dbk = $title->getDBkey();
307 if ( !isset( $this->mTemplates
[$ns] ) ) {
308 $this->mTemplates
[$ns] = array();
310 $this->mTemplates
[$ns][$dbk] = $page_id;
311 if ( !isset( $this->mTemplateIds
[$ns] ) ) {
312 $this->mTemplateIds
[$ns] = array();
314 $this->mTemplateIds
[$ns][$dbk] = $rev_id; // For versioning
318 * @param $title Title object, must be an interwiki link
319 * @throws MWException if given invalid input
321 function addInterwikiLink( $title ) {
322 $prefix = $title->getInterwiki();
323 if( $prefix == '' ) {
324 throw new MWException( 'Non-interwiki link passed, internal parser error.' );
326 if (!isset($this->mInterwikiLinks
[$prefix])) {
327 $this->mInterwikiLinks
[$prefix] = array();
329 $this->mInterwikiLinks
[$prefix][$title->getDBkey()] = 1;
333 * Add some text to the <head>.
334 * If $tag is set, the section with that tag will only be included once
337 function addHeadItem( $section, $tag = false ) {
338 if ( $tag !== false ) {
339 $this->mHeadItems
[$tag] = $section;
341 $this->mHeadItems
[] = $section;
345 public function addModules( $modules ) {
346 $this->mModules
= array_merge( $this->mModules
, (array) $modules );
349 public function addModuleScripts( $modules ) {
350 $this->mModuleScripts
= array_merge( $this->mModuleScripts
, (array)$modules );
353 public function addModuleStyles( $modules ) {
354 $this->mModuleStyles
= array_merge( $this->mModuleStyles
, (array)$modules );
357 public function addModuleMessages( $modules ) {
358 $this->mModuleMessages
= array_merge( $this->mModuleMessages
, (array)$modules );
362 * Copy items from the OutputPage object into this one
364 * @param $out OutputPage object
366 public function addOutputPageMetadata( OutputPage
$out ) {
367 $this->addModules( $out->getModules() );
368 $this->addModuleScripts( $out->getModuleScripts() );
369 $this->addModuleStyles( $out->getModuleStyles() );
370 $this->addModuleMessages( $out->getModuleMessages() );
372 $this->mHeadItems
= array_merge( $this->mHeadItems
, $out->getHeadItemsArray() );
376 * Override the title to be used for display
377 * -- this is assumed to have been validated
378 * (check equal normalisation, etc.)
380 * @param $text String: desired title text
382 public function setDisplayTitle( $text ) {
383 $this->setTitleText( $text );
384 $this->setProperty( 'displaytitle', $text );
388 * Get the title to be used for display
392 public function getDisplayTitle() {
393 $t = $this->getTitleText();
401 * Fairly generic flag setter thingy.
403 public function setFlag( $flag ) {
404 $this->mFlags
[$flag] = true;
407 public function getFlag( $flag ) {
408 return isset( $this->mFlags
[$flag] );
412 * Set a property to be cached in the DB
414 public function setProperty( $name, $value ) {
415 $this->mProperties
[$name] = $value;
418 public function getProperty( $name ){
419 return isset( $this->mProperties
[$name] ) ?
$this->mProperties
[$name] : false;
422 public function getProperties() {
423 if ( !isset( $this->mProperties
) ) {
424 $this->mProperties
= array();
426 return $this->mProperties
;
431 * Returns the options from its ParserOptions which have been taken
432 * into account to produce this output or false if not available.
433 * @return mixed Array
435 public function getUsedOptions() {
436 if ( !isset( $this->mAccessedOptions
) ) {
439 return array_keys( $this->mAccessedOptions
);
443 * Callback passed by the Parser to the ParserOptions to keep track of which options are used.
446 function recordOption( $option ) {
447 $this->mAccessedOptions
[$option] = true;