MediaWiki
master
ParserObserver.php
Go to the documentation of this file.
1
<?php
2
29
namespace
MediaWiki\Parser
;
30
31
use
Content
;
32
use
MapCacheLRU
;
33
use
MediaWiki\Cache\CacheKeyHelper
;
34
use
MediaWiki\Page\PageReference
;
35
use
MediaWiki\Title\Title
;
36
use
ParserOptions
;
37
use Psr\Log\LoggerInterface;
38
use RuntimeException;
39
45
class
ParserObserver
{
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
}
CacheTime\getUsedOptions
getUsedOptions()
Returns the options from its ParserOptions which have been taken into account to produce the output.
Definition
CacheTime.php:214
MapCacheLRU
Store key-value entries in a size-limited in-memory LRU cache.
Definition
MapCacheLRU.php:34
MediaWiki\Cache\CacheKeyHelper
Helper class for mapping value objects representing basic entities to cache keys.
Definition
CacheKeyHelper.php:43
MediaWiki\Parser\ParserObserver
Definition
ParserObserver.php:45
MediaWiki\Parser\ParserObserver\__construct
__construct(LoggerInterface $logger)
Definition
ParserObserver.php:56
MediaWiki\Parser\ParserObserver\notifyParse
notifyParse(PageReference $page, ?int $revId, ParserOptions $options, Content $content, ParserOutput $output)
Definition
ParserObserver.php:68
MediaWiki\Parser\ParserOutput
ParserOutput is a rendering of a Content object or a message.
Definition
ParserOutput.php:95
MediaWiki\Title\Title
Represents a title within MediaWiki.
Definition
Title.php:78
ParserOptions
Set options of the Parser.
Definition
ParserOptions.php:52
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:1436
Content
Base interface for representing page content.
Definition
Content.php:37
Content\serialize
serialize( $format=null)
Convenience method for serializing this Content object.
Content\isValid
isValid()
Returns whether the content is valid.
MediaWiki\Page\PageReference
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.
Definition
PageReference.php:51
MediaWiki\Parser
Definition
MagicWord.php:21
includes
parser
ParserObserver.php
Generated on Tue Apr 23 2024 14:23:42 for MediaWiki by
1.10.0