MediaWiki REL1_33
RevisionRenderer.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Revision;
24
25use Html;
26use InvalidArgumentException;
29use Psr\Log\LoggerInterface;
30use Psr\Log\NullLogger;
32use User;
34
46
49
52
55
57 private $wikiId;
58
64 public function __construct(
66 SlotRoleRegistry $roleRegistry,
67 $wikiId = false
68 ) {
69 $this->loadBalancer = $loadBalancer;
70 $this->roleRegistery = $roleRegistry;
71 $this->wikiId = $wikiId;
72
73 $this->saveParseLogger = new NullLogger();
74 }
75
79 public function setLogger( LoggerInterface $saveParseLogger ) {
80 $this->saveParseLogger = $saveParseLogger;
81 }
82
102 public function getRenderedRevision(
104 ParserOptions $options = null,
105 User $forUser = null,
106 array $hints = []
107 ) {
108 if ( $rev->getWikiId() !== $this->wikiId ) {
109 throw new InvalidArgumentException( 'Mismatching wiki ID ' . $rev->getWikiId() );
110 }
111
112 $audience = $hints['audience']
114
115 if ( !$rev->audienceCan( RevisionRecord::DELETED_TEXT, $audience, $forUser ) ) {
116 // Returning null here is awkward, but consist with the signature of
117 // Revision::getContent() and RevisionRecord::getContent().
118 return null;
119 }
120
121 if ( !$options ) {
122 $options = ParserOptions::newCanonical( $forUser ?: 'canonical' );
123 }
124
125 $useMaster = $hints['use-master'] ?? false;
126
127 $dbIndex = $useMaster
128 ? DB_MASTER // use latest values
129 : DB_REPLICA; // T154554
130
131 $options->setSpeculativeRevIdCallback( function () use ( $dbIndex ) {
132 return $this->getSpeculativeRevId( $dbIndex );
133 } );
134
135 $title = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
136
137 $renderedRevision = new RenderedRevision(
138 $title,
139 $rev,
140 $options,
141 function ( RenderedRevision $rrev, array $hints ) {
142 return $this->combineSlotOutput( $rrev, $hints );
143 },
144 $audience,
145 $forUser
146 );
147
148 $renderedRevision->setSaveParseLogger( $this->saveParseLogger );
149
150 if ( isset( $hints['known-revision-output'] ) ) {
151 $renderedRevision->setRevisionParserOutput( $hints['known-revision-output'] );
152 }
153
154 return $renderedRevision;
155 }
156
157 private function getSpeculativeRevId( $dbIndex ) {
158 // Use a fresh master connection in order to see the latest data, by avoiding
159 // stale data from REPEATABLE-READ snapshots.
160 // HACK: But don't use a fresh connection in unit tests, since it would not have
161 // the fake tables. This should be handled by the LoadBalancer!
162 $flags = defined( 'MW_PHPUNIT_TEST' ) || $dbIndex === DB_REPLICA
163 ? 0 : ILoadBalancer::CONN_TRX_AUTOCOMMIT;
164
165 $db = $this->loadBalancer->getConnectionRef( $dbIndex, [], $this->wikiId, $flags );
166
167 return 1 + (int)$db->selectField(
168 'revision',
169 'MAX(rev_id)',
170 [],
171 __METHOD__
172 );
173 }
174
185 private function combineSlotOutput( RenderedRevision $rrev, array $hints = [] ) {
186 $revision = $rrev->getRevision();
187 $slots = $revision->getSlots()->getSlots();
188
189 $withHtml = $hints['generate-html'] ?? true;
190
191 // short circuit if there is only the main slot
192 if ( array_keys( $slots ) === [ SlotRecord::MAIN ] ) {
193 return $rrev->getSlotParserOutput( SlotRecord::MAIN );
194 }
195
196 // move main slot to front
197 if ( isset( $slots[SlotRecord::MAIN] ) ) {
198 $slots = [ SlotRecord::MAIN => $slots[SlotRecord::MAIN] ] + $slots;
199 }
200
201 $combinedOutput = new ParserOutput( null );
202 $slotOutput = [];
203
204 $options = $rrev->getOptions();
205 $options->registerWatcher( [ $combinedOutput, 'recordOption' ] );
206
207 foreach ( $slots as $role => $slot ) {
208 $out = $rrev->getSlotParserOutput( $role, $hints );
209 $slotOutput[$role] = $out;
210
211 // XXX: should the SlotRoleHandler be able to intervene here?
212 $combinedOutput->mergeInternalMetaDataFrom( $out, $role );
213 $combinedOutput->mergeTrackingMetaDataFrom( $out );
214 }
215
216 if ( $withHtml ) {
217 $html = '';
218 $first = true;
220 foreach ( $slotOutput as $role => $out ) {
221 $roleHandler = $this->roleRegistery->getRoleHandler( $role );
222
223 // TODO: put more fancy layout logic here, see T200915.
224 $layout = $roleHandler->getOutputLayoutHints();
225 $display = $layout['display'] ?? 'section';
226
227 if ( $display === 'none' ) {
228 continue;
229 }
230
231 if ( $first ) {
232 // skip header for the first slot
233 $first = false;
234 } else {
235 // NOTE: this placeholder is hydrated by ParserOutput::getText().
236 $headText = Html::element( 'mw:slotheader', [], $role );
237 $html .= Html::rawElement( 'h1', [ 'class' => 'mw-slot-header' ], $headText );
238 }
239
240 // XXX: do we want to put a wrapper div around the output?
241 // Do we want to let $roleHandler do that?
242 $html .= $out->getRawText();
243 $combinedOutput->mergeHtmlMetaDataFrom( $out );
244 }
245
246 $combinedOutput->setText( $html );
247 }
248
249 $options->registerWatcher( null );
250 return $combinedOutput;
251 }
252
253}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
This class is a collection of static functions that serve two purposes:
Definition Html.php:49
RenderedRevision represents the rendered representation of a revision.
setSaveParseLogger(LoggerInterface $saveParseLogger)
getSlotParserOutput( $role, array $hints=[])
Page revision base class.
The RevisionRenderer service provides access to rendered output for revisions.
setLogger(LoggerInterface $saveParseLogger)
getRenderedRevision(RevisionRecord $rev, ParserOptions $options=null, User $forUser=null, array $hints=[])
__construct(ILoadBalancer $loadBalancer, SlotRoleRegistry $roleRegistry, $wikiId=false)
combineSlotOutput(RenderedRevision $rrev, array $hints=[])
This implements the layout for combining the output of multiple slots.
A registry service for SlotRoleHandlers, used to define which slot roles are available on which page.
Set options of the Parser.
Represents a title within MediaWiki.
Definition Title.php:40
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:48
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition hooks.txt:855
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:1999
namespace and then decline to actually register it file or subcat img or subcat $title
Definition hooks.txt:955
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses & $html
Definition hooks.txt:2011
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition hooks.txt:1779
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
Database cluster connection, tracking, load balancing, and transaction manager interface.
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:26