merge latest master into Wikidata branch
[lhc/web/wiklou.git] / includes / parser / ParserOutput.php
1 <?php
2
3 /**
4 * Output of the PHP parser.
5 *
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.
10 *
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.
15 *
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
20 *
21 * @file
22 * @ingroup Parser
23 */
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 DataUpdate, used to save info from the page somewhere else.
54
55 const EDITSECTION_REGEX = '#<(?:mw:)?editsection page="(.*?)" section="(.*?)"(?:/>|>(.*?)(</(?:mw:)?editsection>))#';
56
57 function __construct( $text = '', $languageLinks = array(), $categoryLinks = array(),
58 $containsOldMagic = false, $titletext = '' )
59 {
60 $this->mText = $text;
61 $this->mLanguageLinks = $languageLinks;
62 $this->mCategories = $categoryLinks;
63 $this->mContainsOldMagic = $containsOldMagic;
64 $this->mTitleText = $titletext;
65 }
66
67 function getText() {
68 if ( $this->mEditSectionTokens ) {
69 return preg_replace_callback( ParserOutput::EDITSECTION_REGEX,
70 array( &$this, 'replaceEditSectionLinksCallback' ), $this->mText );
71 }
72 return preg_replace( ParserOutput::EDITSECTION_REGEX, '', $this->mText );
73 }
74
75 /**
76 * callback used by getText to replace editsection tokens
77 * @private
78 * @return mixed
79 */
80 function replaceEditSectionLinksCallback( $m ) {
81 global $wgOut, $wgLang;
82 $args = array(
83 htmlspecialchars_decode($m[1]),
84 htmlspecialchars_decode($m[2]),
85 isset($m[4]) ? $m[3] : null,
86 );
87 $args[0] = Title::newFromText( $args[0] );
88 if ( !is_object($args[0]) ) {
89 throw new MWException("Bad parser output text.");
90 }
91 $args[] = $wgLang->getCode();
92 $skin = $wgOut->getSkin();
93 return call_user_func_array( array( $skin, 'doEditSectionLink' ), $args );
94 }
95
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; }
120
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 ); }
124
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 ); }
131
132 function addCategory( $c, $sort ) { $this->mCategories[$c] = $sort; }
133 function addLanguageLink( $t ) { $this->mLanguageLinks[] = $t; }
134 function addWarning( $s ) { $this->mWarnings[$s] = 1; }
135
136 function addOutputHook( $hook, $data = false ) {
137 $this->mOutputHooks[] = array( $hook, $data );
138 }
139
140 function setNewSection( $value ) {
141 $this->mNewSection = (bool)$value;
142 }
143 function hideNewSection ( $value ) {
144 $this->mHideNewSection = (bool)$value;
145 }
146 function getHideNewSection () {
147 return (bool)$this->mHideNewSection;
148 }
149 function getNewSection() {
150 return (bool)$this->mNewSection;
151 }
152
153 /**
154 * Checks, if a url is pointing to the own server
155 *
156 * @param $internal String the server to check against
157 * @param $url String the url to check
158 * @return bool
159 */
160 static function isLinkInternal( $internal, $url ) {
161 return (bool)preg_match( '/^' .
162 # If server is proto relative, check also for http/https links
163 ( substr( $internal, 0, 2 ) === '//' ? '(?:https?:)?' : '' ) .
164 preg_quote( $internal, '/' ) .
165 # check for query/path/anchor or end of link in each case
166 '(?:[\?\/\#]|$)/i',
167 $url
168 );
169 }
170
171 function addExternalLink( $url ) {
172 # We don't register links pointing to our own server, unless... :-)
173 global $wgServer, $wgRegisterInternalExternals;
174
175 $registerExternalLink = true;
176 if( !$wgRegisterInternalExternals ) {
177 $registerExternalLink = !self::isLinkInternal( $wgServer, $url );
178 }
179 if( $registerExternalLink ) {
180 $this->mExternalLinks[$url] = 1;
181 }
182 }
183
184 /**
185 * Record a local or interwiki inline link for saving in future link tables.
186 *
187 * @param $title Title object
188 * @param $id Mixed: optional known page_id so we can skip the lookup
189 */
190 function addLink( $title, $id = null ) {
191 if ( $title->isExternal() ) {
192 // Don't record interwikis in pagelinks
193 $this->addInterwikiLink( $title );
194 return;
195 }
196 $ns = $title->getNamespace();
197 $dbk = $title->getDBkey();
198 if ( $ns == NS_MEDIA ) {
199 // Normalize this pseudo-alias if it makes it down here...
200 $ns = NS_FILE;
201 } elseif( $ns == NS_SPECIAL ) {
202 // We don't record Special: links currently
203 // It might actually be wise to, but we'd need to do some normalization.
204 return;
205 } elseif( $dbk === '' ) {
206 // Don't record self links - [[#Foo]]
207 return;
208 }
209 if ( !isset( $this->mLinks[$ns] ) ) {
210 $this->mLinks[$ns] = array();
211 }
212 if ( is_null( $id ) ) {
213 $id = $title->getArticleID();
214 }
215 $this->mLinks[$ns][$dbk] = $id;
216 }
217
218 /**
219 * Register a file dependency for this output
220 * @param $name string Title dbKey
221 * @param $timestamp string MW timestamp of file creation (or false if non-existing)
222 * @param $sha1 string base 36 SHA-1 of file (or false if non-existing)
223 * @return void
224 */
225 function addImage( $name, $timestamp = null, $sha1 = null ) {
226 $this->mImages[$name] = 1;
227 if ( $timestamp !== null && $sha1 !== null ) {
228 $this->mFileSearchOptions[$name] = array( 'time' => $timestamp, 'sha1' => $sha1 );
229 }
230 }
231
232 /**
233 * Register a template dependency for this output
234 * @param $title Title
235 * @param $page_id
236 * @param $rev_id
237 * @return void
238 */
239 function addTemplate( $title, $page_id, $rev_id ) {
240 $ns = $title->getNamespace();
241 $dbk = $title->getDBkey();
242 if ( !isset( $this->mTemplates[$ns] ) ) {
243 $this->mTemplates[$ns] = array();
244 }
245 $this->mTemplates[$ns][$dbk] = $page_id;
246 if ( !isset( $this->mTemplateIds[$ns] ) ) {
247 $this->mTemplateIds[$ns] = array();
248 }
249 $this->mTemplateIds[$ns][$dbk] = $rev_id; // For versioning
250 }
251
252 /**
253 * @param $title Title object, must be an interwiki link
254 * @throws MWException if given invalid input
255 */
256 function addInterwikiLink( $title ) {
257 $prefix = $title->getInterwiki();
258 if( $prefix == '' ) {
259 throw new MWException( 'Non-interwiki link passed, internal parser error.' );
260 }
261 if (!isset($this->mInterwikiLinks[$prefix])) {
262 $this->mInterwikiLinks[$prefix] = array();
263 }
264 $this->mInterwikiLinks[$prefix][$title->getDBkey()] = 1;
265 }
266
267 /**
268 * Add some text to the "<head>".
269 * If $tag is set, the section with that tag will only be included once
270 * in a given page.
271 */
272 function addHeadItem( $section, $tag = false ) {
273 if ( $tag !== false ) {
274 $this->mHeadItems[$tag] = $section;
275 } else {
276 $this->mHeadItems[] = $section;
277 }
278 }
279
280 public function addModules( $modules ) {
281 $this->mModules = array_merge( $this->mModules, (array) $modules );
282 }
283
284 public function addModuleScripts( $modules ) {
285 $this->mModuleScripts = array_merge( $this->mModuleScripts, (array)$modules );
286 }
287
288 public function addModuleStyles( $modules ) {
289 $this->mModuleStyles = array_merge( $this->mModuleStyles, (array)$modules );
290 }
291
292 public function addModuleMessages( $modules ) {
293 $this->mModuleMessages = array_merge( $this->mModuleMessages, (array)$modules );
294 }
295
296 /**
297 * Copy items from the OutputPage object into this one
298 *
299 * @param $out OutputPage object
300 */
301 public function addOutputPageMetadata( OutputPage $out ) {
302 $this->addModules( $out->getModules() );
303 $this->addModuleScripts( $out->getModuleScripts() );
304 $this->addModuleStyles( $out->getModuleStyles() );
305 $this->addModuleMessages( $out->getModuleMessages() );
306
307 $this->mHeadItems = array_merge( $this->mHeadItems, $out->getHeadItemsArray() );
308 }
309
310 /**
311 * Override the title to be used for display
312 * -- this is assumed to have been validated
313 * (check equal normalisation, etc.)
314 *
315 * @param $text String: desired title text
316 */
317 public function setDisplayTitle( $text ) {
318 $this->setTitleText( $text );
319 $this->setProperty( 'displaytitle', $text );
320 }
321
322 /**
323 * Get the title to be used for display
324 *
325 * @return String
326 */
327 public function getDisplayTitle() {
328 $t = $this->getTitleText();
329 if( $t === '' ) {
330 return false;
331 }
332 return $t;
333 }
334
335 /**
336 * Fairly generic flag setter thingy.
337 */
338 public function setFlag( $flag ) {
339 $this->mFlags[$flag] = true;
340 }
341
342 public function getFlag( $flag ) {
343 return isset( $this->mFlags[$flag] );
344 }
345
346 /**
347 * Set a property to be cached in the DB
348 */
349 public function setProperty( $name, $value ) {
350 $this->mProperties[$name] = $value;
351 }
352
353 public function getProperty( $name ){
354 return isset( $this->mProperties[$name] ) ? $this->mProperties[$name] : false;
355 }
356
357 public function getProperties() {
358 if ( !isset( $this->mProperties ) ) {
359 $this->mProperties = array();
360 }
361 return $this->mProperties;
362 }
363
364
365 /**
366 * Returns the options from its ParserOptions which have been taken
367 * into account to produce this output or false if not available.
368 * @return mixed Array
369 */
370 public function getUsedOptions() {
371 if ( !isset( $this->mAccessedOptions ) ) {
372 return array();
373 }
374 return array_keys( $this->mAccessedOptions );
375 }
376
377 /**
378 * Callback passed by the Parser to the ParserOptions to keep track of which options are used.
379 * @access private
380 */
381 function recordOption( $option ) {
382 $this->mAccessedOptions[$option] = true;
383 }
384
385 /**
386 * Adds an update job to the output. Any update jobs added to the output will eventually bexecuted in order to
387 * store any secondary information extracted from the page's content.
388 *
389 * @since 1.20
390 *
391 * @param DataUpdate $update
392 */
393 public function addSecondaryDataUpdate( DataUpdate $update ) {
394 $this->mSecondaryDataUpdates[] = $update;
395 }
396
397 /**
398 * Returns any DataUpdate jobs to be executed in order to store secondary information
399 * extracted from the page's content, including a LinksUpdate object for all links stored in
400 * this ParserOutput object.
401 *
402 * @note: Avoid using this method directly, use ContentHandler::getSecondaryDataUpdates() instead! The content
403 * handler may provide additional update objects.
404 *
405 * @since 1.20
406 *
407 * @param $title Title The title of the page we're updating. If not given, a title object will be created
408 * based on $this->getTitleText()
409 * @param $recursive Boolean: queue jobs for recursive updates?
410 *
411 * @return Array. An array of instances of DataUpdate
412 */
413 public function getSecondaryDataUpdates( Title $title = null, $recursive = true ) {
414 if ( is_null( $title ) ) {
415 $title = Title::newFromText( $this->getTitleText() );
416 }
417
418 $linksUpdate = new LinksUpdate( $title, $this, $recursive );
419
420 if ( $this->mSecondaryDataUpdates === array() ) {
421 return array( $linksUpdate );
422 } else {
423 $updates = array_merge( $this->mSecondaryDataUpdates, array( $linksUpdate ) );
424 }
425
426 return $updates;
427 }
428
429 }