| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\CommentFormatter; |
| 4 | |
| 5 | use MediaWiki\Permissions\Authority; |
| 6 | use MediaWiki\Revision\RevisionRecord; |
| 7 | |
| 8 | /** |
| 9 | * Fluent interface for revision comment batch inputs. |
| 10 | * |
| 11 | * @since 1.38 |
| 12 | */ |
| 13 | class RevisionCommentBatch { |
| 14 | /** @var CommentFormatter */ |
| 15 | private $formatter; |
| 16 | /** @var Authority|null */ |
| 17 | private $authority; |
| 18 | /** @var iterable<RevisionRecord> */ |
| 19 | private $revisions; |
| 20 | /** @var bool */ |
| 21 | private $samePage = false; |
| 22 | /** @var bool */ |
| 23 | private $isPublic = false; |
| 24 | /** @var bool */ |
| 25 | private $useParentheses = false; |
| 26 | /** @var bool */ |
| 27 | private $indexById = false; |
| 28 | |
| 29 | /** |
| 30 | * @param CommentFormatter $formatter |
| 31 | */ |
| 32 | public function __construct( CommentFormatter $formatter ) { |
| 33 | $this->formatter = $formatter; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Set the authority to use for permission checks. This must be called |
| 38 | * prior to execute(). |
| 39 | * |
| 40 | * @param Authority $authority |
| 41 | * @return $this |
| 42 | */ |
| 43 | public function authority( Authority $authority ) { |
| 44 | $this->authority = $authority; |
| 45 | return $this; |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Set the revisions to extract comments from. |
| 50 | * |
| 51 | * @param iterable<RevisionRecord> $revisions |
| 52 | * @return $this |
| 53 | */ |
| 54 | public function revisions( $revisions ) { |
| 55 | $this->revisions = $revisions; |
| 56 | return $this; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Set the same-page option. If this is true, section links and fragment- |
| 61 | * only wikilinks are rendered with an href that is a fragment-only URL. |
| 62 | * If it is false (the default), such links go to the self link title. |
| 63 | * |
| 64 | * This is equivalent to $local in the old Linker methods. |
| 65 | * |
| 66 | * @param bool $samePage |
| 67 | * @return $this |
| 68 | */ |
| 69 | public function samePage( $samePage = true ) { |
| 70 | $this->samePage = $samePage; |
| 71 | return $this; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Wrap the comment with parentheses. This has no effect if the useBlock |
| 76 | * option is not enabled. |
| 77 | * |
| 78 | * Unlike the legacy Linker::commentBlock(), this option defaults to false |
| 79 | * if this method is not called, since that better preserves the fluent |
| 80 | * style. |
| 81 | * |
| 82 | * @param bool $useParentheses |
| 83 | * @return $this |
| 84 | */ |
| 85 | public function useParentheses( $useParentheses = true ) { |
| 86 | $this->useParentheses = $useParentheses; |
| 87 | return $this; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * If this is true, show the comment only if all users can see it. |
| 92 | * |
| 93 | * We'll call it hideIfDeleted() since public is a keyword and isPublic() |
| 94 | * has an inappropriate verb. |
| 95 | * |
| 96 | * @param bool $isPublic |
| 97 | * @return $this |
| 98 | */ |
| 99 | public function hideIfDeleted( $isPublic = true ) { |
| 100 | $this->isPublic = $isPublic; |
| 101 | return $this; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * If this is true, the array keys in the return value will be the revision |
| 106 | * IDs instead of the keys from the input array. |
| 107 | * |
| 108 | * @param bool $indexById |
| 109 | * @return $this |
| 110 | */ |
| 111 | public function indexById( $indexById = true ) { |
| 112 | $this->indexById = $indexById; |
| 113 | return $this; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Format the comments. |
| 118 | * |
| 119 | * @return string[] Formatted comments. The array key is either the field |
| 120 | * value specified by indexField(), or if that was not called, it is the |
| 121 | * key from the array passed to revisions(). |
| 122 | */ |
| 123 | public function execute() { |
| 124 | return $this->formatter->formatRevisions( |
| 125 | $this->revisions, |
| 126 | $this->authority, |
| 127 | $this->samePage, |
| 128 | $this->isPublic, |
| 129 | $this->useParentheses, |
| 130 | $this->indexById |
| 131 | ); |
| 132 | } |
| 133 | } |