MediaWiki REL1_39
ParserObserver.php
Go to the documentation of this file.
1<?php
2
29namespace MediaWiki\Parser;
30
31use Content;
35use ParserOutput;
36use Psr\Log\LoggerInterface;
37use RuntimeException;
38use Title;
39
49 private $logger;
50
54 private $previousParseStackTraces;
55
59 public function __construct( LoggerInterface $logger ) {
60 $this->logger = $logger;
61 $this->previousParseStackTraces = [];
62 }
63
71 public function notifyParse(
72 PageReference $page, ?int $revId, ParserOptions $options, Content $content, ParserOutput $output
73 ) {
74 $pageKey = CacheKeyHelper::getKeyForPage( $page );
75
76 $optionsHash = $options->optionsHash(
77 $output->getUsedOptions(),
78 Title::castFromPageReference( $page )
79 );
80
81 $contentStr = $content->isValid() ? $content->serialize() : null;
82 // $contentStr may be null if the content could not be serialized
83 $contentSha1 = $contentStr ? sha1( $contentStr ) : 'INVALID';
84
85 $index = $this->getParseId( $pageKey, $revId, $optionsHash, $contentSha1 );
86
87 $stackTrace = ( new RuntimeException() )->getTraceAsString();
88 if ( array_key_exists( $index, $this->previousParseStackTraces ) ) {
89
90 // NOTE: there may be legitimate changes to re-parse the same WikiText content,
91 // e.g. if predicted revision ID for the REVISIONID magic word mismatched.
92 // But that should be rare.
93 $this->logger->debug(
94 __METHOD__ . ': Possibly redundant parse!',
95 [
96 'page' => $pageKey,
97 'rev' => $revId,
98 'options-hash' => $optionsHash,
99 'contentSha1' => $contentSha1,
100 'trace' => $stackTrace,
101 'previous-trace' => $this->previousParseStackTraces[$index],
102 ]
103 );
104 }
105 $this->previousParseStackTraces[$index] = $stackTrace;
106 }
107
115 private function getParseId( string $titleStr, ?int $revId, string $optionsHash, string $contentSha1 ): string {
116 // $revId may be null when previewing a new page
117 $revIdStr = $revId ?? "";
118
119 return "$titleStr.$revIdStr.$optionsHash.$contentSha1";
120 }
121
122}
getUsedOptions()
Returns the options from its ParserOptions which have been taken into account to produce the output.
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)
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.
Represents a title within MediaWiki.
Definition Title.php:49
Base interface for content objects.
Definition Content.php:35
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.
$content
Definition router.php:76