Merge "resourceloader: Tidy up RL to simplify ResourceLoaderEditToolbarModule"
[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
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 * -- this is assumed to have been validated
704 * (check equal normalisation, etc.)
705 *
706 * @param string $text Desired title text
707 */
708 public function setDisplayTitle( $text ) {
709 $this->setTitleText( $text );
710 $this->setProperty( 'displaytitle', $text );
711 }
712
713 /**
714 * Get the title to be used for display
715 *
716 * @return string
717 */
718 public function getDisplayTitle() {
719 $t = $this->getTitleText();
720 if ( $t === '' ) {
721 return false;
722 }
723 return $t;
724 }
725
726 /**
727 * Fairly generic flag setter thingy.
728 * @param string $flag
729 */
730 public function setFlag( $flag ) {
731 $this->mFlags[$flag] = true;
732 }
733
734 public function getFlag( $flag ) {
735 return isset( $this->mFlags[$flag] );
736 }
737
738 /**
739 * Set a property to be stored in the page_props database table.
740 *
741 * page_props is a key value store indexed by the page ID. This allows
742 * the parser to set a property on a page which can then be quickly
743 * retrieved given the page ID or via a DB join when given the page
744 * title.
745 *
746 * Since 1.23, page_props are also indexed by numeric value, to allow
747 * for efficient "top k" queries of pages wrt a given property.
748 *
749 * setProperty() is thus used to propagate properties from the parsed
750 * page to request contexts other than a page view of the currently parsed
751 * article.
752 *
753 * Some applications examples:
754 *
755 * * To implement hidden categories, hiding pages from category listings
756 * by storing a property.
757 *
758 * * Overriding the displayed article title.
759 * @see ParserOutput::setDisplayTitle()
760 *
761 * * To implement image tagging, for example displaying an icon on an
762 * image thumbnail to indicate that it is listed for deletion on
763 * Wikimedia Commons.
764 * This is not actually implemented, yet but would be pretty cool.
765 *
766 * @note Do not use setProperty() to set a property which is only used
767 * in a context where the ParserOutput object itself is already available,
768 * for example a normal page view. There is no need to save such a property
769 * in the database since the text is already parsed. You can just hook
770 * OutputPageParserOutput and get your data out of the ParserOutput object.
771 *
772 * If you are writing an extension where you want to set a property in the
773 * parser which is used by an OutputPageParserOutput hook, you have to
774 * associate the extension data directly with the ParserOutput object.
775 * Since MediaWiki 1.21, you can use setExtensionData() to do this:
776 *
777 * @par Example:
778 * @code
779 * $parser->getOutput()->setExtensionData( 'my_ext_foo', '...' );
780 * @endcode
781 *
782 * And then later, in OutputPageParserOutput or similar:
783 *
784 * @par Example:
785 * @code
786 * $output->getExtensionData( 'my_ext_foo' );
787 * @endcode
788 *
789 * In MediaWiki 1.20 and older, you have to use a custom member variable
790 * within the ParserOutput object:
791 *
792 * @par Example:
793 * @code
794 * $parser->getOutput()->my_ext_foo = '...';
795 * @endcode
796 *
797 */
798 public function setProperty( $name, $value ) {
799 $this->mProperties[$name] = $value;
800 }
801
802 /**
803 * @param string $name The property name to look up.
804 *
805 * @return mixed|bool The value previously set using setProperty(). False if null or no value
806 * was set for the given property name.
807 *
808 * @note You need to use getProperties() to check for boolean and null properties.
809 */
810 public function getProperty( $name ) {
811 return isset( $this->mProperties[$name] ) ? $this->mProperties[$name] : false;
812 }
813
814 public function unsetProperty( $name ) {
815 unset( $this->mProperties[$name] );
816 }
817
818 public function getProperties() {
819 if ( !isset( $this->mProperties ) ) {
820 $this->mProperties = array();
821 }
822 return $this->mProperties;
823 }
824
825 /**
826 * Returns the options from its ParserOptions which have been taken
827 * into account to produce this output or false if not available.
828 * @return array
829 */
830 public function getUsedOptions() {
831 if ( !isset( $this->mAccessedOptions ) ) {
832 return array();
833 }
834 return array_keys( $this->mAccessedOptions );
835 }
836
837 /**
838 * Tags a parser option for use in the cache key for this parser output.
839 * Registered as a watcher at ParserOptions::registerWatcher() by Parser::clearState().
840 * The information gathered here is available via getUsedOptions(),
841 * and is used by ParserCache::save().
842 *
843 * @see ParserCache::getKey
844 * @see ParserCache::save
845 * @see ParserOptions::addExtraKey
846 * @see ParserOptions::optionsHash
847 * @param string $option
848 */
849 public function recordOption( $option ) {
850 $this->mAccessedOptions[$option] = true;
851 }
852
853 /**
854 * @deprecated since 1.25. Instead, store any relevant data using setExtensionData,
855 * and implement Content::getSecondaryDataUpdates() if possible, or use the
856 * 'SecondaryDataUpdates' hook to construct the necessary update objects.
857 *
858 * @note Hard deprecation and removal without long deprecation period, since there are no
859 * known users, but known conceptual issues.
860 *
861 * @todo remove in 1.26
862 *
863 * @param DataUpdate $update
864 *
865 * @throws MWException
866 */
867 public function addSecondaryDataUpdate( DataUpdate $update ) {
868 wfDeprecated( __METHOD__, '1.25' );
869 throw new MWException(
870 'ParserOutput::addSecondaryDataUpdate() is no longer supported. ' .
871 'Override Content::getSecondaryDataUpdates() ' .
872 'or use the SecondaryDataUpdates hook instead.'
873 );
874 }
875
876 /**
877 * @deprecated since 1.25.
878 *
879 * @note Hard deprecation and removal without long deprecation period, since there are no
880 * known users, but known conceptual issues.
881 *
882 * @todo remove in 1.26
883 *
884 * @return bool false (since 1.25)
885 */
886 public function hasCustomDataUpdates() {
887 wfDeprecated( __METHOD__, '1.25' );
888 return false;
889 }
890
891 /**
892 * @deprecated since 1.25. Instead, store any relevant data using setExtensionData,
893 * and implement Content::getSecondaryDataUpdates() if possible, or use the
894 * 'SecondaryDataUpdates' hook to construct the necessary update objects.
895 *
896 * @note Hard deprecation and removal without long deprecation period, since there are no
897 * known users, but known conceptual issues.
898 *
899 * @todo remove in 1.26
900 *
901 * @param Title $title
902 * @param bool $recursive
903 *
904 * @return array An array of instances of DataUpdate
905 */
906 public function getSecondaryDataUpdates( Title $title = null, $recursive = true ) {
907 wfDeprecated( __METHOD__, '1.25' );
908 return array();
909 }
910
911 /**
912 * Attaches arbitrary data to this ParserObject. This can be used to store some information in
913 * the ParserOutput object for later use during page output. The data will be cached along with
914 * the ParserOutput object, but unlike data set using setProperty(), it is not recorded in the
915 * database.
916 *
917 * This method is provided to overcome the unsafe practice of attaching extra information to a
918 * ParserObject by directly assigning member variables.
919 *
920 * To use setExtensionData() to pass extension information from a hook inside the parser to a
921 * hook in the page output, use this in the parser hook:
922 *
923 * @par Example:
924 * @code
925 * $parser->getOutput()->setExtensionData( 'my_ext_foo', '...' );
926 * @endcode
927 *
928 * And then later, in OutputPageParserOutput or similar:
929 *
930 * @par Example:
931 * @code
932 * $output->getExtensionData( 'my_ext_foo' );
933 * @endcode
934 *
935 * In MediaWiki 1.20 and older, you have to use a custom member variable
936 * within the ParserOutput object:
937 *
938 * @par Example:
939 * @code
940 * $parser->getOutput()->my_ext_foo = '...';
941 * @endcode
942 *
943 * @since 1.21
944 *
945 * @param string $key The key for accessing the data. Extensions should take care to avoid
946 * conflicts in naming keys. It is suggested to use the extension's name as a prefix.
947 *
948 * @param mixed $value The value to set. Setting a value to null is equivalent to removing
949 * the value.
950 */
951 public function setExtensionData( $key, $value ) {
952 if ( $value === null ) {
953 unset( $this->mExtensionData[$key] );
954 } else {
955 $this->mExtensionData[$key] = $value;
956 }
957 }
958
959 /**
960 * Gets extensions data previously attached to this ParserOutput using setExtensionData().
961 * Typically, such data would be set while parsing the page, e.g. by a parser function.
962 *
963 * @since 1.21
964 *
965 * @param string $key The key to look up.
966 *
967 * @return mixed|null The value previously set for the given key using setExtensionData()
968 * or null if no value was set for this key.
969 */
970 public function getExtensionData( $key ) {
971 if ( isset( $this->mExtensionData[$key] ) ) {
972 return $this->mExtensionData[$key];
973 }
974
975 return null;
976 }
977
978 private static function getTimes( $clock = null ) {
979 $ret = array();
980 if ( !$clock || $clock === 'wall' ) {
981 $ret['wall'] = microtime( true );
982 }
983 if ( !$clock || $clock === 'cpu' ) {
984 $ru = wfGetRusage();
985 if ( $ru ) {
986 $ret['cpu'] = $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6;
987 $ret['cpu'] += $ru['ru_stime.tv_sec'] + $ru['ru_stime.tv_usec'] / 1e6;
988 }
989 }
990 return $ret;
991 }
992
993 /**
994 * Resets the parse start timestamps for future calls to getTimeSinceStart()
995 * @since 1.22
996 */
997 public function resetParseStartTime() {
998 $this->mParseStartTime = self::getTimes();
999 }
1000
1001 /**
1002 * Returns the time since resetParseStartTime() was last called
1003 *
1004 * Clocks available are:
1005 * - wall: Wall clock time
1006 * - cpu: CPU time (requires getrusage)
1007 *
1008 * @since 1.22
1009 * @param string $clock
1010 * @return float|null
1011 */
1012 public function getTimeSinceStart( $clock ) {
1013 if ( !isset( $this->mParseStartTime[$clock] ) ) {
1014 return null;
1015 }
1016
1017 $end = self::getTimes( $clock );
1018 return $end[$clock] - $this->mParseStartTime[$clock];
1019 }
1020
1021 /**
1022 * Sets parser limit report data for a key
1023 *
1024 * The key is used as the prefix for various messages used for formatting:
1025 * - $key: The label for the field in the limit report
1026 * - $key-value-text: Message used to format the value in the "NewPP limit
1027 * report" HTML comment. If missing, uses $key-format.
1028 * - $key-value-html: Message used to format the value in the preview
1029 * limit report table. If missing, uses $key-format.
1030 * - $key-value: Message used to format the value. If missing, uses "$1".
1031 *
1032 * Note that all values are interpreted as wikitext, and so should be
1033 * encoded with htmlspecialchars() as necessary, but should avoid complex
1034 * HTML for sanity of display in the "NewPP limit report" comment.
1035 *
1036 * @since 1.22
1037 * @param string $key Message key
1038 * @param mixed $value Appropriate for Message::params()
1039 */
1040 public function setLimitReportData( $key, $value ) {
1041 $this->mLimitReportData[$key] = $value;
1042 }
1043
1044 /**
1045 * Check whether the cache TTL was lowered due to dynamic content
1046 *
1047 * When content is determined by more than hard state (e.g. page edits),
1048 * such as template/file transclusions based on the current timestamp or
1049 * extension tags that generate lists based on queries, this return true.
1050 *
1051 * @return bool
1052 * @since 1.25
1053 */
1054 public function hasDynamicContent() {
1055 global $wgParserCacheExpireTime;
1056
1057 return $this->getCacheExpiry() < $wgParserCacheExpireTime;
1058 }
1059
1060 /**
1061 * Get or set the prevent-clickjacking flag
1062 *
1063 * @since 1.24
1064 * @param bool|null $flag New flag value, or null to leave it unchanged
1065 * @return bool Old flag value
1066 */
1067 public function preventClickjacking( $flag = null ) {
1068 return wfSetVar( $this->mPreventClickjacking, $flag );
1069 }
1070
1071 /**
1072 * Save space for serialization by removing useless values
1073 * @return array
1074 */
1075 public function __sleep() {
1076 return array_diff(
1077 array_keys( get_object_vars( $this ) ),
1078 array( 'mParseStartTime' )
1079 );
1080 }
1081 }