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