MediaWiki  1.32.5
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 $wikiId;
55 
60  public function __construct( ILoadBalancer $loadBalancer, $wikiId = false ) {
61  $this->loadBalancer = $loadBalancer;
62  $this->wikiId = $wikiId;
63 
64  $this->saveParseLogger = new NullLogger();
65  }
66 
70  public function setLogger( LoggerInterface $saveParseLogger ) {
71  $this->saveParseLogger = $saveParseLogger;
72  }
73 
88  public function getRenderedRevision(
90  ParserOptions $options = null,
91  User $forUser = null,
92  array $hints = []
93  ) {
94  if ( $rev->getWikiId() !== $this->wikiId ) {
95  throw new InvalidArgumentException( 'Mismatching wiki ID ' . $rev->getWikiId() );
96  }
97 
98  $audience = $hints['audience']
100 
101  if ( !$rev->audienceCan( RevisionRecord::DELETED_TEXT, $audience, $forUser ) ) {
102  // Returning null here is awkward, but consist with the signature of
103  // Revision::getContent() and RevisionRecord::getContent().
104  return null;
105  }
106 
107  if ( !$options ) {
108  $options = ParserOptions::newCanonical( $forUser ?: 'canonical' );
109  }
110 
111  $useMaster = $hints['use-master'] ?? false;
112 
113  $dbIndex = $useMaster
114  ? DB_MASTER // use latest values
115  : DB_REPLICA; // T154554
116 
117  $options->setSpeculativeRevIdCallback( function () use ( $dbIndex ) {
118  return $this->getSpeculativeRevId( $dbIndex );
119  } );
120 
121  $title = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
122 
123  $renderedRevision = new RenderedRevision(
124  $title,
125  $rev,
126  $options,
127  function ( RenderedRevision $rrev, array $hints ) {
128  return $this->combineSlotOutput( $rrev, $hints );
129  },
130  $audience,
131  $forUser
132  );
133 
134  $renderedRevision->setSaveParseLogger( $this->saveParseLogger );
135 
136  return $renderedRevision;
137  }
138 
139  private function getSpeculativeRevId( $dbIndex ) {
140  // Use a fresh master connection in order to see the latest data, by avoiding
141  // stale data from REPEATABLE-READ snapshots.
142  // HACK: But don't use a fresh connection in unit tests, since it would not have
143  // the fake tables. This should be handled by the LoadBalancer!
144  $flags = defined( 'MW_PHPUNIT_TEST' ) || $dbIndex === DB_REPLICA
145  ? 0 : ILoadBalancer::CONN_TRX_AUTOCOMMIT;
146 
147  $db = $this->loadBalancer->getConnectionRef( $dbIndex, [], $this->wikiId, $flags );
148 
149  return 1 + (int)$db->selectField(
150  'revision',
151  'MAX(rev_id)',
152  [],
153  __METHOD__
154  );
155  }
156 
167  private function combineSlotOutput( RenderedRevision $rrev, array $hints = [] ) {
168  $revision = $rrev->getRevision();
169  $slots = $revision->getSlots()->getSlots();
170 
171  $withHtml = $hints['generate-html'] ?? true;
172 
173  // short circuit if there is only the main slot
174  if ( array_keys( $slots ) === [ SlotRecord::MAIN ] ) {
175  return $rrev->getSlotParserOutput( SlotRecord::MAIN );
176  }
177 
178  // TODO: put fancy layout logic here, see T200915.
179 
180  // move main slot to front
181  if ( isset( $slots[SlotRecord::MAIN] ) ) {
182  $slots = [ SlotRecord::MAIN => $slots[SlotRecord::MAIN] ] + $slots;
183  }
184 
185  $combinedOutput = new ParserOutput( null );
186  $slotOutput = [];
187 
188  $options = $rrev->getOptions();
189  $options->registerWatcher( [ $combinedOutput, 'recordOption' ] );
190 
191  foreach ( $slots as $role => $slot ) {
192  $out = $rrev->getSlotParserOutput( $role, $hints );
193  $slotOutput[$role] = $out;
194 
195  $combinedOutput->mergeInternalMetaDataFrom( $out, $role );
196  $combinedOutput->mergeTrackingMetaDataFrom( $out );
197  }
198 
199  if ( $withHtml ) {
200  $html = '';
201  $first = true;
203  foreach ( $slotOutput as $role => $out ) {
204  if ( $first ) {
205  // skip header for the first slot
206  $first = false;
207  } else {
208  // NOTE: this placeholder is hydrated by ParserOutput::getText().
209  $headText = Html::element( 'mw:slotheader', [], $role );
210  $html .= Html::rawElement( 'h1', [ 'class' => 'mw-slot-header' ], $headText );
211  }
212 
213  $html .= $out->getRawText();
214  $combinedOutput->mergeHtmlMetaDataFrom( $out );
215  }
216 
217  $combinedOutput->setText( $html );
218  }
219 
220  $options->registerWatcher( null );
221  return $combinedOutput;
222  }
223 
224 }
ParserOptions
Set options of the Parser.
Definition: ParserOptions.php:42
Revision\RenderedRevision\getRevision
getRevision()
Definition: RenderedRevision.php:151
Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:45
ParserOutput
Definition: ParserOutput.php:25
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: IncompleteRevisionException.php:23
Revision\RevisionRenderer\getRenderedRevision
getRenderedRevision(RevisionRecord $rev, ParserOptions $options=null, User $forUser=null, array $hints=[])
Definition: RevisionRenderer.php:88
$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:2044
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:964
Title\newFromLinkTarget
static newFromLinkTarget(LinkTarget $linkTarget)
Create a new Title from a LinkTarget.
Definition: Title.php:251
Revision\RenderedRevision\getSlotParserOutput
getSlotParserOutput( $role, array $hints=[])
Definition: RenderedRevision.php:198
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\__construct
__construct(ILoadBalancer $loadBalancer, $wikiId=false)
Definition: RevisionRenderer.php:60
Revision\RevisionRenderer\getSpeculativeRevId
getSpeculativeRevId( $dbIndex)
Definition: RevisionRenderer.php:139
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
Revision\RevisionRenderer\$wikiId
string bool $wikiId
Definition: RevisionRenderer.php:54
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:167
ParserOptions\newCanonical
static newCanonical( $context=null, $userLang=null)
Creates a "canonical" ParserOptions object.
Definition: ParserOptions.php:1061
Revision\RevisionRenderer\$saveParseLogger
LoggerInterface $saveParseLogger
Definition: RevisionRenderer.php:48
Revision\SlotRecord\MAIN
const MAIN
Definition: SlotRecord.php:41
Revision\RevisionRecord\FOR_PUBLIC
const FOR_PUBLIC
Definition: RevisionRecord.php:57
Title
Represents a title within MediaWiki.
Definition: Title.php:39
$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:2044
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:1808
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
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:210
Revision\RevisionRenderer\$loadBalancer
ILoadBalancer $loadBalancer
Definition: RevisionRenderer.php:51
Revision\RenderedRevision
RenderedRevision represents the rendered representation of a revision.
Definition: RenderedRevision.php:44
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:232
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:47
Revision\RevisionRecord\FOR_THIS_USER
const FOR_THIS_USER
Definition: RevisionRecord.php:58
Revision\RevisionRenderer\setLogger
setLogger(LoggerInterface $saveParseLogger)
Definition: RevisionRenderer.php:70
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
Html
This class is a collection of static functions that serve two purposes:
Definition: Html.php:49
$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 probably a stub 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:813