| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
namespace MediaWiki\Parser; |
| 30 |
|
| 31 |
use MapCacheLRU; |
| 32 |
use MediaWiki\Cache\CacheKeyHelper; |
| 33 |
use MediaWiki\Content\Content; |
| 34 |
use MediaWiki\Page\PageReference; |
| 35 |
use MediaWiki\Title\Title; |
| 36 |
use Psr\Log\LoggerInterface; |
| 37 |
use RuntimeException; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
class ParserObserver { |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
private $logger; |
| 49 |
|
| 50 |
private MapCacheLRU $previousParseStackTraces; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function __construct( LoggerInterface $logger ) { |
| 56 |
$this->logger = $logger; |
| 57 |
$this->previousParseStackTraces = new MapCacheLRU( 10 ); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public function notifyParse( |
| 68 |
PageReference $page, ?int $revId, ParserOptions $options, Content $content, ParserOutput $output |
| 69 |
) { |
| 70 |
$pageKey = CacheKeyHelper::getKeyForPage( $page ); |
| 71 |
|
| 72 |
$optionsHash = $options->optionsHash( |
| 73 |
$output->getUsedOptions(), |
| 74 |
Title::newFromPageReference( $page ) |
| 75 |
); |
| 76 |
|
| 77 |
$contentStr = $content->isValid() ? $content->serialize() : null; |
| 78 |
|
| 79 |
$contentSha1 = $contentStr ? sha1( $contentStr ) : 'INVALID'; |
| 80 |
|
| 81 |
$index = $this->getParseId( $pageKey, $revId, $optionsHash, $contentSha1 ); |
| 82 |
|
| 83 |
$stackTrace = ( new RuntimeException() )->getTraceAsString(); |
| 84 |
if ( $this->previousParseStackTraces->has( $index ) ) { |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
$this->logger->debug( |
| 90 |
__METHOD__ . ': Possibly redundant parse!', |
| 91 |
[ |
| 92 |
'page' => $pageKey, |
| 93 |
'rev' => $revId, |
| 94 |
'options-hash' => $optionsHash, |
| 95 |
'contentSha1' => $contentSha1, |
| 96 |
'trace' => $stackTrace, |
| 97 |
'previous-trace' => $this->previousParseStackTraces->get( $index ), |
| 98 |
] |
| 99 |
); |
| 100 |
} |
| 101 |
$this->previousParseStackTraces->set( $index, $stackTrace ); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
private function getParseId( string $titleStr, ?int $revId, string $optionsHash, string $contentSha1 ): string { |
| 112 |
|
| 113 |
$revIdStr = $revId ?? ""; |
| 114 |
|
| 115 |
return "$titleStr.$revIdStr.$optionsHash.$contentSha1"; |
| 116 |
} |
| 117 |
|
| 118 |
} |