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 SlotRoleRegistry */
54 private $roleRegistery;
56 /** @var string|bool */
60 * @param ILoadBalancer $loadBalancer
61 * @param SlotRoleRegistry $roleRegistry
62 * @param bool|string $dbDomain DB domain of the relevant wiki or false for the current one
64 public function __construct(
65 ILoadBalancer
$loadBalancer,
66 SlotRoleRegistry
$roleRegistry,
69 $this->loadBalancer
= $loadBalancer;
70 $this->roleRegistery
= $roleRegistry;
71 $this->dbDomain
= $dbDomain;
72 $this->saveParseLogger
= new NullLogger();
76 * @param LoggerInterface $saveParseLogger
78 public function setLogger( LoggerInterface
$saveParseLogger ) {
79 $this->saveParseLogger
= $saveParseLogger;
83 * @param RevisionRecord $rev
84 * @param ParserOptions|null $options
85 * @param User|null $forUser User for privileged access. Default is unprivileged (public)
86 * access, unless the 'audience' hint is set to something else RevisionRecord::RAW.
87 * @param array $hints Hints given as an associative array. Known keys:
88 * - 'use-master' Use master when rendering for the parser cache during save.
89 * Default is to use a replica.
90 * - 'audience' the audience to use for content access. Default is
91 * RevisionRecord::FOR_PUBLIC if $forUser is not set, RevisionRecord::FOR_THIS_USER
92 * if $forUser is set. Can be set to RevisionRecord::RAW to disable audience checks.
93 * - 'known-revision-output' a combined ParserOutput for the revision, perhaps from
94 * some cache. the caller is responsible for ensuring that the ParserOutput indeed
95 * matched the $rev and $options. This mechanism is intended as a temporary stop-gap,
96 * for the time until caches have been changed to store RenderedRevision states instead
97 * of ParserOutput objects.
99 * @return RenderedRevision|null The rendered revision, or null if the audience checks fails.
101 public function getRenderedRevision(
103 ParserOptions
$options = null,
104 User
$forUser = null,
107 if ( $rev->getWikiId() !== $this->dbDomain
) {
108 throw new InvalidArgumentException( 'Mismatching wiki ID ' . $rev->getWikiId() );
111 $audience = $hints['audience']
112 ??
( $forUser ? RevisionRecord
::FOR_THIS_USER
: RevisionRecord
::FOR_PUBLIC
);
114 if ( !$rev->audienceCan( RevisionRecord
::DELETED_TEXT
, $audience, $forUser ) ) {
115 // Returning null here is awkward, but consist with the signature of
116 // Revision::getContent() and RevisionRecord::getContent().
121 $options = ParserOptions
::newCanonical( $forUser ?
: 'canonical' );
124 $useMaster = $hints['use-master'] ??
false;
126 $dbIndex = $useMaster
127 ? DB_MASTER
// use latest values
128 : DB_REPLICA
; // T154554
130 $options->setSpeculativeRevIdCallback( function () use ( $dbIndex ) {
131 return $this->getSpeculativeRevId( $dbIndex );
134 if ( !$rev->getId() && $rev->getTimestamp() ) {
135 // This is an unsaved revision with an already determined timestamp.
136 // Make the "current" time used during parsing match that of the revision.
137 // Any REVISION* parser variables will match up if the revision is saved.
138 $options->setTimestamp( $rev->getTimestamp() );
141 $title = Title
::newFromLinkTarget( $rev->getPageAsLinkTarget() );
143 $renderedRevision = new RenderedRevision(
147 function ( RenderedRevision
$rrev, array $hints ) {
148 return $this->combineSlotOutput( $rrev, $hints );
154 $renderedRevision->setSaveParseLogger( $this->saveParseLogger
);
156 if ( isset( $hints['known-revision-output'] ) ) {
157 $renderedRevision->setRevisionParserOutput( $hints['known-revision-output'] );
160 return $renderedRevision;
163 private function getSpeculativeRevId( $dbIndex ) {
164 // Use a fresh master connection in order to see the latest data, by avoiding
165 // stale data from REPEATABLE-READ snapshots.
166 // HACK: But don't use a fresh connection in unit tests, since it would not have
167 // the fake tables. This should be handled by the LoadBalancer!
168 $flags = defined( 'MW_PHPUNIT_TEST' ) ||
$dbIndex === DB_REPLICA
169 ?
0 : ILoadBalancer
::CONN_TRX_AUTOCOMMIT
;
171 $db = $this->loadBalancer
->getConnectionRef( $dbIndex, [], $this->dbDomain
, $flags );
173 return 1 +
(int)$db->selectField(
182 * This implements the layout for combining the output of multiple slots.
184 * @todo Use placement hints from SlotRoleHandlers instead of hard-coding the layout.
186 * @param RenderedRevision $rrev
187 * @param array $hints see RenderedRevision::getRevisionParserOutput()
189 * @return ParserOutput
191 private function combineSlotOutput( RenderedRevision
$rrev, array $hints = [] ) {
192 $revision = $rrev->getRevision();
193 $slots = $revision->getSlots()->getSlots();
195 $withHtml = $hints['generate-html'] ??
true;
197 // short circuit if there is only the main slot
198 if ( array_keys( $slots ) === [ SlotRecord
::MAIN
] ) {
199 return $rrev->getSlotParserOutput( SlotRecord
::MAIN
);
202 // move main slot to front
203 if ( isset( $slots[SlotRecord
::MAIN
] ) ) {
204 $slots = [ SlotRecord
::MAIN
=> $slots[SlotRecord
::MAIN
] ] +
$slots;
207 $combinedOutput = new ParserOutput( null );
210 $options = $rrev->getOptions();
211 $options->registerWatcher( [ $combinedOutput, 'recordOption' ] );
213 foreach ( $slots as $role => $slot ) {
214 $out = $rrev->getSlotParserOutput( $role, $hints );
215 $slotOutput[$role] = $out;
217 // XXX: should the SlotRoleHandler be able to intervene here?
218 $combinedOutput->mergeInternalMetaDataFrom( $out );
219 $combinedOutput->mergeTrackingMetaDataFrom( $out );
225 /** @var ParserOutput $out */
226 foreach ( $slotOutput as $role => $out ) {
227 $roleHandler = $this->roleRegistery
->getRoleHandler( $role );
229 // TODO: put more fancy layout logic here, see T200915.
230 $layout = $roleHandler->getOutputLayoutHints();
231 $display = $layout['display'] ??
'section';
233 if ( $display === 'none' ) {
238 // skip header for the first slot
241 // NOTE: this placeholder is hydrated by ParserOutput::getText().
242 $headText = Html
::element( 'mw:slotheader', [], $role );
243 $html .= Html
::rawElement( 'h1', [ 'class' => 'mw-slot-header' ], $headText );
246 // XXX: do we want to put a wrapper div around the output?
247 // Do we want to let $roleHandler do that?
248 $html .= $out->getRawText();
249 $combinedOutput->mergeHtmlMetaDataFrom( $out );
252 $combinedOutput->setText( $html );
255 $options->registerWatcher( null );
256 return $combinedOutput;