* (bug 14473) Add iwlinks table to track inline interwiki link usage
[lhc/web/wiklou.git] / includes / parser / ParserOutput.php
1 <?php
2 /**
3 * @todo document
4 * @ingroup Parser
5 */
6 class ParserOutput
7 {
8 var $mText, # The output text
9 $mLanguageLinks, # List of the full text of language links, in the order they appear
10 $mCategories, # Map of category names to sort keys
11 $mContainsOldMagic, # Boolean variable indicating if the input contained variables like {{CURRENTDAY}}
12 $mTitleText, # title text of the chosen language variant
13 $mCacheTime = '', # Time when this object was generated, or -1 for uncacheable. Used in ParserCache.
14 $mVersion = Parser::VERSION, # Compatibility check
15 $mLinks = array(), # 2-D map of NS/DBK to ID for the links in the document. ID=zero for broken.
16 $mTemplates = array(), # 2-D map of NS/DBK to ID for the template references. ID=zero for broken.
17 $mTemplateIds = array(), # 2-D map of NS/DBK to rev ID for the template references. ID=zero for broken.
18 $mImages = array(), # DB keys of the images used, in the array key only
19 $mExternalLinks = array(), # External link URLs, in the key only
20 $mInterwikiLinks = array(), # 2-D map of prefix/DBK (in keys only) for the inline interwiki links in the document.
21 $mNewSection = false, # Show a new section link?
22 $mHideNewSection = false, # Hide the new section link?
23 $mNoGallery = false, # No gallery on category page? (__NOGALLERY__)
24 $mHeadItems = array(), # Items to put in the <head> section
25 $mOutputHooks = array(), # Hook tags as per $wgParserOutputHooks
26 $mWarnings = array(), # Warning text to be returned to the user. Wikitext formatted, in the key only
27 $mSections = array(), # Table of contents
28 $mProperties = array(), # Name/value pairs to be cached in the DB
29 $mTOCHTML = ''; # HTML of the TOC
30 private $mIndexPolicy = ''; # 'index' or 'noindex'? Any other value will result in no change.
31
32 function ParserOutput( $text = '', $languageLinks = array(), $categoryLinks = array(),
33 $containsOldMagic = false, $titletext = '' )
34 {
35 $this->mText = $text;
36 $this->mLanguageLinks = $languageLinks;
37 $this->mCategories = $categoryLinks;
38 $this->mContainsOldMagic = $containsOldMagic;
39 $this->mTitleText = $titletext;
40 }
41
42 function getText() { return $this->mText; }
43 function &getLanguageLinks() { return $this->mLanguageLinks; }
44 function getInterwikiLinks() { return $this->mInterwikiLinks; }
45 function getCategoryLinks() { return array_keys( $this->mCategories ); }
46 function &getCategories() { return $this->mCategories; }
47 function getCacheTime() { return $this->mCacheTime; }
48 function getTitleText() { return $this->mTitleText; }
49 function getSections() { return $this->mSections; }
50 function &getLinks() { return $this->mLinks; }
51 function &getTemplates() { return $this->mTemplates; }
52 function &getImages() { return $this->mImages; }
53 function &getExternalLinks() { return $this->mExternalLinks; }
54 function getNoGallery() { return $this->mNoGallery; }
55 function getHeadItems() { return $this->mHeadItems; }
56 function getSubtitle() { return $this->mSubtitle; }
57 function getOutputHooks() { return (array)$this->mOutputHooks; }
58 function getWarnings() { return array_keys( $this->mWarnings ); }
59 function getIndexPolicy() { return $this->mIndexPolicy; }
60 function getTOCHTML() { return $this->mTOCHTML; }
61
62 function containsOldMagic() { return $this->mContainsOldMagic; }
63 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
64 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
65 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategories, $cl ); }
66 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
67 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
68 function setTitleText( $t ) { return wfSetVar( $this->mTitleText, $t ); }
69 function setSections( $toc ) { return wfSetVar( $this->mSections, $toc ); }
70 function setIndexPolicy( $policy ) { return wfSetVar( $this->mIndexPolicy, $policy ); }
71 function setTOCHTML( $tochtml ) { return wfSetVar( $this->mTOCHTML, $tochtml ); }
72
73 function addCategory( $c, $sort ) { $this->mCategories[$c] = $sort; }
74 function addLanguageLink( $t ) { $this->mLanguageLinks[] = $t; }
75 function addWarning( $s ) { $this->mWarnings[$s] = 1; }
76
77 function addOutputHook( $hook, $data = false ) {
78 $this->mOutputHooks[] = array( $hook, $data );
79 }
80
81 function setNewSection( $value ) {
82 $this->mNewSection = (bool)$value;
83 }
84 function hideNewSection ( $value ) {
85 $this->mHideNewSection = (bool)$value;
86 }
87 function getHideNewSection () {
88 return (bool)$this->mHideNewSection;
89 }
90 function getNewSection() {
91 return (bool)$this->mNewSection;
92 }
93
94 function addExternalLink( $url ) {
95 # We don't register links pointing to our own server, unless... :-)
96 global $wgServer, $wgRegisterInternalExternals;
97 if( $wgRegisterInternalExternals or stripos($url,$wgServer.'/')!==0)
98 $this->mExternalLinks[$url] = 1;
99 }
100
101 /**
102 * Record a local or interwiki inline link for saving in future link tables.
103 *
104 * @param Title $title
105 * @param mixed $id optional known page_id so we can skip the lookup
106 */
107 function addLink( $title, $id = null ) {
108 wfDebug(__METHOD__ . " got: " . $title->getPrefixedText() . "\n");
109 if ( $title->isExternal() ) {
110 // Don't record interwikis in pagelinks
111 $this->addInterwikiLink( $title );
112 return;
113 }
114 $ns = $title->getNamespace();
115 $dbk = $title->getDBkey();
116 if ( $ns == NS_MEDIA ) {
117 // Normalize this pseudo-alias if it makes it down here...
118 $ns = NS_FILE;
119 } elseif( $ns == NS_SPECIAL ) {
120 // We don't record Special: links currently
121 // It might actually be wise to, but we'd need to do some normalization.
122 return;
123 } elseif( $dbk === '' ) {
124 // Don't record self links - [[#Foo]]
125 return;
126 }
127 if ( !isset( $this->mLinks[$ns] ) ) {
128 $this->mLinks[$ns] = array();
129 }
130 if ( is_null( $id ) ) {
131 $id = $title->getArticleID();
132 }
133 $this->mLinks[$ns][$dbk] = $id;
134 }
135
136 function addImage( $name ) {
137 $this->mImages[$name] = 1;
138 }
139
140 function addTemplate( $title, $page_id, $rev_id ) {
141 $ns = $title->getNamespace();
142 $dbk = $title->getDBkey();
143 if ( !isset( $this->mTemplates[$ns] ) ) {
144 $this->mTemplates[$ns] = array();
145 }
146 $this->mTemplates[$ns][$dbk] = $page_id;
147 if ( !isset( $this->mTemplateIds[$ns] ) ) {
148 $this->mTemplateIds[$ns] = array();
149 }
150 $this->mTemplateIds[$ns][$dbk] = $rev_id; // For versioning
151 }
152
153 /**
154 * @param Title $title object, must be an interwiki link
155 * @throws MWException if given invalid input
156 */
157 function addInterwikiLink( $title ) {
158 $prefix = $title->getInterwiki();
159 if( $prefix == '' ) {
160 throw new MWException( 'Non-interwiki link passed, internal parser error.' );
161 }
162 if (!isset($this->mInterwikiLinks[$prefix])) {
163 $this->mInterwikiLinks[$prefix] = array();
164 }
165 $this->mInterwikiLinks[$prefix][$title->getDBkey()] = 1;
166 }
167
168 /**
169 * Return true if this cached output object predates the global or
170 * per-article cache invalidation timestamps, or if it comes from
171 * an incompatible older version.
172 *
173 * @param string $touched the affected article's last touched timestamp
174 * @return bool
175 * @public
176 */
177 function expired( $touched ) {
178 global $wgCacheEpoch;
179 return $this->getCacheTime() == -1 || // parser says it's uncacheable
180 $this->getCacheTime() < $touched ||
181 $this->getCacheTime() <= $wgCacheEpoch ||
182 !isset( $this->mVersion ) ||
183 version_compare( $this->mVersion, Parser::VERSION, "lt" );
184 }
185
186 /**
187 * Add some text to the <head>.
188 * If $tag is set, the section with that tag will only be included once
189 * in a given page.
190 */
191 function addHeadItem( $section, $tag = false ) {
192 if ( $tag !== false ) {
193 $this->mHeadItems[$tag] = $section;
194 } else {
195 $this->mHeadItems[] = $section;
196 }
197 }
198
199 /**
200 * Override the title to be used for display
201 * -- this is assumed to have been validated
202 * (check equal normalisation, etc.)
203 *
204 * @param string $text Desired title text
205 */
206 public function setDisplayTitle( $text ) {
207 $this->setTitleText( $text );
208 }
209
210 /**
211 * Get the title to be used for display
212 *
213 * @return string
214 */
215 public function getDisplayTitle() {
216 $t = $this->getTitleText( );
217 if( $t === '' ) {
218 return false;
219 }
220 return $t;
221 }
222
223 /**
224 * Fairly generic flag setter thingy.
225 */
226 public function setFlag( $flag ) {
227 $this->mFlags[$flag] = true;
228 }
229
230 public function getFlag( $flag ) {
231 return isset( $this->mFlags[$flag] );
232 }
233
234 /**
235 * Set a property to be cached in the DB
236 */
237 public function setProperty( $name, $value ) {
238 $this->mProperties[$name] = $value;
239 }
240
241 public function getProperty( $name ){
242 return isset( $this->mProperties[$name] ) ? $this->mProperties[$name] : false;
243 }
244
245 public function getProperties() {
246 if ( !isset( $this->mProperties ) ) {
247 $this->mProperties = array();
248 }
249 return $this->mProperties;
250 }
251 }