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