3 * Output of the PHP parser
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
29 var $mVersion = Parser
::VERSION
, # Compatibility check
30 $mCacheTime = '', # Time when this object was generated, or -1 for uncacheable. Used in ParserCache.
31 $mCacheExpiry = null, # Seconds after which the object should expire, use 0 for uncachable. Used in ParserCache.
32 $mContainsOldMagic; # Boolean variable indicating if the input contained variables like {{CURRENTDAY}}
34 function getCacheTime() { return $this->mCacheTime
; }
36 function containsOldMagic() { return $this->mContainsOldMagic
; }
37 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic
, $com ); }
40 * setCacheTime() sets the timestamp expressing when the page has been rendered.
41 * This doesn not control expiry, see updateCacheExpiry() for that!
45 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime
, $t ); }
48 * Sets the number of seconds after which this object should expire.
49 * This value is used with the ParserCache.
50 * If called with a value greater than the value provided at any previous call,
51 * the new call has no effect. The value returned by getCacheExpiry is smaller
52 * or equal to the smallest number that was provided as an argument to
53 * updateCacheExpiry().
55 * @param $seconds number
57 function updateCacheExpiry( $seconds ) {
58 $seconds = (int)$seconds;
60 if ( $this->mCacheExpiry
=== null ||
$this->mCacheExpiry
> $seconds ) {
61 $this->mCacheExpiry
= $seconds;
64 // hack: set old-style marker for uncacheable entries.
65 if ( $this->mCacheExpiry
!== null && $this->mCacheExpiry
<= 0 ) {
66 $this->mCacheTime
= -1;
71 * Returns the number of seconds after which this object should expire.
72 * This method is used by ParserCache to determine how long the ParserOutput can be cached.
73 * The timestamp of expiry can be calculated by adding getCacheExpiry() to getCacheTime().
74 * The value returned by getCacheExpiry is smaller or equal to the smallest number
75 * that was provided to a call of updateCacheExpiry(), and smaller or equal to the
76 * value of $wgParserCacheExpireTime.
77 * @return int|mixed|null
79 function getCacheExpiry() {
80 global $wgParserCacheExpireTime;
82 if ( $this->mCacheTime
< 0 ) {
84 } // old-style marker for "not cachable"
86 $expire = $this->mCacheExpiry
;
88 if ( $expire === null ) {
89 $expire = $wgParserCacheExpireTime;
91 $expire = min( $expire, $wgParserCacheExpireTime );
94 if( $this->containsOldMagic() ) { //compatibility hack
95 $expire = min( $expire, 3600 ); # 1 hour
99 return 0; // not cachable
108 function isCacheable() {
109 return $this->getCacheExpiry() > 0;
113 * Return true if this cached output object predates the global or
114 * per-article cache invalidation timestamps, or if it comes from
115 * an incompatible older version.
117 * @param $touched String: the affected article's last touched timestamp
120 public function expired( $touched ) {
121 global $wgCacheEpoch;
122 return !$this->isCacheable() ||
// parser says it's uncacheable
123 $this->getCacheTime() < $touched ||
124 $this->getCacheTime() <= $wgCacheEpoch ||
125 $this->getCacheTime() < wfTimestamp( TS_MW
, time() - $this->getCacheExpiry() ) ||
// expiry period has passed
126 !isset( $this->mVersion
) ||
127 version_compare( $this->mVersion
, Parser
::VERSION
, "lt" );
131 class ParserOutput
extends CacheTime
{
132 var $mText, # The output text
133 $mLanguageLinks, # List of the full text of language links, in the order they appear
134 $mCategories, # Map of category names to sort keys
135 $mTitleText, # title text of the chosen language variant
136 $mLinks = array(), # 2-D map of NS/DBK to ID for the links in the document. ID=zero for broken.
137 $mTemplates = array(), # 2-D map of NS/DBK to ID for the template references. ID=zero for broken.
138 $mTemplateIds = array(), # 2-D map of NS/DBK to rev ID for the template references. ID=zero for broken.
139 $mImages = array(), # DB keys of the images used, in the array key only
140 $mFileSearchOptions = array(), # DB keys of the images used mapped to sha1 and MW timestamp
141 $mExternalLinks = array(), # External link URLs, in the key only
142 $mInterwikiLinks = array(), # 2-D map of prefix/DBK (in keys only) for the inline interwiki links in the document.
143 $mNewSection = false, # Show a new section link?
144 $mHideNewSection = false, # Hide the new section link?
145 $mNoGallery = false, # No gallery on category page? (__NOGALLERY__)
146 $mHeadItems = array(), # Items to put in the <head> section
147 $mModules = array(), # Modules to be loaded by the resource loader
148 $mModuleScripts = array(), # Modules of which only the JS will be loaded by the resource loader
149 $mModuleStyles = array(), # Modules of which only the CSSS will be loaded by the resource loader
150 $mModuleMessages = array(), # Modules of which only the messages will be loaded by the resource loader
151 $mOutputHooks = array(), # Hook tags as per $wgParserOutputHooks
152 $mWarnings = array(), # Warning text to be returned to the user. Wikitext formatted, in the key only
153 $mSections = array(), # Table of contents
154 $mEditSectionTokens = false, # prefix/suffix markers if edit sections were output as tokens
155 $mProperties = array(), # Name/value pairs to be cached in the DB
156 $mTOCHTML = '', # HTML of the TOC
157 $mTimestamp; # Timestamp of the revision
160 * 'index' or 'noindex'? Any other value will result in no change.
164 protected $mIndexPolicy = '';
167 * List of ParserOptions (stored in the keys)
171 protected $mAccessedOptions = array();
174 * List of instances of SecondaryDataObject(), used to cause some information extracted from the page in a custom place.
176 * @var array of SecondaryDataObject
178 protected $mSecondaryDataUpdates = array();
180 const EDITSECTION_REGEX
= '#<(?:mw:)?editsection page="(.*?)" section="(.*?)"(?:/>|>(.*?)(</(?:mw:)?editsection>))#';
182 function __construct( $text = '', $languageLinks = array(), $categoryLinks = array(),
183 $containsOldMagic = false, $titletext = '' )
185 $this->mText
= $text;
186 $this->mLanguageLinks
= $languageLinks;
187 $this->mCategories
= $categoryLinks;
188 $this->mContainsOldMagic
= $containsOldMagic;
189 $this->mTitleText
= $titletext;
193 if ( $this->mEditSectionTokens
) {
194 return preg_replace_callback( ParserOutput
::EDITSECTION_REGEX
,
195 array( &$this, 'replaceEditSectionLinksCallback' ), $this->mText
);
197 return preg_replace( ParserOutput
::EDITSECTION_REGEX
, '', $this->mText
);
201 * callback used by getText to replace editsection tokens
205 function replaceEditSectionLinksCallback( $m ) {
206 global $wgOut, $wgLang;
208 htmlspecialchars_decode($m[1]),
209 htmlspecialchars_decode($m[2]),
210 isset($m[4]) ?
$m[3] : null,
212 $args[0] = Title
::newFromText( $args[0] );
213 if ( !is_object($args[0]) ) {
214 throw new MWException("Bad parser output text.");
216 $args[] = $wgLang->getCode();
217 $skin = $wgOut->getSkin();
218 return call_user_func_array( array( $skin, 'doEditSectionLink' ), $args );
221 function &getLanguageLinks() { return $this->mLanguageLinks
; }
222 function getInterwikiLinks() { return $this->mInterwikiLinks
; }
223 function getCategoryLinks() { return array_keys( $this->mCategories
); }
224 function &getCategories() { return $this->mCategories
; }
225 function getTitleText() { return $this->mTitleText
; }
226 function getSections() { return $this->mSections
; }
227 function getEditSectionTokens() { return $this->mEditSectionTokens
; }
228 function &getLinks() { return $this->mLinks
; }
229 function &getTemplates() { return $this->mTemplates
; }
230 function &getTemplateIds() { return $this->mTemplateIds
; }
231 function &getImages() { return $this->mImages
; }
232 function &getFileSearchOptions() { return $this->mFileSearchOptions
; }
233 function &getExternalLinks() { return $this->mExternalLinks
; }
234 function getNoGallery() { return $this->mNoGallery
; }
235 function getHeadItems() { return $this->mHeadItems
; }
236 function getModules() { return $this->mModules
; }
237 function getModuleScripts() { return $this->mModuleScripts
; }
238 function getModuleStyles() { return $this->mModuleStyles
; }
239 function getModuleMessages() { return $this->mModuleMessages
; }
240 function getOutputHooks() { return (array)$this->mOutputHooks
; }
241 function getWarnings() { return array_keys( $this->mWarnings
); }
242 function getIndexPolicy() { return $this->mIndexPolicy
; }
243 function getTOCHTML() { return $this->mTOCHTML
; }
244 function getTimestamp() { return $this->mTimestamp
; }
246 function setText( $text ) { return wfSetVar( $this->mText
, $text ); }
247 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks
, $ll ); }
248 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategories
, $cl ); }
250 function setTitleText( $t ) { return wfSetVar( $this->mTitleText
, $t ); }
251 function setSections( $toc ) { return wfSetVar( $this->mSections
, $toc ); }
252 function setEditSectionTokens( $t ) { return wfSetVar( $this->mEditSectionTokens
, $t ); }
253 function setIndexPolicy( $policy ) { return wfSetVar( $this->mIndexPolicy
, $policy ); }
254 function setTOCHTML( $tochtml ) { return wfSetVar( $this->mTOCHTML
, $tochtml ); }
255 function setTimestamp( $timestamp ) { return wfSetVar( $this->mTimestamp
, $timestamp ); }
257 function addCategory( $c, $sort ) { $this->mCategories
[$c] = $sort; }
258 function addLanguageLink( $t ) { $this->mLanguageLinks
[] = $t; }
259 function addWarning( $s ) { $this->mWarnings
[$s] = 1; }
261 function addOutputHook( $hook, $data = false ) {
262 $this->mOutputHooks
[] = array( $hook, $data );
265 function setNewSection( $value ) {
266 $this->mNewSection
= (bool)$value;
268 function hideNewSection ( $value ) {
269 $this->mHideNewSection
= (bool)$value;
271 function getHideNewSection () {
272 return (bool)$this->mHideNewSection
;
274 function getNewSection() {
275 return (bool)$this->mNewSection
;
278 function addExternalLink( $url ) {
279 # We don't register links pointing to our own server, unless... :-)
280 global $wgServer, $wgRegisterInternalExternals;
281 if( $wgRegisterInternalExternals or stripos($url,$wgServer.'/')!==0)
282 $this->mExternalLinks
[$url] = 1;
286 * Record a local or interwiki inline link for saving in future link tables.
288 * @param $title Title object
289 * @param $id Mixed: optional known page_id so we can skip the lookup
291 function addLink( $title, $id = null ) {
292 if ( $title->isExternal() ) {
293 // Don't record interwikis in pagelinks
294 $this->addInterwikiLink( $title );
297 $ns = $title->getNamespace();
298 $dbk = $title->getDBkey();
299 if ( $ns == NS_MEDIA
) {
300 // Normalize this pseudo-alias if it makes it down here...
302 } elseif( $ns == NS_SPECIAL
) {
303 // We don't record Special: links currently
304 // It might actually be wise to, but we'd need to do some normalization.
306 } elseif( $dbk === '' ) {
307 // Don't record self links - [[#Foo]]
310 if ( !isset( $this->mLinks
[$ns] ) ) {
311 $this->mLinks
[$ns] = array();
313 if ( is_null( $id ) ) {
314 $id = $title->getArticleID();
316 $this->mLinks
[$ns][$dbk] = $id;
320 * Register a file dependency for this output
321 * @param $name string Title dbKey
322 * @param $timestamp string MW timestamp of file creation (or false if non-existing)
323 * @param $sha1 string base 36 SHA-1 of file (or false if non-existing)
326 function addImage( $name, $timestamp = null, $sha1 = null ) {
327 $this->mImages
[$name] = 1;
328 if ( $timestamp !== null && $sha1 !== null ) {
329 $this->mFileSearchOptions
[$name] = array( 'time' => $timestamp, 'sha1' => $sha1 );
334 * Register a template dependency for this output
335 * @param $title Title
340 function addTemplate( $title, $page_id, $rev_id ) {
341 $ns = $title->getNamespace();
342 $dbk = $title->getDBkey();
343 if ( !isset( $this->mTemplates
[$ns] ) ) {
344 $this->mTemplates
[$ns] = array();
346 $this->mTemplates
[$ns][$dbk] = $page_id;
347 if ( !isset( $this->mTemplateIds
[$ns] ) ) {
348 $this->mTemplateIds
[$ns] = array();
350 $this->mTemplateIds
[$ns][$dbk] = $rev_id; // For versioning
354 * @param $title Title object, must be an interwiki link
355 * @throws MWException if given invalid input
357 function addInterwikiLink( $title ) {
358 $prefix = $title->getInterwiki();
359 if( $prefix == '' ) {
360 throw new MWException( 'Non-interwiki link passed, internal parser error.' );
362 if (!isset($this->mInterwikiLinks
[$prefix])) {
363 $this->mInterwikiLinks
[$prefix] = array();
365 $this->mInterwikiLinks
[$prefix][$title->getDBkey()] = 1;
369 * Add some text to the <head>.
370 * If $tag is set, the section with that tag will only be included once
373 function addHeadItem( $section, $tag = false ) {
374 if ( $tag !== false ) {
375 $this->mHeadItems
[$tag] = $section;
377 $this->mHeadItems
[] = $section;
381 public function addModules( $modules ) {
382 $this->mModules
= array_merge( $this->mModules
, (array) $modules );
385 public function addModuleScripts( $modules ) {
386 $this->mModuleScripts
= array_merge( $this->mModuleScripts
, (array)$modules );
389 public function addModuleStyles( $modules ) {
390 $this->mModuleStyles
= array_merge( $this->mModuleStyles
, (array)$modules );
393 public function addModuleMessages( $modules ) {
394 $this->mModuleMessages
= array_merge( $this->mModuleMessages
, (array)$modules );
398 * Copy items from the OutputPage object into this one
400 * @param $out OutputPage object
402 public function addOutputPageMetadata( OutputPage
$out ) {
403 $this->addModules( $out->getModules() );
404 $this->addModuleScripts( $out->getModuleScripts() );
405 $this->addModuleStyles( $out->getModuleStyles() );
406 $this->addModuleMessages( $out->getModuleMessages() );
408 $this->mHeadItems
= array_merge( $this->mHeadItems
, $out->getHeadItemsArray() );
412 * Override the title to be used for display
413 * -- this is assumed to have been validated
414 * (check equal normalisation, etc.)
416 * @param $text String: desired title text
418 public function setDisplayTitle( $text ) {
419 $this->setTitleText( $text );
420 $this->setProperty( 'displaytitle', $text );
424 * Get the title to be used for display
428 public function getDisplayTitle() {
429 $t = $this->getTitleText();
437 * Fairly generic flag setter thingy.
439 public function setFlag( $flag ) {
440 $this->mFlags
[$flag] = true;
443 public function getFlag( $flag ) {
444 return isset( $this->mFlags
[$flag] );
448 * Set a property to be cached in the DB
450 public function setProperty( $name, $value ) {
451 $this->mProperties
[$name] = $value;
454 public function getProperty( $name ){
455 return isset( $this->mProperties
[$name] ) ?
$this->mProperties
[$name] : false;
458 public function getProperties() {
459 if ( !isset( $this->mProperties
) ) {
460 $this->mProperties
= array();
462 return $this->mProperties
;
467 * Returns the options from its ParserOptions which have been taken
468 * into account to produce this output or false if not available.
469 * @return mixed Array
471 public function getUsedOptions() {
472 if ( !isset( $this->mAccessedOptions
) ) {
475 return array_keys( $this->mAccessedOptions
);
479 * Callback passed by the Parser to the ParserOptions to keep track of which options are used.
482 function recordOption( $option ) {
483 $this->mAccessedOptions
[$option] = true;
487 * Adds an update job to the output. Any update jobs added to the output will eventually bexecuted in order to
488 * store any secondary information extracted from the page's content.
492 * @param SecondaryDataUpdate $update
494 public function addSecondaryDataUpdate( SecondaryDataUpdate
$update ) {
495 $this->mSecondaryDataUpdates
[] = $update;
499 * Returns any SecondaryDataUpdate jobs to be executed in order to store secondary information
500 * extracted from the page's content, includingt a LinksUpdate object for all links stopred in
501 * this ParserOutput object.
505 * @param $title Title of the page we're updating. If not given, a title object will be created based on $this->getTitleText()
506 * @param $recursive Boolean: queue jobs for recursive updates?
508 * @return array an array of instances of SecondaryDataUpdate
510 public function getSecondaryDataUpdates( Title
$title = null, $recursive = true ) {
511 if ( is_null( $title ) ) {
512 $title = Title
::newFromText( $this->getTitleText() );
516 $this->mSecondaryDataUpdates
,
517 array( new LinksUpdate( $title, $this, $recursive ) )