Merge "Add LinkTarget interface"
[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 /**
26 * @var string $mText The output text
27 */
28 public $mText;
29
30 /**
31 * @var array $mLanguageLinks List of the full text of language links,
32 * in the order they appear.
33 */
34 public $mLanguageLinks;
35
36 /**
37 * @var array $mCategoriesMap of category names to sort keys
38 */
39 public $mCategories;
40
41 /**
42 * @var array $mIndicators Page status indicators, usually displayed in top-right corner.
43 */
44 public $mIndicators = array();
45
46 /**
47 * @var string $mTitleText Title text of the chosen language variant, as HTML.
48 */
49 public $mTitleText;
50
51 /**
52 * @var array $mLinks 2-D map of NS/DBK to ID for the links in the document.
53 * ID=zero for broken.
54 */
55 public $mLinks = array();
56
57 /**
58 * @var array $mTemplates 2-D map of NS/DBK to ID for the template references.
59 * ID=zero for broken.
60 */
61 public $mTemplates = array();
62
63 /**
64 * @var array $mTemplateIds 2-D map of NS/DBK to rev ID for the template references.
65 * ID=zero for broken.
66 */
67 public $mTemplateIds = array();
68
69 /**
70 * @var array $mImages DB keys of the images used, in the array key only
71 */
72 public $mImages = array();
73
74 /**
75 * @var array $mFileSearchOptions DB keys of the images used mapped to sha1 and MW timestamp.
76 */
77 public $mFileSearchOptions = array();
78
79 /**
80 * @var array $mExternalLinks External link URLs, in the key only.
81 */
82 public $mExternalLinks = array();
83
84 /**
85 * @var array $mInterwikiLinks 2-D map of prefix/DBK (in keys only)
86 * for the inline interwiki links in the document.
87 */
88 public $mInterwikiLinks = array();
89
90 /**
91 * @var bool $mNewSection Show a new section link?
92 */
93 public $mNewSection = false;
94
95 /**
96 * @var bool $mHideNewSection Hide the new section link?
97 */
98 public $mHideNewSection = false;
99
100 /**
101 * @var bool $mNoGallery No gallery on category page? (__NOGALLERY__).
102 */
103 public $mNoGallery = false;
104
105 /**
106 * @var array $mHeadItems Items to put in the <head> section
107 */
108 public $mHeadItems = array();
109
110 /**
111 * @var array $mModules Modules to be loaded by ResourceLoader
112 */
113 public $mModules = array();
114
115 /**
116 * @var array $mModuleScripts Modules of which only the JS will be loaded by ResourceLoader.
117 */
118 public $mModuleScripts = array();
119
120 /**
121 * @var array $mModuleStyles Modules of which only the CSSS will be loaded by ResourceLoader.
122 */
123 public $mModuleStyles = array();
124
125 /**
126 * @var array $mJsConfigVars JavaScript config variable for mw.config combined with this page.
127 */
128 public $mJsConfigVars = array();
129
130 /**
131 * @var array $mOutputHooks Hook tags as per $wgParserOutputHooks.
132 */
133 public $mOutputHooks = array();
134
135 /**
136 * @var array $mWarnings Warning text to be returned to the user.
137 * Wikitext formatted, in the key only.
138 */
139 public $mWarnings = array();
140
141 /**
142 * @var array $mSections Table of contents
143 */
144 public $mSections = array();
145
146 /**
147 * @var bool $mEditSectionTokens prefix/suffix markers if edit sections were output as tokens.
148 */
149 public $mEditSectionTokens = false;
150
151 /**
152 * @var array $mProperties Name/value pairs to be cached in the DB.
153 */
154 public $mProperties = array();
155
156 /**
157 * @var string $mTOCHTML HTML of the TOC.
158 */
159 public $mTOCHTML = '';
160
161 /**
162 * @var string $mTimestamp Timestamp of the revision.
163 */
164 public $mTimestamp;
165
166 /**
167 * @var bool $mTOCEnabled Whether TOC should be shown, can't override __NOTOC__.
168 */
169 public $mTOCEnabled = true;
170
171 /**
172 * @var bool $mEnableOOUI Whether OOUI should be enabled.
173 */
174 public $mEnableOOUI = false;
175
176 /**
177 * @var string $mIndexPolicy 'index' or 'noindex'? Any other value will result in no change.
178 */
179 private $mIndexPolicy = '';
180
181 /**
182 * @var array $mAccessedOptions List of ParserOptions (stored in the keys).
183 */
184 private $mAccessedOptions = array();
185
186 /**
187 * @var array $mExtensionData extra data used by extensions.
188 */
189 private $mExtensionData = array();
190
191 /**
192 * @var array $mLimitReportData Parser limit report data.
193 */
194 private $mLimitReportData = array();
195
196 /**
197 * @var array $mParseStartTime Timestamps for getTimeSinceStart().
198 */
199 private $mParseStartTime = array();
200
201 /**
202 * @var bool $mPreventClickjacking Whether to emit X-Frame-Options: DENY.
203 */
204 private $mPreventClickjacking = false;
205
206 /**
207 * @var array $mFlags Generic flags.
208 */
209 private $mFlags = array();
210
211 const EDITSECTION_REGEX =
212 '#<(?:mw:)?editsection page="(.*?)" section="(.*?)"(?:/>|>(.*?)(</(?:mw:)?editsection>))#';
213
214 public function __construct( $text = '', $languageLinks = array(), $categoryLinks = array(),
215 $unused = false, $titletext = ''
216 ) {
217 $this->mText = $text;
218 $this->mLanguageLinks = $languageLinks;
219 $this->mCategories = $categoryLinks;
220 $this->mTitleText = $titletext;
221 }
222
223 /**
224 * Get the cacheable text with <mw:editsection> markers still in it. The
225 * return value is suitable for writing back via setText() but is not valid
226 * for display to the user.
227 *
228 * @since 1.27
229 */
230 public function getRawText() {
231 return $this->mText;
232 }
233
234 public function getText() {
235 $text = $this->mText;
236 if ( $this->mEditSectionTokens ) {
237 $text = preg_replace_callback(
238 ParserOutput::EDITSECTION_REGEX,
239 function ( $m ) {
240 global $wgOut, $wgLang;
241 $editsectionPage = Title::newFromText( htmlspecialchars_decode( $m[1] ) );
242 $editsectionSection = htmlspecialchars_decode( $m[2] );
243 $editsectionContent = isset( $m[4] ) ? $m[3] : null;
244
245 if ( !is_object( $editsectionPage ) ) {
246 throw new MWException( "Bad parser output text." );
247 }
248
249 $skin = $wgOut->getSkin();
250 return call_user_func_array(
251 array( $skin, 'doEditSectionLink' ),
252 array( $editsectionPage, $editsectionSection,
253 $editsectionContent, $wgLang->getCode() )
254 );
255 },
256 $text
257 );
258 } else {
259 $text = preg_replace( ParserOutput::EDITSECTION_REGEX, '', $text );
260 }
261
262 // If you have an old cached version of this class - sorry, you can't disable the TOC
263 if ( isset( $this->mTOCEnabled ) && $this->mTOCEnabled ) {
264 $text = str_replace( array( Parser::TOC_START, Parser::TOC_END ), '', $text );
265 } else {
266 $text = preg_replace(
267 '#' . preg_quote( Parser::TOC_START, '#' ) . '.*?' . preg_quote( Parser::TOC_END, '#' ) . '#s',
268 '',
269 $text
270 );
271 }
272 return $text;
273 }
274
275 public function &getLanguageLinks() {
276 return $this->mLanguageLinks;
277 }
278
279 public function getInterwikiLinks() {
280 return $this->mInterwikiLinks;
281 }
282
283 public function getCategoryLinks() {
284 return array_keys( $this->mCategories );
285 }
286
287 public function &getCategories() {
288 return $this->mCategories;
289 }
290
291 /**
292 * @since 1.25
293 */
294 public function getIndicators() {
295 return $this->mIndicators;
296 }
297
298 public function getTitleText() {
299 return $this->mTitleText;
300 }
301
302 public function getSections() {
303 return $this->mSections;
304 }
305
306 public function getEditSectionTokens() {
307 return $this->mEditSectionTokens;
308 }
309
310 public function &getLinks() {
311 return $this->mLinks;
312 }
313
314 public function &getTemplates() {
315 return $this->mTemplates;
316 }
317
318 public function &getTemplateIds() {
319 return $this->mTemplateIds;
320 }
321
322 public function &getImages() {
323 return $this->mImages;
324 }
325
326 public function &getFileSearchOptions() {
327 return $this->mFileSearchOptions;
328 }
329
330 public function &getExternalLinks() {
331 return $this->mExternalLinks;
332 }
333
334 public function getNoGallery() {
335 return $this->mNoGallery;
336 }
337
338 public function getHeadItems() {
339 return $this->mHeadItems;
340 }
341
342 public function getModules() {
343 return $this->mModules;
344 }
345
346 public function getModuleScripts() {
347 return $this->mModuleScripts;
348 }
349
350 public function getModuleStyles() {
351 return $this->mModuleStyles;
352 }
353
354 /**
355 * @deprecated since 1.26 Obsolete
356 * @return array
357 */
358 public function getModuleMessages() {
359 wfDeprecated( __METHOD__, '1.26' );
360 return array();
361 }
362
363 /** @since 1.23 */
364 public function getJsConfigVars() {
365 return $this->mJsConfigVars;
366 }
367
368 public function getOutputHooks() {
369 return (array)$this->mOutputHooks;
370 }
371
372 public function getWarnings() {
373 return array_keys( $this->mWarnings );
374 }
375
376 public function getIndexPolicy() {
377 return $this->mIndexPolicy;
378 }
379
380 public function getTOCHTML() {
381 return $this->mTOCHTML;
382 }
383
384 public function getTimestamp() {
385 return $this->mTimestamp;
386 }
387
388 public function getLimitReportData() {
389 return $this->mLimitReportData;
390 }
391
392 public function getTOCEnabled() {
393 return $this->mTOCEnabled;
394 }
395
396 public function getEnableOOUI() {
397 return $this->mEnableOOUI;
398 }
399
400 public function setText( $text ) {
401 return wfSetVar( $this->mText, $text );
402 }
403
404 public function setLanguageLinks( $ll ) {
405 return wfSetVar( $this->mLanguageLinks, $ll );
406 }
407
408 public function setCategoryLinks( $cl ) {
409 return wfSetVar( $this->mCategories, $cl );
410 }
411
412 public function setTitleText( $t ) {
413 return wfSetVar( $this->mTitleText, $t );
414 }
415
416 public function setSections( $toc ) {
417 return wfSetVar( $this->mSections, $toc );
418 }
419
420 public function setEditSectionTokens( $t ) {
421 return wfSetVar( $this->mEditSectionTokens, $t );
422 }
423
424 public function setIndexPolicy( $policy ) {
425 return wfSetVar( $this->mIndexPolicy, $policy );
426 }
427
428 public function setTOCHTML( $tochtml ) {
429 return wfSetVar( $this->mTOCHTML, $tochtml );
430 }
431
432 public function setTimestamp( $timestamp ) {
433 return wfSetVar( $this->mTimestamp, $timestamp );
434 }
435
436 public function setTOCEnabled( $flag ) {
437 return wfSetVar( $this->mTOCEnabled, $flag );
438 }
439
440 public function addCategory( $c, $sort ) {
441 $this->mCategories[$c] = $sort;
442 }
443
444 /**
445 * @since 1.25
446 */
447 public function setIndicator( $id, $content ) {
448 $this->mIndicators[$id] = $content;
449 }
450
451 /**
452 * Enables OOUI, if true, in any OutputPage instance this ParserOutput
453 * object is added to.
454 *
455 * @since 1.26
456 * @param bool $enable If OOUI should be enabled or not
457 */
458 public function setEnableOOUI( $enable = false ) {
459 $this->mEnableOOUI = $enable;
460 }
461
462 public function addLanguageLink( $t ) {
463 $this->mLanguageLinks[] = $t;
464 }
465
466 public function addWarning( $s ) {
467 $this->mWarnings[$s] = 1;
468 }
469
470 public function addOutputHook( $hook, $data = false ) {
471 $this->mOutputHooks[] = array( $hook, $data );
472 }
473
474 public function setNewSection( $value ) {
475 $this->mNewSection = (bool)$value;
476 }
477 public function hideNewSection( $value ) {
478 $this->mHideNewSection = (bool)$value;
479 }
480 public function getHideNewSection() {
481 return (bool)$this->mHideNewSection;
482 }
483 public function getNewSection() {
484 return (bool)$this->mNewSection;
485 }
486
487 /**
488 * Checks, if a url is pointing to the own server
489 *
490 * @param string $internal The server to check against
491 * @param string $url The url to check
492 * @return bool
493 */
494 public static function isLinkInternal( $internal, $url ) {
495 return (bool)preg_match( '/^' .
496 # If server is proto relative, check also for http/https links
497 ( substr( $internal, 0, 2 ) === '//' ? '(?:https?:)?' : '' ) .
498 preg_quote( $internal, '/' ) .
499 # check for query/path/anchor or end of link in each case
500 '(?:[\?\/\#]|$)/i',
501 $url
502 );
503 }
504
505 public function addExternalLink( $url ) {
506 # We don't register links pointing to our own server, unless... :-)
507 global $wgServer, $wgRegisterInternalExternals;
508
509 $registerExternalLink = true;
510 if ( !$wgRegisterInternalExternals ) {
511 $registerExternalLink = !self::isLinkInternal( $wgServer, $url );
512 }
513 if ( $registerExternalLink ) {
514 $this->mExternalLinks[$url] = 1;
515 }
516 }
517
518 /**
519 * Record a local or interwiki inline link for saving in future link tables.
520 *
521 * @param Title $title
522 * @param int|null $id Optional known page_id so we can skip the lookup
523 */
524 public function addLink( Title $title, $id = null ) {
525 if ( $title->isExternal() ) {
526 // Don't record interwikis in pagelinks
527 $this->addInterwikiLink( $title );
528 return;
529 }
530 $ns = $title->getNamespace();
531 $dbk = $title->getDBkey();
532 if ( $ns == NS_MEDIA ) {
533 // Normalize this pseudo-alias if it makes it down here...
534 $ns = NS_FILE;
535 } elseif ( $ns == NS_SPECIAL ) {
536 // We don't record Special: links currently
537 // It might actually be wise to, but we'd need to do some normalization.
538 return;
539 } elseif ( $dbk === '' ) {
540 // Don't record self links - [[#Foo]]
541 return;
542 }
543 if ( !isset( $this->mLinks[$ns] ) ) {
544 $this->mLinks[$ns] = array();
545 }
546 if ( is_null( $id ) ) {
547 $id = $title->getArticleID();
548 }
549 $this->mLinks[$ns][$dbk] = $id;
550 }
551
552 /**
553 * Register a file dependency for this output
554 * @param string $name Title dbKey
555 * @param string $timestamp MW timestamp of file creation (or false if non-existing)
556 * @param string $sha1 Base 36 SHA-1 of file (or false if non-existing)
557 * @return void
558 */
559 public function addImage( $name, $timestamp = null, $sha1 = null ) {
560 $this->mImages[$name] = 1;
561 if ( $timestamp !== null && $sha1 !== null ) {
562 $this->mFileSearchOptions[$name] = array( 'time' => $timestamp, 'sha1' => $sha1 );
563 }
564 }
565
566 /**
567 * Register a template dependency for this output
568 * @param Title $title
569 * @param int $page_id
570 * @param int $rev_id
571 * @return void
572 */
573 public function addTemplate( $title, $page_id, $rev_id ) {
574 $ns = $title->getNamespace();
575 $dbk = $title->getDBkey();
576 if ( !isset( $this->mTemplates[$ns] ) ) {
577 $this->mTemplates[$ns] = array();
578 }
579 $this->mTemplates[$ns][$dbk] = $page_id;
580 if ( !isset( $this->mTemplateIds[$ns] ) ) {
581 $this->mTemplateIds[$ns] = array();
582 }
583 $this->mTemplateIds[$ns][$dbk] = $rev_id; // For versioning
584 }
585
586 /**
587 * @param Title $title Title object, must be an interwiki link
588 * @throws MWException If given invalid input
589 */
590 public function addInterwikiLink( $title ) {
591 if ( !$title->isExternal() ) {
592 throw new MWException( 'Non-interwiki link passed, internal parser error.' );
593 }
594 $prefix = $title->getInterwiki();
595 if ( !isset( $this->mInterwikiLinks[$prefix] ) ) {
596 $this->mInterwikiLinks[$prefix] = array();
597 }
598 $this->mInterwikiLinks[$prefix][$title->getDBkey()] = 1;
599 }
600
601 /**
602 * Add some text to the "<head>".
603 * If $tag is set, the section with that tag will only be included once
604 * in a given page.
605 * @param string $section
606 * @param string|bool $tag
607 */
608 public function addHeadItem( $section, $tag = false ) {
609 if ( $tag !== false ) {
610 $this->mHeadItems[$tag] = $section;
611 } else {
612 $this->mHeadItems[] = $section;
613 }
614 }
615
616 public function addModules( $modules ) {
617 $this->mModules = array_merge( $this->mModules, (array)$modules );
618 }
619
620 public function addModuleScripts( $modules ) {
621 $this->mModuleScripts = array_merge( $this->mModuleScripts, (array)$modules );
622 }
623
624 public function addModuleStyles( $modules ) {
625 $this->mModuleStyles = array_merge( $this->mModuleStyles, (array)$modules );
626 }
627
628 /**
629 * @deprecated since 1.26 Use addModules() instead
630 * @param string|array $modules
631 */
632 public function addModuleMessages( $modules ) {
633 wfDeprecated( __METHOD__, '1.26' );
634 }
635
636 /**
637 * Add one or more variables to be set in mw.config in JavaScript.
638 *
639 * @param string|array $keys Key or array of key/value pairs.
640 * @param mixed $value [optional] Value of the configuration variable.
641 * @since 1.23
642 */
643 public function addJsConfigVars( $keys, $value = null ) {
644 if ( is_array( $keys ) ) {
645 foreach ( $keys as $key => $value ) {
646 $this->mJsConfigVars[$key] = $value;
647 }
648 return;
649 }
650
651 $this->mJsConfigVars[$keys] = $value;
652 }
653
654 /**
655 * Copy items from the OutputPage object into this one
656 *
657 * @param OutputPage $out
658 */
659 public function addOutputPageMetadata( OutputPage $out ) {
660 $this->addModules( $out->getModules() );
661 $this->addModuleScripts( $out->getModuleScripts() );
662 $this->addModuleStyles( $out->getModuleStyles() );
663 $this->addJsConfigVars( $out->getJsConfigVars() );
664
665 $this->mHeadItems = array_merge( $this->mHeadItems, $out->getHeadItemsArray() );
666 $this->mPreventClickjacking = $this->mPreventClickjacking || $out->getPreventClickjacking();
667 }
668
669 /**
670 * Add a tracking category, getting the title from a system message,
671 * or print a debug message if the title is invalid.
672 *
673 * Any message used with this function should be registered so it will
674 * show up on Special:TrackingCategories. Core messages should be added
675 * to SpecialTrackingCategories::$coreTrackingCategories, and extensions
676 * should add to "TrackingCategories" in their extension.json.
677 *
678 * @param string $msg Message key
679 * @param Title $title title of the page which is being tracked
680 * @return bool Whether the addition was successful
681 * @since 1.25
682 */
683 public function addTrackingCategory( $msg, $title ) {
684 if ( $title->getNamespace() === NS_SPECIAL ) {
685 wfDebug( __METHOD__ . ": Not adding tracking category $msg to special page!\n" );
686 return false;
687 }
688
689 // Important to parse with correct title (bug 31469)
690 $cat = wfMessage( $msg )
691 ->title( $title )
692 ->inContentLanguage()
693 ->text();
694
695 # Allow tracking categories to be disabled by setting them to "-"
696 if ( $cat === '-' ) {
697 return false;
698 }
699
700 $containerCategory = Title::makeTitleSafe( NS_CATEGORY, $cat );
701 if ( $containerCategory ) {
702 $this->addCategory( $containerCategory->getDBkey(), $this->getProperty( 'defaultsort' ) ?: '' );
703 return true;
704 } else {
705 wfDebug( __METHOD__ . ": [[MediaWiki:$msg]] is not a valid title!\n" );
706 return false;
707 }
708 }
709
710 /**
711 * Override the title to be used for display
712 *
713 * @note this is assumed to have been validated
714 * (check equal normalisation, etc.)
715 *
716 * @note this is expected to be safe HTML,
717 * ready to be served to the client.
718 *
719 * @param string $text Desired title text
720 */
721 public function setDisplayTitle( $text ) {
722 $this->setTitleText( $text );
723 $this->setProperty( 'displaytitle', $text );
724 }
725
726 /**
727 * Get the title to be used for display.
728 *
729 * As per the contract of setDisplayTitle(), this is safe HTML,
730 * ready to be served to the client.
731 *
732 * @return string HTML
733 */
734 public function getDisplayTitle() {
735 $t = $this->getTitleText();
736 if ( $t === '' ) {
737 return false;
738 }
739 return $t;
740 }
741
742 /**
743 * Fairly generic flag setter thingy.
744 * @param string $flag
745 */
746 public function setFlag( $flag ) {
747 $this->mFlags[$flag] = true;
748 }
749
750 public function getFlag( $flag ) {
751 return isset( $this->mFlags[$flag] );
752 }
753
754 /**
755 * Set a property to be stored in the page_props database table.
756 *
757 * page_props is a key value store indexed by the page ID. This allows
758 * the parser to set a property on a page which can then be quickly
759 * retrieved given the page ID or via a DB join when given the page
760 * title.
761 *
762 * Since 1.23, page_props are also indexed by numeric value, to allow
763 * for efficient "top k" queries of pages wrt a given property.
764 *
765 * setProperty() is thus used to propagate properties from the parsed
766 * page to request contexts other than a page view of the currently parsed
767 * article.
768 *
769 * Some applications examples:
770 *
771 * * To implement hidden categories, hiding pages from category listings
772 * by storing a property.
773 *
774 * * Overriding the displayed article title.
775 * @see ParserOutput::setDisplayTitle()
776 *
777 * * To implement image tagging, for example displaying an icon on an
778 * image thumbnail to indicate that it is listed for deletion on
779 * Wikimedia Commons.
780 * This is not actually implemented, yet but would be pretty cool.
781 *
782 * @note Do not use setProperty() to set a property which is only used
783 * in a context where the ParserOutput object itself is already available,
784 * for example a normal page view. There is no need to save such a property
785 * in the database since the text is already parsed. You can just hook
786 * OutputPageParserOutput and get your data out of the ParserOutput object.
787 *
788 * If you are writing an extension where you want to set a property in the
789 * parser which is used by an OutputPageParserOutput hook, you have to
790 * associate the extension data directly with the ParserOutput object.
791 * Since MediaWiki 1.21, you can use setExtensionData() to do this:
792 *
793 * @par Example:
794 * @code
795 * $parser->getOutput()->setExtensionData( 'my_ext_foo', '...' );
796 * @endcode
797 *
798 * And then later, in OutputPageParserOutput or similar:
799 *
800 * @par Example:
801 * @code
802 * $output->getExtensionData( 'my_ext_foo' );
803 * @endcode
804 *
805 * In MediaWiki 1.20 and older, you have to use a custom member variable
806 * within the ParserOutput object:
807 *
808 * @par Example:
809 * @code
810 * $parser->getOutput()->my_ext_foo = '...';
811 * @endcode
812 *
813 */
814 public function setProperty( $name, $value ) {
815 $this->mProperties[$name] = $value;
816 }
817
818 /**
819 * @param string $name The property name to look up.
820 *
821 * @return mixed|bool The value previously set using setProperty(). False if null or no value
822 * was set for the given property name.
823 *
824 * @note You need to use getProperties() to check for boolean and null properties.
825 */
826 public function getProperty( $name ) {
827 return isset( $this->mProperties[$name] ) ? $this->mProperties[$name] : false;
828 }
829
830 public function unsetProperty( $name ) {
831 unset( $this->mProperties[$name] );
832 }
833
834 public function getProperties() {
835 if ( !isset( $this->mProperties ) ) {
836 $this->mProperties = array();
837 }
838 return $this->mProperties;
839 }
840
841 /**
842 * Returns the options from its ParserOptions which have been taken
843 * into account to produce this output or false if not available.
844 * @return array
845 */
846 public function getUsedOptions() {
847 if ( !isset( $this->mAccessedOptions ) ) {
848 return array();
849 }
850 return array_keys( $this->mAccessedOptions );
851 }
852
853 /**
854 * Tags a parser option for use in the cache key for this parser output.
855 * Registered as a watcher at ParserOptions::registerWatcher() by Parser::clearState().
856 * The information gathered here is available via getUsedOptions(),
857 * and is used by ParserCache::save().
858 *
859 * @see ParserCache::getKey
860 * @see ParserCache::save
861 * @see ParserOptions::addExtraKey
862 * @see ParserOptions::optionsHash
863 * @param string $option
864 */
865 public function recordOption( $option ) {
866 $this->mAccessedOptions[$option] = true;
867 }
868
869 /**
870 * Attaches arbitrary data to this ParserObject. This can be used to store some information in
871 * the ParserOutput object for later use during page output. The data will be cached along with
872 * the ParserOutput object, but unlike data set using setProperty(), it is not recorded in the
873 * database.
874 *
875 * This method is provided to overcome the unsafe practice of attaching extra information to a
876 * ParserObject by directly assigning member variables.
877 *
878 * To use setExtensionData() to pass extension information from a hook inside the parser to a
879 * hook in the page output, use this in the parser hook:
880 *
881 * @par Example:
882 * @code
883 * $parser->getOutput()->setExtensionData( 'my_ext_foo', '...' );
884 * @endcode
885 *
886 * And then later, in OutputPageParserOutput or similar:
887 *
888 * @par Example:
889 * @code
890 * $output->getExtensionData( 'my_ext_foo' );
891 * @endcode
892 *
893 * In MediaWiki 1.20 and older, you have to use a custom member variable
894 * within the ParserOutput object:
895 *
896 * @par Example:
897 * @code
898 * $parser->getOutput()->my_ext_foo = '...';
899 * @endcode
900 *
901 * @since 1.21
902 *
903 * @param string $key The key for accessing the data. Extensions should take care to avoid
904 * conflicts in naming keys. It is suggested to use the extension's name as a prefix.
905 *
906 * @param mixed $value The value to set. Setting a value to null is equivalent to removing
907 * the value.
908 */
909 public function setExtensionData( $key, $value ) {
910 if ( $value === null ) {
911 unset( $this->mExtensionData[$key] );
912 } else {
913 $this->mExtensionData[$key] = $value;
914 }
915 }
916
917 /**
918 * Gets extensions data previously attached to this ParserOutput using setExtensionData().
919 * Typically, such data would be set while parsing the page, e.g. by a parser function.
920 *
921 * @since 1.21
922 *
923 * @param string $key The key to look up.
924 *
925 * @return mixed|null The value previously set for the given key using setExtensionData()
926 * or null if no value was set for this key.
927 */
928 public function getExtensionData( $key ) {
929 if ( isset( $this->mExtensionData[$key] ) ) {
930 return $this->mExtensionData[$key];
931 }
932
933 return null;
934 }
935
936 private static function getTimes( $clock = null ) {
937 $ret = array();
938 if ( !$clock || $clock === 'wall' ) {
939 $ret['wall'] = microtime( true );
940 }
941 if ( !$clock || $clock === 'cpu' ) {
942 $ru = wfGetRusage();
943 if ( $ru ) {
944 $ret['cpu'] = $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6;
945 $ret['cpu'] += $ru['ru_stime.tv_sec'] + $ru['ru_stime.tv_usec'] / 1e6;
946 }
947 }
948 return $ret;
949 }
950
951 /**
952 * Resets the parse start timestamps for future calls to getTimeSinceStart()
953 * @since 1.22
954 */
955 public function resetParseStartTime() {
956 $this->mParseStartTime = self::getTimes();
957 }
958
959 /**
960 * Returns the time since resetParseStartTime() was last called
961 *
962 * Clocks available are:
963 * - wall: Wall clock time
964 * - cpu: CPU time (requires getrusage)
965 *
966 * @since 1.22
967 * @param string $clock
968 * @return float|null
969 */
970 public function getTimeSinceStart( $clock ) {
971 if ( !isset( $this->mParseStartTime[$clock] ) ) {
972 return null;
973 }
974
975 $end = self::getTimes( $clock );
976 return $end[$clock] - $this->mParseStartTime[$clock];
977 }
978
979 /**
980 * Sets parser limit report data for a key
981 *
982 * The key is used as the prefix for various messages used for formatting:
983 * - $key: The label for the field in the limit report
984 * - $key-value-text: Message used to format the value in the "NewPP limit
985 * report" HTML comment. If missing, uses $key-format.
986 * - $key-value-html: Message used to format the value in the preview
987 * limit report table. If missing, uses $key-format.
988 * - $key-value: Message used to format the value. If missing, uses "$1".
989 *
990 * Note that all values are interpreted as wikitext, and so should be
991 * encoded with htmlspecialchars() as necessary, but should avoid complex
992 * HTML for sanity of display in the "NewPP limit report" comment.
993 *
994 * @since 1.22
995 * @param string $key Message key
996 * @param mixed $value Appropriate for Message::params()
997 */
998 public function setLimitReportData( $key, $value ) {
999 $this->mLimitReportData[$key] = $value;
1000 }
1001
1002 /**
1003 * Check whether the cache TTL was lowered due to dynamic content
1004 *
1005 * When content is determined by more than hard state (e.g. page edits),
1006 * such as template/file transclusions based on the current timestamp or
1007 * extension tags that generate lists based on queries, this return true.
1008 *
1009 * @return bool
1010 * @since 1.25
1011 */
1012 public function hasDynamicContent() {
1013 global $wgParserCacheExpireTime;
1014
1015 return $this->getCacheExpiry() < $wgParserCacheExpireTime;
1016 }
1017
1018 /**
1019 * Get or set the prevent-clickjacking flag
1020 *
1021 * @since 1.24
1022 * @param bool|null $flag New flag value, or null to leave it unchanged
1023 * @return bool Old flag value
1024 */
1025 public function preventClickjacking( $flag = null ) {
1026 return wfSetVar( $this->mPreventClickjacking, $flag );
1027 }
1028
1029 /**
1030 * Save space for serialization by removing useless values
1031 * @return array
1032 */
1033 public function __sleep() {
1034 return array_diff(
1035 array_keys( get_object_vars( $this ) ),
1036 array( 'mParseStartTime' )
1037 );
1038 }
1039 }