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