| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\PoolCounter; |
| 22 |
|
| 23 |
use InvalidArgumentException; |
| 24 |
use MediaWiki\Logger\Spi as LoggerSpi; |
| 25 |
use MediaWiki\MainConfigNames; |
| 26 |
use MediaWiki\MediaWikiServices; |
| 27 |
use MediaWiki\Page\PageRecord; |
| 28 |
use MediaWiki\Page\WikiPageFactory; |
| 29 |
use MediaWiki\Parser\ParserCache; |
| 30 |
use MediaWiki\Parser\ParserOptions; |
| 31 |
use MediaWiki\Parser\ParserOutput; |
| 32 |
use MediaWiki\Revision\RevisionRecord; |
| 33 |
use MediaWiki\Revision\RevisionRenderer; |
| 34 |
use MediaWiki\Status\Status; |
| 35 |
use MediaWiki\Utils\MWTimestamp; |
| 36 |
use Wikimedia\Rdbms\ChronologyProtector; |
| 37 |
use Wikimedia\Rdbms\ILBFactory; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
class PoolWorkArticleViewCurrent extends PoolWorkArticleView { |
| 45 |
|
| 46 |
private $workKey; |
| 47 |
|
| 48 |
private $page; |
| 49 |
|
| 50 |
private $parserCache; |
| 51 |
|
| 52 |
private $lbFactory; |
| 53 |
|
| 54 |
private $wikiPageFactory; |
| 55 |
|
| 56 |
private bool $triggerLinksUpdate; |
| 57 |
private ChronologyProtector $chronologyProtector; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public function __construct( |
| 74 |
string $workKey, |
| 75 |
PageRecord $page, |
| 76 |
RevisionRecord $revision, |
| 77 |
ParserOptions $parserOptions, |
| 78 |
RevisionRenderer $revisionRenderer, |
| 79 |
ParserCache $parserCache, |
| 80 |
ILBFactory $lbFactory, |
| 81 |
ChronologyProtector $chronologyProtector, |
| 82 |
LoggerSpi $loggerSpi, |
| 83 |
WikiPageFactory $wikiPageFactory, |
| 84 |
bool $cacheable = true, |
| 85 |
bool $triggerLinksUpdate = false |
| 86 |
) { |
| 87 |
|
| 88 |
|
| 89 |
if ( $revision->getPageId() && $revision->getPageId() !== $page->getId() ) { |
| 90 |
throw new InvalidArgumentException( '$page parameter mismatches $revision parameter' ); |
| 91 |
} |
| 92 |
|
| 93 |
parent::__construct( $workKey, $revision, $parserOptions, $revisionRenderer, $loggerSpi ); |
| 94 |
|
| 95 |
$this->workKey = $workKey; |
| 96 |
$this->page = $page; |
| 97 |
$this->parserCache = $parserCache; |
| 98 |
$this->lbFactory = $lbFactory; |
| 99 |
$this->chronologyProtector = $chronologyProtector; |
| 100 |
$this->wikiPageFactory = $wikiPageFactory; |
| 101 |
$this->cacheable = $cacheable; |
| 102 |
$this->triggerLinksUpdate = $triggerLinksUpdate; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public function doWork() { |
| 109 |
|
| 110 |
|
| 111 |
$sampleRate = MediaWikiServices::getInstance()->getMainConfig()->get( |
| 112 |
MainConfigNames::ParsoidSelectiveUpdateSampleRate |
| 113 |
); |
| 114 |
$doSample = ( $sampleRate && mt_rand( 1, $sampleRate ) === 1 ); |
| 115 |
|
| 116 |
$previousOutput = null; |
| 117 |
if ( $this->parserOptions->getUseParsoid() || $doSample ) { |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
$previousOutput = $this->parserCache->getDirty( $this->page, $this->parserOptions ) ?: null; |
| 122 |
} |
| 123 |
$status = $this->renderRevision( $previousOutput, $doSample, 'PoolWorkArticleViewCurrent' ); |
| 124 |
|
| 125 |
$output = $status->getValue(); |
| 126 |
|
| 127 |
if ( $output ) { |
| 128 |
if ( $this->cacheable && $output->isCacheable() ) { |
| 129 |
$this->parserCache->save( |
| 130 |
$output, |
| 131 |
$this->page, |
| 132 |
$this->parserOptions |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
if ( $this->triggerLinksUpdate ) { |
| 137 |
$this->wikiPageFactory->newFromTitle( $this->page )->triggerOpportunisticLinksUpdate( $output ); |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
return $status; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
public function getCachedWork() { |
| 148 |
$parserOutput = $this->parserCache->get( $this->page, $this->parserOptions ); |
| 149 |
|
| 150 |
$logger = $this->loggerSpi->getLogger( 'PoolWorkArticleView' ); |
| 151 |
$logger->debug( $parserOutput ? 'parser cache hit' : 'parser cache miss' ); |
| 152 |
|
| 153 |
return $parserOutput ? Status::newGood( $parserOutput ) : false; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
public function fallback( $fast ) { |
| 161 |
$parserOutput = $this->parserCache->getDirty( $this->page, $this->parserOptions ); |
| 162 |
|
| 163 |
$logger = $this->loggerSpi->getLogger( 'dirty' ); |
| 164 |
|
| 165 |
if ( !$parserOutput ) { |
| 166 |
$logger->info( 'dirty missing' ); |
| 167 |
return false; |
| 168 |
} |
| 169 |
|
| 170 |
if ( $fast ) { |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
$lastWriteTime = $this->chronologyProtector->getTouched( $this->lbFactory->getMainLB() ); |
| 181 |
$cacheTime = MWTimestamp::convert( TS_UNIX, $parserOutput->getCacheTime() ); |
| 182 |
if ( $lastWriteTime && $cacheTime <= $lastWriteTime ) { |
| 183 |
$logger->info( |
| 184 |
'declining to send dirty output since cache time ' . |
| 185 |
'{cacheTime} is before last write time {lastWriteTime}', |
| 186 |
[ |
| 187 |
'workKey' => $this->workKey, |
| 188 |
'cacheTime' => $cacheTime, |
| 189 |
'lastWriteTime' => $lastWriteTime, |
| 190 |
] |
| 191 |
); |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
return false; |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
$logger->info( $fast ? 'fast dirty output' : 'dirty output', [ 'workKey' => $this->workKey ] ); |
| 200 |
|
| 201 |
$status = Status::newGood( $parserOutput ); |
| 202 |
$status->warning( 'view-pool-dirty-output' ); |
| 203 |
$status->warning( $fast ? 'view-pool-contention' : 'view-pool-overload' ); |
| 204 |
return $status; |
| 205 |
} |
| 206 |
|
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
class_alias( PoolWorkArticleViewCurrent::class, 'PoolWorkArticleViewCurrent' ); |