parser: inject the time for {{REVISIONTIMESTAMP}} on pre-save parse
[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
25 class ParserOutput extends CacheTime {
26 /**
27 * Feature flags to indicate to extensions that MediaWiki core supports and
28 * uses getText() stateless transforms.
29 */
30 const SUPPORTS_STATELESS_TRANSFORMS = 1;
31 const SUPPORTS_UNWRAP_TRANSFORM = 1;
32
33 /**
34 * @var string|null $mText The output text
35 */
36 public $mText = null;
37
38 /**
39 * @var array $mLanguageLinks List of the full text of language links,
40 * in the order they appear.
41 */
42 public $mLanguageLinks;
43
44 /**
45 * @var array $mCategories Map of category names to sort keys
46 */
47 public $mCategories;
48
49 /**
50 * @var array $mIndicators Page status indicators, usually displayed in top-right corner.
51 */
52 public $mIndicators = [];
53
54 /**
55 * @var string $mTitleText Title text of the chosen language variant, as HTML.
56 */
57 public $mTitleText;
58
59 /**
60 * @var array $mLinks 2-D map of NS/DBK to ID for the links in the document.
61 * ID=zero for broken.
62 */
63 public $mLinks = [];
64
65 /**
66 * @var array $mTemplates 2-D map of NS/DBK to ID for the template references.
67 * ID=zero for broken.
68 */
69 public $mTemplates = [];
70
71 /**
72 * @var array $mTemplateIds 2-D map of NS/DBK to rev ID for the template references.
73 * ID=zero for broken.
74 */
75 public $mTemplateIds = [];
76
77 /**
78 * @var array $mImages DB keys of the images used, in the array key only
79 */
80 public $mImages = [];
81
82 /**
83 * @var array $mFileSearchOptions DB keys of the images used mapped to sha1 and MW timestamp.
84 */
85 public $mFileSearchOptions = [];
86
87 /**
88 * @var array $mExternalLinks External link URLs, in the key only.
89 */
90 public $mExternalLinks = [];
91
92 /**
93 * @var array $mInterwikiLinks 2-D map of prefix/DBK (in keys only)
94 * for the inline interwiki links in the document.
95 */
96 public $mInterwikiLinks = [];
97
98 /**
99 * @var bool $mNewSection Show a new section link?
100 */
101 public $mNewSection = false;
102
103 /**
104 * @var bool $mHideNewSection Hide the new section link?
105 */
106 public $mHideNewSection = false;
107
108 /**
109 * @var bool $mNoGallery No gallery on category page? (__NOGALLERY__).
110 */
111 public $mNoGallery = false;
112
113 /**
114 * @var array $mHeadItems Items to put in the <head> section
115 */
116 public $mHeadItems = [];
117
118 /**
119 * @var array $mModules Modules to be loaded by ResourceLoader
120 */
121 public $mModules = [];
122
123 /**
124 * @var array $mModuleStyles Modules of which only the CSSS will be loaded by ResourceLoader.
125 */
126 public $mModuleStyles = [];
127
128 /**
129 * @var array $mJsConfigVars JavaScript config variable for mw.config combined with this page.
130 */
131 public $mJsConfigVars = [];
132
133 /**
134 * @var array $mOutputHooks Hook tags as per $wgParserOutputHooks.
135 */
136 public $mOutputHooks = [];
137
138 /**
139 * @var array $mWarnings Warning text to be returned to the user.
140 * Wikitext formatted, in the key only.
141 */
142 public $mWarnings = [];
143
144 /**
145 * @var array $mSections Table of contents
146 */
147 public $mSections = [];
148
149 /**
150 * @var array $mProperties Name/value pairs to be cached in the DB.
151 */
152 public $mProperties = [];
153
154 /**
155 * @var string $mTOCHTML HTML of the TOC.
156 */
157 public $mTOCHTML = '';
158
159 /**
160 * @var string $mTimestamp Timestamp of the revision.
161 */
162 public $mTimestamp;
163
164 /**
165 * @var bool $mEnableOOUI Whether OOUI should be enabled.
166 */
167 public $mEnableOOUI = false;
168
169 /**
170 * @var string $mIndexPolicy 'index' or 'noindex'? Any other value will result in no change.
171 */
172 private $mIndexPolicy = '';
173
174 /**
175 * @var true[] $mAccessedOptions List of ParserOptions (stored in the keys).
176 */
177 private $mAccessedOptions = [];
178
179 /**
180 * @var array $mExtensionData extra data used by extensions.
181 */
182 private $mExtensionData = [];
183
184 /**
185 * @var array $mLimitReportData Parser limit report data.
186 */
187 private $mLimitReportData = [];
188
189 /** @var array Parser limit report data for JSON */
190 private $mLimitReportJSData = [];
191
192 /**
193 * @var array $mParseStartTime Timestamps for getTimeSinceStart().
194 */
195 private $mParseStartTime = [];
196
197 /**
198 * @var bool $mPreventClickjacking Whether to emit X-Frame-Options: DENY.
199 */
200 private $mPreventClickjacking = false;
201
202 /**
203 * @var array $mFlags Generic flags.
204 */
205 private $mFlags = [];
206
207 /** @var int|null Assumed rev ID for {{REVISIONID}} if no revision is set */
208 private $mSpeculativeRevId;
209
210 /** @var int|null Assumed rev timestamp for {{REVISIONTIMESTAMP}} if no revision is set */
211 private $revisionTimestampUsed;
212
213 /** string CSS classes to use for the wrapping div, stored in the array keys.
214 * If no class is given, no wrapper is added.
215 */
216 private $mWrapperDivClasses = [];
217
218 /** @var int Upper bound of expiry based on parse duration */
219 private $mMaxAdaptiveExpiry = INF;
220
221 const EDITSECTION_REGEX =
222 '#<(?:mw:)?editsection page="(.*?)" section="(.*?)"(?:/>|>(.*?)(</(?:mw:)?editsection>))#s';
223
224 // finalizeAdaptiveCacheExpiry() uses TTL = MAX( m * PARSE_TIME + b, MIN_AR_TTL)
225 // Current values imply that m=3933.333333 and b=-333.333333
226 // See https://www.nngroup.com/articles/website-response-times/
227 const PARSE_FAST_SEC = 0.100; // perceived "fast" page parse
228 const PARSE_SLOW_SEC = 1.0; // perceived "slow" page parse
229 const FAST_AR_TTL = 60; // adaptive TTL for "fast" pages
230 const SLOW_AR_TTL = 3600; // adaptive TTL for "slow" pages
231 const MIN_AR_TTL = 15; // min adaptive TTL (for sanity, pool counter, and edit stashing)
232
233 /**
234 * @param string|null $text HTML. Use null to indicate that this ParserOutput contains only
235 * meta-data, and the HTML output is undetermined, as opposed to empty. Passing null
236 * here causes hasText() to return false.
237 * @param array $languageLinks
238 * @param array $categoryLinks
239 * @param bool $unused
240 * @param string $titletext
241 */
242 public function __construct( $text = '', $languageLinks = [], $categoryLinks = [],
243 $unused = false, $titletext = ''
244 ) {
245 $this->mText = $text;
246 $this->mLanguageLinks = $languageLinks;
247 $this->mCategories = $categoryLinks;
248 $this->mTitleText = $titletext;
249 }
250
251 /**
252 * Returns true if text was passed to the constructor, or set using setText(). Returns false
253 * if null was passed to the $text parameter of the constructor to indicate that this
254 * ParserOutput only contains meta-data, and the HTML output is undetermined.
255 *
256 * @since 1.32
257 *
258 * @return bool Whether this ParserOutput contains rendered text. If this returns false, the
259 * ParserOutput contains meta-data only.
260 */
261 public function hasText() {
262 return ( $this->mText !== null );
263 }
264
265 /**
266 * Get the cacheable text with <mw:editsection> markers still in it. The
267 * return value is suitable for writing back via setText() but is not valid
268 * for display to the user.
269 *
270 * @return string
271 * @since 1.27
272 */
273 public function getRawText() {
274 if ( $this->mText === null ) {
275 throw new LogicException( 'This ParserOutput contains no text!' );
276 }
277
278 return $this->mText;
279 }
280
281 /**
282 * Get the output HTML
283 *
284 * @param array $options (since 1.31) Transformations to apply to the HTML
285 * - allowTOC: (bool) Show the TOC, assuming there were enough headings
286 * to generate one and `__NOTOC__` wasn't used. Default is true,
287 * but might be statefully overridden.
288 * - enableSectionEditLinks: (bool) Include section edit links, assuming
289 * section edit link tokens are present in the HTML. Default is true,
290 * but might be statefully overridden.
291 * - unwrap: (bool) Return text without a wrapper div. Default is false,
292 * meaning a wrapper div will be added if getWrapperDivClass() returns
293 * a non-empty string.
294 * - wrapperDivClass: (string) Wrap the output in a div and apply the given
295 * CSS class to that div. This overrides the output of getWrapperDivClass().
296 * Setting this to an empty string has the same effect as 'unwrap' => true.
297 * - deduplicateStyles: (bool) When true, which is the default, `<style>`
298 * tags with the `data-mw-deduplicate` attribute set are deduplicated by
299 * value of the attribute: all but the first will be replaced by `<link
300 * rel="mw-deduplicated-inline-style" href="mw-data:..."/>` tags, where
301 * the scheme-specific-part of the href is the (percent-encoded) value
302 * of the `data-mw-deduplicate` attribute.
303 * @return string HTML
304 * @return-taint escaped
305 */
306 public function getText( $options = [] ) {
307 $options += [
308 'allowTOC' => true,
309 'enableSectionEditLinks' => true,
310 'unwrap' => false,
311 'deduplicateStyles' => true,
312 'wrapperDivClass' => $this->getWrapperDivClass(),
313 ];
314 $text = $this->getRawText();
315
316 Hooks::runWithoutAbort( 'ParserOutputPostCacheTransform', [ $this, &$text, &$options ] );
317
318 if ( $options['wrapperDivClass'] !== '' && !$options['unwrap'] ) {
319 $text = Html::rawElement( 'div', [ 'class' => $options['wrapperDivClass'] ], $text );
320 }
321
322 if ( $options['enableSectionEditLinks'] ) {
323 $text = preg_replace_callback(
324 self::EDITSECTION_REGEX,
325 function ( $m ) {
326 $editsectionPage = Title::newFromText( htmlspecialchars_decode( $m[1] ) );
327 $editsectionSection = htmlspecialchars_decode( $m[2] );
328 $editsectionContent = isset( $m[4] ) ? Sanitizer::decodeCharReferences( $m[3] ) : null;
329
330 if ( !is_object( $editsectionPage ) ) {
331 throw new MWException( "Bad parser output text." );
332 }
333
334 $context = RequestContext::getMain();
335 return $context->getSkin()->doEditSectionLink(
336 $editsectionPage,
337 $editsectionSection,
338 $editsectionContent,
339 $context->getLanguage()
340 );
341 },
342 $text
343 );
344 } else {
345 $text = preg_replace( self::EDITSECTION_REGEX, '', $text );
346 }
347
348 if ( $options['allowTOC'] ) {
349 $text = str_replace( [ Parser::TOC_START, Parser::TOC_END ], '', $text );
350 } else {
351 $text = preg_replace(
352 '#' . preg_quote( Parser::TOC_START, '#' ) . '.*?' . preg_quote( Parser::TOC_END, '#' ) . '#s',
353 '',
354 $text
355 );
356 }
357
358 if ( $options['deduplicateStyles'] ) {
359 $seen = [];
360 $text = preg_replace_callback(
361 '#<style\s+([^>]*data-mw-deduplicate\s*=[^>]*)>.*?</style>#s',
362 function ( $m ) use ( &$seen ) {
363 $attr = Sanitizer::decodeTagAttributes( $m[1] );
364 if ( !isset( $attr['data-mw-deduplicate'] ) ) {
365 return $m[0];
366 }
367
368 $key = $attr['data-mw-deduplicate'];
369 if ( !isset( $seen[$key] ) ) {
370 $seen[$key] = true;
371 return $m[0];
372 }
373
374 // We were going to use an empty <style> here, but there
375 // was concern that would be too much overhead for browsers.
376 // So let's hope a <link> with a non-standard rel and href isn't
377 // going to be misinterpreted or mangled by any subsequent processing.
378 return Html::element( 'link', [
379 'rel' => 'mw-deduplicated-inline-style',
380 'href' => "mw-data:" . wfUrlencode( $key ),
381 ] );
382 },
383 $text
384 );
385 }
386
387 // Hydrate slot section header placeholders generated by RevisionRenderer.
388 $text = preg_replace_callback(
389 '#<mw:slotheader>(.*?)</mw:slotheader>#',
390 function ( $m ) {
391 $role = htmlspecialchars_decode( $m[1] );
392 // TODO: map to message, using the interface language. Set lang="xyz" accordingly.
393 $headerText = $role;
394 return $headerText;
395 },
396 $text
397 );
398 return $text;
399 }
400
401 /**
402 * Add a CSS class to use for the wrapping div. If no class is given, no wrapper is added.
403 *
404 * @param string $class
405 */
406 public function addWrapperDivClass( $class ) {
407 $this->mWrapperDivClasses[$class] = true;
408 }
409
410 /**
411 * Clears the CSS class to use for the wrapping div, effectively disabling the wrapper div
412 * until addWrapperDivClass() is called.
413 */
414 public function clearWrapperDivClass() {
415 $this->mWrapperDivClasses = [];
416 }
417
418 /**
419 * Returns the class (or classes) to be used with the wrapper div for this otuput.
420 * If there is no wrapper class given, no wrapper div should be added.
421 * The wrapper div is added automatically by getText().
422 *
423 * @return string
424 */
425 public function getWrapperDivClass() {
426 return implode( ' ', array_keys( $this->mWrapperDivClasses ) );
427 }
428
429 /**
430 * @param int $id
431 * @since 1.28
432 */
433 public function setSpeculativeRevIdUsed( $id ) {
434 $this->mSpeculativeRevId = $id;
435 }
436
437 /**
438 * @return int|null
439 * @since 1.28
440 */
441 public function getSpeculativeRevIdUsed() {
442 return $this->mSpeculativeRevId;
443 }
444
445 /**
446 * @param string $timestamp TS_MW timestamp
447 * @since 1.34
448 */
449 public function setRevisionTimestampUsed( $timestamp ) {
450 $this->revisionTimestampUsed = $timestamp;
451 }
452
453 /**
454 * @return string|null TS_MW timestamp or null if not used
455 * @since 1.34
456 */
457 public function getRevisionTimestampUsed() {
458 return $this->revisionTimestampUsed;
459 }
460
461 public function &getLanguageLinks() {
462 return $this->mLanguageLinks;
463 }
464
465 public function getInterwikiLinks() {
466 return $this->mInterwikiLinks;
467 }
468
469 public function getCategoryLinks() {
470 return array_keys( $this->mCategories );
471 }
472
473 public function &getCategories() {
474 return $this->mCategories;
475 }
476
477 /**
478 * @return array
479 * @since 1.25
480 */
481 public function getIndicators() {
482 return $this->mIndicators;
483 }
484
485 public function getTitleText() {
486 return $this->mTitleText;
487 }
488
489 public function getSections() {
490 return $this->mSections;
491 }
492
493 public function &getLinks() {
494 return $this->mLinks;
495 }
496
497 public function &getTemplates() {
498 return $this->mTemplates;
499 }
500
501 public function &getTemplateIds() {
502 return $this->mTemplateIds;
503 }
504
505 public function &getImages() {
506 return $this->mImages;
507 }
508
509 public function &getFileSearchOptions() {
510 return $this->mFileSearchOptions;
511 }
512
513 public function &getExternalLinks() {
514 return $this->mExternalLinks;
515 }
516
517 public function setNoGallery( $value ) {
518 $this->mNoGallery = (bool)$value;
519 }
520
521 public function getNoGallery() {
522 return $this->mNoGallery;
523 }
524
525 public function getHeadItems() {
526 return $this->mHeadItems;
527 }
528
529 public function getModules() {
530 return $this->mModules;
531 }
532
533 public function getModuleStyles() {
534 return $this->mModuleStyles;
535 }
536
537 /**
538 * @return array
539 * @since 1.23
540 */
541 public function getJsConfigVars() {
542 return $this->mJsConfigVars;
543 }
544
545 public function getOutputHooks() {
546 return (array)$this->mOutputHooks;
547 }
548
549 public function getWarnings() {
550 return array_keys( $this->mWarnings );
551 }
552
553 public function getIndexPolicy() {
554 return $this->mIndexPolicy;
555 }
556
557 public function getTOCHTML() {
558 return $this->mTOCHTML;
559 }
560
561 /**
562 * @return string|null TS_MW timestamp of the revision content
563 */
564 public function getTimestamp() {
565 return $this->mTimestamp;
566 }
567
568 public function getLimitReportData() {
569 return $this->mLimitReportData;
570 }
571
572 public function getLimitReportJSData() {
573 return $this->mLimitReportJSData;
574 }
575
576 public function getEnableOOUI() {
577 return $this->mEnableOOUI;
578 }
579
580 public function setText( $text ) {
581 return wfSetVar( $this->mText, $text );
582 }
583
584 public function setLanguageLinks( $ll ) {
585 return wfSetVar( $this->mLanguageLinks, $ll );
586 }
587
588 public function setCategoryLinks( $cl ) {
589 return wfSetVar( $this->mCategories, $cl );
590 }
591
592 public function setTitleText( $t ) {
593 return wfSetVar( $this->mTitleText, $t );
594 }
595
596 public function setSections( $toc ) {
597 return wfSetVar( $this->mSections, $toc );
598 }
599
600 public function setIndexPolicy( $policy ) {
601 return wfSetVar( $this->mIndexPolicy, $policy );
602 }
603
604 public function setTOCHTML( $tochtml ) {
605 return wfSetVar( $this->mTOCHTML, $tochtml );
606 }
607
608 public function setTimestamp( $timestamp ) {
609 return wfSetVar( $this->mTimestamp, $timestamp );
610 }
611
612 public function addCategory( $c, $sort ) {
613 $this->mCategories[$c] = $sort;
614 }
615
616 /**
617 * @param string $id
618 * @param string $content
619 * @since 1.25
620 */
621 public function setIndicator( $id, $content ) {
622 $this->mIndicators[$id] = $content;
623 }
624
625 /**
626 * Enables OOUI, if true, in any OutputPage instance this ParserOutput
627 * object is added to.
628 *
629 * @since 1.26
630 * @param bool $enable If OOUI should be enabled or not
631 */
632 public function setEnableOOUI( $enable = false ) {
633 $this->mEnableOOUI = $enable;
634 }
635
636 public function addLanguageLink( $t ) {
637 $this->mLanguageLinks[] = $t;
638 }
639
640 public function addWarning( $s ) {
641 $this->mWarnings[$s] = 1;
642 }
643
644 public function addOutputHook( $hook, $data = false ) {
645 $this->mOutputHooks[] = [ $hook, $data ];
646 }
647
648 public function setNewSection( $value ) {
649 $this->mNewSection = (bool)$value;
650 }
651
652 public function hideNewSection( $value ) {
653 $this->mHideNewSection = (bool)$value;
654 }
655
656 public function getHideNewSection() {
657 return (bool)$this->mHideNewSection;
658 }
659
660 public function getNewSection() {
661 return (bool)$this->mNewSection;
662 }
663
664 /**
665 * Checks, if a url is pointing to the own server
666 *
667 * @param string $internal The server to check against
668 * @param string $url The url to check
669 * @return bool
670 */
671 public static function isLinkInternal( $internal, $url ) {
672 return (bool)preg_match( '/^' .
673 # If server is proto relative, check also for http/https links
674 ( substr( $internal, 0, 2 ) === '//' ? '(?:https?:)?' : '' ) .
675 preg_quote( $internal, '/' ) .
676 # check for query/path/anchor or end of link in each case
677 '(?:[\?\/\#]|$)/i',
678 $url
679 );
680 }
681
682 public function addExternalLink( $url ) {
683 # We don't register links pointing to our own server, unless... :-)
684 global $wgServer, $wgRegisterInternalExternals;
685
686 # Replace unnecessary URL escape codes with the referenced character
687 # This prevents spammers from hiding links from the filters
688 $url = Parser::normalizeLinkUrl( $url );
689
690 $registerExternalLink = true;
691 if ( !$wgRegisterInternalExternals ) {
692 $registerExternalLink = !self::isLinkInternal( $wgServer, $url );
693 }
694 if ( $registerExternalLink ) {
695 $this->mExternalLinks[$url] = 1;
696 }
697 }
698
699 /**
700 * Record a local or interwiki inline link for saving in future link tables.
701 *
702 * @param Title $title
703 * @param int|null $id Optional known page_id so we can skip the lookup
704 */
705 public function addLink( Title $title, $id = null ) {
706 if ( $title->isExternal() ) {
707 // Don't record interwikis in pagelinks
708 $this->addInterwikiLink( $title );
709 return;
710 }
711 $ns = $title->getNamespace();
712 $dbk = $title->getDBkey();
713 if ( $ns == NS_MEDIA ) {
714 // Normalize this pseudo-alias if it makes it down here...
715 $ns = NS_FILE;
716 } elseif ( $ns == NS_SPECIAL ) {
717 // We don't record Special: links currently
718 // It might actually be wise to, but we'd need to do some normalization.
719 return;
720 } elseif ( $dbk === '' ) {
721 // Don't record self links - [[#Foo]]
722 return;
723 }
724 if ( !isset( $this->mLinks[$ns] ) ) {
725 $this->mLinks[$ns] = [];
726 }
727 if ( is_null( $id ) ) {
728 $id = $title->getArticleID();
729 }
730 $this->mLinks[$ns][$dbk] = $id;
731 }
732
733 /**
734 * Register a file dependency for this output
735 * @param string $name Title dbKey
736 * @param string|false|null $timestamp MW timestamp of file creation (or false if non-existing)
737 * @param string|false|null $sha1 Base 36 SHA-1 of file (or false if non-existing)
738 */
739 public function addImage( $name, $timestamp = null, $sha1 = null ) {
740 $this->mImages[$name] = 1;
741 if ( $timestamp !== null && $sha1 !== null ) {
742 $this->mFileSearchOptions[$name] = [ 'time' => $timestamp, 'sha1' => $sha1 ];
743 }
744 }
745
746 /**
747 * Register a template dependency for this output
748 * @param Title $title
749 * @param int $page_id
750 * @param int $rev_id
751 */
752 public function addTemplate( $title, $page_id, $rev_id ) {
753 $ns = $title->getNamespace();
754 $dbk = $title->getDBkey();
755 if ( !isset( $this->mTemplates[$ns] ) ) {
756 $this->mTemplates[$ns] = [];
757 }
758 $this->mTemplates[$ns][$dbk] = $page_id;
759 if ( !isset( $this->mTemplateIds[$ns] ) ) {
760 $this->mTemplateIds[$ns] = [];
761 }
762 $this->mTemplateIds[$ns][$dbk] = $rev_id; // For versioning
763 }
764
765 /**
766 * @param Title $title Title object, must be an interwiki link
767 * @throws MWException If given invalid input
768 */
769 public function addInterwikiLink( $title ) {
770 if ( !$title->isExternal() ) {
771 throw new MWException( 'Non-interwiki link passed, internal parser error.' );
772 }
773 $prefix = $title->getInterwiki();
774 if ( !isset( $this->mInterwikiLinks[$prefix] ) ) {
775 $this->mInterwikiLinks[$prefix] = [];
776 }
777 $this->mInterwikiLinks[$prefix][$title->getDBkey()] = 1;
778 }
779
780 /**
781 * Add some text to the "<head>".
782 * If $tag is set, the section with that tag will only be included once
783 * in a given page.
784 * @param string $section
785 * @param string|bool $tag
786 */
787 public function addHeadItem( $section, $tag = false ) {
788 if ( $tag !== false ) {
789 $this->mHeadItems[$tag] = $section;
790 } else {
791 $this->mHeadItems[] = $section;
792 }
793 }
794
795 /**
796 * @see OutputPage::addModules
797 */
798 public function addModules( $modules ) {
799 $this->mModules = array_merge( $this->mModules, (array)$modules );
800 }
801
802 /**
803 * @see OutputPage::addModuleStyles
804 */
805 public function addModuleStyles( $modules ) {
806 $this->mModuleStyles = array_merge( $this->mModuleStyles, (array)$modules );
807 }
808
809 /**
810 * Add one or more variables to be set in mw.config in JavaScript.
811 *
812 * @param string|array $keys Key or array of key/value pairs.
813 * @param mixed|null $value [optional] Value of the configuration variable.
814 * @since 1.23
815 */
816 public function addJsConfigVars( $keys, $value = null ) {
817 if ( is_array( $keys ) ) {
818 foreach ( $keys as $key => $value ) {
819 $this->mJsConfigVars[$key] = $value;
820 }
821 return;
822 }
823
824 $this->mJsConfigVars[$keys] = $value;
825 }
826
827 /**
828 * Copy items from the OutputPage object into this one
829 *
830 * @param OutputPage $out
831 */
832 public function addOutputPageMetadata( OutputPage $out ) {
833 $this->addModules( $out->getModules() );
834 $this->addModuleStyles( $out->getModuleStyles() );
835 $this->addJsConfigVars( $out->getJsConfigVars() );
836
837 $this->mHeadItems = array_merge( $this->mHeadItems, $out->getHeadItemsArray() );
838 $this->mPreventClickjacking = $this->mPreventClickjacking || $out->getPreventClickjacking();
839 }
840
841 /**
842 * Add a tracking category, getting the title from a system message,
843 * or print a debug message if the title is invalid.
844 *
845 * Any message used with this function should be registered so it will
846 * show up on Special:TrackingCategories. Core messages should be added
847 * to SpecialTrackingCategories::$coreTrackingCategories, and extensions
848 * should add to "TrackingCategories" in their extension.json.
849 *
850 * @todo Migrate some code to TrackingCategories
851 *
852 * @param string $msg Message key
853 * @param Title $title title of the page which is being tracked
854 * @return bool Whether the addition was successful
855 * @since 1.25
856 */
857 public function addTrackingCategory( $msg, $title ) {
858 if ( $title->isSpecialPage() ) {
859 wfDebug( __METHOD__ . ": Not adding tracking category $msg to special page!\n" );
860 return false;
861 }
862
863 // Important to parse with correct title (T33469)
864 $cat = wfMessage( $msg )
865 ->title( $title )
866 ->inContentLanguage()
867 ->text();
868
869 # Allow tracking categories to be disabled by setting them to "-"
870 if ( $cat === '-' ) {
871 return false;
872 }
873
874 $containerCategory = Title::makeTitleSafe( NS_CATEGORY, $cat );
875 if ( $containerCategory ) {
876 $this->addCategory( $containerCategory->getDBkey(), $this->getProperty( 'defaultsort' ) ?: '' );
877 return true;
878 } else {
879 wfDebug( __METHOD__ . ": [[MediaWiki:$msg]] is not a valid title!\n" );
880 return false;
881 }
882 }
883
884 /**
885 * Override the title to be used for display
886 *
887 * @note this is assumed to have been validated
888 * (check equal normalisation, etc.)
889 *
890 * @note this is expected to be safe HTML,
891 * ready to be served to the client.
892 *
893 * @param string $text Desired title text
894 */
895 public function setDisplayTitle( $text ) {
896 $this->setTitleText( $text );
897 $this->setProperty( 'displaytitle', $text );
898 }
899
900 /**
901 * Get the title to be used for display.
902 *
903 * As per the contract of setDisplayTitle(), this is safe HTML,
904 * ready to be served to the client.
905 *
906 * @return string HTML
907 */
908 public function getDisplayTitle() {
909 $t = $this->getTitleText();
910 if ( $t === '' ) {
911 return false;
912 }
913 return $t;
914 }
915
916 /**
917 * Fairly generic flag setter thingy.
918 * @param string $flag
919 */
920 public function setFlag( $flag ) {
921 $this->mFlags[$flag] = true;
922 }
923
924 public function getFlag( $flag ) {
925 return isset( $this->mFlags[$flag] );
926 }
927
928 /**
929 * Set a property to be stored in the page_props database table.
930 *
931 * page_props is a key value store indexed by the page ID. This allows
932 * the parser to set a property on a page which can then be quickly
933 * retrieved given the page ID or via a DB join when given the page
934 * title.
935 *
936 * Since 1.23, page_props are also indexed by numeric value, to allow
937 * for efficient "top k" queries of pages wrt a given property.
938 *
939 * setProperty() is thus used to propagate properties from the parsed
940 * page to request contexts other than a page view of the currently parsed
941 * article.
942 *
943 * Some applications examples:
944 *
945 * * To implement hidden categories, hiding pages from category listings
946 * by storing a property.
947 *
948 * * Overriding the displayed article title (ParserOutput::setDisplayTitle()).
949 *
950 * * To implement image tagging, for example displaying an icon on an
951 * image thumbnail to indicate that it is listed for deletion on
952 * Wikimedia Commons.
953 * This is not actually implemented, yet but would be pretty cool.
954 *
955 * @note Do not use setProperty() to set a property which is only used
956 * in a context where the ParserOutput object itself is already available,
957 * for example a normal page view. There is no need to save such a property
958 * in the database since the text is already parsed. You can just hook
959 * OutputPageParserOutput and get your data out of the ParserOutput object.
960 *
961 * If you are writing an extension where you want to set a property in the
962 * parser which is used by an OutputPageParserOutput hook, you have to
963 * associate the extension data directly with the ParserOutput object.
964 * Since MediaWiki 1.21, you can use setExtensionData() to do this:
965 *
966 * @par Example:
967 * @code
968 * $parser->getOutput()->setExtensionData( 'my_ext_foo', '...' );
969 * @endcode
970 *
971 * And then later, in OutputPageParserOutput or similar:
972 *
973 * @par Example:
974 * @code
975 * $output->getExtensionData( 'my_ext_foo' );
976 * @endcode
977 *
978 * In MediaWiki 1.20 and older, you have to use a custom member variable
979 * within the ParserOutput object:
980 *
981 * @par Example:
982 * @code
983 * $parser->getOutput()->my_ext_foo = '...';
984 * @endcode
985 * @param string $name
986 * @param mixed $value
987 */
988 public function setProperty( $name, $value ) {
989 $this->mProperties[$name] = $value;
990 }
991
992 /**
993 * @param string $name The property name to look up.
994 *
995 * @return mixed|bool The value previously set using setProperty(). False if null or no value
996 * was set for the given property name.
997 *
998 * @note You need to use getProperties() to check for boolean and null properties.
999 */
1000 public function getProperty( $name ) {
1001 return $this->mProperties[$name] ?? false;
1002 }
1003
1004 public function unsetProperty( $name ) {
1005 unset( $this->mProperties[$name] );
1006 }
1007
1008 public function getProperties() {
1009 if ( !isset( $this->mProperties ) ) {
1010 $this->mProperties = [];
1011 }
1012 return $this->mProperties;
1013 }
1014
1015 /**
1016 * Returns the options from its ParserOptions which have been taken
1017 * into account to produce this output.
1018 * @return string[]
1019 */
1020 public function getUsedOptions() {
1021 if ( !isset( $this->mAccessedOptions ) ) {
1022 return [];
1023 }
1024 return array_keys( $this->mAccessedOptions );
1025 }
1026
1027 /**
1028 * Tags a parser option for use in the cache key for this parser output.
1029 * Registered as a watcher at ParserOptions::registerWatcher() by Parser::clearState().
1030 * The information gathered here is available via getUsedOptions(),
1031 * and is used by ParserCache::save().
1032 *
1033 * @see ParserCache::getKey
1034 * @see ParserCache::save
1035 * @see ParserOptions::addExtraKey
1036 * @see ParserOptions::optionsHash
1037 * @param string $option
1038 */
1039 public function recordOption( $option ) {
1040 $this->mAccessedOptions[$option] = true;
1041 }
1042
1043 /**
1044 * Attaches arbitrary data to this ParserObject. This can be used to store some information in
1045 * the ParserOutput object for later use during page output. The data will be cached along with
1046 * the ParserOutput object, but unlike data set using setProperty(), it is not recorded in the
1047 * database.
1048 *
1049 * This method is provided to overcome the unsafe practice of attaching extra information to a
1050 * ParserObject by directly assigning member variables.
1051 *
1052 * To use setExtensionData() to pass extension information from a hook inside the parser to a
1053 * hook in the page output, use this in the parser hook:
1054 *
1055 * @par Example:
1056 * @code
1057 * $parser->getOutput()->setExtensionData( 'my_ext_foo', '...' );
1058 * @endcode
1059 *
1060 * And then later, in OutputPageParserOutput or similar:
1061 *
1062 * @par Example:
1063 * @code
1064 * $output->getExtensionData( 'my_ext_foo' );
1065 * @endcode
1066 *
1067 * In MediaWiki 1.20 and older, you have to use a custom member variable
1068 * within the ParserOutput object:
1069 *
1070 * @par Example:
1071 * @code
1072 * $parser->getOutput()->my_ext_foo = '...';
1073 * @endcode
1074 *
1075 * @since 1.21
1076 *
1077 * @param string $key The key for accessing the data. Extensions should take care to avoid
1078 * conflicts in naming keys. It is suggested to use the extension's name as a prefix.
1079 *
1080 * @param mixed $value The value to set. Setting a value to null is equivalent to removing
1081 * the value.
1082 */
1083 public function setExtensionData( $key, $value ) {
1084 if ( $value === null ) {
1085 unset( $this->mExtensionData[$key] );
1086 } else {
1087 $this->mExtensionData[$key] = $value;
1088 }
1089 }
1090
1091 /**
1092 * Gets extensions data previously attached to this ParserOutput using setExtensionData().
1093 * Typically, such data would be set while parsing the page, e.g. by a parser function.
1094 *
1095 * @since 1.21
1096 *
1097 * @param string $key The key to look up.
1098 *
1099 * @return mixed|null The value previously set for the given key using setExtensionData()
1100 * or null if no value was set for this key.
1101 */
1102 public function getExtensionData( $key ) {
1103 return $this->mExtensionData[$key] ?? null;
1104 }
1105
1106 private static function getTimes( $clock = null ) {
1107 $ret = [];
1108 if ( !$clock || $clock === 'wall' ) {
1109 $ret['wall'] = microtime( true );
1110 }
1111 if ( !$clock || $clock === 'cpu' ) {
1112 $ru = wfGetRusage();
1113 if ( $ru ) {
1114 $ret['cpu'] = $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6;
1115 $ret['cpu'] += $ru['ru_stime.tv_sec'] + $ru['ru_stime.tv_usec'] / 1e6;
1116 }
1117 }
1118 return $ret;
1119 }
1120
1121 /**
1122 * Resets the parse start timestamps for future calls to getTimeSinceStart()
1123 * @since 1.22
1124 */
1125 public function resetParseStartTime() {
1126 $this->mParseStartTime = self::getTimes();
1127 }
1128
1129 /**
1130 * Returns the time since resetParseStartTime() was last called
1131 *
1132 * Clocks available are:
1133 * - wall: Wall clock time
1134 * - cpu: CPU time (requires getrusage)
1135 *
1136 * @since 1.22
1137 * @param string $clock
1138 * @return float|null
1139 */
1140 public function getTimeSinceStart( $clock ) {
1141 if ( !isset( $this->mParseStartTime[$clock] ) ) {
1142 return null;
1143 }
1144
1145 $end = self::getTimes( $clock );
1146 return $end[$clock] - $this->mParseStartTime[$clock];
1147 }
1148
1149 /**
1150 * Sets parser limit report data for a key
1151 *
1152 * The key is used as the prefix for various messages used for formatting:
1153 * - $key: The label for the field in the limit report
1154 * - $key-value-text: Message used to format the value in the "NewPP limit
1155 * report" HTML comment. If missing, uses $key-format.
1156 * - $key-value-html: Message used to format the value in the preview
1157 * limit report table. If missing, uses $key-format.
1158 * - $key-value: Message used to format the value. If missing, uses "$1".
1159 *
1160 * Note that all values are interpreted as wikitext, and so should be
1161 * encoded with htmlspecialchars() as necessary, but should avoid complex
1162 * HTML for sanity of display in the "NewPP limit report" comment.
1163 *
1164 * @since 1.22
1165 * @param string $key Message key
1166 * @param mixed $value Appropriate for Message::params()
1167 */
1168 public function setLimitReportData( $key, $value ) {
1169 $this->mLimitReportData[$key] = $value;
1170
1171 if ( is_array( $value ) ) {
1172 if ( array_keys( $value ) === [ 0, 1 ]
1173 && is_numeric( $value[0] )
1174 && is_numeric( $value[1] )
1175 ) {
1176 $data = [ 'value' => $value[0], 'limit' => $value[1] ];
1177 } else {
1178 $data = $value;
1179 }
1180 } else {
1181 $data = $value;
1182 }
1183
1184 if ( strpos( $key, '-' ) ) {
1185 list( $ns, $name ) = explode( '-', $key, 2 );
1186 $this->mLimitReportJSData[$ns][$name] = $data;
1187 } else {
1188 $this->mLimitReportJSData[$key] = $data;
1189 }
1190 }
1191
1192 /**
1193 * Check whether the cache TTL was lowered due to dynamic content
1194 *
1195 * When content is determined by more than hard state (e.g. page edits),
1196 * such as template/file transclusions based on the current timestamp or
1197 * extension tags that generate lists based on queries, this return true.
1198 *
1199 * @return bool
1200 * @since 1.25
1201 */
1202 public function hasDynamicContent() {
1203 global $wgParserCacheExpireTime;
1204
1205 return $this->getCacheExpiry() < $wgParserCacheExpireTime;
1206 }
1207
1208 /**
1209 * Get or set the prevent-clickjacking flag
1210 *
1211 * @since 1.24
1212 * @param bool|null $flag New flag value, or null to leave it unchanged
1213 * @return bool Old flag value
1214 */
1215 public function preventClickjacking( $flag = null ) {
1216 return wfSetVar( $this->mPreventClickjacking, $flag );
1217 }
1218
1219 /**
1220 * Lower the runtime adaptive TTL to at most this value
1221 *
1222 * @param int $ttl
1223 * @since 1.28
1224 */
1225 public function updateRuntimeAdaptiveExpiry( $ttl ) {
1226 $this->mMaxAdaptiveExpiry = min( $ttl, $this->mMaxAdaptiveExpiry );
1227 $this->updateCacheExpiry( $ttl );
1228 }
1229
1230 /**
1231 * Call this when parsing is done to lower the TTL based on low parse times
1232 *
1233 * @since 1.28
1234 */
1235 public function finalizeAdaptiveCacheExpiry() {
1236 if ( is_infinite( $this->mMaxAdaptiveExpiry ) ) {
1237 return; // not set
1238 }
1239
1240 $runtime = $this->getTimeSinceStart( 'wall' );
1241 if ( is_float( $runtime ) ) {
1242 $slope = ( self::SLOW_AR_TTL - self::FAST_AR_TTL )
1243 / ( self::PARSE_SLOW_SEC - self::PARSE_FAST_SEC );
1244 // SLOW_AR_TTL = PARSE_SLOW_SEC * $slope + $point
1245 $point = self::SLOW_AR_TTL - self::PARSE_SLOW_SEC * $slope;
1246
1247 $adaptiveTTL = min(
1248 max( $slope * $runtime + $point, self::MIN_AR_TTL ),
1249 $this->mMaxAdaptiveExpiry
1250 );
1251 $this->updateCacheExpiry( $adaptiveTTL );
1252 }
1253 }
1254
1255 public function __sleep() {
1256 return array_diff(
1257 array_keys( get_object_vars( $this ) ),
1258 [ 'mParseStartTime' ]
1259 );
1260 }
1261
1262 /**
1263 * Merges internal metadata such as flags, accessed options, and profiling info
1264 * from $source into this ParserOutput. This should be used whenever the state of $source
1265 * has any impact on the state of this ParserOutput.
1266 *
1267 * @param ParserOutput $source
1268 */
1269 public function mergeInternalMetaDataFrom( ParserOutput $source ) {
1270 $this->mOutputHooks = self::mergeList( $this->mOutputHooks, $source->getOutputHooks() );
1271 $this->mWarnings = self::mergeMap( $this->mWarnings, $source->mWarnings ); // don't use getter
1272 $this->mTimestamp = $this->useMaxValue( $this->mTimestamp, $source->getTimestamp() );
1273
1274 if ( $this->mSpeculativeRevId && $source->mSpeculativeRevId
1275 && $this->mSpeculativeRevId !== $source->mSpeculativeRevId
1276 ) {
1277 wfLogWarning(
1278 'Inconsistent speculative revision ID encountered while merging parser output!'
1279 );
1280 }
1281
1282 $this->mSpeculativeRevId = $this->useMaxValue(
1283 $this->mSpeculativeRevId,
1284 $source->getSpeculativeRevIdUsed()
1285 );
1286 $this->mParseStartTime = $this->useEachMinValue(
1287 $this->mParseStartTime,
1288 $source->mParseStartTime
1289 );
1290
1291 $this->mFlags = self::mergeMap( $this->mFlags, $source->mFlags );
1292 $this->mAccessedOptions = self::mergeMap( $this->mAccessedOptions, $source->mAccessedOptions );
1293
1294 // TODO: maintain per-slot limit reports!
1295 if ( empty( $this->mLimitReportData ) ) {
1296 $this->mLimitReportData = $source->mLimitReportData;
1297 }
1298 if ( empty( $this->mLimitReportJSData ) ) {
1299 $this->mLimitReportJSData = $source->mLimitReportJSData;
1300 }
1301 }
1302
1303 /**
1304 * Merges HTML metadata such as head items, JS config vars, and HTTP cache control info
1305 * from $source into this ParserOutput. This should be used whenever the HTML in $source
1306 * has been somehow mered into the HTML of this ParserOutput.
1307 *
1308 * @param ParserOutput $source
1309 */
1310 public function mergeHtmlMetaDataFrom( ParserOutput $source ) {
1311 // HTML and HTTP
1312 $this->mHeadItems = self::mergeMixedList( $this->mHeadItems, $source->getHeadItems() );
1313 $this->mModules = self::mergeList( $this->mModules, $source->getModules() );
1314 $this->mModuleStyles = self::mergeList( $this->mModuleStyles, $source->getModuleStyles() );
1315 $this->mJsConfigVars = self::mergeMap( $this->mJsConfigVars, $source->getJsConfigVars() );
1316 $this->mMaxAdaptiveExpiry = min( $this->mMaxAdaptiveExpiry, $source->mMaxAdaptiveExpiry );
1317
1318 // "noindex" always wins!
1319 if ( $this->mIndexPolicy === 'noindex' || $source->mIndexPolicy === 'noindex' ) {
1320 $this->mIndexPolicy = 'noindex';
1321 } elseif ( $this->mIndexPolicy !== 'index' ) {
1322 $this->mIndexPolicy = $source->mIndexPolicy;
1323 }
1324
1325 // Skin control
1326 $this->mNewSection = $this->mNewSection || $source->getNewSection();
1327 $this->mHideNewSection = $this->mHideNewSection || $source->getHideNewSection();
1328 $this->mNoGallery = $this->mNoGallery || $source->getNoGallery();
1329 $this->mEnableOOUI = $this->mEnableOOUI || $source->getEnableOOUI();
1330 $this->mPreventClickjacking = $this->mPreventClickjacking || $source->preventClickjacking();
1331
1332 // TODO: we'll have to be smarter about this!
1333 $this->mSections = array_merge( $this->mSections, $source->getSections() );
1334 $this->mTOCHTML .= $source->mTOCHTML;
1335
1336 // XXX: we don't want to concatenate title text, so first write wins.
1337 // We should use the first *modified* title text, but we don't have the original to check.
1338 if ( $this->mTitleText === null || $this->mTitleText === '' ) {
1339 $this->mTitleText = $source->mTitleText;
1340 }
1341
1342 // class names are stored in array keys
1343 $this->mWrapperDivClasses = self::mergeMap(
1344 $this->mWrapperDivClasses,
1345 $source->mWrapperDivClasses
1346 );
1347
1348 // NOTE: last write wins, same as within one ParserOutput
1349 $this->mIndicators = self::mergeMap( $this->mIndicators, $source->getIndicators() );
1350
1351 // NOTE: include extension data in "tracking meta data" as well as "html meta data"!
1352 // TODO: add a $mergeStrategy parameter to setExtensionData to allow different
1353 // kinds of extension data to be merged in different ways.
1354 $this->mExtensionData = self::mergeMap(
1355 $this->mExtensionData,
1356 $source->mExtensionData
1357 );
1358 }
1359
1360 /**
1361 * Merges dependency tracking metadata such as backlinks, images used, and extension data
1362 * from $source into this ParserOutput. This allows dependency tracking to be done for the
1363 * combined output of multiple content slots.
1364 *
1365 * @param ParserOutput $source
1366 */
1367 public function mergeTrackingMetaDataFrom( ParserOutput $source ) {
1368 $this->mLanguageLinks = self::mergeList( $this->mLanguageLinks, $source->getLanguageLinks() );
1369 $this->mCategories = self::mergeMap( $this->mCategories, $source->getCategories() );
1370 $this->mLinks = self::merge2D( $this->mLinks, $source->getLinks() );
1371 $this->mTemplates = self::merge2D( $this->mTemplates, $source->getTemplates() );
1372 $this->mTemplateIds = self::merge2D( $this->mTemplateIds, $source->getTemplateIds() );
1373 $this->mImages = self::mergeMap( $this->mImages, $source->getImages() );
1374 $this->mFileSearchOptions = self::mergeMap(
1375 $this->mFileSearchOptions,
1376 $source->getFileSearchOptions()
1377 );
1378 $this->mExternalLinks = self::mergeMap( $this->mExternalLinks, $source->getExternalLinks() );
1379 $this->mInterwikiLinks = self::merge2D(
1380 $this->mInterwikiLinks,
1381 $source->getInterwikiLinks()
1382 );
1383
1384 // TODO: add a $mergeStrategy parameter to setProperty to allow different
1385 // kinds of properties to be merged in different ways.
1386 $this->mProperties = self::mergeMap( $this->mProperties, $source->getProperties() );
1387
1388 // NOTE: include extension data in "tracking meta data" as well as "html meta data"!
1389 // TODO: add a $mergeStrategy parameter to setExtensionData to allow different
1390 // kinds of extension data to be merged in different ways.
1391 $this->mExtensionData = self::mergeMap(
1392 $this->mExtensionData,
1393 $source->mExtensionData
1394 );
1395 }
1396
1397 private static function mergeMixedList( array $a, array $b ) {
1398 return array_unique( array_merge( $a, $b ), SORT_REGULAR );
1399 }
1400
1401 private static function mergeList( array $a, array $b ) {
1402 return array_values( array_unique( array_merge( $a, $b ), SORT_REGULAR ) );
1403 }
1404
1405 private static function mergeMap( array $a, array $b ) {
1406 return array_replace( $a, $b );
1407 }
1408
1409 private static function merge2D( array $a, array $b ) {
1410 $values = [];
1411 $keys = array_merge( array_keys( $a ), array_keys( $b ) );
1412
1413 foreach ( $keys as $k ) {
1414 if ( empty( $a[$k] ) ) {
1415 $values[$k] = $b[$k];
1416 } elseif ( empty( $b[$k] ) ) {
1417 $values[$k] = $a[$k];
1418 } elseif ( is_array( $a[$k] ) && is_array( $b[$k] ) ) {
1419 $values[$k] = array_replace( $a[$k], $b[$k] );
1420 } else {
1421 $values[$k] = $b[$k];
1422 }
1423 }
1424
1425 return $values;
1426 }
1427
1428 private static function useEachMinValue( array $a, array $b ) {
1429 $values = [];
1430 $keys = array_merge( array_keys( $a ), array_keys( $b ) );
1431
1432 foreach ( $keys as $k ) {
1433 if ( is_array( $a[$k] ?? null ) && is_array( $b[$k] ?? null ) ) {
1434 $values[$k] = self::useEachMinValue( $a[$k], $b[$k] );
1435 } else {
1436 $values[$k] = self::useMinValue( $a[$k] ?? null, $b[$k] ?? null );
1437 }
1438 }
1439
1440 return $values;
1441 }
1442
1443 private static function useMinValue( $a, $b ) {
1444 if ( $a === null ) {
1445 return $b;
1446 }
1447
1448 if ( $b === null ) {
1449 return $a;
1450 }
1451
1452 return min( $a, $b );
1453 }
1454
1455 private static function useMaxValue( $a, $b ) {
1456 if ( $a === null ) {
1457 return $b;
1458 }
1459
1460 if ( $b === null ) {
1461 return $a;
1462 }
1463
1464 return max( $a, $b );
1465 }
1466
1467 }