MediaWiki  master
ParserObserver.php
Go to the documentation of this file.
1 <?php
2 
29 namespace MediaWiki\Parser;
30 
33 use ParserOptions;
34 use ParserOutput;
35 use Psr\Log\LoggerInterface;
36 use RuntimeException;
37 use Title;
38 
48  private $logger;
49 
54 
58  public function __construct( LoggerInterface $logger ) {
59  $this->logger = $logger;
60  $this->previousParseStackTraces = [];
61  }
62 
69  public function notifyParse(
70  PageReference $page, ?int $revId, ParserOptions $options, ParserOutput $output
71  ) {
72  $pageKey = CacheKeyHelper::getKeyForPage( $page );
73 
74  $optionsHash = $options->optionsHash(
75  $output->getUsedOptions(),
77  );
78 
79  $index = $this->getParseId( $pageKey, $revId, $optionsHash );
80 
81  $stackTrace = ( new RuntimeException() )->getTraceAsString();
82  if ( array_key_exists( $index, $this->previousParseStackTraces ) ) {
83 
84  // NOTE: there may be legitimate changes to re-parse the same WikiText content,
85  // e.g. if predicted revision ID for the REVISIONID magic word mismatched.
86  // But that should be rare.
87  $this->logger->debug(
88  __METHOD__ . ': Possibly redundant parse!',
89  [
90  'page' => $pageKey,
91  'rev' => $revId,
92  'options-hash' => $optionsHash,
93  'trace' => $stackTrace,
94  'previous-trace' => $this->previousParseStackTraces[$index],
95  ]
96  );
97  }
98  $this->previousParseStackTraces[$index] = $stackTrace;
99  }
100 
107  private function getParseId( string $titleStr, ?int $revId, string $optionsHash ): string {
108  // $revId may be null when previewing a new page
109  $revIdStr = $revId ?? "";
110 
111  return "$titleStr.$revIdStr.$optionsHash";
112  }
113 
114 }
Parser\ParserObserver\notifyParse
notifyParse(PageReference $page, ?int $revId, ParserOptions $options, ParserOutput $output)
Definition: ParserObserver.php:69
ParserOptions
Set options of the Parser.
Definition: ParserOptions.php:45
ParserOutput
Definition: ParserOutput.php:35
CacheTime\getUsedOptions
getUsedOptions()
Returns the options from its ParserOptions which have been taken into account to produce the output.
Definition: CacheTime.php:215
MediaWiki\Parser
Definition: ParserCacheFactory.php:22
Page\PageReference
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.
Definition: PageReference.php:49
Parser\ParserObserver\getParseId
getParseId(string $titleStr, ?int $revId, string $optionsHash)
Definition: ParserObserver.php:107
Parser\ParserObserver
Definition: ParserObserver.php:44
Parser\ParserObserver\__construct
__construct(LoggerInterface $logger)
Definition: ParserObserver.php:58
MediaWiki\Cache\CacheKeyHelper\getKeyForPage
static getKeyForPage( $page)
Definition: CacheKeyHelper.php:59
Title
Represents a title within MediaWiki.
Definition: Title.php:47
Parser\ParserObserver\$previousParseStackTraces
array $previousParseStackTraces
Definition: ParserObserver.php:53
MediaWiki\Cache\CacheKeyHelper
Helper class for mapping value objects representing basic entities to cache keys.
Definition: CacheKeyHelper.php:43
Title\castFromPageReference
static castFromPageReference(?PageReference $pageReference)
Return a Title for a given Reference.
Definition: Title.php:339
Parser\ParserObserver\$logger
LoggerInterface $logger
Definition: ParserObserver.php:48
ParserOptions\optionsHash
optionsHash( $forOptions, $title=null)
Generate a hash string with the values set on these ParserOptions for the keys given in the array.
Definition: ParserOptions.php:1350