Merge "Only pass strings to mysqli::real_escape_string"
[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 $mTOCEnabled = true; # Whether TOC should be shown, can't override __NOTOC__
52 private $mIndexPolicy = ''; # 'index' or 'noindex'? Any other value will result in no change.
53 private $mAccessedOptions = array(); # List of ParserOptions (stored in the keys)
54 private $mSecondaryDataUpdates = array(); # List of DataUpdate, used to save info from the page somewhere else.
55 private $mExtensionData = array(); # extra data used by extensions
56 private $mLimitReportData = array(); # Parser limit report data
57 private $mParseStartTime = array(); # Timestamps for getTimeSinceStart()
58
59 const EDITSECTION_REGEX = '#<(?:mw:)?editsection page="(.*?)" section="(.*?)"(?:/>|>(.*?)(</(?:mw:)?editsection>))#';
60
61 function __construct( $text = '', $languageLinks = array(), $categoryLinks = array(),
62 $containsOldMagic = false, $titletext = ''
63 ) {
64 $this->mText = $text;
65 $this->mLanguageLinks = $languageLinks;
66 $this->mCategories = $categoryLinks;
67 $this->mContainsOldMagic = $containsOldMagic;
68 $this->mTitleText = $titletext;
69 }
70
71 function getText() {
72 wfProfileIn( __METHOD__ );
73 $text = $this->mText;
74 if ( $this->mEditSectionTokens ) {
75 $text = preg_replace_callback( ParserOutput::EDITSECTION_REGEX,
76 array( &$this, 'replaceEditSectionLinksCallback' ), $text );
77 } else {
78 $text = preg_replace( ParserOutput::EDITSECTION_REGEX, '', $text );
79 }
80
81 // If you have an old cached version of this class - sorry, you can't disable the TOC
82 if ( isset( $this->mTOCEnabled ) && $this->mTOCEnabled ) {
83 $text = str_replace( array( Parser::TOC_START, Parser::TOC_END ), '', $text );
84 } else {
85 $text = preg_replace(
86 '#' . preg_quote( Parser::TOC_START ) . '.*?' . preg_quote( Parser::TOC_END ) . '#s',
87 '',
88 $text
89 );
90 }
91 wfProfileOut( __METHOD__ );
92 return $text;
93 }
94
95 /**
96 * callback used by getText to replace editsection tokens
97 * @private
98 * @param $m
99 * @throws MWException
100 * @return mixed
101 */
102 function replaceEditSectionLinksCallback( $m ) {
103 global $wgOut, $wgLang;
104 $args = array(
105 htmlspecialchars_decode( $m[1] ),
106 htmlspecialchars_decode( $m[2] ),
107 isset( $m[4] ) ? $m[3] : null,
108 );
109 $args[0] = Title::newFromText( $args[0] );
110 if ( !is_object( $args[0] ) ) {
111 throw new MWException( "Bad parser output text." );
112 }
113 $args[] = $wgLang->getCode();
114 $skin = $wgOut->getSkin();
115 return call_user_func_array( array( $skin, 'doEditSectionLink' ), $args );
116 }
117
118 function &getLanguageLinks() { return $this->mLanguageLinks; }
119 function getInterwikiLinks() { return $this->mInterwikiLinks; }
120 function getCategoryLinks() { return array_keys( $this->mCategories ); }
121 function &getCategories() { return $this->mCategories; }
122 function getTitleText() { return $this->mTitleText; }
123 function getSections() { return $this->mSections; }
124 function getEditSectionTokens() { return $this->mEditSectionTokens; }
125 function &getLinks() { return $this->mLinks; }
126 function &getTemplates() { return $this->mTemplates; }
127 function &getTemplateIds() { return $this->mTemplateIds; }
128 function &getImages() { return $this->mImages; }
129 function &getFileSearchOptions() { return $this->mFileSearchOptions; }
130 function &getExternalLinks() { return $this->mExternalLinks; }
131 function getNoGallery() { return $this->mNoGallery; }
132 function getHeadItems() { return $this->mHeadItems; }
133 function getModules() { return $this->mModules; }
134 function getModuleScripts() { return $this->mModuleScripts; }
135 function getModuleStyles() { return $this->mModuleStyles; }
136 function getModuleMessages() { return $this->mModuleMessages; }
137 function getOutputHooks() { return (array)$this->mOutputHooks; }
138 function getWarnings() { return array_keys( $this->mWarnings ); }
139 function getIndexPolicy() { return $this->mIndexPolicy; }
140 function getTOCHTML() { return $this->mTOCHTML; }
141 function getTimestamp() { return $this->mTimestamp; }
142 function getLimitReportData() { return $this->mLimitReportData; }
143 function getTOCEnabled() { return $this->mTOCEnabled; }
144
145 function setText( $text ) { return wfSetVar( $this->mText, $text ); }
146 function setLanguageLinks( $ll ) { return wfSetVar( $this->mLanguageLinks, $ll ); }
147 function setCategoryLinks( $cl ) { return wfSetVar( $this->mCategories, $cl ); }
148
149 function setTitleText( $t ) { return wfSetVar( $this->mTitleText, $t ); }
150 function setSections( $toc ) { return wfSetVar( $this->mSections, $toc ); }
151 function setEditSectionTokens( $t ) { return wfSetVar( $this->mEditSectionTokens, $t ); }
152 function setIndexPolicy( $policy ) { return wfSetVar( $this->mIndexPolicy, $policy ); }
153 function setTOCHTML( $tochtml ) { return wfSetVar( $this->mTOCHTML, $tochtml ); }
154 function setTimestamp( $timestamp ) { return wfSetVar( $this->mTimestamp, $timestamp ); }
155 function setTOCEnabled( $flag ) { return wfSetVar( $this->mTOCEnabled, $flag ); }
156
157 function addCategory( $c, $sort ) { $this->mCategories[$c] = $sort; }
158 function addLanguageLink( $t ) { $this->mLanguageLinks[] = $t; }
159 function addWarning( $s ) { $this->mWarnings[$s] = 1; }
160
161 function addOutputHook( $hook, $data = false ) {
162 $this->mOutputHooks[] = array( $hook, $data );
163 }
164
165 function setNewSection( $value ) {
166 $this->mNewSection = (bool)$value;
167 }
168 function hideNewSection( $value ) {
169 $this->mHideNewSection = (bool)$value;
170 }
171 function getHideNewSection() {
172 return (bool)$this->mHideNewSection;
173 }
174 function getNewSection() {
175 return (bool)$this->mNewSection;
176 }
177
178 /**
179 * Checks, if a url is pointing to the own server
180 *
181 * @param string $internal the server to check against
182 * @param string $url the url to check
183 * @return bool
184 */
185 static function isLinkInternal( $internal, $url ) {
186 return (bool)preg_match( '/^' .
187 # If server is proto relative, check also for http/https links
188 ( substr( $internal, 0, 2 ) === '//' ? '(?:https?:)?' : '' ) .
189 preg_quote( $internal, '/' ) .
190 # check for query/path/anchor or end of link in each case
191 '(?:[\?\/\#]|$)/i',
192 $url
193 );
194 }
195
196 function addExternalLink( $url ) {
197 # We don't register links pointing to our own server, unless... :-)
198 global $wgServer, $wgRegisterInternalExternals;
199
200 $registerExternalLink = true;
201 if ( !$wgRegisterInternalExternals ) {
202 $registerExternalLink = !self::isLinkInternal( $wgServer, $url );
203 }
204 if ( $registerExternalLink ) {
205 $this->mExternalLinks[$url] = 1;
206 }
207 }
208
209 /**
210 * Record a local or interwiki inline link for saving in future link tables.
211 *
212 * @param $title Title object
213 * @param $id Mixed: optional known page_id so we can skip the lookup
214 */
215 function addLink( Title $title, $id = null ) {
216 if ( $title->isExternal() ) {
217 // Don't record interwikis in pagelinks
218 $this->addInterwikiLink( $title );
219 return;
220 }
221 $ns = $title->getNamespace();
222 $dbk = $title->getDBkey();
223 if ( $ns == NS_MEDIA ) {
224 // Normalize this pseudo-alias if it makes it down here...
225 $ns = NS_FILE;
226 } elseif ( $ns == NS_SPECIAL ) {
227 // We don't record Special: links currently
228 // It might actually be wise to, but we'd need to do some normalization.
229 return;
230 } elseif ( $dbk === '' ) {
231 // Don't record self links - [[#Foo]]
232 return;
233 }
234 if ( !isset( $this->mLinks[$ns] ) ) {
235 $this->mLinks[$ns] = array();
236 }
237 if ( is_null( $id ) ) {
238 $id = $title->getArticleID();
239 }
240 $this->mLinks[$ns][$dbk] = $id;
241 }
242
243 /**
244 * Register a file dependency for this output
245 * @param string $name Title dbKey
246 * @param string $timestamp MW timestamp of file creation (or false if non-existing)
247 * @param string $sha1 base 36 SHA-1 of file (or false if non-existing)
248 * @return void
249 */
250 function addImage( $name, $timestamp = null, $sha1 = null ) {
251 $this->mImages[$name] = 1;
252 if ( $timestamp !== null && $sha1 !== null ) {
253 $this->mFileSearchOptions[$name] = array( 'time' => $timestamp, 'sha1' => $sha1 );
254 }
255 }
256
257 /**
258 * Register a template dependency for this output
259 * @param $title Title
260 * @param $page_id
261 * @param $rev_id
262 * @return void
263 */
264 function addTemplate( $title, $page_id, $rev_id ) {
265 $ns = $title->getNamespace();
266 $dbk = $title->getDBkey();
267 if ( !isset( $this->mTemplates[$ns] ) ) {
268 $this->mTemplates[$ns] = array();
269 }
270 $this->mTemplates[$ns][$dbk] = $page_id;
271 if ( !isset( $this->mTemplateIds[$ns] ) ) {
272 $this->mTemplateIds[$ns] = array();
273 }
274 $this->mTemplateIds[$ns][$dbk] = $rev_id; // For versioning
275 }
276
277 /**
278 * @param $title Title object, must be an interwiki link
279 * @throws MWException if given invalid input
280 */
281 function addInterwikiLink( $title ) {
282 if ( !$title->isExternal() ) {
283 throw new MWException( 'Non-interwiki link passed, internal parser error.' );
284 }
285 $prefix = $title->getInterwiki();
286 if ( !isset( $this->mInterwikiLinks[$prefix] ) ) {
287 $this->mInterwikiLinks[$prefix] = array();
288 }
289 $this->mInterwikiLinks[$prefix][$title->getDBkey()] = 1;
290 }
291
292 /**
293 * Add some text to the "<head>".
294 * If $tag is set, the section with that tag will only be included once
295 * in a given page.
296 */
297 function addHeadItem( $section, $tag = false ) {
298 if ( $tag !== false ) {
299 $this->mHeadItems[$tag] = $section;
300 } else {
301 $this->mHeadItems[] = $section;
302 }
303 }
304
305 public function addModules( $modules ) {
306 $this->mModules = array_merge( $this->mModules, (array)$modules );
307 }
308
309 public function addModuleScripts( $modules ) {
310 $this->mModuleScripts = array_merge( $this->mModuleScripts, (array)$modules );
311 }
312
313 public function addModuleStyles( $modules ) {
314 $this->mModuleStyles = array_merge( $this->mModuleStyles, (array)$modules );
315 }
316
317 public function addModuleMessages( $modules ) {
318 $this->mModuleMessages = array_merge( $this->mModuleMessages, (array)$modules );
319 }
320
321 /**
322 * Copy items from the OutputPage object into this one
323 *
324 * @param $out OutputPage object
325 */
326 public function addOutputPageMetadata( OutputPage $out ) {
327 $this->addModules( $out->getModules() );
328 $this->addModuleScripts( $out->getModuleScripts() );
329 $this->addModuleStyles( $out->getModuleStyles() );
330 $this->addModuleMessages( $out->getModuleMessages() );
331
332 $this->mHeadItems = array_merge( $this->mHeadItems, $out->getHeadItemsArray() );
333 }
334
335 /**
336 * Override the title to be used for display
337 * -- this is assumed to have been validated
338 * (check equal normalisation, etc.)
339 *
340 * @param string $text desired title text
341 */
342 public function setDisplayTitle( $text ) {
343 $this->setTitleText( $text );
344 $this->setProperty( 'displaytitle', $text );
345 }
346
347 /**
348 * Get the title to be used for display
349 *
350 * @return String
351 */
352 public function getDisplayTitle() {
353 $t = $this->getTitleText();
354 if ( $t === '' ) {
355 return false;
356 }
357 return $t;
358 }
359
360 /**
361 * Fairly generic flag setter thingy.
362 */
363 public function setFlag( $flag ) {
364 $this->mFlags[$flag] = true;
365 }
366
367 public function getFlag( $flag ) {
368 return isset( $this->mFlags[$flag] );
369 }
370
371 /**
372 * Set a property to be stored in the page_props database table.
373 *
374 * page_props is a key value store indexed by the page ID. This allows
375 * the parser to set a property on a page which can then be quickly
376 * retrieved given the page ID or via a DB join when given the page
377 * title.
378 *
379 * setProperty() is thus used to propagate properties from the parsed
380 * page to request contexts other than a page view of the currently parsed
381 * article.
382 *
383 * Some applications examples:
384 *
385 * * To implement hidden categories, hiding pages from category listings
386 * by storing a property.
387 *
388 * * Overriding the displayed article title.
389 * @see ParserOutput::setDisplayTitle()
390 *
391 * * To implement image tagging, for example displaying an icon on an
392 * image thumbnail to indicate that it is listed for deletion on
393 * Wikimedia Commons.
394 * This is not actually implemented, yet but would be pretty cool.
395 *
396 * @note: Do not use setProperty() to set a property which is only used
397 * in a context where the ParserOutput object itself is already available,
398 * for example a normal page view. There is no need to save such a property
399 * in the database since it the text is already parsed. You can just hook
400 * OutputPageParserOutput and get your data out of the ParserOutput object.
401 *
402 * If you are writing an extension where you want to set a property in the
403 * parser which is used by an OutputPageParserOutput hook, you have to
404 * associate the extension data directly with the ParserOutput object.
405 * Since MediaWiki 1.21, you can use setExtensionData() to do this:
406 *
407 * @par Example:
408 * @code
409 * $parser->getOutput()->setExtensionData( 'my_ext_foo', '...' );
410 * @endcode
411 *
412 * And then later, in OutputPageParserOutput or similar:
413 *
414 * @par Example:
415 * @code
416 * $output->getExtensionData( 'my_ext_foo' );
417 * @endcode
418 *
419 * In MediaWiki 1.20 and older, you have to use a custom member variable
420 * within the ParserOutput object:
421 *
422 * @par Example:
423 * @code
424 * $parser->getOutput()->my_ext_foo = '...';
425 * @endcode
426 *
427 */
428 public function setProperty( $name, $value ) {
429 $this->mProperties[$name] = $value;
430 }
431
432 public function getProperty( $name ) {
433 return isset( $this->mProperties[$name] ) ? $this->mProperties[$name] : false;
434 }
435
436 public function getProperties() {
437 if ( !isset( $this->mProperties ) ) {
438 $this->mProperties = array();
439 }
440 return $this->mProperties;
441 }
442
443 /**
444 * Returns the options from its ParserOptions which have been taken
445 * into account to produce this output or false if not available.
446 * @return mixed Array
447 */
448 public function getUsedOptions() {
449 if ( !isset( $this->mAccessedOptions ) ) {
450 return array();
451 }
452 return array_keys( $this->mAccessedOptions );
453 }
454
455 /**
456 * Tags a parser option for use in the cache key for this parser output.
457 * Registered as a watcher at ParserOptions::registerWatcher() by Parser::clearState().
458 *
459 * @see ParserCache::getKey
460 * @see ParserCache::save
461 * @see ParserOptions::addExtraKey
462 * @see ParserOptions::optionsHash
463 */
464 public function recordOption( $option ) {
465 $this->mAccessedOptions[$option] = true;
466 }
467
468 /**
469 * Adds an update job to the output. Any update jobs added to the output will eventually bexecuted in order to
470 * store any secondary information extracted from the page's content.
471 *
472 * @since 1.20
473 *
474 * @param DataUpdate $update
475 */
476 public function addSecondaryDataUpdate( DataUpdate $update ) {
477 $this->mSecondaryDataUpdates[] = $update;
478 }
479
480 /**
481 * Returns any DataUpdate jobs to be executed in order to store secondary information
482 * extracted from the page's content, including a LinksUpdate object for all links stored in
483 * this ParserOutput object.
484 *
485 * @note: Avoid using this method directly, use ContentHandler::getSecondaryDataUpdates() instead! The content
486 * handler may provide additional update objects.
487 *
488 * @since 1.20
489 *
490 * @param $title Title The title of the page we're updating. If not given, a title object will be created
491 * based on $this->getTitleText()
492 * @param $recursive Boolean: queue jobs for recursive updates?
493 *
494 * @return Array. An array of instances of DataUpdate
495 */
496 public function getSecondaryDataUpdates( Title $title = null, $recursive = true ) {
497 if ( is_null( $title ) ) {
498 $title = Title::newFromText( $this->getTitleText() );
499 }
500
501 $linksUpdate = new LinksUpdate( $title, $this, $recursive );
502
503 return array_merge( $this->mSecondaryDataUpdates, array( $linksUpdate ) );
504 }
505
506 /**
507 * Attaches arbitrary data to this ParserObject. This can be used to store some information in
508 * the ParserOutput object for later use during page output. The data will be cached along with
509 * the ParserOutput object, but unlike data set using setProperty(), it is not recorded in the
510 * database.
511 *
512 * This method is provided to overcome the unsafe practice of attaching extra information to a
513 * ParserObject by directly assigning member variables.
514 *
515 * To use setExtensionData() to pass extension information from a hook inside the parser to a
516 * hook in the page output, use this in the parser hook:
517 *
518 * @par Example:
519 * @code
520 * $parser->getOutput()->setExtensionData( 'my_ext_foo', '...' );
521 * @endcode
522 *
523 * And then later, in OutputPageParserOutput or similar:
524 *
525 * @par Example:
526 * @code
527 * $output->getExtensionData( 'my_ext_foo' );
528 * @endcode
529 *
530 * In MediaWiki 1.20 and older, you have to use a custom member variable
531 * within the ParserOutput object:
532 *
533 * @par Example:
534 * @code
535 * $parser->getOutput()->my_ext_foo = '...';
536 * @endcode
537 *
538 * @since 1.21
539 *
540 * @param string $key The key for accessing the data. Extensions should take care to avoid
541 * conflicts in naming keys. It is suggested to use the extension's name as a
542 * prefix.
543 *
544 * @param mixed $value The value to set. Setting a value to null is equivalent to removing
545 * the value.
546 */
547 public function setExtensionData( $key, $value ) {
548 if ( $value === null ) {
549 unset( $this->mExtensionData[$key] );
550 } else {
551 $this->mExtensionData[$key] = $value;
552 }
553 }
554
555 /**
556 * Gets extensions data previously attached to this ParserOutput using setExtensionData().
557 * Typically, such data would be set while parsing the page, e.g. by a parser function.
558 *
559 * @since 1.21
560 *
561 * @param string $key The key to look up.
562 *
563 * @return mixed The value previously set for the given key using setExtensionData( $key ),
564 * or null if no value was set for this key.
565 */
566 public function getExtensionData( $key ) {
567 if ( isset( $this->mExtensionData[$key] ) ) {
568 return $this->mExtensionData[$key];
569 }
570
571 return null;
572 }
573
574 private static function getTimes( $clock = null ) {
575 $ret = array();
576 if ( !$clock || $clock === 'wall' ) {
577 $ret['wall'] = microtime( true );
578 }
579 if ( ( !$clock || $clock === 'cpu' ) && function_exists( 'getrusage' ) ) {
580 $ru = getrusage();
581 $ret['cpu'] = $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6;
582 $ret['cpu'] += $ru['ru_stime.tv_sec'] + $ru['ru_stime.tv_usec'] / 1e6;
583 }
584 return $ret;
585 }
586
587 /**
588 * Resets the parse start timestamps for future calls to getTimeSinceStart()
589 * @since 1.22
590 */
591 function resetParseStartTime() {
592 $this->mParseStartTime = self::getTimes();
593 }
594
595 /**
596 * Returns the time since resetParseStartTime() was last called
597 *
598 * Clocks available are:
599 * - wall: Wall clock time
600 * - cpu: CPU time (requires getrusage)
601 *
602 * @since 1.22
603 * @param string $clock
604 * @return float|null
605 */
606 function getTimeSinceStart( $clock ) {
607 if ( !isset( $this->mParseStartTime[$clock] ) ) {
608 return null;
609 }
610
611 $end = self::getTimes( $clock );
612 return $end[$clock] - $this->mParseStartTime[$clock];
613 }
614
615 /**
616 * Sets parser limit report data for a key
617 *
618 * The key is used as the prefix for various messages used for formatting:
619 * - $key: The label for the field in the limit report
620 * - $key-value-text: Message used to format the value in the "NewPP limit
621 * report" HTML comment. If missing, uses $key-format.
622 * - $key-value-html: Message used to format the value in the preview
623 * limit report table. If missing, uses $key-format.
624 * - $key-value: Message used to format the value. If missing, uses "$1".
625 *
626 * Note that all values are interpreted as wikitext, and so should be
627 * encoded with htmlspecialchars() as necessary, but should avoid complex
628 * HTML for sanity of display in the "NewPP limit report" comment.
629 *
630 * @since 1.22
631 * @param string $key Message key
632 * @param mixed $value Appropriate for Message::params()
633 */
634 function setLimitReportData( $key, $value ) {
635 $this->mLimitReportData[$key] = $value;
636 }
637 }