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