http://www.mediawiki.org/wiki/User:Catrope/Stub_threshold shows us people setting...
[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 $mCacheExpiry = null, # Seconds after which the object should expire, use 0 for uncachable. Used in ParserCache.
15 $mVersion = Parser::VERSION, # Compatibility check
16 $mLinks = array(), # 2-D map of NS/DBK to ID for the links in the document. ID=zero for broken.
17 $mTemplates = array(), # 2-D map of NS/DBK to ID for the template references. ID=zero for broken.
18 $mTemplateIds = array(), # 2-D map of NS/DBK to rev ID for the template references. ID=zero for broken.
19 $mImages = array(), # DB keys of the images used, in the array key only
20 $mExternalLinks = array(), # External link URLs, in the key only
21 $mInterwikiLinks = array(), # 2-D map of prefix/DBK (in keys only) for the inline interwiki links in the document.
22 $mNewSection = false, # Show a new section link?
23 $mHideNewSection = false, # Hide the new section link?
24 $mNoGallery = false, # No gallery on category page? (__NOGALLERY__)
25 $mHeadItems = array(), # Items to put in the <head> section
26 $mOutputHooks = array(), # Hook tags as per $wgParserOutputHooks
27 $mWarnings = array(), # Warning text to be returned to the user. Wikitext formatted, in the key only
28 $mSections = array(), # Table of contents
29 $mProperties = array(), # Name/value pairs to be cached in the DB
30 $mTOCHTML = ''; # HTML of the TOC
31 private $mIndexPolicy = ''; # 'index' or 'noindex'? Any other value will result in no change.
32
33 function ParserOutput( $text = '', $languageLinks = array(), $categoryLinks = array(),
34 $containsOldMagic = false, $titletext = '' )
35 {
36 $this->mText = $text;
37 $this->mLanguageLinks = $languageLinks;
38 $this->mCategories = $categoryLinks;
39 $this->mContainsOldMagic = $containsOldMagic;
40 $this->mTitleText = $titletext;
41 }
42
43 function getText() { return $this->mText; }
44 function &getLanguageLinks() { return $this->mLanguageLinks; }
45 function getInterwikiLinks() { return $this->mInterwikiLinks; }
46 function getCategoryLinks() { return array_keys( $this->mCategories ); }
47 function &getCategories() { return $this->mCategories; }
48 function getCacheTime() { return $this->mCacheTime; }
49 function getTitleText() { return $this->mTitleText; }
50 function getSections() { return $this->mSections; }
51 function &getLinks() { return $this->mLinks; }
52 function &getTemplates() { return $this->mTemplates; }
53 function &getImages() { return $this->mImages; }
54 function &getExternalLinks() { return $this->mExternalLinks; }
55 function getNoGallery() { return $this->mNoGallery; }
56 function getHeadItems() { return $this->mHeadItems; }
57 function getSubtitle() { return $this->mSubtitle; }
58 function getOutputHooks() { return (array)$this->mOutputHooks; }
59 function getWarnings() { return array_keys( $this->mWarnings ); }
60 function getIndexPolicy() { return $this->mIndexPolicy; }
61 function getTOCHTML() { return $this->mTOCHTML; }
62
63 function containsOldMagic() { return $this->mContainsOldMagic; }
64 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
65 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
66 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategories, $cl ); }
67 function setContainsOldMagic( $com ) { return wfSetVar( $this->mContainsOldMagic, $com ); }
68
69 /** setCacheTime() sets the timestamp expressing when the page has been rendered.
70 * This doesn not control expiry, see updateCacheExpiry() for that!
71 */
72 function setCacheTime( $t ) { return wfSetVar( $this->mCacheTime, $t ); }
73 function setTitleText( $t ) { return wfSetVar( $this->mTitleText, $t ); }
74 function setSections( $toc ) { return wfSetVar( $this->mSections, $toc ); }
75 function setIndexPolicy( $policy ) { return wfSetVar( $this->mIndexPolicy, $policy ); }
76 function setTOCHTML( $tochtml ) { return wfSetVar( $this->mTOCHTML, $tochtml ); }
77
78
79 /** Sets the number of seconds after which this object should expire.
80 * This value is used with the ParserCache.
81 * If called with a value greater than the value provided at any previous call,
82 * the new call has no effect. The value returned by getCacheExpiry is smaller
83 * or equal to the smallest number that was provided as an argument to
84 * updateCacheExpiry().
85 */
86 function updateCacheExpiry( $seconds ) {
87 $seconds = (int)$seconds;
88
89 if ( $this->mCacheExpiry === null || $this->mCacheExpiry > $seconds )
90 $this->mCacheExpiry = $seconds;
91
92 // hack: set old-style marker for uncacheable entries.
93 if ( $this->mCacheExpiry !== null && $this->mCacheExpiry <= 0 )
94 $this->mCacheTime = -1;
95 }
96
97 /** Returns the number of seconds after which this object should expire.
98 * This method is used by ParserCache to determine how long the ParserOutput can be cached.
99 * The timestamp of expiry can be calculated by adding getCacheExpiry() to getCacheTime().
100 * The value returned by getCacheExpiry is smaller or equal to the smallest number
101 * that was provided to a call of updateCacheExpiry(), and smaller or equal to the
102 * value of $wgParserCacheExpireTime.
103 */
104 function getCacheExpiry() {
105 global $wgParserCacheExpireTime;
106
107 if ( $this->mCacheTime < 0 ) return 0; // old-style marker for "not cachable"
108
109 $expire = $this->mCacheExpiry;
110
111 if ( $expire === null )
112 $expire = $wgParserCacheExpireTime;
113 else
114 $expire = min( $expire, $wgParserCacheExpireTime );
115
116 if( $this->containsOldMagic() ) { //compatibility hack
117 $expire = min( $expire, 3600 ); # 1 hour
118 }
119
120 if ( $expire <= 0 ) return 0; // not cachable
121 else return $expire;
122 }
123
124 function isCacheable() {
125 return $this->getCacheExpiry() > 0;
126 }
127
128 function addCategory( $c, $sort ) { $this->mCategories[$c] = $sort; }
129 function addLanguageLink( $t ) { $this->mLanguageLinks[] = $t; }
130 function addWarning( $s ) { $this->mWarnings[$s] = 1; }
131
132 function addOutputHook( $hook, $data = false ) {
133 $this->mOutputHooks[] = array( $hook, $data );
134 }
135
136 function setNewSection( $value ) {
137 $this->mNewSection = (bool)$value;
138 }
139 function hideNewSection ( $value ) {
140 $this->mHideNewSection = (bool)$value;
141 }
142 function getHideNewSection () {
143 return (bool)$this->mHideNewSection;
144 }
145 function getNewSection() {
146 return (bool)$this->mNewSection;
147 }
148
149 function addExternalLink( $url ) {
150 # We don't register links pointing to our own server, unless... :-)
151 global $wgServer, $wgRegisterInternalExternals;
152 if( $wgRegisterInternalExternals or stripos($url,$wgServer.'/')!==0)
153 $this->mExternalLinks[$url] = 1;
154 }
155
156 /**
157 * Record a local or interwiki inline link for saving in future link tables.
158 *
159 * @param $title Title object
160 * @param $id Mixed: optional known page_id so we can skip the lookup
161 */
162 function addLink( $title, $id = null ) {
163 if ( $title->isExternal() ) {
164 // Don't record interwikis in pagelinks
165 $this->addInterwikiLink( $title );
166 return;
167 }
168 $ns = $title->getNamespace();
169 $dbk = $title->getDBkey();
170 if ( $ns == NS_MEDIA ) {
171 // Normalize this pseudo-alias if it makes it down here...
172 $ns = NS_FILE;
173 } elseif( $ns == NS_SPECIAL ) {
174 // We don't record Special: links currently
175 // It might actually be wise to, but we'd need to do some normalization.
176 return;
177 } elseif( $dbk === '' ) {
178 // Don't record self links - [[#Foo]]
179 return;
180 }
181 if ( !isset( $this->mLinks[$ns] ) ) {
182 $this->mLinks[$ns] = array();
183 }
184 if ( is_null( $id ) ) {
185 $id = $title->getArticleID();
186 }
187 $this->mLinks[$ns][$dbk] = $id;
188 }
189
190 function addImage( $name ) {
191 $this->mImages[$name] = 1;
192 }
193
194 function addTemplate( $title, $page_id, $rev_id ) {
195 $ns = $title->getNamespace();
196 $dbk = $title->getDBkey();
197 if ( !isset( $this->mTemplates[$ns] ) ) {
198 $this->mTemplates[$ns] = array();
199 }
200 $this->mTemplates[$ns][$dbk] = $page_id;
201 if ( !isset( $this->mTemplateIds[$ns] ) ) {
202 $this->mTemplateIds[$ns] = array();
203 }
204 $this->mTemplateIds[$ns][$dbk] = $rev_id; // For versioning
205 }
206
207 /**
208 * @param $title Title object, must be an interwiki link
209 * @throws MWException if given invalid input
210 */
211 function addInterwikiLink( $title ) {
212 $prefix = $title->getInterwiki();
213 if( $prefix == '' ) {
214 throw new MWException( 'Non-interwiki link passed, internal parser error.' );
215 }
216 if (!isset($this->mInterwikiLinks[$prefix])) {
217 $this->mInterwikiLinks[$prefix] = array();
218 }
219 $this->mInterwikiLinks[$prefix][$title->getDBkey()] = 1;
220 }
221
222 /**
223 * Return true if this cached output object predates the global or
224 * per-article cache invalidation timestamps, or if it comes from
225 * an incompatible older version.
226 *
227 * @param $touched String: the affected article's last touched timestamp
228 * @return Boolean
229 */
230 public function expired( $touched ) {
231 global $wgCacheEpoch;
232 return !$this->isCacheable() || // parser says it's uncacheable
233 $this->getCacheTime() < $touched ||
234 $this->getCacheTime() <= $wgCacheEpoch ||
235 $this->getCacheTime() < wfTimestamp( TS_MW, time() - $this->getCacheExpiry() ) || // expiry period has passed
236 !isset( $this->mVersion ) ||
237 version_compare( $this->mVersion, Parser::VERSION, "lt" );
238 }
239
240 /**
241 * Add some text to the <head>.
242 * If $tag is set, the section with that tag will only be included once
243 * in a given page.
244 */
245 function addHeadItem( $section, $tag = false ) {
246 if ( $tag !== false ) {
247 $this->mHeadItems[$tag] = $section;
248 } else {
249 $this->mHeadItems[] = $section;
250 }
251 }
252
253 /**
254 * Override the title to be used for display
255 * -- this is assumed to have been validated
256 * (check equal normalisation, etc.)
257 *
258 * @param $text String: desired title text
259 */
260 public function setDisplayTitle( $text ) {
261 $this->setTitleText( $text );
262 $this->setProperty( 'displaytitle', $text );
263 }
264
265 /**
266 * Get the title to be used for display
267 *
268 * @return String
269 */
270 public function getDisplayTitle() {
271 $t = $this->getTitleText( );
272 if( $t === '' ) {
273 return false;
274 }
275 return $t;
276 }
277
278 /**
279 * Fairly generic flag setter thingy.
280 */
281 public function setFlag( $flag ) {
282 $this->mFlags[$flag] = true;
283 }
284
285 public function getFlag( $flag ) {
286 return isset( $this->mFlags[$flag] );
287 }
288
289 /**
290 * Set a property to be cached in the DB
291 */
292 public function setProperty( $name, $value ) {
293 $this->mProperties[$name] = $value;
294 }
295
296 public function getProperty( $name ){
297 return isset( $this->mProperties[$name] ) ? $this->mProperties[$name] : false;
298 }
299
300 public function getProperties() {
301 if ( !isset( $this->mProperties ) ) {
302 $this->mProperties = array();
303 }
304 return $this->mProperties;
305 }
306 }