| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Revision; |
| 24 |
|
| 25 |
use InvalidArgumentException; |
| 26 |
use MediaWiki\Content\Renderer\ContentRenderer; |
| 27 |
use MediaWiki\Html\Html; |
| 28 |
use MediaWiki\Parser\ParserOptions; |
| 29 |
use MediaWiki\Parser\ParserOutput; |
| 30 |
use MediaWiki\Parser\ParserOutputFlags; |
| 31 |
use MediaWiki\Permissions\Authority; |
| 32 |
use Psr\Log\LoggerInterface; |
| 33 |
use Psr\Log\NullLogger; |
| 34 |
use Wikimedia\Rdbms\ILoadBalancer; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
class RevisionRenderer { |
| 47 |
|
| 48 |
|
| 49 |
private $saveParseLogger; |
| 50 |
|
| 51 |
|
| 52 |
private $loadBalancer; |
| 53 |
|
| 54 |
|
| 55 |
private $roleRegistry; |
| 56 |
|
| 57 |
|
| 58 |
private $contentRenderer; |
| 59 |
|
| 60 |
|
| 61 |
private $dbDomain; |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public function __construct( |
| 70 |
ILoadBalancer $loadBalancer, |
| 71 |
SlotRoleRegistry $roleRegistry, |
| 72 |
ContentRenderer $contentRenderer, |
| 73 |
$dbDomain = false |
| 74 |
) { |
| 75 |
$this->loadBalancer = $loadBalancer; |
| 76 |
$this->roleRegistry = $roleRegistry; |
| 77 |
$this->contentRenderer = $contentRenderer; |
| 78 |
$this->dbDomain = $dbDomain; |
| 79 |
$this->saveParseLogger = new NullLogger(); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public function setLogger( LoggerInterface $saveParseLogger ) { |
| 86 |
$this->saveParseLogger = $saveParseLogger; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public function getRenderedRevision( |
| 118 |
RevisionRecord $rev, |
| 119 |
?ParserOptions $options = null, |
| 120 |
?Authority $forPerformer = null, |
| 121 |
array $hints = [] |
| 122 |
) { |
| 123 |
if ( $rev->getWikiId() !== $this->dbDomain ) { |
| 124 |
throw new InvalidArgumentException( 'Mismatching wiki ID ' . $rev->getWikiId() ); |
| 125 |
} |
| 126 |
|
| 127 |
$audience = $hints['audience'] |
| 128 |
?? ( $forPerformer ? RevisionRecord::FOR_THIS_USER : RevisionRecord::FOR_PUBLIC ); |
| 129 |
|
| 130 |
if ( !$rev->audienceCan( RevisionRecord::DELETED_TEXT, $audience, $forPerformer ) ) { |
| 131 |
|
| 132 |
|
| 133 |
return null; |
| 134 |
} |
| 135 |
|
| 136 |
if ( !$options ) { |
| 137 |
$options = $forPerformer ? |
| 138 |
ParserOptions::newFromUser( $forPerformer->getUser() ) : |
| 139 |
ParserOptions::newFromAnon(); |
| 140 |
} |
| 141 |
|
| 142 |
if ( isset( $hints['causeAction'] ) ) { |
| 143 |
$options->setRenderReason( $hints['causeAction'] ); |
| 144 |
} |
| 145 |
|
| 146 |
$usePrimary = $hints['use-master'] ?? false; |
| 147 |
|
| 148 |
$dbIndex = $usePrimary |
| 149 |
? DB_PRIMARY |
| 150 |
: DB_REPLICA; |
| 151 |
|
| 152 |
$options->setSpeculativeRevIdCallback( function () use ( $dbIndex ) { |
| 153 |
return $this->getSpeculativeRevId( $dbIndex ); |
| 154 |
} ); |
| 155 |
$options->setSpeculativePageIdCallback( function () use ( $dbIndex ) { |
| 156 |
return $this->getSpeculativePageId( $dbIndex ); |
| 157 |
} ); |
| 158 |
|
| 159 |
if ( !$rev->getId() && $rev->getTimestamp() ) { |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
$options->setTimestamp( $rev->getTimestamp() ); |
| 164 |
} |
| 165 |
|
| 166 |
$previousOutput = $hints['previous-output'] ?? null; |
| 167 |
$renderedRevision = new RenderedRevision( |
| 168 |
$rev, |
| 169 |
$options, |
| 170 |
$this->contentRenderer, |
| 171 |
function ( RenderedRevision $rrev, array $hints ) use ( $options, $previousOutput ) { |
| 172 |
$h = [ 'previous-output' => $previousOutput ] + $hints; |
| 173 |
return $this->combineSlotOutput( $rrev, $options, $h ); |
| 174 |
}, |
| 175 |
$audience, |
| 176 |
$forPerformer |
| 177 |
); |
| 178 |
|
| 179 |
$renderedRevision->setSaveParseLogger( $this->saveParseLogger ); |
| 180 |
|
| 181 |
if ( isset( $hints['known-revision-output'] ) ) { |
| 182 |
$renderedRevision->setRevisionParserOutput( $hints['known-revision-output'] ); |
| 183 |
} |
| 184 |
|
| 185 |
return $renderedRevision; |
| 186 |
} |
| 187 |
|
| 188 |
private function getSpeculativeRevId( $dbIndex ) { |
| 189 |
|
| 190 |
|
| 191 |
$flags = ILoadBalancer::CONN_TRX_AUTOCOMMIT; |
| 192 |
|
| 193 |
$db = $this->loadBalancer->getConnection( $dbIndex, [], $this->dbDomain, $flags ); |
| 194 |
|
| 195 |
return 1 + (int)$db->newSelectQueryBuilder() |
| 196 |
->select( 'MAX(rev_id)' ) |
| 197 |
->from( 'revision' ) |
| 198 |
->caller( __METHOD__ )->fetchField(); |
| 199 |
} |
| 200 |
|
| 201 |
private function getSpeculativePageId( $dbIndex ) { |
| 202 |
|
| 203 |
|
| 204 |
$flags = ILoadBalancer::CONN_TRX_AUTOCOMMIT; |
| 205 |
|
| 206 |
$db = $this->loadBalancer->getConnection( $dbIndex, [], $this->dbDomain, $flags ); |
| 207 |
|
| 208 |
return 1 + (int)$db->newSelectQueryBuilder() |
| 209 |
->select( 'MAX(page_id)' ) |
| 210 |
->from( 'page' ) |
| 211 |
->caller( __METHOD__ )->fetchField(); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
private function combineSlotOutput( RenderedRevision $rrev, ParserOptions $options, array $hints = [] ) { |
| 229 |
$revision = $rrev->getRevision(); |
| 230 |
$slots = $revision->getSlots()->getSlots(); |
| 231 |
|
| 232 |
$withHtml = $hints['generate-html'] ?? true; |
| 233 |
$previousOutputs = $this->splitSlotOutput( $rrev, $options, $hints['previous-output'] ?? null ); |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
if ( array_keys( $slots ) === [ SlotRecord::MAIN ] || $options->getUseParsoid() ) { |
| 239 |
$h = [ 'previous-output' => $previousOutputs[SlotRecord::MAIN] ] + $hints; |
| 240 |
return $rrev->getSlotParserOutput( SlotRecord::MAIN, $h ); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
if ( isset( $slots[SlotRecord::MAIN] ) ) { |
| 245 |
$slots = [ SlotRecord::MAIN => $slots[SlotRecord::MAIN] ] + $slots; |
| 246 |
} |
| 247 |
|
| 248 |
$combinedOutput = new ParserOutput( null ); |
| 249 |
$slotOutput = []; |
| 250 |
|
| 251 |
$options = $rrev->getOptions(); |
| 252 |
$options->registerWatcher( [ $combinedOutput, 'recordOption' ] ); |
| 253 |
|
| 254 |
foreach ( $slots as $role => $slot ) { |
| 255 |
$h = [ 'previous-output' => $previousOutputs[$role] ] + $hints; |
| 256 |
$out = $rrev->getSlotParserOutput( $role, $h ); |
| 257 |
$slotOutput[$role] = $out; |
| 258 |
|
| 259 |
|
| 260 |
$combinedOutput->mergeInternalMetaDataFrom( $out ); |
| 261 |
$combinedOutput->mergeTrackingMetaDataFrom( $out ); |
| 262 |
} |
| 263 |
|
| 264 |
if ( $withHtml ) { |
| 265 |
$html = ''; |
| 266 |
$first = true; |
| 267 |
|
| 268 |
foreach ( $slotOutput as $role => $out ) { |
| 269 |
$roleHandler = $this->roleRegistry->getRoleHandler( $role ); |
| 270 |
|
| 271 |
|
| 272 |
$layout = $roleHandler->getOutputLayoutHints(); |
| 273 |
$display = $layout['display'] ?? 'section'; |
| 274 |
|
| 275 |
if ( $display === 'none' ) { |
| 276 |
continue; |
| 277 |
} |
| 278 |
|
| 279 |
if ( $first ) { |
| 280 |
|
| 281 |
$first = false; |
| 282 |
} else { |
| 283 |
|
| 284 |
$headText = Html::element( 'mw:slotheader', [], $role ); |
| 285 |
$html .= Html::rawElement( 'h1', [ 'class' => 'mw-slot-header' ], $headText ); |
| 286 |
$combinedOutput->setOutputFlag( ParserOutputFlags::HAS_SLOT_HEADERS ); |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
$html .= $out->getRawText(); |
| 292 |
$combinedOutput->mergeHtmlMetaDataFrom( $out ); |
| 293 |
} |
| 294 |
|
| 295 |
$combinedOutput->setRawText( $html ); |
| 296 |
} |
| 297 |
|
| 298 |
$options->registerWatcher( null ); |
| 299 |
return $combinedOutput; |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
private function splitSlotOutput( RenderedRevision $rrev, ParserOptions $options, ?ParserOutput $previousOutput ) { |
| 320 |
|
| 321 |
$revision = $rrev->getRevision(); |
| 322 |
$revslots = $revision->getSlots(); |
| 323 |
if ( $previousOutput === null ) { |
| 324 |
return array_fill_keys( $revslots->getSlotRoles(), null ); |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
if ( $revslots->getSlotRoles() === [ SlotRecord::MAIN ] || $options->getUseParsoid() ) { |
| 331 |
return [ SlotRecord::MAIN => $previousOutput ]; |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
return array_fill_keys( $revslots->getSlotRoles(), null ); |
| 336 |
} |
| 337 |
} |