MediaWiki master
ParserObserver.php
Go to the documentation of this file.
1<?php
2
29namespace MediaWiki\Parser;
30
31use MapCacheLRU;
36use Psr\Log\LoggerInterface;
37use RuntimeException;
38
48 private $logger;
49
50 private MapCacheLRU $previousParseStackTraces;
51
55 public function __construct( LoggerInterface $logger ) {
56 $this->logger = $logger;
57 $this->previousParseStackTraces = new MapCacheLRU( 10 );
58 }
59
67 public function notifyParse(
68 PageReference $page, ?int $revId, ParserOptions $options, Content $content, ParserOutput $output
69 ) {
70 $pageKey = CacheKeyHelper::getKeyForPage( $page );
71
72 $optionsHash = $options->optionsHash(
73 $output->getUsedOptions(),
74 Title::newFromPageReference( $page )
75 );
76
77 $contentStr = $content->isValid() ? $content->serialize() : null;
78 // $contentStr may be null if the content could not be serialized
79 $contentSha1 = $contentStr ? sha1( $contentStr ) : 'INVALID';
80
81 $index = $this->getParseId( $pageKey, $revId, $optionsHash, $contentSha1 );
82
83 $stackTrace = ( new RuntimeException() )->getTraceAsString();
84 if ( $this->previousParseStackTraces->has( $index ) ) {
85
86 // NOTE: there may be legitimate changes to re-parse the same WikiText content,
87 // e.g. if predicted revision ID for the REVISIONID magic word mismatched.
88 // But that should be rare.
89 $this->logger->debug(
90 __METHOD__ . ': Possibly redundant parse!',
91 [
92 'page' => $pageKey,
93 'rev' => $revId,
94 'options-hash' => $optionsHash,
95 'contentSha1' => $contentSha1,
96 'trace' => $stackTrace,
97 'previous-trace' => $this->previousParseStackTraces->get( $index ),
98 ]
99 );
100 }
101 $this->previousParseStackTraces->set( $index, $stackTrace );
102 }
103
111 private function getParseId( string $titleStr, ?int $revId, string $optionsHash, string $contentSha1 ): string {
112 // $revId may be null when previewing a new page
113 $revIdStr = $revId ?? "";
114
115 return "$titleStr.$revIdStr.$optionsHash.$contentSha1";
116 }
117
118}
Store key-value entries in a size-limited in-memory LRU cache.
Helper class for mapping value objects representing basic entities to cache keys.
getUsedOptions()
Returns the options from its ParserOptions which have been taken into account to produce the output.
__construct(LoggerInterface $logger)
notifyParse(PageReference $page, ?int $revId, ParserOptions $options, Content $content, ParserOutput $output)
Set options of the Parser.
optionsHash( $forOptions, $title=null)
Generate a hash string with the values set on these ParserOptions for the keys given in the array.
ParserOutput is a rendering of a Content object or a message.
Represents a title within MediaWiki.
Definition Title.php:78
Base interface for representing page content.
Definition Content.php:39
serialize( $format=null)
Convenience method for serializing this Content object.
isValid()
Returns whether the content is valid.
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.