MediaWiki
REL1_39
ParserObserver.php
Go to the documentation of this file.
1
<?php
2
29
namespace
MediaWiki\Parser
;
30
31
use
Content
;
32
use
MediaWiki\Cache\CacheKeyHelper
;
33
use
MediaWiki\Page\PageReference
;
34
use
ParserOptions
;
35
use
ParserOutput
;
36
use Psr\Log\LoggerInterface;
37
use RuntimeException;
38
use
Title
;
39
45
class
ParserObserver
{
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
}
CacheTime\getUsedOptions
getUsedOptions()
Returns the options from its ParserOptions which have been taken into account to produce the output.
Definition
CacheTime.php:217
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:59
MediaWiki\Parser\ParserObserver\notifyParse
notifyParse(PageReference $page, ?int $revId, ParserOptions $options, Content $content, ParserOutput $output)
Definition
ParserObserver.php:71
ParserOptions
Set options of the Parser.
Definition
ParserOptions.php:46
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:1372
ParserOutput
Definition
ParserOutput.php:38
Title
Represents a title within MediaWiki.
Definition
Title.php:49
Content
Base interface for content objects.
Definition
Content.php:35
MediaWiki\Page\PageReference
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.
Definition
PageReference.php:49
MediaWiki\Parser
Definition
ParserCacheFactory.php:22
$content
$content
Definition
router.php:76
includes
parser
ParserObserver.php
Generated on Thu Nov 21 2024 05:23:42 for MediaWiki by
1.10.0