| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\CommentFormatter; |
| 4 |
|
| 5 |
use MediaWiki\Linker\Linker; |
| 6 |
use MediaWiki\Linker\LinkTarget; |
| 7 |
use MediaWiki\Permissions\Authority; |
| 8 |
use MediaWiki\Revision\RevisionRecord; |
| 9 |
use Traversable; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class CommentFormatter { |
| 18 |
|
| 19 |
protected $parserFactory; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public function __construct( CommentParserFactory $parserFactory ) { |
| 27 |
$this->parserFactory = $parserFactory; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public function createBatch() { |
| 36 |
return new CommentBatch( $this ); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 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 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 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 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 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 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 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 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 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 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 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 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 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 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 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 |
|
| 240 |
$outputs[$key] = $this->preprocessRevComment( |
| 241 |
$parser, $authority, $rev, $samePage, $isPublic, $useParentheses ); |
| 242 |
} |
| 243 |
return $parser->finalize( $outputs ); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
public function createRevisionBatch() { |
| 252 |
return new RevisionCommentBatch( $this ); |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
public function formatItems( $items ) { |
| 265 |
return $this->formatItemsInternal( $items ); |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 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 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
protected function wrapCommentWithBlock( |
| 317 |
$formatted, $useParentheses |
| 318 |
) { |
| 319 |
|
| 320 |
|
| 321 |
r |
| 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 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 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 |
} |