MediaWiki master
CommentFormatter.php
Go to the documentation of this file.
1<?php
2
4
9use Traversable;
10
19 protected $parserFactory;
20
27 $this->parserFactory = $parserFactory;
28 }
29
35 public function createBatch() {
36 return new CommentBatch( $this );
37 }
38
51 public function format( string $comment, ?LinkTarget $selfLinkTarget = null,
52 $samePage = false, $wikiId = false
53 ) {
54 return $this->formatInternal( $comment, true, false, false,
55 $selfLinkTarget, $samePage, $wikiId );
56 }
57
72 public function formatBlock( string $comment, ?LinkTarget $selfLinkTarget = null,
73 $samePage = false, $wikiId = false, $useParentheses = true
74 ) {
75 return $this->formatInternal( $comment, true, true, $useParentheses,
76 $selfLinkTarget, $samePage, $wikiId );
77 }
78
97 public function formatLinksUnsafe( string $comment, ?LinkTarget $selfLinkTarget = null,
98 $samePage = false, $wikiId = false
99 ) {
100 $parser = $this->parserFactory->create();
101 $preprocessed = $parser->preprocessUnsafe( $comment, $selfLinkTarget,
102 $samePage, $wikiId, false );
103 return $parser->finalize( $preprocessed );
104 }
105
118 public function formatLinks( string $comment, ?LinkTarget $selfLinkTarget = null,
119 $samePage = false, $wikiId = false
120 ) {
121 return $this->formatInternal( $comment, false, false, false,
122 $selfLinkTarget, $samePage, $wikiId );
123 }
124
140 private function formatInternal( $comment, $enableSectionLinks, $useBlock, $useParentheses,
141 $selfLinkTarget = null, $samePage = false, $wikiId = false
142 ) {
143 $parser = $this->parserFactory->create();
144 $preprocessed = $parser->preprocess( $comment, $selfLinkTarget, $samePage, $wikiId,
145 $enableSectionLinks );
146 $output = $parser->finalize( $preprocessed );
147 if ( $useBlock ) {
148 $output = $this->wrapCommentWithBlock( $output, $useParentheses );
149 }
150 return $output;
151 }
152
168 public function formatStrings( $strings, ?LinkTarget $selfLinkTarget = null,
169 $samePage = false, $wikiId = false
170 ) {
171 $parser = $this->parserFactory->create();
172 $outputs = [];
173 foreach ( $strings as $i => $comment ) {
174 $outputs[$i] = $parser->preprocess( $comment, $selfLinkTarget, $samePage, $wikiId );
175 }
176 return $parser->finalize( $outputs );
177 }
178
198 public function formatRevision(
199 RevisionRecord $revision,
200 Authority $authority,
201 $samePage = false,
202 $isPublic = false,
203 $useParentheses = true
204 ) {
205 $parser = $this->parserFactory->create();
206 return $parser->finalize( $this->preprocessRevComment(
207 $parser, $authority, $revision, $samePage, $isPublic, $useParentheses ) );
208 }
209
223 public function formatRevisions(
224 $revisions,
225 Authority $authority,
226 $samePage = false,
227 $isPublic = false,
228 $useParentheses = true,
229 $indexById = false
230 ) {
231 $parser = $this->parserFactory->create();
232 $outputs = [];
233 foreach ( $revisions as $i => $rev ) {
234 if ( $indexById ) {
235 $key = $rev->getId();
236 } else {
237 $key = $i;
238 }
239 // @phan-suppress-next-line PhanTypeMismatchDimAssignment getId does not return null here
240 $outputs[$key] = $this->preprocessRevComment(
241 $parser, $authority, $rev, $samePage, $isPublic, $useParentheses );
242 }
243 return $parser->finalize( $outputs );
244 }
245
251 public function createRevisionBatch() {
252 return new RevisionCommentBatch( $this );
253 }
254
264 public function formatItems( $items ) {
265 return $this->formatItemsInternal( $items );
266 }
267
282 public function formatItemsInternal( $items, $selfLinkTarget = null,
283 $samePage = null, $wikiId = null, $enableSectionLinks = null,
284 $useBlock = null, $useParentheses = null
285 ) {
286 $outputs = [];
287 $parser = $this->parserFactory->create();
288 foreach ( $items as $index => $item ) {
289 $preprocessed = $parser->preprocess(
290 $item->comment,
291 $item->selfLinkTarget ?? $selfLinkTarget,
292 $item->samePage ?? $samePage ?? false,
293 $item->wikiId ?? $wikiId ?? false,
294 $enableSectionLinks ?? true
295 );
296 if ( $useBlock ?? false ) {
297 $preprocessed = $this->wrapCommentWithBlock(
298 $preprocessed,
299 $useParentheses ?? true
300 );
301 }
302 $outputs[$index] = $preprocessed;
303 }
304 return $parser->finalize( $outputs );
305 }
306
316 protected function wrapCommentWithBlock(
317 $formatted, $useParentheses
318 ) {
319 // '*' used to be the comment inserted by the software way back
320 // in antiquity in case none was provided, here for backwards
321 // compatibility, acc. to [brooke] -ævar
322 if ( $formatted == '' || $formatted == '*' ) {
323 return '';
324 }
325 if ( $useParentheses ) {
326 $formatted = wfMessage( 'parentheses' )->rawParams( $formatted )->escaped();
327 $classNames = 'comment';
328 } else {
329 $classNames = 'comment comment--without-parentheses';
330 }
331 return " <span class=\"$classNames\">$formatted</span>";
332 }
333
345 private function preprocessRevComment(
346 CommentParser $parser,
347 Authority $authority,
348 RevisionRecord $revRecord,
349 $samePage = false,
350 $isPublic = false,
351 $useParentheses = true
352 ) {
353 if ( $revRecord->getComment( RevisionRecord::RAW ) === null ) {
354 return "";
355 }
356 if ( $revRecord->audienceCan(
357 RevisionRecord::DELETED_COMMENT,
358 $isPublic ? RevisionRecord::FOR_PUBLIC : RevisionRecord::FOR_THIS_USER,
359 $authority )
360 ) {
361 $comment = $revRecord->getComment( RevisionRecord::FOR_THIS_USER, $authority );
362 $block = $parser->preprocess(
363 $comment ? $comment->text : '',
364 $revRecord->getPageAsLinkTarget(),
365 $samePage,
366 null,
367 true
368 );
369 $block = $this->wrapCommentWithBlock( $block, $useParentheses );
370 } else {
371 $block = " <span class=\"comment\">" . wfMessage( 'rev-deleted-comment' )->escaped() . "</span>";
372 }
373 if ( $revRecord->isDeleted( RevisionRecord::DELETED_COMMENT ) ) {
374 $class = Linker::getRevisionDeletedClass( $revRecord );
375 return " <span class=\"$class comment\">$block</span>";
376 }
377 return $block;
378 }
379
380}
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...
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...
createBatch()
Format comments using a fluent interface.
formatRevisions( $revisions, Authority $authority, $samePage=false, $isPublic=false, $useParentheses=true, $indexById=false)
Format multiple revision comments.
format(string $comment, ?LinkTarget $selfLinkTarget=null, $samePage=false, $wikiId=false)
Format a single comment.
formatLinksUnsafe(string $comment, ?LinkTarget $selfLinkTarget=null, $samePage=false, $wikiId=false)
Format a comment, passing through HTML in the input to the output.
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...
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...
__construct(CommentParserFactory $parserFactory)
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.
formatLinks(string $comment, ?LinkTarget $selfLinkTarget=null, $samePage=false, $wikiId=false)
Format links in a comment, ignoring section links in C-style comments.
formatItemsInternal( $items, $selfLinkTarget=null, $samePage=null, $wikiId=null, $enableSectionLinks=null, $useBlock=null, $useParentheses=null)
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.
Some internal bits split of from Skin.php.
Definition Linker.php:63
Page revision base class.
getComment( $audience=self::FOR_PUBLIC, ?Authority $performer=null)
Fetch revision comment, if it's available to the specified audience.
audienceCan( $field, $audience, ?Authority $performer=null)
Check that the given audience has access to the given field.
isDeleted( $field)
MCR migration note: this replaced Revision::isDeleted.
Represents the target of a wiki link.
This interface represents the authority associated with the current execution context,...
Definition Authority.php:37