32153e653a30258722a4ec6e10e8214f089b58e6
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
{
104 var $mText, # The output text
105 $mLanguageLinks, # List of the full text of language links, in the order they appear
106 $mCategories, # Map of category names to sort keys
107 $mTitleText, # title text of the chosen language variant
108 $mLinks = array(), # 2-D map of NS/DBK to ID for the links in the document. ID=zero for broken.
109 $mTemplates = array(), # 2-D map of NS/DBK to ID for the template references. ID=zero for broken.
110 $mTemplateIds = array(), # 2-D map of NS/DBK to rev ID for the template references. ID=zero for broken.
111 $mImages = array(), # DB keys of the images used, in the array key only
112 $mExternalLinks = array(), # External link URLs, in the key only
113 $mInterwikiLinks = array(), # 2-D map of prefix/DBK (in keys only) for the inline interwiki links in the document.
114 $mNewSection = false, # Show a new section link?
115 $mHideNewSection = false, # Hide the new section link?
116 $mNoGallery = false, # No gallery on category page? (__NOGALLERY__)
117 $mHeadItems = array(), # Items to put in the <head> section
118 $mModules = array(), # Modules to be loaded by the resource loader
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 $mEditSectionTokens = false, # prefix/suffix markers if edit sections were output as tokens
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.
126 private $mAccessedOptions = null; # List of ParserOptions (stored in the keys)
128 function __construct( $text = '', $languageLinks = array(), $categoryLinks = array(),
129 $containsOldMagic = false, $titletext = '' )
131 $this->mText
= $text;
132 $this->mLanguageLinks
= $languageLinks;
133 $this->mCategories
= $categoryLinks;
134 $this->mContainsOldMagic
= $containsOldMagic;
135 $this->mTitleText
= $titletext;
139 if ( $this->mEditSectionTokens
) {
140 $editSectionTokens = $this->mEditSectionTokens
;
141 return preg_replace_callback( "#{$editSectionTokens[0]}(.*?){$editSectionTokens[1]}#", array( &$this, 'replaceEditSectionLinksCallback' ), $this->mText
);
147 * callback used by getText to replace editsection tokens
150 function replaceEditSectionLinksCallback( $m ) {
151 global $wgUser, $wgLang;
152 $args = array_map('urldecode', explode('|', $m[1], 3));
153 $args[0] = Title
::newFromText( $args[0] );
154 if ( !is_object($args[0]) ) {
155 throw new MWException("Bad parser output text.");
157 $args[] = $wgLang->getCode();
158 $skin = $wgUser->getSkin();
159 return call_user_func_array( array( $skin, 'doEditSectionLink' ), $args );
162 function &getLanguageLinks() { return $this->mLanguageLinks
; }
163 function getInterwikiLinks() { return $this->mInterwikiLinks
; }
164 function getCategoryLinks() { return array_keys( $this->mCategories
); }
165 function &getCategories() { return $this->mCategories
; }
166 function getTitleText() { return $this->mTitleText
; }
167 function getSections() { return $this->mSections
; }
168 function getEditSectionTokens() { return $this->mEditSectionTokens
; }
169 function &getLinks() { return $this->mLinks
; }
170 function &getTemplates() { return $this->mTemplates
; }
171 function &getImages() { return $this->mImages
; }
172 function &getExternalLinks() { return $this->mExternalLinks
; }
173 function getNoGallery() { return $this->mNoGallery
; }
174 function getHeadItems() { return $this->mHeadItems
; }
175 function getModules() { return $this->mModules
; }
176 function getOutputHooks() { return (array)$this->mOutputHooks
; }
177 function getWarnings() { return array_keys( $this->mWarnings
); }
178 function getIndexPolicy() { return $this->mIndexPolicy
; }
179 function getTOCHTML() { return $this->mTOCHTML
; }
181 function setText( $text ) { return wfSetVar( $this->mText
, $text ); }
182 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks
, $ll ); }
183 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategories
, $cl ); }
185 function setTitleText( $t ) { return wfSetVar( $this->mTitleText
, $t ); }
186 function setSections( $toc ) { return wfSetVar( $this->mSections
, $toc ); }
187 function setEditSectionTokens( $p, $s ) { return wfSetVar( $this->mEditSectionTokens
, array( $p, $s ) ); }
188 function setIndexPolicy( $policy ) { return wfSetVar( $this->mIndexPolicy
, $policy ); }
189 function setTOCHTML( $tochtml ) { return wfSetVar( $this->mTOCHTML
, $tochtml ); }
191 function addCategory( $c, $sort ) { $this->mCategories
[$c] = $sort; }
192 function addLanguageLink( $t ) { $this->mLanguageLinks
[] = $t; }
193 function addWarning( $s ) { $this->mWarnings
[$s] = 1; }
195 function addOutputHook( $hook, $data = false ) {
196 $this->mOutputHooks
[] = array( $hook, $data );
199 function setNewSection( $value ) {
200 $this->mNewSection
= (bool)$value;
202 function hideNewSection ( $value ) {
203 $this->mHideNewSection
= (bool)$value;
205 function getHideNewSection () {
206 return (bool)$this->mHideNewSection
;
208 function getNewSection() {
209 return (bool)$this->mNewSection
;
212 function addExternalLink( $url ) {
213 # We don't register links pointing to our own server, unless... :-)
214 global $wgServer, $wgRegisterInternalExternals;
215 if( $wgRegisterInternalExternals or stripos($url,$wgServer.'/')!==0)
216 $this->mExternalLinks
[$url] = 1;
220 * Record a local or interwiki inline link for saving in future link tables.
222 * @param $title Title object
223 * @param $id Mixed: optional known page_id so we can skip the lookup
225 function addLink( $title, $id = null ) {
226 if ( $title->isExternal() ) {
227 // Don't record interwikis in pagelinks
228 $this->addInterwikiLink( $title );
231 $ns = $title->getNamespace();
232 $dbk = $title->getDBkey();
233 if ( $ns == NS_MEDIA
) {
234 // Normalize this pseudo-alias if it makes it down here...
236 } elseif( $ns == NS_SPECIAL
) {
237 // We don't record Special: links currently
238 // It might actually be wise to, but we'd need to do some normalization.
240 } elseif( $dbk === '' ) {
241 // Don't record self links - [[#Foo]]
244 if ( !isset( $this->mLinks
[$ns] ) ) {
245 $this->mLinks
[$ns] = array();
247 if ( is_null( $id ) ) {
248 $id = $title->getArticleID();
250 $this->mLinks
[$ns][$dbk] = $id;
253 function addImage( $name ) {
254 $this->mImages
[$name] = 1;
257 function addTemplate( $title, $page_id, $rev_id ) {
258 $ns = $title->getNamespace();
259 $dbk = $title->getDBkey();
260 if ( !isset( $this->mTemplates
[$ns] ) ) {
261 $this->mTemplates
[$ns] = array();
263 $this->mTemplates
[$ns][$dbk] = $page_id;
264 if ( !isset( $this->mTemplateIds
[$ns] ) ) {
265 $this->mTemplateIds
[$ns] = array();
267 $this->mTemplateIds
[$ns][$dbk] = $rev_id; // For versioning
271 * @param $title Title object, must be an interwiki link
272 * @throws MWException if given invalid input
274 function addInterwikiLink( $title ) {
275 $prefix = $title->getInterwiki();
276 if( $prefix == '' ) {
277 throw new MWException( 'Non-interwiki link passed, internal parser error.' );
279 if (!isset($this->mInterwikiLinks
[$prefix])) {
280 $this->mInterwikiLinks
[$prefix] = array();
282 $this->mInterwikiLinks
[$prefix][$title->getDBkey()] = 1;
286 * Add some text to the <head>.
287 * If $tag is set, the section with that tag will only be included once
290 function addHeadItem( $section, $tag = false ) {
291 if ( $tag !== false ) {
292 $this->mHeadItems
[$tag] = $section;
294 $this->mHeadItems
[] = $section;
298 function addModules( $modules ) {
299 $this->mModules
= array_merge( $this->mModules
, (array) $modules );
303 * Override the title to be used for display
304 * -- this is assumed to have been validated
305 * (check equal normalisation, etc.)
307 * @param $text String: desired title text
309 public function setDisplayTitle( $text ) {
310 $this->setTitleText( $text );
311 $this->setProperty( 'displaytitle', $text );
315 * Get the title to be used for display
319 public function getDisplayTitle() {
320 $t = $this->getTitleText();
328 * Fairly generic flag setter thingy.
330 public function setFlag( $flag ) {
331 $this->mFlags
[$flag] = true;
334 public function getFlag( $flag ) {
335 return isset( $this->mFlags
[$flag] );
339 * Set a property to be cached in the DB
341 public function setProperty( $name, $value ) {
342 $this->mProperties
[$name] = $value;
345 public function getProperty( $name ){
346 return isset( $this->mProperties
[$name] ) ?
$this->mProperties
[$name] : false;
349 public function getProperties() {
350 if ( !isset( $this->mProperties
) ) {
351 $this->mProperties
= array();
353 return $this->mProperties
;
358 * Returns the options from its ParserOptions which have been taken
359 * into account to produce this output or false if not available.
360 * @return mixed Array/false
362 public function getUsedOptions() {
363 if ( !isset( $this->mAccessedOptions
) ) {
366 return array_keys( $this->mAccessedOptions
);
370 * Callback passed by the Parser to the ParserOptions to keep track of which options are used.
373 function recordOption( $option ) {
374 $this->mAccessedOptions
[$option] = true;