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
52 public function __construct( LoggerInterface $logger ) {
53 $this->logger = $logger;
54 $this->previousParseStackTraces = new MapCacheLRU( 10 );
55 }
56
64 public function notifyParse(
65 PageReference $page, ?int $revId, ParserOptions $options, Content $content, ParserOutput $output
66 ) {
67 $pageKey = CacheKeyHelper::getKeyForPage( $page );
68
69 $optionsHash = $options->optionsHash(
70 $output->getUsedOptions(),
71 Title::newFromPageReference( $page )
72 );
73
74 $contentStr = $content->isValid() ? $content->serialize() : null;
75 // $contentStr may be null if the content could not be serialized
76 $contentSha1 = $contentStr ? sha1( $contentStr ) : 'INVALID';
77
78 $index = $this->getParseId( $pageKey, $revId, $optionsHash, $contentSha1 );
79
80 $stackTrace = ( new RuntimeException() )->getTraceAsString();
81 if ( $this->previousParseStackTraces->has( $index ) ) {
82
83 // NOTE: there may be legitimate changes to re-parse the same WikiText content,
84 // e.g. if predicted revision ID for the REVISIONID magic word mismatched.
85 // But that should be rare.
86 $this->logger->debug(
87 __METHOD__ . ': Possibly redundant parse!',
88 [
89 'page' => $pageKey,
90 'rev' => $revId,
91 'options-hash' => $optionsHash,
92 'contentSha1' => $contentSha1,
93 'trace' => $stackTrace,
94 'previous-trace' => $this->previousParseStackTraces->get( $index ),
95 ]
96 );
97 }
98 $this->previousParseStackTraces->set( $index, $stackTrace );
99 }
100
108 private function getParseId( string $titleStr, ?int $revId, string $optionsHash, string $contentSha1 ): string {
109 // $revId may be null when previewing a new page
110 $revIdStr = $revId ?? "";
111
112 return "$titleStr.$revIdStr.$optionsHash.$contentSha1";
113 }
114
115}
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
Content objects represent page content, e.g.
Definition Content.php:42
serialize( $format=null)
Serialize this Content object.
isValid()
Whether the content is valid.
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.