MediaWiki master
SpecialLog.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Specials;
22
23use ChangeTags;
26use LogPage;
42use Wikimedia\IPUtils;
44use Wikimedia\Timestamp\TimestampException;
45
51class 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
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
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
221 ->onGetLogTypesOnUser( $types );
222 return $types;
223 }
224
230 public function getSubpagesForPrefixSearch() {
231 $subpages = LogPage::validTypes();
232 $subpages[] = 'all';
233 sort( $subpages );
234 return $subpages;
235 }
236
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
389class_alias( SpecialLog::class, 'SpecialLog' );
const NS_USER
Definition Defines.php:67
const NS_MAIN
Definition Defines.php:65
wfScript( $script='index')
Get the URL path to a MediaWiki entry point.
Recent changes tagging.
static showTagEditingUI(Authority $performer)
Indicate whether change tag editing UI is relevant.
Class to simplify the use of log pages.
Definition LogPage.php:46
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Helper class to keep track of options when mixing links and form elements.
add( $name, $default, $type=self::AUTO)
Add an option to be handled by this FormOptions instance.
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
Class for generating clickable toggle links for a list of checkboxes.
A class containing constants representing the names of configuration variables.
const LogRestrictions
Name constant for the LogRestrictions setting, for use with Config::get()
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Parent class for all special pages.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getSkin()
Shortcut to get the skin being used for this instance.
static getTitleFor( $name, $subpage=false, $fragment='')
Get a localised Title object for a specified special page name If you don't need a full Title object,...
getConfig()
Shortcut to get main config object.
getContext()
Gets the context this SpecialPage is executed in.
getRequest()
Get the WebRequest being used for this instance.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getOutput()
Get the OutputPage being used for this instance.
getAuthority()
Shortcut to get the Authority executing this instance.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages By default the message key is the canonical name of...
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
A special page that lists log entries.
static getLogTypesOnUser(?HookRunner $runner=null)
List log type for which the target is a user Thus if the given target is in NS_MAIN we can alter it t...
__construct(LinkBatchFactory $linkBatchFactory, IConnectionProvider $dbProvider, ActorNormalization $actorNormalization, UserIdentityLookup $userIdentityLookup, UserNameUtils $userNameUtils, LogFormatterFactory $logFormatterFactory)
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
execute( $par)
Default execute method Checks user permissions.
getSubpagesForPrefixSearch()
Return an array of subpages that this special page will accept.
Represents a title within MediaWiki.
Definition Title.php:78
UserNameUtils service.
Library for creating and parsing MW-style timestamps.
Show an error when a user tries to do something they do not have the necessary permissions for.
$runner
Service for dealing with the actor table.
Service for looking up UserIdentity.
Provide primary and replica IDatabase connections.
element(SerializerNode $parent, SerializerNode $node, $contents)