Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
90.70% |
39 / 43 |
|
85.71% |
6 / 7 |
CRAP | |
0.00% |
0 / 1 |
EditConstraintFactory | |
90.70% |
39 / 43 |
|
85.71% |
6 / 7 |
7.04 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
newEditFilterMergedContentHookConstraint | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
newPageSizeConstraint | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
newReadOnlyConstraint | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
newLinkPurgeRateLimitConstraint | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
newSimpleAntiSpamConstraint | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
newSpamRegexConstraint | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | namespace MediaWiki\EditPage\Constraint; |
22 | |
23 | use MediaWiki\Config\ServiceOptions; |
24 | use MediaWiki\Content\Content; |
25 | use MediaWiki\Context\IContextSource; |
26 | use MediaWiki\EditPage\SpamChecker; |
27 | use MediaWiki\HookContainer\HookContainer; |
28 | use MediaWiki\Language\Language; |
29 | use MediaWiki\Logger\Spi; |
30 | use MediaWiki\MainConfigNames; |
31 | use MediaWiki\Permissions\RateLimiter; |
32 | use MediaWiki\Permissions\RateLimitSubject; |
33 | use MediaWiki\Title\Title; |
34 | use MediaWiki\User\User; |
35 | use MediaWiki\User\UserIdentity; |
36 | use Wikimedia\Rdbms\ReadOnlyMode; |
37 | |
38 | /** |
39 | * Constraints reflect possible errors that need to be checked |
40 | * |
41 | * @since 1.36 |
42 | * @internal |
43 | * @author DannyS712 |
44 | */ |
45 | class EditConstraintFactory { |
46 | |
47 | /** @internal */ |
48 | public const CONSTRUCTOR_OPTIONS = [ |
49 | // PageSizeConstraint |
50 | MainConfigNames::MaxArticleSize, |
51 | ]; |
52 | |
53 | private ServiceOptions $options; |
54 | private Spi $loggerFactory; |
55 | private HookContainer $hookContainer; |
56 | private ReadOnlyMode $readOnlyMode; |
57 | private SpamChecker $spamRegexChecker; |
58 | private RateLimiter $rateLimiter; |
59 | |
60 | /** |
61 | * Some constraints have dependencies that need to be injected, |
62 | * this class serves as a factory for all of the different constraints |
63 | * that need dependencies injected. |
64 | * |
65 | * The checks in EditPage use wfDebugLog and logged to different channels, hence the need |
66 | * for multiple loggers retrieved from the Spi. The channels used are: |
67 | * - SimpleAntiSpam (in SimpleAntiSpamConstraint) |
68 | * - SpamRegex (in SpamRegexConstraint) |
69 | * |
70 | * TODO can they be combined into the same channel? |
71 | * |
72 | * @param ServiceOptions $options |
73 | * @param Spi $loggerFactory |
74 | * @param HookContainer $hookContainer |
75 | * @param ReadOnlyMode $readOnlyMode |
76 | * @param SpamChecker $spamRegexChecker |
77 | * @param RateLimiter $rateLimiter |
78 | */ |
79 | public function __construct( |
80 | ServiceOptions $options, |
81 | Spi $loggerFactory, |
82 | HookContainer $hookContainer, |
83 | ReadOnlyMode $readOnlyMode, |
84 | SpamChecker $spamRegexChecker, |
85 | RateLimiter $rateLimiter |
86 | ) { |
87 | $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
88 | |
89 | // Multiple |
90 | $this->options = $options; |
91 | $this->loggerFactory = $loggerFactory; |
92 | |
93 | // EditFilterMergedContentHookConstraint |
94 | $this->hookContainer = $hookContainer; |
95 | |
96 | // ReadOnlyConstraint |
97 | $this->readOnlyMode = $readOnlyMode; |
98 | |
99 | // SpamRegexConstraint |
100 | $this->spamRegexChecker = $spamRegexChecker; |
101 | |
102 | // LinkPurgeRateLimitConstraint |
103 | $this->rateLimiter = $rateLimiter; |
104 | } |
105 | |
106 | /** |
107 | * @param Content $content |
108 | * @param IContextSource $context |
109 | * @param string $summary |
110 | * @param bool $minorEdit |
111 | * @param Language $language |
112 | * @param User $user |
113 | * @return EditFilterMergedContentHookConstraint |
114 | */ |
115 | public function newEditFilterMergedContentHookConstraint( |
116 | Content $content, |
117 | IContextSource $context, |
118 | string $summary, |
119 | bool $minorEdit, |
120 | Language $language, |
121 | User $user |
122 | ): EditFilterMergedContentHookConstraint { |
123 | return new EditFilterMergedContentHookConstraint( |
124 | $this->hookContainer, |
125 | $content, |
126 | $context, |
127 | $summary, |
128 | $minorEdit, |
129 | $language, |
130 | $user |
131 | ); |
132 | } |
133 | |
134 | /** |
135 | * @param int $contentSize |
136 | * @param string $type |
137 | * @return PageSizeConstraint |
138 | */ |
139 | public function newPageSizeConstraint( |
140 | int $contentSize, |
141 | string $type |
142 | ): PageSizeConstraint { |
143 | return new PageSizeConstraint( |
144 | $this->options->get( MainConfigNames::MaxArticleSize ), |
145 | $contentSize, |
146 | $type |
147 | ); |
148 | } |
149 | |
150 | public function newReadOnlyConstraint(): ReadOnlyConstraint { |
151 | return new ReadOnlyConstraint( |
152 | $this->readOnlyMode |
153 | ); |
154 | } |
155 | |
156 | /** |
157 | * @param RateLimitSubject $subject |
158 | * |
159 | * @return LinkPurgeRateLimitConstraint |
160 | */ |
161 | public function newLinkPurgeRateLimitConstraint( |
162 | RateLimitSubject $subject |
163 | ): LinkPurgeRateLimitConstraint { |
164 | return new LinkPurgeRateLimitConstraint( |
165 | $this->rateLimiter, |
166 | $subject |
167 | ); |
168 | } |
169 | |
170 | /** |
171 | * @param string $input |
172 | * @param UserIdentity $user |
173 | * @param Title $title |
174 | * @return SimpleAntiSpamConstraint |
175 | */ |
176 | public function newSimpleAntiSpamConstraint( |
177 | string $input, |
178 | UserIdentity $user, |
179 | Title $title |
180 | ): SimpleAntiSpamConstraint { |
181 | return new SimpleAntiSpamConstraint( |
182 | $this->loggerFactory->getLogger( 'SimpleAntiSpam' ), |
183 | $input, |
184 | $user, |
185 | $title |
186 | ); |
187 | } |
188 | |
189 | /** |
190 | * @param string $summary |
191 | * @param ?string $sectionHeading |
192 | * @param string $text |
193 | * @param string $reqIP |
194 | * @param Title $title |
195 | * @return SpamRegexConstraint |
196 | */ |
197 | public function newSpamRegexConstraint( |
198 | string $summary, |
199 | ?string $sectionHeading, |
200 | string $text, |
201 | string $reqIP, |
202 | Title $title |
203 | ): SpamRegexConstraint { |
204 | return new SpamRegexConstraint( |
205 | $this->loggerFactory->getLogger( 'SpamRegex' ), |
206 | $this->spamRegexChecker, |
207 | $summary, |
208 | $sectionHeading, |
209 | $text, |
210 | $reqIP, |
211 | $title |
212 | ); |
213 | } |
214 | |
215 | } |