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