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 
41  private $loadBalancer;
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(),
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
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 }
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:170
SpecialLog\$linkBatchFactory
LinkBatchFactory $linkBatchFactory
Definition: SpecialLog.php:38
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:385
LogPage\validTypes
static validTypes()
Get the list of valid log types.
Definition: LogPage.php:206
SpecialLog\parseParams
parseParams(FormOptions $opts, $par)
Set options based on the subpage title parts:
Definition: SpecialLog.php:206
SpecialPage\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: SpecialPage.php:789
SpecialLog\__construct
__construct(LinkBatchFactory $linkBatchFactory, ILoadBalancer $loadBalancer, ActorNormalization $actorNormalization)
Definition: SpecialLog.php:51
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:290
LogPager
Definition: LogPager.php:35
User\newFromName
static newFromName( $name, $validate='valid')
Definition: User.php:592
SpecialPage\getSkin
getSkin()
Shortcut to get the skin being used for this instance.
Definition: SpecialPage.php:819
SpecialPage\getAuthority
getAuthority()
Shortcut to get the Authority executing this instance.
Definition: SpecialPage.php:809
PermissionsError
Show an error when a user tries to do something they do not have the necessary permissions for.
Definition: PermissionsError.php:32
SpecialLog\$actorNormalization
ActorNormalization $actorNormalization
Definition: SpecialLog.php:44
LogEventsList\USE_CHECKBOXES
const USE_CHECKBOXES
Definition: LogEventsList.php:36
NS_MAIN
const NS_MAIN
Definition: Defines.php:64
$dbr
$dbr
Definition: testCompression.php:54
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:2375
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:218
MediaWiki\Cache\LinkBatchFactory
Definition: LinkBatchFactory.php:39
LogPage
Class to simplify the use of log pages.
Definition: LogPage.php:38
MWTimestamp\getInstance
static getInstance( $ts=false)
Get a timestamp instance in GMT.
Definition: MWTimestamp.php:45
SpecialPage\setHeaders
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes!
Definition: SpecialPage.php:617
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
LogEventsList
Definition: LogEventsList.php:33
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:676
$s
foreach( $mmfl['setupFiles'] as $fileName) if( $queue) if(empty( $mmfl['quiet'])) $s
Definition: mergeMessageFileList.php:206
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:366
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:223
NS_USER
const NS_USER
Definition: Defines.php:66
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:41
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:558
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:1713
SpecialLog\execute
execute( $par)
Default execute method Checks user permissions.
Definition: SpecialLog.php:62
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:190
SpecialLog\addHeader
addHeader( $type)
Set page title and show header for this log type.
Definition: SpecialLog.php:359
MediaWiki\User\ActorNormalization
Service for dealing with the actor table.
Definition: ActorNormalization.php:32
$type
$type
Definition: testCompression.php:52