MediaWiki master
ParserObserver.php
Go to the documentation of this file.
1<?php
2
15namespace MediaWiki\Parser;
16
21use Psr\Log\LoggerInterface;
22use RuntimeException;
24
31 private readonly MapCacheLRU $previousParseStackTraces;
32
33 public function __construct( private readonly LoggerInterface $logger ) {
34 $this->previousParseStackTraces = new MapCacheLRU( 10 );
35 }
36
44 public function notifyParse(
45 PageReference $page, ?int $revId, ParserOptions $options, Content $content, ParserOutput $output
46 ) {
47 $pageKey = CacheKeyHelper::getKeyForPage( $page );
48
49 $optionsHash = $options->optionsHash(
50 $output->getUsedOptions(),
51 Title::newFromPageReference( $page )
52 );
53
54 $contentStr = $content->isValid() ? $content->serialize() : null;
55 // $contentStr may be null if the content could not be serialized
56 $contentSha1 = $contentStr ? sha1( $contentStr ) : 'INVALID';
57
58 $index = $this->getParseId( $pageKey, $revId, $optionsHash, $contentSha1 );
59
60 $stackTrace = ( new RuntimeException() )->getTraceAsString();
61 if ( $this->previousParseStackTraces->has( $index ) ) {
62
63 // NOTE: there may be legitimate changes to re-parse the same WikiText content,
64 // e.g. if predicted revision ID for the REVISIONID magic word mismatched.
65 // But that should be rare.
66 $this->logger->debug(
67 __METHOD__ . ': Possibly redundant parse!',
68 [
69 'page' => $pageKey,
70 'rev' => $revId,
71 'options-hash' => $optionsHash,
72 'contentSha1' => $contentSha1,
73 'trace' => $stackTrace,
74 'previous-trace' => $this->previousParseStackTraces->get( $index ),
75 ]
76 );
77 }
78 $this->previousParseStackTraces->set( $index, $stackTrace );
79 }
80
88 private function getParseId( string $titleStr, ?int $revId, string $optionsHash, string $contentSha1 ): string {
89 // $revId may be null when previewing a new page
90 $revIdStr = $revId ?? "";
91
92 return "$titleStr.$revIdStr.$optionsHash.$contentSha1";
93 }
94
95}
Helper class for mapping page value objects to a string key.
getUsedOptions()
Returns the options from its ParserOptions which have been taken into account to produce the output.
__construct(private readonly 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:69
Store key-value entries in a size-limited in-memory LRU cache.
Content objects represent page content, e.g.
Definition Content.php:28
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.