Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
67.55% |
127 / 188 |
|
25.00% |
2 / 8 |
CRAP | |
0.00% |
0 / 1 |
SpecialLog | |
67.91% |
127 / 187 |
|
25.00% |
2 / 8 |
104.08 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
72.31% |
47 / 65 |
|
0.00% |
0 / 1 |
26.67 | |||
getLogTypesOnUser | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
2 | |||
getSubpagesForPrefixSearch | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
parseParams | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
6.05 | |||
show | |
92.31% |
48 / 52 |
|
0.00% |
0 / 1 |
8.03 | |||
getActionButtons | |
13.51% |
5 / 37 |
|
0.00% |
0 / 1 |
21.17 | |||
getGroupName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
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\Specials; |
22 | |
23 | use ChangeTags; |
24 | use LogEventsList; |
25 | use LogFormatterFactory; |
26 | use LogPage; |
27 | use MediaWiki\Cache\LinkBatchFactory; |
28 | use MediaWiki\HookContainer\HookRunner; |
29 | use MediaWiki\Html\FormOptions; |
30 | use MediaWiki\Html\Html; |
31 | use MediaWiki\Html\ListToggle; |
32 | use MediaWiki\MainConfigNames; |
33 | use MediaWiki\MediaWikiServices; |
34 | use MediaWiki\Pager\LogPager; |
35 | use MediaWiki\SpecialPage\SpecialPage; |
36 | use MediaWiki\Title\Title; |
37 | use MediaWiki\User\ActorNormalization; |
38 | use MediaWiki\User\UserIdentityLookup; |
39 | use MediaWiki\User\UserNameUtils; |
40 | use MediaWiki\Utils\MWTimestamp; |
41 | use PermissionsError; |
42 | use Wikimedia\IPUtils; |
43 | use Wikimedia\Rdbms\IConnectionProvider; |
44 | use Wikimedia\Timestamp\TimestampException; |
45 | |
46 | /** |
47 | * A special page that lists log entries |
48 | * |
49 | * @ingroup SpecialPage |
50 | */ |
51 | class SpecialLog extends SpecialPage { |
52 | |
53 | private LinkBatchFactory $linkBatchFactory; |
54 | |
55 | private IConnectionProvider $dbProvider; |
56 | |
57 | private ActorNormalization $actorNormalization; |
58 | |
59 | private UserIdentityLookup $userIdentityLookup; |
60 | |
61 | private UserNameUtils $userNameUtils; |
62 | |
63 | private LogFormatterFactory $logFormatterFactory; |
64 | |
65 | /** |
66 | * @param LinkBatchFactory $linkBatchFactory |
67 | * @param IConnectionProvider $dbProvider |
68 | * @param ActorNormalization $actorNormalization |
69 | * @param UserIdentityLookup $userIdentityLookup |
70 | * @param UserNameUtils $userNameUtils |
71 | * @param LogFormatterFactory $logFormatterFactory |
72 | */ |
73 | public function __construct( |
74 | LinkBatchFactory $linkBatchFactory, |
75 | IConnectionProvider $dbProvider, |
76 | ActorNormalization $actorNormalization, |
77 | UserIdentityLookup $userIdentityLookup, |
78 | UserNameUtils $userNameUtils, |
79 | LogFormatterFactory $logFormatterFactory |
80 | ) { |
81 | parent::__construct( 'Log' ); |
82 | $this->linkBatchFactory = $linkBatchFactory; |
83 | $this->dbProvider = $dbProvider; |
84 | $this->actorNormalization = $actorNormalization; |
85 | $this->userIdentityLookup = $userIdentityLookup; |
86 | $this->userNameUtils = $userNameUtils; |
87 | $this->logFormatterFactory = $logFormatterFactory; |
88 | } |
89 | |
90 | public function execute( $par ) { |
91 | $this->setHeaders(); |
92 | $this->outputHeader(); |
93 | $out = $this->getOutput(); |
94 | $out->addModules( 'mediawiki.userSuggest' ); |
95 | $out->addModuleStyles( 'mediawiki.interface.helpers.styles' ); |
96 | $this->addHelpLink( 'Help:Log' ); |
97 | |
98 | $opts = new FormOptions; |
99 | $opts->add( 'type', '' ); |
100 | $opts->add( 'user', '' ); |
101 | $opts->add( 'page', '' ); |
102 | $opts->add( 'pattern', false ); |
103 | $opts->add( 'year', null, FormOptions::INTNULL ); |
104 | $opts->add( 'month', null, FormOptions::INTNULL ); |
105 | $opts->add( 'day', null, FormOptions::INTNULL ); |
106 | $opts->add( 'tagfilter', '' ); |
107 | $opts->add( 'tagInvert', false ); |
108 | $opts->add( 'offset', '' ); |
109 | $opts->add( 'dir', '' ); |
110 | $opts->add( 'offender', '' ); |
111 | $opts->add( 'subtype', '' ); |
112 | $opts->add( 'logid', '' ); |
113 | |
114 | // Set values |
115 | if ( $par !== null ) { |
116 | $this->parseParams( (string)$par ); |
117 | } |
118 | $opts->fetchValuesFromRequest( $this->getRequest() ); |
119 | |
120 | // Set date values |
121 | $dateString = $this->getRequest()->getVal( 'wpdate' ); |
122 | if ( $dateString ) { |
123 | try { |
124 | $dateStamp = MWTimestamp::getInstance( $dateString . ' 00:00:00' ); |
125 | } catch ( TimestampException $e ) { |
126 | // If users provide an invalid date, silently ignore it |
127 | // instead of letting an exception bubble up (T201411) |
128 | $dateStamp = false; |
129 | } |
130 | if ( $dateStamp ) { |
131 | $opts->setValue( 'year', (int)$dateStamp->format( 'Y' ) ); |
132 | $opts->setValue( 'month', (int)$dateStamp->format( 'm' ) ); |
133 | $opts->setValue( 'day', (int)$dateStamp->format( 'd' ) ); |
134 | } |
135 | } |
136 | |
137 | // If the user doesn't have the right permission to view the specific |
138 | // log type, throw a PermissionsError |
139 | $logRestrictions = $this->getConfig()->get( MainConfigNames::LogRestrictions ); |
140 | $type = $opts->getValue( 'type' ); |
141 | if ( isset( $logRestrictions[$type] ) |
142 | && !$this->getAuthority()->isAllowed( $logRestrictions[$type] ) |
143 | ) { |
144 | throw new PermissionsError( $logRestrictions[$type] ); |
145 | } |
146 | |
147 | # TODO: Move this into LogPager like other query conditions. |
148 | # Handle type-specific inputs |
149 | $qc = []; |
150 | $offenderName = $opts->getValue( 'offender' ); |
151 | if ( $opts->getValue( 'type' ) == 'suppress' && $offenderName !== '' ) { |
152 | $dbr = $this->dbProvider->getReplicaDatabase(); |
153 | $offenderId = $this->actorNormalization->findActorIdByName( $offenderName, $dbr ); |
154 | if ( $offenderId ) { |
155 | $qc = [ 'ls_field' => 'target_author_actor', 'ls_value' => strval( $offenderId ) ]; |
156 | } else { |
157 | // Unknown offender, thus results have to be empty |
158 | $qc = [ '1=0' ]; |
159 | } |
160 | } else { |
161 | // Allow extensions to add relations to their search types |
162 | $this->getHookRunner()->onSpecialLogAddLogSearchRelations( |
163 | $opts->getValue( 'type' ), $this->getRequest(), $qc ); |
164 | } |
165 | |
166 | # TODO: Move this into LogEventList and use it as filter-callback in the field descriptor. |
167 | # Some log types are only for a 'User:' title but we might have been given |
168 | # only the username instead of the full title 'User:username'. This part try |
169 | # to lookup for a user by that name and eventually fix user input. See T3697. |
170 | if ( in_array( $opts->getValue( 'type' ), self::getLogTypesOnUser( $this->getHookRunner() ) ) ) { |
171 | # ok we have a type of log which expect a user title. |
172 | $page = $opts->getValue( 'page' ); |
173 | $target = Title::newFromText( $page ); |
174 | if ( $target && $target->getNamespace() === NS_MAIN ) { |
175 | if ( IPUtils::isValidRange( $target->getText() ) ) { |
176 | $page = IPUtils::sanitizeRange( $target->getText() ); |
177 | } |
178 | # User forgot to add 'User:', we are adding it for him |
179 | $target = Title::makeTitleSafe( NS_USER, $page ); |
180 | } elseif ( $target && $target->getNamespace() === NS_USER |
181 | && IPUtils::isValidRange( $target->getText() ) |
182 | ) { |
183 | $ipOrRange = IPUtils::sanitizeRange( $target->getText() ); |
184 | if ( $ipOrRange !== $target->getText() ) { |
185 | $target = Title::makeTitleSafe( NS_USER, $ipOrRange ); |
186 | } |
187 | } |
188 | if ( $target !== null ) { |
189 | $page = $target->getPrefixedText(); |
190 | $opts->setValue( 'page', $page ); |
191 | $this->getRequest()->setVal( 'page', $page ); |
192 | } |
193 | } |
194 | |
195 | $this->show( $opts, $qc ); |
196 | } |
197 | |
198 | /** |
199 | * List log type for which the target is a user |
200 | * Thus if the given target is in NS_MAIN we can alter it to be an NS_USER |
201 | * Title user instead. |
202 | * |
203 | * @since 1.25 |
204 | * @since 1.36 Added $runner parameter |
205 | * |
206 | * @param HookRunner|null $runner |
207 | * @return array |
208 | */ |
209 | public static function getLogTypesOnUser( ?HookRunner $runner = null ) { |
210 | static $types = null; |
211 | if ( $types !== null ) { |
212 | return $types; |
213 | } |
214 | $types = [ |
215 | 'block', |
216 | 'newusers', |
217 | 'rights', |
218 | 'renameuser', |
219 | ]; |
220 | |
221 | ( $runner ?? new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) ) |
222 | ->onGetLogTypesOnUser( $types ); |
223 | return $types; |
224 | } |
225 | |
226 | /** |
227 | * Return an array of subpages that this special page will accept. |
228 | * |
229 | * @return string[] subpages |
230 | */ |
231 | public function getSubpagesForPrefixSearch() { |
232 | $subpages = LogPage::validTypes(); |
233 | $subpages[] = 'all'; |
234 | sort( $subpages ); |
235 | return $subpages; |
236 | } |
237 | |
238 | /** |
239 | * Set options based on the subpage title parts: |
240 | * - One part that is a valid log type: Special:Log/logtype |
241 | * - Two parts: Special:Log/logtype/username |
242 | * - Otherwise, assume the whole subpage is a username. |
243 | * |
244 | * @param string $par |
245 | */ |
246 | private function parseParams( string $par ) { |
247 | # Get parameters |
248 | $parms = explode( '/', $par, 2 ); |
249 | $symsForAll = [ '*', 'all' ]; |
250 | if ( $parms[0] !== '' && |
251 | ( in_array( $parms[0], LogPage::validTypes() ) || in_array( $parms[0], $symsForAll ) ) |
252 | ) { |
253 | $this->getRequest()->setVal( 'type', $parms[0] ); |
254 | if ( count( $parms ) === 2 ) { |
255 | $this->getRequest()->setVal( 'user', $parms[1] ); |
256 | } |
257 | } elseif ( $par !== '' ) { |
258 | $this->getRequest()->setVal( 'user', $par ); |
259 | } |
260 | } |
261 | |
262 | private function show( FormOptions $opts, array $extraConds ) { |
263 | # Create a LogPager item to get the results and a LogEventsList item to format them... |
264 | $loglist = new LogEventsList( |
265 | $this->getContext(), |
266 | $this->getLinkRenderer(), |
267 | LogEventsList::USE_CHECKBOXES |
268 | ); |
269 | $pager = new LogPager( |
270 | $loglist, |
271 | $opts->getValue( 'type' ), |
272 | $opts->getValue( 'user' ), |
273 | $opts->getValue( 'page' ), |
274 | $opts->getValue( 'pattern' ), |
275 | $extraConds, |
276 | $opts->getValue( 'year' ), |
277 | $opts->getValue( 'month' ), |
278 | $opts->getValue( 'day' ), |
279 | $opts->getValue( 'tagfilter' ), |
280 | $opts->getValue( 'subtype' ), |
281 | $opts->getValue( 'logid' ), |
282 | $this->linkBatchFactory, |
283 | $this->actorNormalization, |
284 | $this->logFormatterFactory, |
285 | $opts->getValue( 'tagInvert' ) |
286 | ); |
287 | |
288 | # Set relevant user |
289 | $performer = $pager->getPerformer(); |
290 | if ( $performer ) { |
291 | $performerUser = $this->userIdentityLookup->getUserIdentityByName( $performer ); |
292 | // Only set valid local user as the relevant user (T344886) |
293 | // Uses the same condition as the SpecialContributions class did |
294 | if ( $performerUser && !IPUtils::isValidRange( $performer ) && |
295 | ( $this->userNameUtils->isIP( $performer ) || $performerUser->isRegistered() ) |
296 | ) { |
297 | $this->getSkin()->setRelevantUser( $performerUser ); |
298 | } |
299 | } |
300 | |
301 | # Show form options |
302 | $succeed = $loglist->showOptions( |
303 | $opts->getValue( 'type' ), |
304 | $opts->getValue( 'year' ), |
305 | $opts->getValue( 'month' ), |
306 | $opts->getValue( 'day' ) |
307 | ); |
308 | if ( !$succeed ) { |
309 | return; |
310 | } |
311 | |
312 | $this->getOutput()->setPageTitleMsg( |
313 | ( new LogPage( $opts->getValue( 'type' ) ) )->getName() |
314 | ); |
315 | |
316 | # Insert list |
317 | $logBody = $pager->getBody(); |
318 | if ( $logBody ) { |
319 | $this->getOutput()->addHTML( |
320 | $pager->getNavigationBar() . |
321 | $this->getActionButtons( |
322 | $loglist->beginLogEventsList() . |
323 | $logBody . |
324 | $loglist->endLogEventsList() |
325 | ) . |
326 | $pager->getNavigationBar() |
327 | ); |
328 | } else { |
329 | $this->getOutput()->addWikiMsg( 'logempty' ); |
330 | } |
331 | } |
332 | |
333 | private function getActionButtons( $formcontents ) { |
334 | $canRevDelete = $this->getAuthority() |
335 | ->isAllowedAll( 'deletedhistory', 'deletelogentry' ); |
336 | $showTagEditUI = ChangeTags::showTagEditingUI( $this->getAuthority() ); |
337 | # If the user doesn't have the ability to delete log entries nor edit tags, |
338 | # don't bother showing them the button(s). |
339 | if ( !$canRevDelete && !$showTagEditUI ) { |
340 | return $formcontents; |
341 | } |
342 | |
343 | # Show button to hide log entries and/or edit change tags |
344 | $s = Html::openElement( |
345 | 'form', |
346 | [ 'action' => wfScript(), 'id' => 'mw-log-deleterevision-submit' ] |
347 | ) . "\n"; |
348 | $s .= Html::hidden( 'type', 'logging' ) . "\n"; |
349 | |
350 | $buttons = ''; |
351 | if ( $canRevDelete ) { |
352 | $buttons .= Html::element( |
353 | 'button', |
354 | [ |
355 | 'type' => 'submit', |
356 | 'name' => 'title', |
357 | 'value' => SpecialPage::getTitleFor( 'Revisiondelete' )->getPrefixedDBkey(), |
358 | 'class' => "deleterevision-log-submit mw-log-deleterevision-button mw-ui-button" |
359 | ], |
360 | $this->msg( 'showhideselectedlogentries' )->text() |
361 | ) . "\n"; |
362 | } |
363 | if ( $showTagEditUI ) { |
364 | $buttons .= Html::element( |
365 | 'button', |
366 | [ |
367 | 'type' => 'submit', |
368 | 'name' => 'title', |
369 | 'value' => SpecialPage::getTitleFor( 'EditTags' )->getPrefixedDBkey(), |
370 | 'class' => "editchangetags-log-submit mw-log-editchangetags-button mw-ui-button" |
371 | ], |
372 | $this->msg( 'log-edit-tags' )->text() |
373 | ) . "\n"; |
374 | } |
375 | |
376 | $buttons .= ( new ListToggle( $this->getOutput() ) )->getHTML(); |
377 | |
378 | $s .= $buttons . $formcontents . $buttons; |
379 | $s .= Html::closeElement( 'form' ); |
380 | |
381 | return $s; |
382 | } |
383 | |
384 | protected function getGroupName() { |
385 | return 'changes'; |
386 | } |
387 | } |
388 | |
389 | /** @deprecated class alias since 1.41 */ |
390 | class_alias( SpecialLog::class, 'SpecialLog' ); |