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->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
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
222 ->onGetLogTypesOnUser( $types );
223 return $types;
224 }
225
231 public function getSubpagesForPrefixSearch() {
232 $subpages = LogPage::validTypes();
233 $subpages[] = 'all';
234 sort( $subpages );
235 return $subpages;
236 }
237
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
390class_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.
Provide primary and replica IDatabase connections.
element(SerializerNode $parent, SerializerNode $node, $contents)