Merge "Revert "buttons: Update focus for quiet buttons""
[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 * Add a tracking category, getting the title from a system message,
476 * or print a debug message if the title is invalid.
477 *
478 * Please add any message that you use with this function to
479 * $wgTrackingCategories. That way they will be listed on
480 * Special:TrackingCategories.
481 *
482 * @param string $msg Message key
483 * @param Title $title title of the page which is being tracked
484 * @return bool Whether the addition was successful
485 * @since 1.25
486 */
487 public function addTrackingCategory( $msg, $title ) {
488 if ( $title->getNamespace() === NS_SPECIAL ) {
489 wfDebug( __METHOD__ . ": Not adding tracking category $msg to special page!\n" );
490 return false;
491 }
492
493 // Important to parse with correct title (bug 31469)
494 $cat = wfMessage( $msg )
495 ->title( $title )
496 ->inContentLanguage()
497 ->text();
498
499 # Allow tracking categories to be disabled by setting them to "-"
500 if ( $cat === '-' ) {
501 return false;
502 }
503
504 $containerCategory = Title::makeTitleSafe( NS_CATEGORY, $cat );
505 if ( $containerCategory ) {
506 $this->addCategory( $containerCategory->getDBkey(), $this->getProperty( 'defaultsort' ) ?: '' );
507 return true;
508 } else {
509 wfDebug( __METHOD__ . ": [[MediaWiki:$msg]] is not a valid title!\n" );
510 return false;
511 }
512 }
513
514 /**
515 * Override the title to be used for display
516 * -- this is assumed to have been validated
517 * (check equal normalisation, etc.)
518 *
519 * @param string $text Desired title text
520 */
521 public function setDisplayTitle( $text ) {
522 $this->setTitleText( $text );
523 $this->setProperty( 'displaytitle', $text );
524 }
525
526 /**
527 * Get the title to be used for display
528 *
529 * @return string
530 */
531 public function getDisplayTitle() {
532 $t = $this->getTitleText();
533 if ( $t === '' ) {
534 return false;
535 }
536 return $t;
537 }
538
539 /**
540 * Fairly generic flag setter thingy.
541 * @param string $flag
542 */
543 public function setFlag( $flag ) {
544 $this->mFlags[$flag] = true;
545 }
546
547 public function getFlag( $flag ) {
548 return isset( $this->mFlags[$flag] );
549 }
550
551 /**
552 * Set a property to be stored in the page_props database table.
553 *
554 * page_props is a key value store indexed by the page ID. This allows
555 * the parser to set a property on a page which can then be quickly
556 * retrieved given the page ID or via a DB join when given the page
557 * title.
558 *
559 * Since 1.23, page_props are also indexed by numeric value, to allow
560 * for efficient "top k" queries of pages wrt a given property.
561 *
562 * setProperty() is thus used to propagate properties from the parsed
563 * page to request contexts other than a page view of the currently parsed
564 * article.
565 *
566 * Some applications examples:
567 *
568 * * To implement hidden categories, hiding pages from category listings
569 * by storing a property.
570 *
571 * * Overriding the displayed article title.
572 * @see ParserOutput::setDisplayTitle()
573 *
574 * * To implement image tagging, for example displaying an icon on an
575 * image thumbnail to indicate that it is listed for deletion on
576 * Wikimedia Commons.
577 * This is not actually implemented, yet but would be pretty cool.
578 *
579 * @note Do not use setProperty() to set a property which is only used
580 * in a context where the ParserOutput object itself is already available,
581 * for example a normal page view. There is no need to save such a property
582 * in the database since the text is already parsed. You can just hook
583 * OutputPageParserOutput and get your data out of the ParserOutput object.
584 *
585 * If you are writing an extension where you want to set a property in the
586 * parser which is used by an OutputPageParserOutput hook, you have to
587 * associate the extension data directly with the ParserOutput object.
588 * Since MediaWiki 1.21, you can use setExtensionData() to do this:
589 *
590 * @par Example:
591 * @code
592 * $parser->getOutput()->setExtensionData( 'my_ext_foo', '...' );
593 * @endcode
594 *
595 * And then later, in OutputPageParserOutput or similar:
596 *
597 * @par Example:
598 * @code
599 * $output->getExtensionData( 'my_ext_foo' );
600 * @endcode
601 *
602 * In MediaWiki 1.20 and older, you have to use a custom member variable
603 * within the ParserOutput object:
604 *
605 * @par Example:
606 * @code
607 * $parser->getOutput()->my_ext_foo = '...';
608 * @endcode
609 *
610 */
611 public function setProperty( $name, $value ) {
612 $this->mProperties[$name] = $value;
613 }
614
615 /**
616 * @param string $name The property name to look up.
617 *
618 * @return mixed|bool The value previously set using setProperty(). False if null or no value
619 * was set for the given property name.
620 *
621 * @note You need to use getProperties() to check for boolean and null properties.
622 */
623 public function getProperty( $name ) {
624 return isset( $this->mProperties[$name] ) ? $this->mProperties[$name] : false;
625 }
626
627 public function unsetProperty( $name ) {
628 unset( $this->mProperties[$name] );
629 }
630
631 public function getProperties() {
632 if ( !isset( $this->mProperties ) ) {
633 $this->mProperties = array();
634 }
635 return $this->mProperties;
636 }
637
638 /**
639 * Returns the options from its ParserOptions which have been taken
640 * into account to produce this output or false if not available.
641 * @return array
642 */
643 public function getUsedOptions() {
644 if ( !isset( $this->mAccessedOptions ) ) {
645 return array();
646 }
647 return array_keys( $this->mAccessedOptions );
648 }
649
650 /**
651 * Tags a parser option for use in the cache key for this parser output.
652 * Registered as a watcher at ParserOptions::registerWatcher() by Parser::clearState().
653 *
654 * @see ParserCache::getKey
655 * @see ParserCache::save
656 * @see ParserOptions::addExtraKey
657 * @see ParserOptions::optionsHash
658 * @param string $option
659 */
660 public function recordOption( $option ) {
661 $this->mAccessedOptions[$option] = true;
662 }
663
664 /**
665 * Adds an update job to the output. Any update jobs added to the output will
666 * eventually be executed in order to store any secondary information extracted
667 * from the page's content. This is triggered by calling getSecondaryDataUpdates()
668 * and is used for forward links updates on edit and backlink updates by jobs.
669 *
670 * @since 1.20
671 *
672 * @param DataUpdate $update
673 */
674 public function addSecondaryDataUpdate( DataUpdate $update ) {
675 $this->mSecondaryDataUpdates[] = $update;
676 }
677
678 /**
679 * Returns any DataUpdate jobs to be executed in order to store secondary information
680 * extracted from the page's content, including a LinksUpdate object for all links stored in
681 * this ParserOutput object.
682 *
683 * @note Avoid using this method directly, use ContentHandler::getSecondaryDataUpdates()
684 * instead! The content handler may provide additional update objects.
685 *
686 * @since 1.20
687 *
688 * @param Title $title The title of the page we're updating. If not given, a title object will
689 * be created based on $this->getTitleText()
690 * @param bool $recursive Queue jobs for recursive updates?
691 *
692 * @return array An array of instances of DataUpdate
693 */
694 public function getSecondaryDataUpdates( Title $title = null, $recursive = true ) {
695 if ( is_null( $title ) ) {
696 $title = Title::newFromText( $this->getTitleText() );
697 }
698
699 $linksUpdate = new LinksUpdate( $title, $this, $recursive );
700
701 return array_merge( $this->mSecondaryDataUpdates, array( $linksUpdate ) );
702 }
703
704 /**
705 * Attaches arbitrary data to this ParserObject. This can be used to store some information in
706 * the ParserOutput object for later use during page output. The data will be cached along with
707 * the ParserOutput object, but unlike data set using setProperty(), it is not recorded in the
708 * database.
709 *
710 * This method is provided to overcome the unsafe practice of attaching extra information to a
711 * ParserObject by directly assigning member variables.
712 *
713 * To use setExtensionData() to pass extension information from a hook inside the parser to a
714 * hook in the page output, use this in the parser hook:
715 *
716 * @par Example:
717 * @code
718 * $parser->getOutput()->setExtensionData( 'my_ext_foo', '...' );
719 * @endcode
720 *
721 * And then later, in OutputPageParserOutput or similar:
722 *
723 * @par Example:
724 * @code
725 * $output->getExtensionData( 'my_ext_foo' );
726 * @endcode
727 *
728 * In MediaWiki 1.20 and older, you have to use a custom member variable
729 * within the ParserOutput object:
730 *
731 * @par Example:
732 * @code
733 * $parser->getOutput()->my_ext_foo = '...';
734 * @endcode
735 *
736 * @since 1.21
737 *
738 * @param string $key The key for accessing the data. Extensions should take care to avoid
739 * conflicts in naming keys. It is suggested to use the extension's name as a prefix.
740 *
741 * @param mixed $value The value to set. Setting a value to null is equivalent to removing
742 * the value.
743 */
744 public function setExtensionData( $key, $value ) {
745 if ( $value === null ) {
746 unset( $this->mExtensionData[$key] );
747 } else {
748 $this->mExtensionData[$key] = $value;
749 }
750 }
751
752 /**
753 * Gets extensions data previously attached to this ParserOutput using setExtensionData().
754 * Typically, such data would be set while parsing the page, e.g. by a parser function.
755 *
756 * @since 1.21
757 *
758 * @param string $key The key to look up.
759 *
760 * @return mixed|null The value previously set for the given key using setExtensionData()
761 * or null if no value was set for this key.
762 */
763 public function getExtensionData( $key ) {
764 if ( isset( $this->mExtensionData[$key] ) ) {
765 return $this->mExtensionData[$key];
766 }
767
768 return null;
769 }
770
771 private static function getTimes( $clock = null ) {
772 $ret = array();
773 if ( !$clock || $clock === 'wall' ) {
774 $ret['wall'] = microtime( true );
775 }
776 if ( !$clock || $clock === 'cpu' ) {
777 $ru = wfGetRusage();
778 if ( $ru ) {
779 $ret['cpu'] = $ru['ru_utime.tv_sec'] + $ru['ru_utime.tv_usec'] / 1e6;
780 $ret['cpu'] += $ru['ru_stime.tv_sec'] + $ru['ru_stime.tv_usec'] / 1e6;
781 }
782 }
783 return $ret;
784 }
785
786 /**
787 * Resets the parse start timestamps for future calls to getTimeSinceStart()
788 * @since 1.22
789 */
790 public function resetParseStartTime() {
791 $this->mParseStartTime = self::getTimes();
792 }
793
794 /**
795 * Returns the time since resetParseStartTime() was last called
796 *
797 * Clocks available are:
798 * - wall: Wall clock time
799 * - cpu: CPU time (requires getrusage)
800 *
801 * @since 1.22
802 * @param string $clock
803 * @return float|null
804 */
805 public function getTimeSinceStart( $clock ) {
806 if ( !isset( $this->mParseStartTime[$clock] ) ) {
807 return null;
808 }
809
810 $end = self::getTimes( $clock );
811 return $end[$clock] - $this->mParseStartTime[$clock];
812 }
813
814 /**
815 * Sets parser limit report data for a key
816 *
817 * The key is used as the prefix for various messages used for formatting:
818 * - $key: The label for the field in the limit report
819 * - $key-value-text: Message used to format the value in the "NewPP limit
820 * report" HTML comment. If missing, uses $key-format.
821 * - $key-value-html: Message used to format the value in the preview
822 * limit report table. If missing, uses $key-format.
823 * - $key-value: Message used to format the value. If missing, uses "$1".
824 *
825 * Note that all values are interpreted as wikitext, and so should be
826 * encoded with htmlspecialchars() as necessary, but should avoid complex
827 * HTML for sanity of display in the "NewPP limit report" comment.
828 *
829 * @since 1.22
830 * @param string $key Message key
831 * @param mixed $value Appropriate for Message::params()
832 */
833 public function setLimitReportData( $key, $value ) {
834 $this->mLimitReportData[$key] = $value;
835 }
836
837 /**
838 * Get or set the prevent-clickjacking flag
839 *
840 * @since 1.24
841 * @param bool|null $flag New flag value, or null to leave it unchanged
842 * @return bool Old flag value
843 */
844 public function preventClickjacking( $flag = null ) {
845 return wfSetVar( $this->mPreventClickjacking, $flag );
846 }
847
848 /**
849 * Save space for for serialization by removing useless values
850 * @return array
851 */
852 public function __sleep() {
853 return array_diff(
854 array_keys( get_object_vars( $this ) ),
855 array( 'mSecondaryDataUpdates', 'mParseStartTime' )
856 );
857 }
858 }