MediaWiki  master
CommentFormatter.php
Go to the documentation of this file.
1 <?php
2 
4 
8 use Traversable;
9 
18  protected $parserFactory;
19 
26  $this->parserFactory = $parserFactory;
27  }
28 
34  public function createBatch() {
35  return new CommentBatch( $this );
36  }
37 
50  public function format( string $comment, LinkTarget $selfLinkTarget = null,
51  $samePage = false, $wikiId = false
52  ) {
53  return $this->formatInternal( $comment, true, false, false,
54  $selfLinkTarget, $samePage, $wikiId );
55  }
56 
71  public function formatBlock( string $comment, LinkTarget $selfLinkTarget = null,
72  $samePage = false, $wikiId = false, $useParentheses = true
73  ) {
74  return $this->formatInternal( $comment, true, true, $useParentheses,
75  $selfLinkTarget, $samePage, $wikiId );
76  }
77 
96  public function formatLinksUnsafe( string $comment, LinkTarget $selfLinkTarget = null,
97  $samePage = false, $wikiId = false
98  ) {
99  $parser = $this->parserFactory->create();
100  $preprocessed = $parser->preprocessUnsafe( $comment, $selfLinkTarget,
101  $samePage, $wikiId, false );
102  return $parser->finalize( $preprocessed );
103  }
104 
117  public function formatLinks( string $comment, LinkTarget $selfLinkTarget = null,
118  $samePage = false, $wikiId = false
119  ) {
120  return $this->formatInternal( $comment, false, false, false,
121  $selfLinkTarget, $samePage, $wikiId );
122  }
123 
139  private function formatInternal( $comment, $enableSectionLinks, $useBlock, $useParentheses,
140  $selfLinkTarget = null, $samePage = false, $wikiId = false
141  ) {
142  $parser = $this->parserFactory->create();
143  $preprocessed = $parser->preprocess( $comment, $selfLinkTarget, $samePage, $wikiId,
144  $enableSectionLinks );
145  $output = $parser->finalize( $preprocessed );
146  if ( $useBlock ) {
147  $output = $this->wrapCommentWithBlock( $output, $useParentheses );
148  }
149  return $output;
150  }
151 
167  public function formatStrings( $strings, LinkTarget $selfLinkTarget = null,
168  $samePage = false, $wikiId = false
169  ) {
170  $parser = $this->parserFactory->create();
171  $outputs = [];
172  foreach ( $strings as $i => $comment ) {
173  $outputs[$i] = $parser->preprocess( $comment, $selfLinkTarget, $samePage, $wikiId );
174  }
175  return $parser->finalize( $outputs );
176  }
177 
195  public function formatStringsAsBlock( $strings, LinkTarget $selfLinkTarget = null,
196  $samePage = false, $wikiId = false, $useParentheses = true
197  ) {
198  $parser = $this->parserFactory->create();
199  $outputs = [];
200  foreach ( $strings as $i => $comment ) {
201  $outputs[$i] = $this->wrapCommentWithBlock(
202  $parser->preprocess( $comment, $selfLinkTarget, $samePage, $wikiId ),
203  $useParentheses );
204  }
205  return $parser->finalize( $outputs );
206  }
207 
227  public function formatRevision(
228  RevisionRecord $revision,
229  Authority $authority,
230  $samePage = false,
231  $isPublic = false,
232  $useParentheses = true
233  ) {
234  $parser = $this->parserFactory->create();
235  return $parser->finalize( $this->preprocessRevComment(
236  $parser, $authority, $revision, $samePage, $isPublic, $useParentheses ) );
237  }
238 
252  public function formatRevisions(
253  $revisions,
254  Authority $authority,
255  $samePage = false,
256  $isPublic = false,
257  $useParentheses = true,
258  $indexById = false
259  ) {
260  $parser = $this->parserFactory->create();
261  $outputs = [];
262  foreach ( $revisions as $i => $rev ) {
263  if ( $indexById ) {
264  $key = $rev->getId();
265  } else {
266  $key = $i;
267  }
268  $outputs[$key] = $this->preprocessRevComment(
269  $parser, $authority, $rev, $samePage, $isPublic, $useParentheses );
270  }
271  return $parser->finalize( $outputs );
272  }
273 
279  public function createRevisionBatch() {
280  return new RevisionCommentBatch( $this );
281  }
282 
292  public function formatItems( $items ) {
293  return $this->formatItemsInternal( $items );
294  }
295 
310  public function formatItemsInternal( $items, $selfLinkTarget = null,
311  $samePage = null, $wikiId = null, $enableSectionLinks = null,
312  $useBlock = null, $useParentheses = null
313  ) {
314  $outputs = [];
315  $parser = $this->parserFactory->create();
316  foreach ( $items as $index => $item ) {
317  $preprocessed = $parser->preprocess(
318  $item->comment,
319  $item->selfLinkTarget ?? $selfLinkTarget,
320  $item->samePage ?? $samePage ?? false,
321  $item->wikiId ?? $wikiId ?? false,
322  $enableSectionLinks ?? true
323  );
324  if ( $useBlock ?? false ) {
325  $preprocessed = $this->wrapCommentWithBlock(
326  $preprocessed,
327  $useParentheses ?? true
328  );
329  }
330  $outputs[$index] = $preprocessed;
331  }
332  return $parser->finalize( $outputs );
333  }
334 
344  protected function wrapCommentWithBlock(
345  $formatted, $useParentheses
346  ) {
347  // '*' used to be the comment inserted by the software way back
348  // in antiquity in case none was provided, here for backwards
349  // compatibility, acc. to brion -√¶var
350  if ( $formatted == '' || $formatted == '*' ) {
351  return '';
352  }
353  if ( $useParentheses ) {
354  $formatted = wfMessage( 'parentheses' )->rawParams( $formatted )->escaped();
355  $classNames = 'comment';
356  } else {
357  $classNames = 'comment comment--without-parentheses';
358  }
359  return " <span class=\"$classNames\">$formatted</span>";
360  }
361 
373  private function preprocessRevComment(
374  CommentParser $parser,
375  Authority $authority,
376  RevisionRecord $revRecord,
377  $samePage = false,
378  $isPublic = false,
379  $useParentheses = true
380  ) {
381  if ( $revRecord->getComment( RevisionRecord::RAW ) === null ) {
382  return "";
383  }
384  if ( $revRecord->audienceCan(
387  $authority )
388  ) {
389  $comment = $revRecord->getComment( RevisionRecord::FOR_THIS_USER, $authority );
390  $block = $parser->preprocess(
391  $comment ? $comment->text : '',
392  $revRecord->getPageAsLinkTarget(),
393  $samePage,
394  null,
395  true
396  );
397  $block = $this->wrapCommentWithBlock( $block, $useParentheses );
398  } else {
399  $block = " <span class=\"comment\">" . wfMessage( 'rev-deleted-comment' )->escaped() . "</span>";
400  }
401  if ( $revRecord->isDeleted( RevisionRecord::DELETED_COMMENT ) ) {
402  $class = \Linker::getRevisionDeletedClass( $revRecord );
403  return " <span class=\"$class comment\">$block</span>";
404  }
405  return $block;
406  }
407 
408 }
MediaWiki\Revision\RevisionRecord\RAW
const RAW
Definition: RevisionRecord.php:64
MediaWiki\CommentFormatter\CommentFormatter\preprocessRevComment
preprocessRevComment(CommentParser $parser, Authority $authority, RevisionRecord $revRecord, $samePage=false, $isPublic=false, $useParentheses=true)
Preprocess and wrap a revision comment.
Definition: CommentFormatter.php:373
MediaWiki\CommentFormatter\CommentFormatter\createBatch
createBatch()
Format comments using a fluent interface.
Definition: CommentFormatter.php:34
MediaWiki\Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:47
MediaWiki\CommentFormatter\CommentFormatter\formatLinksUnsafe
formatLinksUnsafe(string $comment, LinkTarget $selfLinkTarget=null, $samePage=false, $wikiId=false)
Format a comment, passing through HTML in the input to the output.
Definition: CommentFormatter.php:96
MediaWiki\CommentFormatter\CommentParser
The text processing backend for CommentFormatter.
Definition: CommentParser.php:32
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1183
MediaWiki\CommentFormatter\CommentFormatter
This is the main service interface for converting single-line comments from various DB comment fields...
Definition: CommentFormatter.php:16
MediaWiki\CommentFormatter\CommentFormatter\__construct
__construct(CommentParserFactory $parserFactory)
Definition: CommentFormatter.php:25
MediaWiki\Revision\RevisionRecord\DELETED_COMMENT
const DELETED_COMMENT
Definition: RevisionRecord.php:54
Linker\getRevisionDeletedClass
static getRevisionDeletedClass(RevisionRecord $revisionRecord)
Returns css class of a deleted revision.
Definition: Linker.php:1300
MediaWiki\CommentFormatter\CommentFormatter\formatRevision
formatRevision(RevisionRecord $revision, Authority $authority, $samePage=false, $isPublic=false, $useParentheses=true)
Wrap and format the given revision's comment block, if the specified user is allowed to view it.
Definition: CommentFormatter.php:227
MediaWiki\CommentFormatter\CommentFormatter\$parserFactory
CommentParserFactory $parserFactory
Definition: CommentFormatter.php:18
MediaWiki\CommentFormatter\CommentFormatter\formatItems
formatItems( $items)
Format an iterator over CommentItem objects.
Definition: CommentFormatter.php:292
MediaWiki\CommentFormatter\CommentFormatter\format
format(string $comment, LinkTarget $selfLinkTarget=null, $samePage=false, $wikiId=false)
Format a single comment.
Definition: CommentFormatter.php:50
MediaWiki\CommentFormatter\CommentBatch
This class provides a fluent interface for formatting a batch of comments.
Definition: CommentBatch.php:13
MediaWiki\CommentFormatter\RevisionCommentBatch
Fluent interface for revision comment batch inputs.
Definition: RevisionCommentBatch.php:13
MediaWiki\Revision\RevisionRecord\getComment
getComment( $audience=self::FOR_PUBLIC, Authority $performer=null)
Fetch revision comment, if it's available to the specified audience.
Definition: RevisionRecord.php:413
MediaWiki\CommentFormatter\CommentFormatter\formatLinks
formatLinks(string $comment, LinkTarget $selfLinkTarget=null, $samePage=false, $wikiId=false)
Format links in a comment, ignoring section links in C-style comments.
Definition: CommentFormatter.php:117
MediaWiki\Revision\RevisionRecord\audienceCan
audienceCan( $field, $audience, Authority $performer=null)
Check that the given audience has access to the given field.
Definition: RevisionRecord.php:479
MediaWiki\Permissions\Authority
This interface represents the authority associated the current execution context, such as a web reque...
Definition: Authority.php:37
MediaWiki\CommentFormatter\CommentFormatter\formatStringsAsBlock
formatStringsAsBlock( $strings, LinkTarget $selfLinkTarget=null, $samePage=false, $wikiId=false, $useParentheses=true)
Given an array of comments as strings which all have the same self link target, format the comments a...
Definition: CommentFormatter.php:195
MediaWiki\CommentFormatter\CommentFormatter\formatItemsInternal
formatItemsInternal( $items, $selfLinkTarget=null, $samePage=null, $wikiId=null, $enableSectionLinks=null, $useBlock=null, $useParentheses=null)
Definition: CommentFormatter.php:310
MediaWiki\Revision\RevisionRecord\getPageAsLinkTarget
getPageAsLinkTarget()
Returns the title of the page this revision is associated with as a LinkTarget object.
Definition: RevisionRecord.php:355
MediaWiki\CommentFormatter\CommentParserFactory
Definition: CommentParserFactory.php:17
MediaWiki\CommentFormatter\CommentFormatter\formatRevisions
formatRevisions( $revisions, Authority $authority, $samePage=false, $isPublic=false, $useParentheses=true, $indexById=false)
Format multiple revision comments.
Definition: CommentFormatter.php:252
MediaWiki\CommentFormatter\CommentFormatter\wrapCommentWithBlock
wrapCommentWithBlock( $formatted, $useParentheses)
Wrap a comment in standard punctuation and formatting if it's non-empty, otherwise return empty strin...
Definition: CommentFormatter.php:344
MediaWiki\CommentFormatter\CommentFormatter\formatBlock
formatBlock(string $comment, LinkTarget $selfLinkTarget=null, $samePage=false, $wikiId=false, $useParentheses=true)
Wrap a comment in standard punctuation and formatting if it's non-empty, otherwise return an empty st...
Definition: CommentFormatter.php:71
MediaWiki\CommentFormatter\CommentFormatter\formatInternal
formatInternal( $comment, $enableSectionLinks, $useBlock, $useParentheses, $selfLinkTarget=null, $samePage=false, $wikiId=false)
Format a single comment with many ugly boolean parameters.
Definition: CommentFormatter.php:139
MediaWiki\CommentFormatter\CommentFormatter\createRevisionBatch
createRevisionBatch()
Format a batch of revision comments using a fluent interface.
Definition: CommentFormatter.php:279
MediaWiki\Revision\RevisionRecord\isDeleted
isDeleted( $field)
MCR migration note: this replaced Revision::isDeleted.
Definition: RevisionRecord.php:437
MediaWiki\CommentFormatter
Definition: CommentBatch.php:3
MediaWiki\Revision\RevisionRecord\FOR_THIS_USER
const FOR_THIS_USER
Definition: RevisionRecord.php:63
MediaWiki\CommentFormatter\CommentFormatter\formatStrings
formatStrings( $strings, LinkTarget $selfLinkTarget=null, $samePage=false, $wikiId=false)
Format comments which are provided as strings and all have the same self-link target and other option...
Definition: CommentFormatter.php:167
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
MediaWiki\Revision\RevisionRecord\FOR_PUBLIC
const FOR_PUBLIC
Definition: RevisionRecord.php:62
MediaWiki\CommentFormatter\CommentParser\preprocess
preprocess(string $comment, LinkTarget $selfLinkTarget=null, $samePage=false, $wikiId=false, $enableSectionLinks=true)
Convert a comment to HTML, but replace links with markers which are resolved later.
Definition: CommentParser.php:109