| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
namespace MediaWiki\Parser\Parsoid; |
| 21 |
|
| 22 |
use MediaWiki\Content\IContentHandlerFactory; |
| 23 |
use MediaWiki\MediaWikiServices; |
| 24 |
use MediaWiki\Page\PageIdentity; |
| 25 |
use MediaWiki\Page\PageLookup; |
| 26 |
use MediaWiki\Page\PageRecord; |
| 27 |
use MediaWiki\Page\ParserOutputAccess; |
| 28 |
use MediaWiki\Parser\ParserOptions; |
| 29 |
use MediaWiki\Parser\ParserOutput; |
| 30 |
use MediaWiki\Parser\Parsoid\Config\SiteConfig; |
| 31 |
use MediaWiki\Revision\RevisionAccessException; |
| 32 |
use MediaWiki\Revision\RevisionLookup; |
| 33 |
use MediaWiki\Revision\RevisionRecord; |
| 34 |
use MediaWiki\Revision\SlotRecord; |
| 35 |
use MediaWiki\Status\Status; |
| 36 |
use Wikimedia\Parsoid\Core\ClientError; |
| 37 |
use Wikimedia\Parsoid\Core\ResourceLimitExceededException; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
class ParsoidOutputAccess { |
| 54 |
private ParsoidParserFactory $parsoidParserFactory; |
| 55 |
private PageLookup $pageLookup; |
| 56 |
private RevisionLookup $revisionLookup; |
| 57 |
private ParserOutputAccess $parserOutputAccess; |
| 58 |
private SiteConfig $siteConfig; |
| 59 |
private IContentHandlerFactory $contentHandlerFactory; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public function __construct( |
| 70 |
ParsoidParserFactory $parsoidParserFactory, |
| 71 |
ParserOutputAccess $parserOutputAccess, |
| 72 |
PageLookup $pageLookup, |
| 73 |
RevisionLookup $revisionLookup, |
| 74 |
SiteConfig $siteConfig, |
| 75 |
IContentHandlerFactory $contentHandlerFactory |
| 76 |
) { |
| 77 |
$this->parsoidParserFactory = $parsoidParserFactory; |
| 78 |
$this->parserOutputAccess = $parserOutputAccess; |
| 79 |
$this->pageLookup = $pageLookup; |
| 80 |
$this->revisionLookup = $revisionLookup; |
| 81 |
$this->siteConfig = $siteConfig; |
| 82 |
$this->contentHandlerFactory = $contentHandlerFactory; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public function getParserOutput( |
| 96 |
PageIdentity $page, |
| 97 |
ParserOptions $parserOpts, |
| 98 |
$revision = null, |
| 99 |
int $options = 0, |
| 100 |
bool $lenientRevHandling = false |
| 101 |
): Status { |
| 102 |
wfDeprecated( __METHOD__, '1.43' ); |
| 103 |
[ $page, $revision, $uncacheable ] = $this->resolveRevision( $page, $revision, $lenientRevHandling ); |
| 104 |
|
| 105 |
try { |
| 106 |
if ( $uncacheable ) { |
| 107 |
$options |= ParserOutputAccess::OPT_NO_UPDATE_CACHE; |
| 108 |
} |
| 109 |
|
| 110 |
$this->adjustParserOptions( $revision, $parserOpts ); |
| 111 |
$status = $this->parserOutputAccess->getParserOutput( |
| 112 |
$page, $parserOpts, $revision, $options |
| 113 |
); |
| 114 |
} catch ( ClientError $e ) { |
| 115 |
$status = Status::newFatal( 'parsoid-client-error', $e->getMessage() ); |
| 116 |
} catch ( ResourceLimitExceededException $e ) { |
| 117 |
$status = Status::newFatal( 'parsoid-resource-limit-exceeded', $e->getMessage() ); |
| 118 |
} |
| 119 |
return $status; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public function getCachedParserOutput( |
| 132 |
PageIdentity $page, |
| 133 |
ParserOptions $parserOpts, |
| 134 |
$revision = null, |
| 135 |
bool $lenientRevHandling = false |
| 136 |
): ?ParserOutput { |
| 137 |
wfDeprecated( __METHOD__, '1.43' ); |
| 138 |
[ $page, $revision, $ignored ] = $this->resolveRevision( $page, $revision, $lenientRevHandling ); |
| 139 |
|
| 140 |
$this->adjustParserOptions( $revision, $parserOpts ); |
| 141 |
return $this->parserOutputAccess->getCachedParserOutput( $page, $parserOpts, $revision ); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public function parseUncacheable( |
| 157 |
PageIdentity $page, |
| 158 |
ParserOptions $parserOpts, |
| 159 |
$revision, |
| 160 |
bool $lenientRevHandling = false |
| 161 |
): Status { |
| 162 |
wfDeprecated( __METHOD__, '1.43' ); |
| 163 |
|
| 164 |
|
| 165 |
if ( !$revision instanceof RevisionRecord ) { |
| 166 |
[ $page, $revision, $ignored ] = $this->resolveRevision( $page, $revision, $lenientRevHandling ); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
$revId = $revision->getId(); |
| 171 |
if ( $revId !== 0 && $revId !== null ) { |
| 172 |
return Status::newFatal( 'parsoid-revision-access', |
| 173 |
"parseUncacheable should not be called for a real revision" ); |
| 174 |
} |
| 175 |
|
| 176 |
try { |
| 177 |
|
| 178 |
|
| 179 |
$parser = $this->parsoidParserFactory->create(); |
| 180 |
$parserOutput = $this->parsoidParserFactory->create()->parseFakeRevision( |
| 181 |
$revision, $page, $parserOpts ); |
| 182 |
$parserOutput->updateCacheExpiry( 0 ); |
| 183 |
|
| 184 |
$globalIdGenerator = MediaWikiServices::getInstance()->getGlobalIdGenerator(); |
| 185 |
$parserOutput->setRenderId( $globalIdGenerator->newUUIDv1() ); |
| 186 |
$parserOutput->setCacheRevisionId( $revision->getId() ); |
| 187 |
$parserOutput->setRevisionTimestamp( $revision->getTimestamp() ); |
| 188 |
$parserOutput->setCacheTime( wfTimestampNow() ); |
| 189 |
|
| 190 |
$status = Status::newGood( $parserOutput ); |
| 191 |
} catch ( RevisionAccessException $e ) { |
| 192 |
return Status::newFatal( 'parsoid-revision-access', $e->getMessage() ); |
| 193 |
} catch ( ClientError $e ) { |
| 194 |
$status = Status::newFatal( 'parsoid-client-error', $e->getMessage() ); |
| 195 |
} catch ( ResourceLimitExceededException $e ) { |
| 196 |
$status = Status::newFatal( 'parsoid-resource-limit-exceeded', $e->getMessage() ); |
| 197 |
} |
| 198 |
return $status; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
private function resolveRevision( PageIdentity $page, $revision, bool $lenientRevHandling = false ): array { |
| 209 |
$uncacheable = false; |
| 210 |
if ( !$page instanceof PageRecord ) { |
| 211 |
$name = "$page"; |
| 212 |
$page = $this->pageLookup->getPageByReference( $page ); |
| 213 |
if ( !$page ) { |
| 214 |
throw new RevisionAccessException( |
| 215 |
'Page {name} not found', |
| 216 |
[ 'name' => $name ] |
| 217 |
); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
$revision ??= $page->getLatest(); |
| 222 |
|
| 223 |
if ( is_int( $revision ) ) { |
| 224 |
$revId = $revision; |
| 225 |
$revision = $this->revisionLookup->getRevisionById( $revId ); |
| 226 |
|
| 227 |
if ( !$revision ) { |
| 228 |
throw new RevisionAccessException( |
| 229 |
'Revision {revId} not found', |
| 230 |
[ 'revId' => $revId ] |
| 231 |
); |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
if ( $page->getId() !== $revision->getPageId() ) { |
| 236 |
if ( $lenientRevHandling ) { |
| 237 |
$page = $this->pageLookup->getPageById( $revision->getPageId() ); |
| 238 |
if ( !$page ) { |
| 239 |
|
| 240 |
throw new \RuntimeException( |
| 241 |
"Unexpected NULL page for pageid " . $revision->getPageId() . |
| 242 |
" from revision " . $revision->getId() |
| 243 |
); |
| 244 |
} |
| 245 |
|
| 246 |
$uncacheable = true; |
| 247 |
} else { |
| 248 |
throw new RevisionAccessException( |
| 249 |
'Revision {revId} does not belong to page {name}', |
| 250 |
[ 'name' => $page->getDBkey(), 'revId' => $revision->getId() ] |
| 251 |
); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
return [ $page, $revision, $uncacheable ]; |
| 256 |
} |
| 257 |
|
| 258 |
private function adjustParserOptions( RevisionRecord $revision, ParserOptions $parserOpts ): void { |
| 259 |
$mainSlot = $revision->getSlot( SlotRecord::MAIN ); |
| 260 |
$contentModel = $mainSlot->getModel(); |
| 261 |
if ( $this->siteConfig->supportsContentModel( $contentModel ) ) { |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
$parserOpts->setUseParsoid(); |
| 267 |
} |
| 268 |
} |
| 269 |
} |