MediaWiki  master
SpecialLog.php
Go to the documentation of this file.
1 <?php
33 use Wikimedia\IPUtils;
35 use Wikimedia\Timestamp\TimestampException;
36 
42 class SpecialLog extends SpecialPage {
43 
45  private $linkBatchFactory;
46 
48  private $loadBalancer;
49 
51  private $actorNormalization;
52 
54  private $userIdentityLookup;
55 
62  public function __construct(
63  LinkBatchFactory $linkBatchFactory,
64  ILoadBalancer $loadBalancer,
65  ActorNormalization $actorNormalization,
66  UserIdentityLookup $userIdentityLookup
67  ) {
68  parent::__construct( 'Log' );
69  $this->linkBatchFactory = $linkBatchFactory;
70  $this->loadBalancer = $loadBalancer;
71  $this->actorNormalization = $actorNormalization;
72  $this->userIdentityLookup = $userIdentityLookup;
73  }
74 
75  public function execute( $par ) {
76  $this->setHeaders();
77  $this->outputHeader();
78  $out = $this->getOutput();
79  $out->addModules( 'mediawiki.userSuggest' );
80  $out->addModuleStyles( 'mediawiki.interface.helpers.styles' );
81  $this->addHelpLink( 'Help:Log' );
82 
83  $opts = new FormOptions;
84  $opts->add( 'type', '' );
85  $opts->add( 'user', '' );
86  $opts->add( 'page', '' );
87  $opts->add( 'pattern', false );
88  $opts->add( 'year', null, FormOptions::INTNULL );
89  $opts->add( 'month', null, FormOptions::INTNULL );
90  $opts->add( 'day', null, FormOptions::INTNULL );
91  $opts->add( 'tagfilter', '' );
92  $opts->add( 'tagInvert', false );
93  $opts->add( 'offset', '' );
94  $opts->add( 'dir', '' );
95  $opts->add( 'offender', '' );
96  $opts->add( 'subtype', '' );
97  $opts->add( 'logid', '' );
98 
99  // Set values
100  $opts->fetchValuesFromRequest( $this->getRequest() );
101  if ( $par !== null ) {
102  $this->parseParams( $opts, (string)$par );
103  }
104 
105  // Set date values
106  $dateString = $this->getRequest()->getVal( 'wpdate' );
107  if ( !empty( $dateString ) ) {
108  try {
109  $dateStamp = MWTimestamp::getInstance( $dateString . ' 00:00:00' );
110  } catch ( TimestampException $e ) {
111  // If users provide an invalid date, silently ignore it
112  // instead of letting an exception bubble up (T201411)
113  $dateStamp = false;
114  }
115  if ( $dateStamp ) {
116  $opts->setValue( 'year', (int)$dateStamp->format( 'Y' ) );
117  $opts->setValue( 'month', (int)$dateStamp->format( 'm' ) );
118  $opts->setValue( 'day', (int)$dateStamp->format( 'd' ) );
119  }
120  }
121 
122  // If the user doesn't have the right permission to view the specific
123  // log type, throw a PermissionsError
124  // If the log type is invalid, just show all public logs
125  $logRestrictions = $this->getConfig()->get( MainConfigNames::LogRestrictions );
126  $type = $opts->getValue( 'type' );
127  if ( !LogPage::isLogType( $type ) ) {
128  $opts->setValue( 'type', '' );
129  } elseif ( isset( $logRestrictions[$type] )
130  && !$this->getAuthority()->isAllowed( $logRestrictions[$type] )
131  ) {
132  throw new PermissionsError( $logRestrictions[$type] );
133  }
134 
135  # Handle type-specific inputs
136  $qc = [];
137  if ( $opts->getValue( 'type' ) == 'suppress' ) {
138  $dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
139  $offenderName = $opts->getValue( 'offender' );
140  $offenderId = $this->actorNormalization->findActorIdByName( $offenderName, $dbr );
141  if ( $offenderId ) {
142  $qc = [ 'ls_field' => 'target_author_actor', 'ls_value' => $offenderId ];
143  }
144  } else {
145  // Allow extensions to add relations to their search types
146  $this->getHookRunner()->onSpecialLogAddLogSearchRelations(
147  $opts->getValue( 'type' ), $this->getRequest(), $qc );
148  }
149 
150  # Some log types are only for a 'User:' title but we might have been given
151  # only the username instead of the full title 'User:username'. This part try
152  # to lookup for a user by that name and eventually fix user input. See T3697.
153  if ( in_array( $opts->getValue( 'type' ), self::getLogTypesOnUser( $this->getHookRunner() ) ) ) {
154  # ok we have a type of log which expect a user title.
155  $page = $opts->getValue( 'page' );
156  $target = Title::newFromText( $page );
157  if ( $target && $target->getNamespace() === NS_MAIN ) {
158  if ( IPUtils::isValidRange( $target->getText() ) ) {
159  $page = IPUtils::sanitizeRange( $target->getText() );
160  }
161  # User forgot to add 'User:', we are adding it for him
162  $opts->setValue( 'page',
163  Title::makeTitleSafe( NS_USER, $page )
164  );
165  } elseif ( $target && $target->getNamespace() === NS_USER
166  && IPUtils::isValidRange( $target->getText() )
167  ) {
168  $page = IPUtils::sanitizeRange( $target->getText() );
169  if ( $page !== $target->getText() ) {
170  $opts->setValue( 'page', Title::makeTitleSafe( NS_USER, $page ) );
171  }
172  }
173  }
174 
175  $this->show( $opts, $qc );
176  }
177 
189  public static function getLogTypesOnUser( HookRunner $runner = null ) {
190  static $types = null;
191  if ( $types !== null ) {
192  return $types;
193  }
194  $types = [
195  'block',
196  'newusers',
197  'rights',
198  'renameuser',
199  ];
200 
201  ( $runner ?? Hooks::runner() )->onGetLogTypesOnUser( $types );
202  return $types;
203  }
204 
210  public function getSubpagesForPrefixSearch() {
211  $subpages = LogPage::validTypes();
212  $subpages[] = 'all';
213  sort( $subpages );
214  return $subpages;
215  }
216 
226  private function parseParams( FormOptions $opts, $par ) {
227  # Get parameters
228  $par ??= '';
229  $parms = explode( '/', $par );
230  $symsForAll = [ '*', 'all' ];
231  if ( $parms[0] != '' &&
232  ( in_array( $par, LogPage::validTypes() ) || in_array( $par, $symsForAll ) )
233  ) {
234  $opts->setValue( 'type', $par );
235  } elseif ( count( $parms ) == 2 ) {
236  $opts->setValue( 'type', $parms[0] );
237  $opts->setValue( 'user', $parms[1] );
238  } elseif ( $par != '' ) {
239  $opts->setValue( 'user', $par );
240  }
241  }
242 
243  private function show( FormOptions $opts, array $extraConds ) {
244  # Create a LogPager item to get the results and a LogEventsList item to format them...
245  $loglist = new LogEventsList(
246  $this->getContext(),
247  $this->getLinkRenderer(),
249  );
250  $pager = new LogPager(
251  $loglist,
252  $opts->getValue( 'type' ),
253  $opts->getValue( 'user' ),
254  $opts->getValue( 'page' ),
255  $opts->getValue( 'pattern' ),
256  $extraConds,
257  $opts->getValue( 'year' ),
258  $opts->getValue( 'month' ),
259  $opts->getValue( 'day' ),
260  $opts->getValue( 'tagfilter' ),
261  $opts->getValue( 'subtype' ),
262  $opts->getValue( 'logid' ),
263  $this->linkBatchFactory,
264  $this->loadBalancer,
265  $this->actorNormalization,
266  $opts->getValue( 'tagInvert' )
267  );
268 
269  $this->addHeader( $opts->getValue( 'type' ) );
270 
271  # Set relevant user
272  $performer = $pager->getPerformer();
273  if ( $performer ) {
274  $performerUser = $this->userIdentityLookup->getUserIdentityByName( $performer );
275  if ( $performerUser ) {
276  $this->getSkin()->setRelevantUser( $performerUser );
277  }
278  }
279 
280  # Show form options
281  $loglist->showOptions(
282  $pager->getType(),
283  $performer,
284  $pager->getPage(),
285  $pager->getPattern(),
286  $pager->getYear(),
287  $pager->getMonth(),
288  $pager->getDay(),
289  $pager->getFilterParams(),
290  $pager->getTagFilter(),
291  $pager->getAction(),
292  [
293  'offender' => $opts->getValue( 'offender' ),
294  ],
295  $pager->getTagInvert()
296  );
297 
298  # Insert list
299  $logBody = $pager->getBody();
300  if ( $logBody ) {
301  $this->getOutput()->addHTML(
302  $pager->getNavigationBar() .
303  $this->getActionButtons(
304  $loglist->beginLogEventsList() .
305  $logBody .
306  $loglist->endLogEventsList()
307  ) .
308  $pager->getNavigationBar()
309  );
310  } else {
311  $this->getOutput()->addWikiMsg( 'logempty' );
312  }
313  }
314 
315  private function getActionButtons( $formcontents ) {
316  $canRevDelete = $this->getAuthority()
317  ->isAllowedAll( 'deletedhistory', 'deletelogentry' );
318  $showTagEditUI = ChangeTags::showTagEditingUI( $this->getAuthority() );
319  # If the user doesn't have the ability to delete log entries nor edit tags,
320  # don't bother showing them the button(s).
321  if ( !$canRevDelete && !$showTagEditUI ) {
322  return $formcontents;
323  }
324 
325  # Show button to hide log entries and/or edit change tags
326  $s = Html::openElement(
327  'form',
328  [ 'action' => wfScript(), 'id' => 'mw-log-deleterevision-submit' ]
329  ) . "\n";
330  $s .= Html::hidden( 'action', 'historysubmit' ) . "\n";
331  $s .= Html::hidden( 'type', 'logging' ) . "\n";
332 
333  // If no title is set, the fallback is to use the main page, as defined
334  // by MediaWiki:Mainpage
335  // On wikis where the main page can be translated, MediaWiki:Mainpage
336  // is sometimes set to use Special:MyLanguage to redirect to the
337  // appropriate version. This is interpreted as a special page, and
338  // Action::getActionName forces the action to be 'view' if the title
339  // cannot be used as a WikiPage, which includes all pages in NS_SPECIAL.
340  // Set a dummy title to avoid this. The title provided is unused
341  // by the SpecialPageAction class and does not matter.
342  // See T205908
343  $s .= Html::hidden( 'title', 'Unused' ) . "\n";
344 
345  $buttons = '';
346  if ( $canRevDelete ) {
347  $buttons .= Html::element(
348  'button',
349  [
350  'type' => 'submit',
351  'name' => 'revisiondelete',
352  'value' => '1',
353  'class' => "deleterevision-log-submit mw-log-deleterevision-button mw-ui-button"
354  ],
355  $this->msg( 'showhideselectedlogentries' )->text()
356  ) . "\n";
357  }
358  if ( $showTagEditUI ) {
359  $buttons .= Html::element(
360  'button',
361  [
362  'type' => 'submit',
363  'name' => 'editchangetags',
364  'value' => '1',
365  'class' => "editchangetags-log-submit mw-log-editchangetags-button mw-ui-button"
366  ],
367  $this->msg( 'log-edit-tags' )->text()
368  ) . "\n";
369  }
370 
371  $buttons .= ( new ListToggle( $this->getOutput() ) )->getHTML();
372 
373  $s .= $buttons . $formcontents . $buttons;
374  $s .= Html::closeElement( 'form' );
375 
376  return $s;
377  }
378 
384  protected function addHeader( $type ) {
385  $page = new LogPage( $type );
386  $this->getOutput()->setPageTitle( $page->getName() );
387  $this->getOutput()->addHTML( $page->getDescription()
388  ->setContext( $this->getContext() )->parseAsBlock() );
389  }
390 
391  protected function getGroupName() {
392  return 'changes';
393  }
394 }
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.
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition: Hooks.php:170
const USE_CHECKBOXES
Class to simplify the use of log pages.
Definition: LogPage.php:41
static isLogType( $type)
Is $type a valid log type.
Definition: LogPage.php:221
static validTypes()
Get the list of valid log types.
Definition: LogPage.php:209
static getInstance( $ts=false)
Get a timestamp instance in GMT.
Definition: MWTimestamp.php:47
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Definition: HookRunner.php:568
Helper class to keep track of options when mixing links and form elements.
Definition: FormOptions.php:41
fetchValuesFromRequest(WebRequest $r, $optionKeys=null)
Fetch values for all options (or selected options) from the given WebRequest, making them available f...
add( $name, $default, $type=self::AUTO)
Add an option to be handled by this FormOptions instance.
Definition: FormOptions.php:89
getValue( $name)
Get the value for the given option name.
setValue( $name, $value, $force=false)
Use to set the value of an option.
This class is a collection of static functions that serve two purposes:
Definition: Html.php:55
Class for generating clickable toggle links for a list of checkboxes.
Definition: ListToggle.php:35
A class containing constants representing the names of configuration variables.
Represents a title within MediaWiki.
Definition: Title.php:82
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.
Definition: SpecialLog.php:42
getSubpagesForPrefixSearch()
Return an array of subpages that this special page will accept.
Definition: SpecialLog.php:210
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:189
execute( $par)
Default execute method Checks user permissions.
Definition: SpecialLog.php:75
__construct(LinkBatchFactory $linkBatchFactory, ILoadBalancer $loadBalancer, ActorNormalization $actorNormalization, UserIdentityLookup $userIdentityLookup)
Definition: SpecialLog.php:62
addHeader( $type)
Set page title and show header for this log type.
Definition: SpecialLog.php:384
getGroupName()
Under which header this special page is listed in Special:SpecialPages See messages 'specialpages-gro...
Definition: SpecialLog.php:391
Parent class for all special pages.
Definition: SpecialPage.php:45
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.
$runner
Service for dealing with the actor table.
This class is a delegate to ILBFactory for a given database cluster.
foreach( $mmfl['setupFiles'] as $fileName) if( $queue) if(empty( $mmfl['quiet'])) $s
const DB_REPLICA
Definition: defines.php:26