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
196 public function formatStringsAsBlock( $strings, LinkTarget $selfLinkTarget = null,
197 $samePage = false, $wikiId = false, $useParentheses = true
198 ) {
199 $parser = $this->parserFactory->create();
200 $outputs = [];
201 foreach ( $strings as $i => $comment ) {
202 $outputs[$i] = $this->wrapCommentWithBlock(
203 $parser->preprocess( $comment, $selfLinkTarget, $samePage, $wikiId ),
204 $useParentheses );
205 }
206 return $parser->finalize( $outputs );
207 }
208
228 public function formatRevision(
229 RevisionRecord $revision,
230 Authority $authority,
231 $samePage = false,
232 $isPublic = false,
233 $useParentheses = true
234 ) {
235 $parser = $this->parserFactory->create();
236 return $parser->finalize( $this->preprocessRevComment(
237 $parser, $authority, $revision, $samePage, $isPublic, $useParentheses ) );
238 }
239
253 public function formatRevisions(
254 $revisions,
255 Authority $authority,
256 $samePage = false,
257 $isPublic = false,
258 $useParentheses = true,
259 $indexById = false
260 ) {
261 $parser = $this->parserFactory->create();
262 $outputs = [];
263 foreach ( $revisions as $i => $rev ) {
264 if ( $indexById ) {
265 $key = $rev->getId();
266 } else {
267 $key = $i;
268 }
269 // @phan-suppress-next-line PhanTypeMismatchDimAssignment getId does not return null here
270 $outputs[$key] = $this->preprocessRevComment(
271 $parser, $authority, $rev, $samePage, $isPublic, $useParentheses );
272 }
273 return $parser->finalize( $outputs );
274 }
275
281 public function createRevisionBatch() {
282 return new RevisionCommentBatch( $this );
283 }
284
294 public function formatItems( $items ) {
295 return $this->formatItemsInternal( $items );
296 }
297
312 public function formatItemsInternal( $items, $selfLinkTarget = null,
313 $samePage = null, $wikiId = null, $enableSectionLinks = null,
314 $useBlock = null, $useParentheses = null
315 ) {
316 $outputs = [];
317 $parser = $this->parserFactory->create();
318 foreach ( $items as $index => $item ) {
319 $preprocessed = $parser->preprocess(
320 $item->comment,
321 $item->selfLinkTarget ?? $selfLinkTarget,
322 $item->samePage ?? $samePage ?? false,
323 $item->wikiId ?? $wikiId ?? false,
324 $enableSectionLinks ?? true
325 );
326 if ( $useBlock ?? false ) {
327 $preprocessed = $this->wrapCommentWithBlock(
328 $preprocessed,
329 $useParentheses ?? true
330 );
331 }
332 $outputs[$index] = $preprocessed;
333 }
334 return $parser->finalize( $outputs );
335 }
336
346 protected function wrapCommentWithBlock(
347 $formatted, $useParentheses
348 ) {
349 // '*' used to be the comment inserted by the software way back
350 // in antiquity in case none was provided, here for backwards
351 // compatibility, acc. to [brooke] -ævar
352 if ( $formatted == '' || $formatted == '*' ) {
353 return '';
354 }
355 if ( $useParentheses ) {
356 $formatted = wfMessage( 'parentheses' )->rawParams( $formatted )->escaped();
357 $classNames = 'comment';
358 } else {
359 $classNames = 'comment comment--without-parentheses';
360 }
361 return " <span class=\"$classNames\">$formatted</span>";
362 }
363
375 private function preprocessRevComment(
376 CommentParser $parser,
377 Authority $authority,
378 RevisionRecord $revRecord,
379 $samePage = false,
380 $isPublic = false,
381 $useParentheses = true
382 ) {
383 if ( $revRecord->getComment( RevisionRecord::RAW ) === null ) {
384 return "";
385 }
386 if ( $revRecord->audienceCan(
387 RevisionRecord::DELETED_COMMENT,
388 $isPublic ? RevisionRecord::FOR_PUBLIC : RevisionRecord::FOR_THIS_USER,
389 $authority )
390 ) {
391 $comment = $revRecord->getComment( RevisionRecord::FOR_THIS_USER, $authority );
392 $block = $parser->preprocess(
393 $comment ? $comment->text : '',
394 $revRecord->getPageAsLinkTarget(),
395 $samePage,
396 null,
397 true
398 );
399 $block = $this->wrapCommentWithBlock( $block, $useParentheses );
400 } else {
401 $block = " <span class=\"comment\">" . wfMessage( 'rev-deleted-comment' )->escaped() . "</span>";
402 }
403 if ( $revRecord->isDeleted( RevisionRecord::DELETED_COMMENT ) ) {
404 $class = Linker::getRevisionDeletedClass( $revRecord );
405 return " <span class=\"$class comment\">$block</span>";
406 }
407 return $block;
408 }
409
410}
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.
Some internal bits split of from Skin.php.
Definition Linker.php:65
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.
Represents the target of a wiki link.
This interface represents the authority associated with the current execution context,...
Definition Authority.php:37