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