3 * This file is part of MediaWiki.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 namespace MediaWiki\Revision
;
26 use InvalidArgumentException
;
29 use Psr\Log\LoggerInterface
;
30 use Psr\Log\NullLogger
;
33 use Wikimedia\Rdbms\ILoadBalancer
;
36 * The RevisionRenderer service provides access to rendered output for revisions.
37 * It does so be acting as a factory for RenderedRevision instances, which in turn
38 * provide lazy access to ParserOutput objects.
40 * One key responsibility of RevisionRenderer is implementing the layout used to combine
41 * the output of multiple slots.
45 class RevisionRenderer
{
47 /** @var LoggerInterface */
48 private $saveParseLogger;
50 /** @var ILoadBalancer */
51 private $loadBalancer;
53 /** @var string|bool */
57 * @param ILoadBalancer $loadBalancer
58 * @param bool|string $wikiId
60 public function __construct( ILoadBalancer
$loadBalancer, $wikiId = false ) {
61 $this->loadBalancer
= $loadBalancer;
62 $this->wikiId
= $wikiId;
64 $this->saveParseLogger
= new NullLogger();
68 * @param RevisionRecord $rev
69 * @param ParserOptions|null $options
70 * @param User|null $forUser User for privileged access. Default is unprivileged (public)
71 * access, unless the 'audience' hint is set to something else RevisionRecord::RAW.
72 * @param array $hints Hints given as an associative array. Known keys:
73 * - 'use-master' Use master when rendering for the parser cache during save.
74 * Default is to use a replica.
75 * - 'audience' the audience to use for content access. Default is
76 * RevisionRecord::FOR_PUBLIC if $forUser is not set, RevisionRecord::FOR_THIS_USER
77 * if $forUser is set. Can be set to RevisionRecord::RAW to disable audience checks.
79 * @return RenderedRevision|null The rendered revision, or null if the audience checks fails.
81 public function getRenderedRevision(
83 ParserOptions
$options = null,
87 if ( $rev->getWikiId() !== $this->wikiId
) {
88 throw new InvalidArgumentException( 'Mismatching wiki ID ' . $rev->getWikiId() );
91 $audience = $hints['audience']
92 ??
( $forUser ? RevisionRecord
::FOR_THIS_USER
: RevisionRecord
::FOR_PUBLIC
);
94 if ( !$rev->audienceCan( RevisionRecord
::DELETED_TEXT
, $audience, $forUser ) ) {
95 // Returning null here is awkward, but consist with the signature of
96 // Revision::getContent() and RevisionRecord::getContent().
101 $options = ParserOptions
::newCanonical( $forUser ?
: 'canonical' );
104 $useMaster = $hints['use-master'] ??
false;
106 $dbIndex = $useMaster
107 ? DB_MASTER
// use latest values
108 : DB_REPLICA
; // T154554
110 $options->setSpeculativeRevIdCallback( function () use ( $dbIndex ) {
111 return $this->getSpeculativeRevId( $dbIndex );
114 $title = Title
::newFromLinkTarget( $rev->getPageAsLinkTarget() );
116 $renderedRevision = new RenderedRevision(
120 function ( RenderedRevision
$rrev, array $hints ) {
121 return $this->combineSlotOutput( $rrev, $hints );
127 $renderedRevision->setSaveParseLogger( $this->saveParseLogger
);
129 return $renderedRevision;
132 private function getSpeculativeRevId( $dbIndex ) {
133 // Use a fresh master connection in order to see the latest data, by avoiding
134 // stale data from REPEATABLE-READ snapshots.
135 // HACK: But don't use a fresh connection in unit tests, since it would not have
136 // the fake tables. This should be handled by the LoadBalancer!
137 $flags = defined( 'MW_PHPUNIT_TEST' ) ||
$dbIndex === DB_REPLICA
138 ?
0 : ILoadBalancer
::CONN_TRX_AUTOCOMMIT
;
140 $db = $this->loadBalancer
->getConnectionRef( $dbIndex, [], $this->wikiId
, $flags );
142 return 1 +
(int)$db->selectField(
151 * This implements the layout for combining the output of multiple slots.
153 * @todo Use placement hints from SlotRoleHandlers instead of hard-coding the layout.
155 * @param RenderedRevision $rrev
156 * @param array $hints see RenderedRevision::getRevisionParserOutput()
158 * @return ParserOutput
160 private function combineSlotOutput( RenderedRevision
$rrev, array $hints = [] ) {
161 $revision = $rrev->getRevision();
162 $slots = $revision->getSlots()->getSlots();
164 $withHtml = $hints['generate-html'] ??
true;
166 // short circuit if there is only the main slot
167 if ( array_keys( $slots ) === [ SlotRecord
::MAIN
] ) {
168 return $rrev->getSlotParserOutput( SlotRecord
::MAIN
);
171 // TODO: put fancy layout logic here, see T200915.
173 // move main slot to front
174 if ( isset( $slots[SlotRecord
::MAIN
] ) ) {
175 $slots = [ SlotRecord
::MAIN
=> $slots[SlotRecord
::MAIN
] ] +
$slots;
178 $combinedOutput = new ParserOutput( null );
181 $options = $rrev->getOptions();
182 $options->registerWatcher( [ $combinedOutput, 'recordOption' ] );
184 foreach ( $slots as $role => $slot ) {
185 $out = $rrev->getSlotParserOutput( $role, $hints );
186 $slotOutput[$role] = $out;
188 $combinedOutput->mergeInternalMetaDataFrom( $out, $role );
189 $combinedOutput->mergeTrackingMetaDataFrom( $out );
195 /** @var ParserOutput $out */
196 foreach ( $slotOutput as $role => $out ) {
198 // skip header for the first slot
201 // NOTE: this placeholder is hydrated by ParserOutput::getText().
202 $headText = Html
::element( 'mw:slotheader', [], $role );
203 $html .= Html
::rawElement( 'h1', [ 'class' => 'mw-slot-header' ], $headText );
206 $html .= $out->getRawText();
207 $combinedOutput->mergeHtmlMetaDataFrom( $out );
210 $combinedOutput->setText( $html );
213 $options->registerWatcher( null );
214 return $combinedOutput;