MediaWiki REL1_39
CommentFormatter.php
Go to the documentation of this file.
1<?php
2
4
8use 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 // @phan-suppress-next-line PhanTypeMismatchDimAssignment getId does not return null here
269 $outputs[$key] = $this->preprocessRevComment(
270 $parser, $authority, $rev, $samePage, $isPublic, $useParentheses );
271 }
272 return $parser->finalize( $outputs );
273 }
274
280 public function createRevisionBatch() {
281 return new RevisionCommentBatch( $this );
282 }
283
293 public function formatItems( $items ) {
294 return $this->formatItemsInternal( $items );
295 }
296
311 public function formatItemsInternal( $items, $selfLinkTarget = null,
312 $samePage = null, $wikiId = null, $enableSectionLinks = null,
313 $useBlock = null, $useParentheses = null
314 ) {
315 $outputs = [];
316 $parser = $this->parserFactory->create();
317 foreach ( $items as $index => $item ) {
318 $preprocessed = $parser->preprocess(
319 $item->comment,
320 $item->selfLinkTarget ?? $selfLinkTarget,
321 $item->samePage ?? $samePage ?? false,
322 $item->wikiId ?? $wikiId ?? false,
323 $enableSectionLinks ?? true
324 );
325 if ( $useBlock ?? false ) {
326 $preprocessed = $this->wrapCommentWithBlock(
327 $preprocessed,
328 $useParentheses ?? true
329 );
330 }
331 $outputs[$index] = $preprocessed;
332 }
333 return $parser->finalize( $outputs );
334 }
335
345 protected function wrapCommentWithBlock(
346 $formatted, $useParentheses
347 ) {
348 // '*' used to be the comment inserted by the software way back
349 // in antiquity in case none was provided, here for backwards
350 // compatibility, acc. to brion -ævar
351 if ( $formatted == '' || $formatted == '*' ) {
352 return '';
353 }
354 if ( $useParentheses ) {
355 $formatted = wfMessage( 'parentheses' )->rawParams( $formatted )->escaped();
356 $classNames = 'comment';
357 } else {
358 $classNames = 'comment comment--without-parentheses';
359 }
360 return " <span class=\"$classNames\">$formatted</span>";
361 }
362
374 private function preprocessRevComment(
375 CommentParser $parser,
376 Authority $authority,
377 RevisionRecord $revRecord,
378 $samePage = false,
379 $isPublic = false,
380 $useParentheses = true
381 ) {
382 if ( $revRecord->getComment( RevisionRecord::RAW ) === null ) {
383 return "";
384 }
385 if ( $revRecord->audienceCan(
386 RevisionRecord::DELETED_COMMENT,
387 $isPublic ? RevisionRecord::FOR_PUBLIC : RevisionRecord::FOR_THIS_USER,
388 $authority )
389 ) {
390 $comment = $revRecord->getComment( RevisionRecord::FOR_THIS_USER, $authority );
391 $block = $parser->preprocess(
392 $comment ? $comment->text : '',
393 $revRecord->getPageAsLinkTarget(),
394 $samePage,
395 null,
396 true
397 );
398 $block = $this->wrapCommentWithBlock( $block, $useParentheses );
399 } else {
400 $block = " <span class=\"comment\">" . wfMessage( 'rev-deleted-comment' )->escaped() . "</span>";
401 }
402 if ( $revRecord->isDeleted( RevisionRecord::DELETED_COMMENT ) ) {
403 $class = \Linker::getRevisionDeletedClass( $revRecord );
404 return " <span class=\"$class comment\">$block</span>";
405 }
406 return $block;
407 }
408
409}
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
This class provides a fluent interface for formatting a batch of comments.
This is the main service interface for converting single-line comments from various DB comment fields...
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...
createRevisionBatch()
Format a batch of revision comments using a fluent interface.
wrapCommentWithBlock( $formatted, $useParentheses)
Wrap a comment in standard punctuation and formatting if it's non-empty, otherwise return empty strin...
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...
createBatch()
Format comments using a fluent interface.
formatRevisions( $revisions, Authority $authority, $samePage=false, $isPublic=false, $useParentheses=true, $indexById=false)
Format multiple revision comments.
__construct(CommentParserFactory $parserFactory)
format(string $comment, LinkTarget $selfLinkTarget=null, $samePage=false, $wikiId=false)
Format a single comment.
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...
formatLinksUnsafe(string $comment, LinkTarget $selfLinkTarget=null, $samePage=false, $wikiId=false)
Format a comment, passing through HTML in the input to the output.
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.
formatItemsInternal( $items, $selfLinkTarget=null, $samePage=null, $wikiId=null, $enableSectionLinks=null, $useBlock=null, $useParentheses=null)
formatLinks(string $comment, LinkTarget $selfLinkTarget=null, $samePage=false, $wikiId=false)
Format links in a comment, ignoring section links in C-style comments.
formatItems( $items)
Format an iterator over CommentItem objects.
The text processing backend for CommentFormatter.
finalize( $comments)
Execute pending batch queries and replace markers in the specified string(s) with actual links.
preprocessUnsafe( $comment, LinkTarget $selfLinkTarget=null, $samePage=false, $wikiId=false, $enableSectionLinks=true)
Convert a comment in pseudo-HTML format to HTML, replacing links with markers.
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.
Fluent interface for revision comment batch inputs.
Page revision base class.
audienceCan( $field, $audience, Authority $performer=null)
Check that the given audience has access to the given field.
getComment( $audience=self::FOR_PUBLIC, Authority $performer=null)
Fetch revision comment, if it's available to the specified audience.
isDeleted( $field)
MCR migration note: this replaced Revision::isDeleted.
This interface represents the authority associated the current execution context, such as a web reque...
Definition Authority.php:37
return true
Definition router.php:92