MediaWiki  master
SpecialLog.php
Go to the documentation of this file.
1 <?php
26 
32 class SpecialLog extends SpecialPage {
33  public function __construct() {
34  parent::__construct( 'Log' );
35  }
36 
37  public function execute( $par ) {
38  $this->setHeaders();
39  $this->outputHeader();
40  $out = $this->getOutput();
41  $out->addModules( 'mediawiki.userSuggest' );
42  $out->addModuleStyles( 'mediawiki.interface.helpers.styles' );
43  $this->addHelpLink( 'Help:Log' );
44 
45  $opts = new FormOptions;
46  $opts->add( 'type', '' );
47  $opts->add( 'user', '' );
48  $opts->add( 'page', '' );
49  $opts->add( 'pattern', false );
50  $opts->add( 'year', null, FormOptions::INTNULL );
51  $opts->add( 'month', null, FormOptions::INTNULL );
52  $opts->add( 'day', null, FormOptions::INTNULL );
53  $opts->add( 'tagfilter', '' );
54  $opts->add( 'offset', '' );
55  $opts->add( 'dir', '' );
56  $opts->add( 'offender', '' );
57  $opts->add( 'subtype', '' );
58  $opts->add( 'logid', '' );
59 
60  // Set values
61  $opts->fetchValuesFromRequest( $this->getRequest() );
62  if ( $par !== null ) {
63  $this->parseParams( $opts, (string)$par );
64  }
65 
66  // Set date values
67  $dateString = $this->getRequest()->getVal( 'wpdate' );
68  if ( !empty( $dateString ) ) {
69  try {
70  $dateStamp = MWTimestamp::getInstance( $dateString . ' 00:00:00' );
71  } catch ( TimestampException $e ) {
72  // If users provide an invalid date, silently ignore it
73  // instead of letting an exception bubble up (T201411)
74  $dateStamp = false;
75  }
76  if ( $dateStamp ) {
77  $opts->setValue( 'year', (int)$dateStamp->format( 'Y' ) );
78  $opts->setValue( 'month', (int)$dateStamp->format( 'm' ) );
79  $opts->setValue( 'day', (int)$dateStamp->format( 'd' ) );
80  }
81  }
82 
83  # Don't let the user get stuck with a certain date
84  if ( $opts->getValue( 'offset' ) || $opts->getValue( 'dir' ) == 'prev' ) {
85  $opts->setValue( 'year', '' );
86  $opts->setValue( 'month', '' );
87  }
88 
89  // If the user doesn't have the right permission to view the specific
90  // log type, throw a PermissionsError
91  // If the log type is invalid, just show all public logs
92  $logRestrictions = $this->getConfig()->get( 'LogRestrictions' );
93  $type = $opts->getValue( 'type' );
94  if ( !LogPage::isLogType( $type ) ) {
95  $opts->setValue( 'type', '' );
96  } elseif ( isset( $logRestrictions[$type] )
97  && !MediaWikiServices::getInstance()
99  ->userHasRight( $this->getUser(), $logRestrictions[$type] )
100  ) {
101  throw new PermissionsError( $logRestrictions[$type] );
102  }
103 
104  # Handle type-specific inputs
105  $qc = [];
106  if ( $opts->getValue( 'type' ) == 'suppress' ) {
107  $offenderName = $opts->getValue( 'offender' );
108  $offender = empty( $offenderName ) ? null : User::newFromName( $offenderName, false );
109  if ( $offender ) {
110  $qc = [ 'ls_field' => 'target_author_actor', 'ls_value' => $offender->getActorId() ];
111  }
112  } else {
113  // Allow extensions to add relations to their search types
114  Hooks::run(
115  'SpecialLogAddLogSearchRelations',
116  [ $opts->getValue( 'type' ), $this->getRequest(), &$qc ]
117  );
118  }
119 
120  # Some log types are only for a 'User:' title but we might have been given
121  # only the username instead of the full title 'User:username'. This part try
122  # to lookup for a user by that name and eventually fix user input. See T3697.
123  if ( in_array( $opts->getValue( 'type' ), self::getLogTypesOnUser() ) ) {
124  # ok we have a type of log which expect a user title.
125  $target = Title::newFromText( $opts->getValue( 'page' ) );
126  if ( $target && $target->getNamespace() === NS_MAIN ) {
127  # User forgot to add 'User:', we are adding it for him
128  $opts->setValue( 'page',
129  Title::makeTitleSafe( NS_USER, $opts->getValue( 'page' ) )
130  );
131  }
132  }
133 
134  $this->show( $opts, $qc );
135  }
136 
145  public static function getLogTypesOnUser() {
146  static $types = null;
147  if ( $types !== null ) {
148  return $types;
149  }
150  $types = [
151  'block',
152  'newusers',
153  'rights',
154  ];
155 
156  Hooks::run( 'GetLogTypesOnUser', [ &$types ] );
157  return $types;
158  }
159 
165  public function getSubpagesForPrefixSearch() {
166  $subpages = LogPage::validTypes();
167  $subpages[] = 'all';
168  sort( $subpages );
169  return $subpages;
170  }
171 
181  private function parseParams( FormOptions $opts, $par ) {
182  # Get parameters
183  $par = $par ?? '';
184  $parms = explode( '/', $par );
185  $symsForAll = [ '*', 'all' ];
186  if ( $parms[0] != '' &&
187  ( in_array( $par, LogPage::validTypes() ) || in_array( $par, $symsForAll ) )
188  ) {
189  $opts->setValue( 'type', $par );
190  } elseif ( count( $parms ) == 2 ) {
191  $opts->setValue( 'type', $parms[0] );
192  $opts->setValue( 'user', $parms[1] );
193  } elseif ( $par != '' ) {
194  $opts->setValue( 'user', $par );
195  }
196  }
197 
198  private function show( FormOptions $opts, array $extraConds ) {
199  # Create a LogPager item to get the results and a LogEventsList item to format them...
200  $loglist = new LogEventsList(
201  $this->getContext(),
202  $this->getLinkRenderer(),
204  );
205 
206  $pager = new LogPager(
207  $loglist,
208  $opts->getValue( 'type' ),
209  $opts->getValue( 'user' ),
210  $opts->getValue( 'page' ),
211  $opts->getValue( 'pattern' ),
212  $extraConds,
213  $opts->getValue( 'year' ),
214  $opts->getValue( 'month' ),
215  $opts->getValue( 'day' ),
216  $opts->getValue( 'tagfilter' ),
217  $opts->getValue( 'subtype' ),
218  $opts->getValue( 'logid' )
219  );
220 
221  $this->addHeader( $opts->getValue( 'type' ) );
222 
223  # Set relevant user
224  if ( $pager->getPerformer() ) {
225  $performerUser = User::newFromName( $pager->getPerformer(), false );
226  $this->getSkin()->setRelevantUser( $performerUser );
227  }
228 
229  # Show form options
230  $loglist->showOptions(
231  $pager->getType(),
232  $pager->getPerformer(),
233  $pager->getPage(),
234  $pager->getPattern(),
235  $pager->getYear(),
236  $pager->getMonth(),
237  $pager->getDay(),
238  $pager->getFilterParams(),
239  $pager->getTagFilter(),
240  $pager->getAction()
241  );
242 
243  # Insert list
244  $logBody = $pager->getBody();
245  if ( $logBody ) {
246  $this->getOutput()->addHTML(
247  $pager->getNavigationBar() .
248  $this->getActionButtons(
249  $loglist->beginLogEventsList() .
250  $logBody .
251  $loglist->endLogEventsList()
252  ) .
253  $pager->getNavigationBar()
254  );
255  } else {
256  $this->getOutput()->addWikiMsg( 'logempty' );
257  }
258  }
259 
260  private function getActionButtons( $formcontents ) {
261  $user = $this->getUser();
262  $canRevDelete = MediaWikiServices::getInstance()
263  ->getPermissionManager()
264  ->userHasAllRights( $user, 'deletedhistory', 'deletelogentry' );
265  $showTagEditUI = ChangeTags::showTagEditingUI( $user );
266  # If the user doesn't have the ability to delete log entries nor edit tags,
267  # don't bother showing them the button(s).
268  if ( !$canRevDelete && !$showTagEditUI ) {
269  return $formcontents;
270  }
271 
272  # Show button to hide log entries and/or edit change tags
274  'form',
275  [ 'action' => wfScript(), 'id' => 'mw-log-deleterevision-submit' ]
276  ) . "\n";
277  $s .= Html::hidden( 'action', 'historysubmit' ) . "\n";
278  $s .= Html::hidden( 'type', 'logging' ) . "\n";
279 
280  $buttons = '';
281  if ( $canRevDelete ) {
282  $buttons .= Html::element(
283  'button',
284  [
285  'type' => 'submit',
286  'name' => 'revisiondelete',
287  'value' => '1',
288  'class' => "deleterevision-log-submit mw-log-deleterevision-button"
289  ],
290  $this->msg( 'showhideselectedlogentries' )->text()
291  ) . "\n";
292  }
293  if ( $showTagEditUI ) {
294  $buttons .= Html::element(
295  'button',
296  [
297  'type' => 'submit',
298  'name' => 'editchangetags',
299  'value' => '1',
300  'class' => "editchangetags-log-submit mw-log-editchangetags-button"
301  ],
302  $this->msg( 'log-edit-tags' )->text()
303  ) . "\n";
304  }
305 
306  $buttons .= ( new ListToggle( $this->getOutput() ) )->getHTML();
307 
308  $s .= $buttons . $formcontents . $buttons;
309  $s .= Html::closeElement( 'form' );
310 
311  return $s;
312  }
313 
319  protected function addHeader( $type ) {
320  $page = new LogPage( $type );
321  $this->getOutput()->setPageTitle( $page->getName() );
322  $this->getOutput()->addHTML( $page->getDescription()
323  ->setContext( $this->getContext() )->parseAsBlock() );
324  }
325 
326  protected function getGroupName() {
327  return 'changes';
328  }
329 }
Class for generating clickable toggle links for a list of checkboxes.
Definition: ListToggle.php:31
Helper class to keep track of options when mixing links and form elements.
Definition: FormOptions.php:35
const USE_CHECKBOXES
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:231
getContext()
Gets the context this SpecialPage is executed in.
parseParams(FormOptions $opts, $par)
Set options based on the subpage title parts:
Definition: SpecialLog.php:181
const NS_MAIN
Definition: Defines.php:60
const INTNULL
Integer type or null, maps to WebRequest::getIntOrNull() This is useful for the namespace selector...
Definition: FormOptions.php:55
getSubpagesForPrefixSearch()
Return an array of subpages that this special page will accept.
Definition: SpecialLog.php:165
static openElement( $element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing &#39;/&#39;...
Definition: Html.php:251
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
getActionButtons( $formcontents)
Definition: SpecialLog.php:260
getOutput()
Get the OutputPage being used for this instance.
execute( $par)
Definition: SpecialLog.php:37
add( $name, $default, $type=self::AUTO)
Add an option to be handled by this FormOptions instance.
Definition: FormOptions.php:83
Class to simplify the use of log pages.
Definition: LogPage.php:33
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
static getInstance( $ts=false)
Get a timestamp instance in GMT.
Definition: MWTimestamp.php:39
getPermissionManager()
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getSkin()
Shortcut to get the skin being used for this instance.
static closeElement( $element)
Returns "</$element>".
Definition: Html.php:315
static showTagEditingUI(User $user)
Indicate whether change tag editing UI is relevant.
setHeaders()
Sets headers - this should be called from the execute() method of all derived classes! ...
A special page that lists log entries.
Definition: SpecialLog.php:32
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:612
static isLogType( $type)
Is $type a valid log type.
Definition: LogPage.php:210
setValue( $name, $value, $force=false)
Use to set the value of an option.
show(FormOptions $opts, array $extraConds)
Definition: SpecialLog.php:198
getUser()
Shortcut to get the User executing this instance.
static hidden( $name, $value, array $attribs=[])
Convenience function to produce an input element with type=hidden.
Definition: Html.php:802
static getLogTypesOnUser()
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:145
getConfig()
Shortcut to get main config object.
Show an error when a user tries to do something they do not have the necessary permissions for...
addHeader( $type)
Set page title and show header for this log type.
Definition: SpecialLog.php:319
getRequest()
Get the WebRequest being used for this instance.
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:519
static validTypes()
Get the list of valid log types.
Definition: LogPage.php:198
outputHeader( $summaryMessageKey='')
Outputs a summary message on top of special pages Per default the message key is the canonical name o...
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
getValue( $name)
Get the value for the given option name.
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:319