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