4 * Output of the PHP parser.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
24 class ParserOutput
extends CacheTime
{
25 var $mText, # The output text
26 $mLanguageLinks, # List of the full text of language links, in the order they appear
27 $mCategories, # Map of category names to sort keys
28 $mTitleText, # title text of the chosen language variant
29 $mLinks = array(), # 2-D map of NS/DBK to ID for the links in the document. ID=zero for broken.
30 $mTemplates = array(), # 2-D map of NS/DBK to ID for the template references. ID=zero for broken.
31 $mTemplateIds = array(), # 2-D map of NS/DBK to rev ID for the template references. ID=zero for broken.
32 $mImages = array(), # DB keys of the images used, in the array key only
33 $mFileSearchOptions = array(), # DB keys of the images used mapped to sha1 and MW timestamp
34 $mExternalLinks = array(), # External link URLs, in the key only
35 $mInterwikiLinks = array(), # 2-D map of prefix/DBK (in keys only) for the inline interwiki links in the document.
36 $mNewSection = false, # Show a new section link?
37 $mHideNewSection = false, # Hide the new section link?
38 $mNoGallery = false, # No gallery on category page? (__NOGALLERY__)
39 $mHeadItems = array(), # Items to put in the <head> section
40 $mModules = array(), # Modules to be loaded by the resource loader
41 $mModuleScripts = array(), # Modules of which only the JS will be loaded by the resource loader
42 $mModuleStyles = array(), # Modules of which only the CSSS will be loaded by the resource loader
43 $mModuleMessages = array(), # Modules of which only the messages will be loaded by the resource loader
44 $mOutputHooks = array(), # Hook tags as per $wgParserOutputHooks
45 $mWarnings = array(), # Warning text to be returned to the user. Wikitext formatted, in the key only
46 $mSections = array(), # Table of contents
47 $mEditSectionTokens = false, # prefix/suffix markers if edit sections were output as tokens
48 $mProperties = array(), # Name/value pairs to be cached in the DB
49 $mTOCHTML = '', # HTML of the TOC
50 $mTimestamp; # Timestamp of the revision
51 private $mIndexPolicy = ''; # 'index' or 'noindex'? Any other value will result in no change.
52 private $mAccessedOptions = array(); # List of ParserOptions (stored in the keys)
53 private $mSecondaryDataUpdates = array(); # List of instances of SecondaryDataObject(), used to cause some information extracted from the page in a custom place.
55 const EDITSECTION_REGEX
= '#<(?:mw:)?editsection page="(.*?)" section="(.*?)"(?:/>|>(.*?)(</(?:mw:)?editsection>))#';
57 function __construct( $text = '', $languageLinks = array(), $categoryLinks = array(),
58 $containsOldMagic = false, $titletext = '' )
61 $this->mLanguageLinks
= $languageLinks;
62 $this->mCategories
= $categoryLinks;
63 $this->mContainsOldMagic
= $containsOldMagic;
64 $this->mTitleText
= $titletext;
68 if ( $this->mEditSectionTokens
) {
69 return preg_replace_callback( ParserOutput
::EDITSECTION_REGEX
,
70 array( &$this, 'replaceEditSectionLinksCallback' ), $this->mText
);
72 return preg_replace( ParserOutput
::EDITSECTION_REGEX
, '', $this->mText
);
76 * callback used by getText to replace editsection tokens
80 function replaceEditSectionLinksCallback( $m ) {
81 global $wgOut, $wgLang;
83 htmlspecialchars_decode($m[1]),
84 htmlspecialchars_decode($m[2]),
85 isset($m[4]) ?
$m[3] : null,
87 $args[0] = Title
::newFromText( $args[0] );
88 if ( !is_object($args[0]) ) {
89 throw new MWException("Bad parser output text.");
91 $args[] = $wgLang->getCode();
92 $skin = $wgOut->getSkin();
93 return call_user_func_array( array( $skin, 'doEditSectionLink' ), $args );
96 function &getLanguageLinks() { return $this->mLanguageLinks
; }
97 function getInterwikiLinks() { return $this->mInterwikiLinks
; }
98 function getCategoryLinks() { return array_keys( $this->mCategories
); }
99 function &getCategories() { return $this->mCategories
; }
100 function getTitleText() { return $this->mTitleText
; }
101 function getSections() { return $this->mSections
; }
102 function getEditSectionTokens() { return $this->mEditSectionTokens
; }
103 function &getLinks() { return $this->mLinks
; }
104 function &getTemplates() { return $this->mTemplates
; }
105 function &getTemplateIds() { return $this->mTemplateIds
; }
106 function &getImages() { return $this->mImages
; }
107 function &getFileSearchOptions() { return $this->mFileSearchOptions
; }
108 function &getExternalLinks() { return $this->mExternalLinks
; }
109 function getNoGallery() { return $this->mNoGallery
; }
110 function getHeadItems() { return $this->mHeadItems
; }
111 function getModules() { return $this->mModules
; }
112 function getModuleScripts() { return $this->mModuleScripts
; }
113 function getModuleStyles() { return $this->mModuleStyles
; }
114 function getModuleMessages() { return $this->mModuleMessages
; }
115 function getOutputHooks() { return (array)$this->mOutputHooks
; }
116 function getWarnings() { return array_keys( $this->mWarnings
); }
117 function getIndexPolicy() { return $this->mIndexPolicy
; }
118 function getTOCHTML() { return $this->mTOCHTML
; }
119 function getTimestamp() { return $this->mTimestamp
; }
121 function setText( $text ) { return wfSetVar( $this->mText
, $text ); }
122 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks
, $ll ); }
123 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategories
, $cl ); }
125 function setTitleText( $t ) { return wfSetVar( $this->mTitleText
, $t ); }
126 function setSections( $toc ) { return wfSetVar( $this->mSections
, $toc ); }
127 function setEditSectionTokens( $t ) { return wfSetVar( $this->mEditSectionTokens
, $t ); }
128 function setIndexPolicy( $policy ) { return wfSetVar( $this->mIndexPolicy
, $policy ); }
129 function setTOCHTML( $tochtml ) { return wfSetVar( $this->mTOCHTML
, $tochtml ); }
130 function setTimestamp( $timestamp ) { return wfSetVar( $this->mTimestamp
, $timestamp ); }
132 function addCategory( $c, $sort ) { $this->mCategories
[$c] = $sort; }
133 function addLanguageLink( $t ) { $this->mLanguageLinks
[] = $t; }
134 function addWarning( $s ) { $this->mWarnings
[$s] = 1; }
136 function addOutputHook( $hook, $data = false ) {
137 $this->mOutputHooks
[] = array( $hook, $data );
140 function setNewSection( $value ) {
141 $this->mNewSection
= (bool)$value;
143 function hideNewSection ( $value ) {
144 $this->mHideNewSection
= (bool)$value;
146 function getHideNewSection () {
147 return (bool)$this->mHideNewSection
;
149 function getNewSection() {
150 return (bool)$this->mNewSection
;
153 function addExternalLink( $url ) {
154 # We don't register links pointing to our own server, unless... :-)
155 global $wgServer, $wgRegisterInternalExternals;
156 if( $wgRegisterInternalExternals or stripos($url,$wgServer.'/')!==0)
157 $this->mExternalLinks
[$url] = 1;
161 * Record a local or interwiki inline link for saving in future link tables.
163 * @param $title Title object
164 * @param $id Mixed: optional known page_id so we can skip the lookup
166 function addLink( $title, $id = null ) {
167 if ( $title->isExternal() ) {
168 // Don't record interwikis in pagelinks
169 $this->addInterwikiLink( $title );
172 $ns = $title->getNamespace();
173 $dbk = $title->getDBkey();
174 if ( $ns == NS_MEDIA
) {
175 // Normalize this pseudo-alias if it makes it down here...
177 } elseif( $ns == NS_SPECIAL
) {
178 // We don't record Special: links currently
179 // It might actually be wise to, but we'd need to do some normalization.
181 } elseif( $dbk === '' ) {
182 // Don't record self links - [[#Foo]]
185 if ( !isset( $this->mLinks
[$ns] ) ) {
186 $this->mLinks
[$ns] = array();
188 if ( is_null( $id ) ) {
189 $id = $title->getArticleID();
191 $this->mLinks
[$ns][$dbk] = $id;
195 * Register a file dependency for this output
196 * @param $name string Title dbKey
197 * @param $timestamp string MW timestamp of file creation (or false if non-existing)
198 * @param $sha1 string base 36 SHA-1 of file (or false if non-existing)
201 function addImage( $name, $timestamp = null, $sha1 = null ) {
202 $this->mImages
[$name] = 1;
203 if ( $timestamp !== null && $sha1 !== null ) {
204 $this->mFileSearchOptions
[$name] = array( 'time' => $timestamp, 'sha1' => $sha1 );
209 * Register a template dependency for this output
210 * @param $title Title
215 function addTemplate( $title, $page_id, $rev_id ) {
216 $ns = $title->getNamespace();
217 $dbk = $title->getDBkey();
218 if ( !isset( $this->mTemplates
[$ns] ) ) {
219 $this->mTemplates
[$ns] = array();
221 $this->mTemplates
[$ns][$dbk] = $page_id;
222 if ( !isset( $this->mTemplateIds
[$ns] ) ) {
223 $this->mTemplateIds
[$ns] = array();
225 $this->mTemplateIds
[$ns][$dbk] = $rev_id; // For versioning
229 * @param $title Title object, must be an interwiki link
230 * @throws MWException if given invalid input
232 function addInterwikiLink( $title ) {
233 $prefix = $title->getInterwiki();
234 if( $prefix == '' ) {
235 throw new MWException( 'Non-interwiki link passed, internal parser error.' );
237 if (!isset($this->mInterwikiLinks
[$prefix])) {
238 $this->mInterwikiLinks
[$prefix] = array();
240 $this->mInterwikiLinks
[$prefix][$title->getDBkey()] = 1;
244 * Add some text to the "<head>".
245 * If $tag is set, the section with that tag will only be included once
248 function addHeadItem( $section, $tag = false ) {
249 if ( $tag !== false ) {
250 $this->mHeadItems
[$tag] = $section;
252 $this->mHeadItems
[] = $section;
256 public function addModules( $modules ) {
257 $this->mModules
= array_merge( $this->mModules
, (array) $modules );
260 public function addModuleScripts( $modules ) {
261 $this->mModuleScripts
= array_merge( $this->mModuleScripts
, (array)$modules );
264 public function addModuleStyles( $modules ) {
265 $this->mModuleStyles
= array_merge( $this->mModuleStyles
, (array)$modules );
268 public function addModuleMessages( $modules ) {
269 $this->mModuleMessages
= array_merge( $this->mModuleMessages
, (array)$modules );
273 * Copy items from the OutputPage object into this one
275 * @param $out OutputPage object
277 public function addOutputPageMetadata( OutputPage
$out ) {
278 $this->addModules( $out->getModules() );
279 $this->addModuleScripts( $out->getModuleScripts() );
280 $this->addModuleStyles( $out->getModuleStyles() );
281 $this->addModuleMessages( $out->getModuleMessages() );
283 $this->mHeadItems
= array_merge( $this->mHeadItems
, $out->getHeadItemsArray() );
287 * Override the title to be used for display
288 * -- this is assumed to have been validated
289 * (check equal normalisation, etc.)
291 * @param $text String: desired title text
293 public function setDisplayTitle( $text ) {
294 $this->setTitleText( $text );
295 $this->setProperty( 'displaytitle', $text );
299 * Get the title to be used for display
303 public function getDisplayTitle() {
304 $t = $this->getTitleText();
312 * Fairly generic flag setter thingy.
314 public function setFlag( $flag ) {
315 $this->mFlags
[$flag] = true;
318 public function getFlag( $flag ) {
319 return isset( $this->mFlags
[$flag] );
323 * Set a property to be cached in the DB
325 public function setProperty( $name, $value ) {
326 $this->mProperties
[$name] = $value;
329 public function getProperty( $name ){
330 return isset( $this->mProperties
[$name] ) ?
$this->mProperties
[$name] : false;
333 public function getProperties() {
334 if ( !isset( $this->mProperties
) ) {
335 $this->mProperties
= array();
337 return $this->mProperties
;
342 * Returns the options from its ParserOptions which have been taken
343 * into account to produce this output or false if not available.
344 * @return mixed Array
346 public function getUsedOptions() {
347 if ( !isset( $this->mAccessedOptions
) ) {
350 return array_keys( $this->mAccessedOptions
);
354 * Callback passed by the Parser to the ParserOptions to keep track of which options are used.
357 function recordOption( $option ) {
358 $this->mAccessedOptions
[$option] = true;
362 * Adds an update job to the output. Any update jobs added to the output will eventually bexecuted in order to
363 * store any secondary information extracted from the page's content.
365 * @param StorageUpdate $update
367 public function addSecondaryDataUpdate( DataUpdate
$update ) {
368 $this->mSecondaryDataUpdates
[] = $update;
372 * Returns any DataUpdate jobs to be executed in order to store secondary information
373 * extracted from the page's content, including a LinksUpdate object for all links stored in
374 * this ParserOutput object.
376 * @param $title Title of the page we're updating. If not given, a title object will be created based on $this->getTitleText()
377 * @param $recursive Boolean: queue jobs for recursive updates?
379 * @return Array. An array of instances of DataUpdate
381 public function getSecondaryDataUpdates( Title
$title = null, $recursive = true ) {
383 $title = Title
::newFromText( $this->getTitleText() );
386 $linksUpdate = new LinksUpdate( $title, $this, $recursive );
388 if ( !$this->mSecondaryDataUpdates
) {
389 return array( $linksUpdate );
391 $updates = array_merge( $this->mSecondaryDataUpdates
, array( $linksUpdate ) );