MediaWiki  1.33.1
RevisionRenderer.php
Go to the documentation of this file.
1 <?php
23 namespace MediaWiki\Revision;
24 
25 use Html;
26 use InvalidArgumentException;
29 use Psr\Log\LoggerInterface;
30 use Psr\Log\NullLogger;
31 use Title;
32 use User;
34 
46 
49 
51  private $loadBalancer;
52 
54  private $roleRegistery;
55 
57  private $wikiId;
58 
64  public function __construct(
65  ILoadBalancer $loadBalancer,
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 }
ParserOptions
Set options of the Parser.
Definition: ParserOptions.php:42
Revision\RenderedRevision\getRevision
getRevision()
Definition: RenderedRevision.php:151
Revision\RevisionRenderer\$roleRegistery
SlotRoleRegistry $roleRegistery
Definition: RevisionRenderer.php:54
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:45
ParserOutput
Definition: ParserOutput.php:25
$out
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:780
Revision\RenderedRevision\setSaveParseLogger
setSaveParseLogger(LoggerInterface $saveParseLogger)
Definition: RenderedRevision.php:137
User
User
Definition: All_system_messages.txt:425
php
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:35
MediaWiki\Revision
Created by PhpStorm.
Definition: FallbackSlotRoleHandler.php:23
Revision\RevisionRenderer\getRenderedRevision
getRenderedRevision(RevisionRecord $rev, ParserOptions $options=null, User $forUser=null, array $hints=[])
Definition: RevisionRenderer.php:102
$html
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:1993
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:925
Revision\RenderedRevision\getSlotParserOutput
getSlotParserOutput( $role, array $hints=[])
Definition: RenderedRevision.php:220
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
Revision\RevisionRenderer\getSpeculativeRevId
getSpeculativeRevId( $dbIndex)
Definition: RevisionRenderer.php:157
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
Revision\RevisionRenderer\$wikiId
string bool $wikiId
Definition: RevisionRenderer.php:57
DB_MASTER
const DB_MASTER
Definition: defines.php:26
array
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))
Revision\RevisionRenderer
The RevisionRenderer service provides access to rendered output for revisions.
Definition: RevisionRenderer.php:45
Revision\RevisionRenderer\combineSlotOutput
combineSlotOutput(RenderedRevision $rrev, array $hints=[])
This implements the layout for combining the output of multiple slots.
Definition: RevisionRenderer.php:185
ParserOptions\newCanonical
static newCanonical( $context=null, $userLang=null)
Creates a "canonical" ParserOptions object.
Definition: ParserOptions.php:1064
Revision\RevisionRenderer\$saveParseLogger
LoggerInterface $saveParseLogger
Definition: RevisionRenderer.php:48
Revision\SlotRecord\MAIN
const MAIN
Definition: SlotRecord.php:41
Title\newFromLinkTarget
static newFromLinkTarget(LinkTarget $linkTarget, $forceClone='')
Returns a Title given a LinkTarget.
Definition: Title.php:269
Revision\RevisionRecord\FOR_PUBLIC
const FOR_PUBLIC
Definition: RevisionRecord.php:57
Title
Represents a title within MediaWiki.
Definition: Title.php:40
$options
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:1993
Revision\RevisionRecord\DELETED_TEXT
const DELETED_TEXT
Definition: RevisionRecord.php:48
$rev
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:1769
as
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
Definition: distributors.txt:9
Revision\RevisionRenderer\__construct
__construct(ILoadBalancer $loadBalancer, SlotRoleRegistry $roleRegistry, $wikiId=false)
Definition: RevisionRenderer.php:64
Revision\RevisionRenderer\$loadBalancer
ILoadBalancer $loadBalancer
Definition: RevisionRenderer.php:51
Revision\SlotRoleRegistry
A registry service for SlotRoleHandlers, used to define which slot roles are available on which page.
Definition: SlotRoleRegistry.php:48
Revision\RenderedRevision
RenderedRevision represents the rendered representation of a revision.
Definition: RenderedRevision.php:44
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:48
Revision\RevisionRecord\FOR_THIS_USER
const FOR_THIS_USER
Definition: RevisionRecord.php:58
Revision\RevisionRenderer\setLogger
setLogger(LoggerInterface $saveParseLogger)
Definition: RevisionRenderer.php:79
Wikimedia\Rdbms\ILoadBalancer
Database cluster connection, tracking, load balancing, and transaction manager interface.
Definition: ILoadBalancer.php:78
Revision\RenderedRevision\getOptions
getOptions()
Definition: RenderedRevision.php:158