MediaWiki REL1_37
SpecialLog.php
Go to the documentation of this file.
1<?php
28use Wikimedia\Timestamp\TimestampException;
29
35class SpecialLog extends SpecialPage {
36
39
42
45
51 public function __construct(
55 ) {
56 parent::__construct( 'Log' );
57 $this->linkBatchFactory = $linkBatchFactory;
58 $this->loadBalancer = $loadBalancer;
59 $this->actorNormalization = $actorNormalization;
60 }
61
62 public function execute( $par ) {
63 $this->setHeaders();
64 $this->outputHeader();
65 $out = $this->getOutput();
66 $out->addModules( 'mediawiki.userSuggest' );
67 $out->addModuleStyles( 'mediawiki.interface.helpers.styles' );
68 $this->addHelpLink( 'Help:Log' );
69
70 $opts = new FormOptions;
71 $opts->add( 'type', '' );
72 $opts->add( 'user', '' );
73 $opts->add( 'page', '' );
74 $opts->add( 'pattern', false );
75 $opts->add( 'year', null, FormOptions::INTNULL );
76 $opts->add( 'month', null, FormOptions::INTNULL );
77 $opts->add( 'day', null, FormOptions::INTNULL );
78 $opts->add( 'tagfilter', '' );
79 $opts->add( 'offset', '' );
80 $opts->add( 'dir', '' );
81 $opts->add( 'offender', '' );
82 $opts->add( 'subtype', '' );
83 $opts->add( 'logid', '' );
84
85 // Set values
86 $opts->fetchValuesFromRequest( $this->getRequest() );
87 if ( $par !== null ) {
88 $this->parseParams( $opts, (string)$par );
89 }
90
91 // Set date values
92 $dateString = $this->getRequest()->getVal( 'wpdate' );
93 if ( !empty( $dateString ) ) {
94 try {
95 $dateStamp = MWTimestamp::getInstance( $dateString . ' 00:00:00' );
96 } catch ( TimestampException $e ) {
97 // If users provide an invalid date, silently ignore it
98 // instead of letting an exception bubble up (T201411)
99 $dateStamp = false;
100 }
101 if ( $dateStamp ) {
102 $opts->setValue( 'year', (int)$dateStamp->format( 'Y' ) );
103 $opts->setValue( 'month', (int)$dateStamp->format( 'm' ) );
104 $opts->setValue( 'day', (int)$dateStamp->format( 'd' ) );
105 }
106 }
107
108 # Don't let the user get stuck with a certain date
109 if ( $opts->getValue( 'offset' ) || $opts->getValue( 'dir' ) == 'prev' ) {
110 $opts->setValue( 'year', '' );
111 $opts->setValue( 'month', '' );
112 }
113
114 // If the user doesn't have the right permission to view the specific
115 // log type, throw a PermissionsError
116 // If the log type is invalid, just show all public logs
117 $logRestrictions = $this->getConfig()->get( 'LogRestrictions' );
118 $type = $opts->getValue( 'type' );
119 if ( !LogPage::isLogType( $type ) ) {
120 $opts->setValue( 'type', '' );
121 } elseif ( isset( $logRestrictions[$type] )
122 && !$this->getAuthority()->isAllowed( $logRestrictions[$type] )
123 ) {
124 throw new PermissionsError( $logRestrictions[$type] );
125 }
126
127 # Handle type-specific inputs
128 $qc = [];
129 if ( $opts->getValue( 'type' ) == 'suppress' ) {
130 $dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
131 $offenderName = $opts->getValue( 'offender' );
132 $offenderId = $this->actorNormalization->findActorIdByName( $offenderName, $dbr );
133 if ( $offenderId ) {
134 $qc = [ 'ls_field' => 'target_author_actor', 'ls_value' => $offenderId ];
135 }
136 } else {
137 // Allow extensions to add relations to their search types
138 $this->getHookRunner()->onSpecialLogAddLogSearchRelations(
139 $opts->getValue( 'type' ), $this->getRequest(), $qc );
140 }
141
142 # Some log types are only for a 'User:' title but we might have been given
143 # only the username instead of the full title 'User:username'. This part try
144 # to lookup for a user by that name and eventually fix user input. See T3697.
145 if ( in_array( $opts->getValue( 'type' ), self::getLogTypesOnUser( $this->getHookRunner() ) ) ) {
146 # ok we have a type of log which expect a user title.
147 $target = Title::newFromText( $opts->getValue( 'page' ) );
148 if ( $target && $target->getNamespace() === NS_MAIN ) {
149 # User forgot to add 'User:', we are adding it for him
150 $opts->setValue( 'page',
151 Title::makeTitleSafe( NS_USER, $opts->getValue( 'page' ) )
152 );
153 }
154 }
155
156 $this->show( $opts, $qc );
157 }
158
170 public static function getLogTypesOnUser( HookRunner $runner = null ) {
171 static $types = null;
172 if ( $types !== null ) {
173 return $types;
174 }
175 $types = [
176 'block',
177 'newusers',
178 'rights',
179 ];
180
181 ( $runner ?? Hooks::runner() )->onGetLogTypesOnUser( $types );
182 return $types;
183 }
184
190 public function getSubpagesForPrefixSearch() {
191 $subpages = LogPage::validTypes();
192 $subpages[] = 'all';
193 sort( $subpages );
194 return $subpages;
195 }
196
206 private function parseParams( FormOptions $opts, $par ) {
207 # Get parameters
208 $par = $par ?? '';
209 $parms = explode( '/', $par );
210 $symsForAll = [ '*', 'all' ];
211 if ( $parms[0] != '' &&
212 ( in_array( $par, LogPage::validTypes() ) || in_array( $par, $symsForAll ) )
213 ) {
214 $opts->setValue( 'type', $par );
215 } elseif ( count( $parms ) == 2 ) {
216 $opts->setValue( 'type', $parms[0] );
217 $opts->setValue( 'user', $parms[1] );
218 } elseif ( $par != '' ) {
219 $opts->setValue( 'user', $par );
220 }
221 }
222
223 private function show( FormOptions $opts, array $extraConds ) {
224 # Create a LogPager item to get the results and a LogEventsList item to format them...
225 $loglist = new LogEventsList(
226 $this->getContext(),
227 $this->getLinkRenderer(),
228 LogEventsList::USE_CHECKBOXES
229 );
230
231 $pager = new LogPager(
232 $loglist,
233 $opts->getValue( 'type' ),
234 $opts->getValue( 'user' ),
235 $opts->getValue( 'page' ),
236 $opts->getValue( 'pattern' ),
237 $extraConds,
238 $opts->getValue( 'year' ),
239 $opts->getValue( 'month' ),
240 $opts->getValue( 'day' ),
241 $opts->getValue( 'tagfilter' ),
242 $opts->getValue( 'subtype' ),
243 $opts->getValue( 'logid' ),
244 $this->linkBatchFactory,
245 $this->loadBalancer,
246 $this->actorNormalization
247 );
248
249 $this->addHeader( $opts->getValue( 'type' ) );
250
251 # Set relevant user
252 if ( $pager->getPerformer() ) {
253 $performerUser = User::newFromName( $pager->getPerformer(), false );
254 if ( $performerUser ) {
255 $this->getSkin()->setRelevantUser( $performerUser );
256 }
257 }
258
259 # Show form options
260 $loglist->showOptions(
261 $pager->getType(),
262 $pager->getPerformer(),
263 $pager->getPage(),
264 $pager->getPattern(),
265 $pager->getYear(),
266 $pager->getMonth(),
267 $pager->getDay(),
268 $pager->getFilterParams(),
269 $pager->getTagFilter(),
270 $pager->getAction()
271 );
272
273 # Insert list
274 $logBody = $pager->getBody();
275 if ( $logBody ) {
276 $this->getOutput()->addHTML(
277 $pager->getNavigationBar() .
278 $this->getActionButtons(
279 $loglist->beginLogEventsList() .
280 $logBody .
281 $loglist->endLogEventsList()
282 ) .
283 $pager->getNavigationBar()
284 );
285 } else {
286 $this->getOutput()->addWikiMsg( 'logempty' );
287 }
288 }
289
290 private function getActionButtons( $formcontents ) {
291 $canRevDelete = $this->getAuthority()
292 ->isAllowedAll( 'deletedhistory', 'deletelogentry' );
293 $showTagEditUI = ChangeTags::showTagEditingUI( $this->getAuthority() );
294 # If the user doesn't have the ability to delete log entries nor edit tags,
295 # don't bother showing them the button(s).
296 if ( !$canRevDelete && !$showTagEditUI ) {
297 return $formcontents;
298 }
299
300 # Show button to hide log entries and/or edit change tags
301 $s = Html::openElement(
302 'form',
303 [ 'action' => wfScript(), 'id' => 'mw-log-deleterevision-submit' ]
304 ) . "\n";
305 $s .= Html::hidden( 'action', 'historysubmit' ) . "\n";
306 $s .= Html::hidden( 'type', 'logging' ) . "\n";
307
308 // If no title is set, the fallback is to use the main page, as defined
309 // by MediaWiki:Mainpage
310 // On wikis where the main page can be translated, MediaWiki:Mainpage
311 // is sometimes set to use Special:MyLanguage to redirect to the
312 // appropriate version. This is interpreted as a special page, and
313 // Action::getActionName forces the action to be 'view' if the title
314 // cannot be used as a WikiPage, which includes all pages in NS_SPECIAL.
315 // Set a dummy title to avoid this. The title provided is unused
316 // by the SpecialPageAction class and does not matter.
317 // See T205908
318 $s .= Html::hidden( 'title', 'Unused' ) . "\n";
319
320 $buttons = '';
321 if ( $canRevDelete ) {
322 $buttons .= Html::element(
323 'button',
324 [
325 'type' => 'submit',
326 'name' => 'revisiondelete',
327 'value' => '1',
328 'class' => "deleterevision-log-submit mw-log-deleterevision-button mw-ui-button"
329 ],
330 $this->msg( 'showhideselectedlogentries' )->text()
331 ) . "\n";
332 }
333 if ( $showTagEditUI ) {
334 $buttons .= Html::element(
335 'button',
336 [
337 'type' => 'submit',
338 'name' => 'editchangetags',
339 'value' => '1',
340 'class' => "editchangetags-log-submit mw-log-editchangetags-button mw-ui-button"
341 ],
342 $this->msg( 'log-edit-tags' )->text()
343 ) . "\n";
344 }
345
346 $buttons .= ( new ListToggle( $this->getOutput() ) )->getHTML();
347
348 $s .= $buttons . $formcontents . $buttons;
349 $s .= Html::closeElement( 'form' );
350
351 return $s;
352 }
353
359 protected function addHeader( $type ) {
360 $page = new LogPage( $type );
361 $this->getOutput()->setPageTitle( $page->getName() );
362 $this->getOutput()->addHTML( $page->getDescription()
363 ->setContext( $this->getContext() )->parseAsBlock() );
364 }
365
366 protected function getGroupName() {
367 return 'changes';
368 }
369}
const NS_USER
Definition Defines.php:66
const NS_MAIN
Definition Defines.php:64
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
static showTagEditingUI(Authority $performer)
Indicate whether change tag editing UI is relevant.
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.
setValue( $name, $value, $force=false)
Use to set the value of an option.
getValue( $name)
Get the value for the given option name.
Class for generating clickable toggle links for a list of checkboxes.
Class to simplify the use of log pages.
Definition LogPage.php:38
static isLogType( $type)
Is $type a valid log type.
Definition LogPage.php:218
static validTypes()
Get the list of valid log types.
Definition LogPage.php:206
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Show an error when a user tries to do something they do not have the necessary permissions for.
A special page that lists log entries.
getSubpagesForPrefixSearch()
Return an array of subpages that this special page will accept.
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...
execute( $par)
Default execute method Checks user permissions.
show(FormOptions $opts, array $extraConds)
ActorNormalization $actorNormalization
parseParams(FormOptions $opts, $par)
Set options based on the subpage title parts:
ILoadBalancer $loadBalancer
__construct(LinkBatchFactory $linkBatchFactory, ILoadBalancer $loadBalancer, ActorNormalization $actorNormalization)
LinkBatchFactory $linkBatchFactory
getActionButtons( $formcontents)
addHeader( $type)
Set page title and show header for this log type.
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Parent class for all special pages.
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
getOutput()
Get the OutputPage being used for this instance.
getSkin()
Shortcut to get the skin being used for this instance.
getContext()
Gets the context this SpecialPage is executed in.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getAuthority()
Shortcut to get the Authority executing this instance.
getConfig()
Shortcut to get main config object.
getRequest()
Get the WebRequest being used for this instance.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
static newFromName( $name, $validate='valid')
Definition User.php:607
Service for dealing with the actor table.
Database cluster connection, tracking, load balancing, and transaction manager interface.
foreach( $mmfl['setupFiles'] as $fileName) if($queue) if(empty( $mmfl['quiet'])) $s
const DB_REPLICA
Definition defines.php:25