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