Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
28 / 28 |
|
100.00% |
11 / 11 |
CRAP | |
100.00% |
1 / 1 |
CommentBatch | |
100.00% |
28 / 28 |
|
100.00% |
11 / 11 |
11 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
comments | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
strings | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
useBlock | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
useParentheses | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
selfLinkTarget | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
enableSectionLinks | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
disableSectionLinks | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
samePage | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
wikiId | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace MediaWiki\CommentFormatter; |
4 | |
5 | use MediaWiki\Linker\LinkTarget; |
6 | use Traversable; |
7 | |
8 | /** |
9 | * This class provides a fluent interface for formatting a batch of comments. |
10 | * |
11 | * @since 1.38 |
12 | */ |
13 | class CommentBatch { |
14 | /** @var CommentFormatter */ |
15 | private $formatter; |
16 | /** @var iterable<CommentItem>|Traversable */ |
17 | private $comments; |
18 | /** @var bool|null */ |
19 | private $useBlock; |
20 | /** @var bool|null */ |
21 | private $useParentheses; |
22 | /** @var LinkTarget|null */ |
23 | private $selfLinkTarget; |
24 | /** @var bool|null */ |
25 | private $samePage; |
26 | /** @var string|false|null */ |
27 | private $wikiId; |
28 | /** @var bool|null */ |
29 | private $enableSectionLinks; |
30 | |
31 | /** |
32 | * @internal Use CommentFormatter::createBatch() |
33 | * |
34 | * @param CommentFormatter $formatter |
35 | */ |
36 | public function __construct( CommentFormatter $formatter ) { |
37 | $this->formatter = $formatter; |
38 | } |
39 | |
40 | /** |
41 | * Set the comments to be formatted. This can be an array of CommentItem |
42 | * objects, or it can be an iterator which generates CommentItem objects. |
43 | * |
44 | * Theoretically iterable should imply Traversable, but PHPStorm gives an |
45 | * error when RowCommentIterator is passed as iterable<CommentItem>. |
46 | * |
47 | * @param iterable<CommentItem>|Traversable $comments |
48 | * @return $this |
49 | */ |
50 | public function comments( $comments ) { |
51 | $this->comments = $comments; |
52 | return $this; |
53 | } |
54 | |
55 | /** |
56 | * Specify the comments to be formatted as an array of strings. This is a |
57 | * simplified wrapper for comments() which does not allow you to set options |
58 | * on a per-comment basis. |
59 | * |
60 | * $strings must be an array -- use comments() if you want to use an iterator. |
61 | * |
62 | * @param string[] $strings |
63 | * @return $this |
64 | */ |
65 | public function strings( array $strings ) { |
66 | $this->comments = new StringCommentIterator( $strings ); |
67 | return $this; |
68 | } |
69 | |
70 | /** |
71 | * Wrap each comment in standard punctuation and formatting if it's |
72 | * non-empty. Empty comments remain empty. This causes the batch to work |
73 | * like the old Linker::commentBlock(). |
74 | * |
75 | * If this function is not called, the option is false. |
76 | * |
77 | * @param bool $useBlock |
78 | * @return $this |
79 | */ |
80 | public function useBlock( $useBlock = true ) { |
81 | $this->useBlock = $useBlock; |
82 | return $this; |
83 | } |
84 | |
85 | /** |
86 | * Wrap each comment with parentheses. This has no effect if the useBlock |
87 | * option is not enabled. |
88 | * |
89 | * Unlike the legacy Linker::commentBlock(), this option defaults to false |
90 | * if this method is not called, since that better preserves the fluent |
91 | * style. |
92 | * |
93 | * @param bool $useParentheses |
94 | * @return $this |
95 | */ |
96 | public function useParentheses( $useParentheses = true ) { |
97 | $this->useParentheses = $useParentheses; |
98 | return $this; |
99 | } |
100 | |
101 | /** |
102 | * Set the title to be used for self links in the comments. If there is no |
103 | * title specified either here or in the item, fragment links are not |
104 | * expanded. |
105 | * |
106 | * @param LinkTarget $selfLinkTarget |
107 | * @return $this |
108 | */ |
109 | public function selfLinkTarget( LinkTarget $selfLinkTarget ) { |
110 | $this->selfLinkTarget = $selfLinkTarget; |
111 | return $this; |
112 | } |
113 | |
114 | /** |
115 | * Set the option to enable/disable section links formatted as C-style |
116 | * comments, as used in revision comments to indicate the section which |
117 | * was edited. |
118 | * |
119 | * If the method is not called, the option is true. Setting this to false |
120 | * approximately emulates Linker::formatLinksInComment() except that HTML |
121 | * in the input is escaped. |
122 | * |
123 | * @param bool $enable |
124 | * @return $this |
125 | */ |
126 | public function enableSectionLinks( $enable ) { |
127 | $this->enableSectionLinks = $enable; |
128 | return $this; |
129 | } |
130 | |
131 | /** |
132 | * Disable section links formatted as C-style comments, as used in revision |
133 | * comments to indicate the section which was edited. Calling this |
134 | * approximately emulates Linker::formatLinksInComment() except that HTML |
135 | * in the input is escaped. |
136 | * |
137 | * @return $this |
138 | */ |
139 | public function disableSectionLinks() { |
140 | $this->enableSectionLinks = false; |
141 | return $this; |
142 | } |
143 | |
144 | /** |
145 | * Set the same-page option. If this is true, section links and fragment- |
146 | * only wikilinks are rendered with an href that is a fragment-only URL. |
147 | * If it is false (the default), such links go to the self link title. |
148 | * |
149 | * This can also be set per-item using CommentItem::samePage(). |
150 | * |
151 | * This is equivalent to $local in the old Linker methods. |
152 | * |
153 | * @param bool $samePage |
154 | * @return $this |
155 | */ |
156 | public function samePage( $samePage = true ) { |
157 | $this->samePage = $samePage; |
158 | return $this; |
159 | } |
160 | |
161 | /** |
162 | * ID of the wiki to link to (if not the local wiki), as used by WikiMap. |
163 | * This is used to render comments which are loaded from a foreign wiki. |
164 | * This only affects links which are syntactically internal -- it has no |
165 | * effect on interwiki links. |
166 | * |
167 | * This can also be set per-item using CommentItem::wikiId(). |
168 | * |
169 | * @param string|false|null $wikiId |
170 | * @return $this |
171 | */ |
172 | public function wikiId( $wikiId ) { |
173 | $this->wikiId = $wikiId; |
174 | return $this; |
175 | } |
176 | |
177 | /** |
178 | * Format the comments and produce an array of HTML fragments. |
179 | * |
180 | * @return string[] |
181 | */ |
182 | public function execute() { |
183 | return $this->formatter->formatItemsInternal( |
184 | $this->comments, |
185 | $this->selfLinkTarget, |
186 | $this->samePage, |
187 | $this->wikiId, |
188 | $this->enableSectionLinks, |
189 | $this->useBlock, |
190 | $this->useParentheses |
191 | ); |
192 | } |
193 | |
194 | } |