3 * Contain classes to list log entries
5 * Copyright © 2004 Brion Vibber <brion@pobox.com>, 2008 Aaron Schulz
6 * https://www.mediawiki.org/
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
26 use MediaWiki\MediaWikiServices
;
28 class LogEventsList
extends ContextSource
{
29 const NO_ACTION_LINK
= 1;
30 const NO_EXTRA_USER_LINKS
= 2;
31 const USE_CHECKBOXES
= 4;
38 protected $mDefaultQuery;
43 protected $showTagEditUI;
48 protected $allowedActions = null;
52 * The first two parameters used to be $skin and $out, but now only a context
53 * is needed, that's why there's a second unused parameter.
55 * @param IContextSource|Skin $context Context to use; formerly it was
56 * a Skin object. Use of Skin is deprecated.
57 * @param null $unused Unused; used to be an OutputPage object.
58 * @param int $flags Can be a combination of self::NO_ACTION_LINK,
59 * self::NO_EXTRA_USER_LINKS or self::USE_CHECKBOXES.
61 public function __construct( $context, $unused = null, $flags = 0 ) {
62 if ( $context instanceof IContextSource
) {
63 $this->setContext( $context );
65 // Old parameters, $context should be a Skin object
66 $this->setContext( $context->getContext() );
69 $this->flags
= $flags;
70 $this->showTagEditUI
= ChangeTags
::showTagEditingUI( $this->getUser() );
74 * Show options for the log list
76 * @param array|string $types
79 * @param string $pattern
80 * @param int $year Year
81 * @param int $month Month
82 * @param array $filter
83 * @param string $tagFilter Tag to select by default
84 * @param string $action
86 public function showOptions( $types = [], $user = '', $page = '', $pattern = '', $year = 0,
87 $month = 0, $filter = null, $tagFilter = '', $action = null
89 global $wgScript, $wgMiserMode;
91 $title = SpecialPage
::getTitleFor( 'Log' );
93 // For B/C, we take strings, but make sure they are converted...
94 $types = ( $types === '' ) ?
[] : (array)$types;
96 $tagSelector = ChangeTags
::buildTagFilterSelector( $tagFilter, false, $this->getContext() );
98 $html = Html
::hidden( 'title', $title->getPrefixedDBkey() );
101 $html .= $this->getTypeMenu( $types ) . "\n";
102 $html .= $this->getUserInput( $user ) . "\n";
103 $html .= $this->getTitleInput( $page ) . "\n";
104 $html .= $this->getExtraInputs( $types ) . "\n";
106 // Title pattern, if allowed
107 if ( !$wgMiserMode ) {
108 $html .= $this->getTitlePattern( $pattern ) . "\n";
112 $html .= Xml
::tags( 'p', null, Xml
::dateMenu( (int)$year, (int)$month ) );
115 if ( $tagSelector ) {
116 $html .= Xml
::tags( 'p', null, implode( ' ', $tagSelector ) );
121 $html .= Xml
::tags( 'p', null, $this->getFilterLinks( $filter ) );
125 if ( $action !== null ) {
126 $html .= Xml
::tags( 'p', null, $this->getActionSelector( $types, $action ) );
130 $html .= Xml
::submitButton( $this->msg( 'logeventslist-submit' )->text() );
133 $html = Xml
::fieldset( $this->msg( 'log' )->text(), $html );
136 $html = Xml
::tags( 'form', [ 'action' => $wgScript, 'method' => 'get' ], $html );
138 $this->getOutput()->addHTML( $html );
142 * @param array $filter
143 * @return string Formatted HTML
145 private function getFilterLinks( $filter ) {
147 $messages = [ $this->msg( 'show' )->text(), $this->msg( 'hide' )->text() ];
148 // Option value -> message mapping
150 $hiddens = ''; // keep track for "go" button
151 $linkRenderer = MediaWikiServices
::getInstance()->getLinkRenderer();
152 foreach ( $filter as $type => $val ) {
153 // Should the below assignment be outside the foreach?
154 // Then it would have to be copied. Not certain what is more expensive.
155 $query = $this->getDefaultQuery();
156 $queryKey = "hide_{$type}_log";
158 $hideVal = 1 - intval( $val );
159 $query[$queryKey] = $hideVal;
161 $link = $linkRenderer->makeKnownLink(
168 // Message: log-show-hide-patrol
169 $links[$type] = $this->msg( "log-show-hide-{$type}" )->rawParams( $link )->escaped();
170 $hiddens .= Html
::hidden( "hide_{$type}_log", $val ) . "\n";
174 return '<small>' . $this->getLanguage()->pipeList( $links ) . '</small>' . $hiddens;
177 private function getDefaultQuery() {
178 if ( !isset( $this->mDefaultQuery
) ) {
179 $this->mDefaultQuery
= $this->getRequest()->getQueryValues();
180 unset( $this->mDefaultQuery
['title'] );
181 unset( $this->mDefaultQuery
['dir'] );
182 unset( $this->mDefaultQuery
['offset'] );
183 unset( $this->mDefaultQuery
['limit'] );
184 unset( $this->mDefaultQuery
['order'] );
185 unset( $this->mDefaultQuery
['month'] );
186 unset( $this->mDefaultQuery
['year'] );
189 return $this->mDefaultQuery
;
193 * @param array $queryTypes
194 * @return string Formatted HTML
196 private function getTypeMenu( $queryTypes ) {
197 $queryType = count( $queryTypes ) == 1 ?
$queryTypes[0] : '';
198 $selector = $this->getTypeSelector();
199 $selector->setDefault( $queryType );
201 return $selector->getHTML();
205 * Returns log page selector.
209 public function getTypeSelector() {
210 $typesByName = []; // Temporary array
211 // First pass to load the log names
212 foreach ( LogPage
::validTypes() as $type ) {
213 $page = new LogPage( $type );
214 $restriction = $page->getRestriction();
215 if ( $this->getUser()->isAllowed( $restriction ) ) {
216 $typesByName[$type] = $page->getName()->text();
220 // Second pass to sort by name
221 asort( $typesByName );
223 // Always put "All public logs" on top
224 $public = $typesByName[''];
225 unset( $typesByName[''] );
226 $typesByName = [ '' => $public ] +
$typesByName;
228 $select = new XmlSelect( 'type' );
229 foreach ( $typesByName as $type => $name ) {
230 $select->addOption( $name, $type );
237 * @param string $user
238 * @return string Formatted HTML
240 private function getUserInput( $user ) {
241 $label = Xml
::inputLabel(
242 $this->msg( 'specialloguserlabel' )->text(),
247 [ 'class' => 'mw-autocomplete-user' ]
250 return '<span class="mw-input-with-label">' . $label . '</span>';
254 * @param string $title
255 * @return string Formatted HTML
257 private function getTitleInput( $title ) {
258 $label = Xml
::inputLabel(
259 $this->msg( 'speciallogtitlelabel' )->text(),
266 return '<span class="mw-input-with-label">' . $label . '</span>';
270 * @param string $pattern
271 * @return string Checkbox
273 private function getTitlePattern( $pattern ) {
274 return '<span class="mw-input-with-label">' .
275 Xml
::checkLabel( $this->msg( 'log-title-wildcard' )->text(), 'pattern', 'pattern', $pattern ) .
280 * @param array $types
283 private function getExtraInputs( $types ) {
284 if ( count( $types ) == 1 ) {
285 if ( $types[0] == 'suppress' ) {
286 $offender = $this->getRequest()->getVal( 'offender' );
287 $user = User
::newFromName( $offender, false );
288 if ( !$user ||
( $user->getId() == 0 && !IP
::isIPAddress( $offender ) ) ) {
289 $offender = ''; // Blank field if invalid
291 return Xml
::inputLabel( $this->msg( 'revdelete-offender' )->text(), 'offender',
292 'mw-log-offender', 20, $offender );
294 // Allow extensions to add their own extra inputs
296 Hooks
::run( 'LogEventsListGetExtraInputs', [ $types[0], $this, &$input ] );
305 * Drop down menu for selection of actions that can be used to filter the log
306 * @param array $types
307 * @param string $action
311 private function getActionSelector( $types, $action ) {
312 if ( $this->allowedActions
=== null ||
!count( $this->allowedActions
) ) {
316 $html .= Xml
::label( wfMessage( 'log-action-filter-' . $types[0] )->text(),
317 'action-filter-' .$types[0] ) . "\n";
318 $select = new XmlSelect( 'subtype' );
319 $select->addOption( wfMessage( 'log-action-filter-all' )->text(), '' );
320 foreach ( $this->allowedActions
as $value ) {
321 $msgKey = 'log-action-filter-' . $types[0] . '-' . $value;
322 $select->addOption( wfMessage( $msgKey )->text(), $value );
324 $select->setDefault( $action );
325 $html .= $select->getHTML();
330 * Sets the action types allowed for log filtering
331 * To one action type may correspond several log_actions
332 * @param array $actions
335 public function setAllowedActions( $actions ) {
336 $this->allowedActions
= $actions;
342 public function beginLogEventsList() {
349 public function endLogEventsList() {
354 * @param stdClass $row A single row from the result set
355 * @return string Formatted HTML list item
357 public function logLine( $row ) {
358 $entry = DatabaseLogEntry
::newFromRow( $row );
359 $formatter = LogFormatter
::newFromEntry( $entry );
360 $formatter->setContext( $this->getContext() );
361 $formatter->setShowUserToolLinks( !( $this->flags
& self
::NO_EXTRA_USER_LINKS
) );
363 $time = htmlspecialchars( $this->getLanguage()->userTimeAndDate(
364 $entry->getTimestamp(), $this->getUser() ) );
366 $action = $formatter->getActionText();
368 if ( $this->flags
& self
::NO_ACTION_LINK
) {
371 $revert = $formatter->getActionLinks();
372 if ( $revert != '' ) {
373 $revert = '<span class="mw-logevent-actionlink">' . $revert . '</span>';
377 $comment = $formatter->getComment();
379 // Some user can hide log items and have review links
380 $del = $this->getShowHideLinks( $row );
383 list( $tagDisplay, $newClasses ) = ChangeTags
::formatSummaryRow(
388 $classes = array_merge(
389 [ 'mw-logline-' . $entry->getType() ],
393 return Html
::rawElement( 'li', [ 'class' => $classes ],
394 "$del $time $action $comment $revert $tagDisplay" ) . "\n";
398 * @param stdClass $row Row
401 private function getShowHideLinks( $row ) {
402 // We don't want to see the links and
403 if ( $this->flags
== self
::NO_ACTION_LINK
) {
407 $user = $this->getUser();
409 // If change tag editing is available to this user, return the checkbox
410 if ( $this->flags
& self
::USE_CHECKBOXES
&& $this->showTagEditUI
) {
414 [ 'name' => 'ids[' . $row->log_id
. ']' ]
418 // no one can hide items from the suppress log.
419 if ( $row->log_type
== 'suppress' ) {
424 // Don't show useless checkbox to people who cannot hide log entries
425 if ( $user->isAllowed( 'deletedhistory' ) ) {
426 $canHide = $user->isAllowed( 'deletelogentry' );
427 $canViewSuppressedOnly = $user->isAllowed( 'viewsuppressed' ) &&
428 !$user->isAllowed( 'suppressrevision' );
429 $entryIsSuppressed = self
::isDeleted( $row, LogPage
::DELETED_RESTRICTED
);
430 $canViewThisSuppressedEntry = $canViewSuppressedOnly && $entryIsSuppressed;
431 if ( $row->log_deleted ||
$canHide ) {
432 // Show checkboxes instead of links.
433 if ( $canHide && $this->flags
& self
::USE_CHECKBOXES
&& !$canViewThisSuppressedEntry ) {
434 // If event was hidden from sysops
435 if ( !self
::userCan( $row, LogPage
::DELETED_RESTRICTED
, $user ) ) {
436 $del = Xml
::check( 'deleterevisions', false, [ 'disabled' => 'disabled' ] );
441 [ 'name' => 'ids[' . $row->log_id
. ']' ]
445 // If event was hidden from sysops
446 if ( !self
::userCan( $row, LogPage
::DELETED_RESTRICTED
, $user ) ) {
447 $del = Linker
::revDeleteLinkDisabled( $canHide );
450 'target' => SpecialPage
::getTitleFor( 'Log', $row->log_type
)->getPrefixedDBkey(),
452 'ids' => $row->log_id
,
454 $del = Linker
::revDeleteLink(
457 $canHide && !$canViewThisSuppressedEntry
468 * @param stdClass $row Row
469 * @param string|array $type
470 * @param string|array $action
471 * @param string $right
474 public static function typeAction( $row, $type, $action, $right = '' ) {
475 $match = is_array( $type ) ?
476 in_array( $row->log_type
, $type ) : $row->log_type
== $type;
478 $match = is_array( $action ) ?
479 in_array( $row->log_action
, $action ) : $row->log_action
== $action;
480 if ( $match && $right ) {
482 $match = $wgUser->isAllowed( $right );
490 * Determine if the current user is allowed to view a particular
491 * field of this log row, if it's marked as deleted.
493 * @param stdClass $row Row
495 * @param User $user User to check, or null to use $wgUser
498 public static function userCan( $row, $field, User
$user = null ) {
499 return self
::userCanBitfield( $row->log_deleted
, $field, $user );
503 * Determine if the current user is allowed to view a particular
504 * field of this log row, if it's marked as deleted.
506 * @param int $bitfield Current field
508 * @param User $user User to check, or null to use $wgUser
511 public static function userCanBitfield( $bitfield, $field, User
$user = null ) {
512 if ( $bitfield & $field ) {
513 if ( $user === null ) {
517 if ( $bitfield & LogPage
::DELETED_RESTRICTED
) {
518 $permissions = [ 'suppressrevision', 'viewsuppressed' ];
520 $permissions = [ 'deletedhistory' ];
522 $permissionlist = implode( ', ', $permissions );
523 wfDebug( "Checking for $permissionlist due to $field match on $bitfield\n" );
524 return call_user_func_array( [ $user, 'isAllowedAny' ], $permissions );
530 * @param stdClass $row Row
531 * @param int $field One of DELETED_* bitfield constants
534 public static function isDeleted( $row, $field ) {
535 return ( $row->log_deleted
& $field ) == $field;
539 * Show log extract. Either with text and a box (set $msgKey) or without (don't set $msgKey)
541 * @param OutputPage|string $out By-reference
542 * @param string|array $types Log types to show
543 * @param string|Title $page The page title to show log entries for
544 * @param string $user The user who made the log entries
545 * @param array $param Associative Array with the following additional options:
546 * - lim Integer Limit of items to show, default is 50
547 * - conds Array Extra conditions for the query (e.g. "log_action != 'revision'")
548 * - showIfEmpty boolean Set to false if you don't want any output in case the loglist is empty
549 * if set to true (default), "No matching items in log" is displayed if loglist is empty
550 * - msgKey Array If you want a nice box with a message, set this to the key of the message.
551 * First element is the message key, additional optional elements are parameters for the key
552 * that are processed with wfMessage
553 * - offset Set to overwrite offset parameter in WebRequest
554 * set to '' to unset offset
555 * - wrap String Wrap the message in html (usually something like "<div ...>$1</div>").
556 * - flags Integer display flags (NO_ACTION_LINK,NO_EXTRA_USER_LINKS)
557 * - useRequestParams boolean Set true to use Pager-related parameters in the WebRequest
558 * - useMaster boolean Use master DB
559 * - extraUrlParams array|bool Additional url parameters for "full log" link (if it is shown)
560 * @return int Number of total log items (not limited by $lim)
562 public static function showLogExtract(
563 &$out, $types = [], $page = '', $user = '', $param = []
565 $defaultParameters = [
568 'showIfEmpty' => true,
572 'useRequestParams' => false,
573 'useMaster' => false,
574 'extraUrlParams' => false,
576 # The + operator appends elements of remaining keys from the right
577 # handed array to the left handed, whereas duplicated keys are NOT overwritten.
578 $param +
= $defaultParameters;
579 # Convert $param array to individual variables
580 $lim = $param['lim'];
581 $conds = $param['conds'];
582 $showIfEmpty = $param['showIfEmpty'];
583 $msgKey = $param['msgKey'];
584 $wrap = $param['wrap'];
585 $flags = $param['flags'];
586 $extraUrlParams = $param['extraUrlParams'];
588 $useRequestParams = $param['useRequestParams'];
589 if ( !is_array( $msgKey ) ) {
590 $msgKey = [ $msgKey ];
593 if ( $out instanceof OutputPage
) {
594 $context = $out->getContext();
596 $context = RequestContext
::getMain();
599 # Insert list of top 50 (or top $lim) items
600 $loglist = new LogEventsList( $context, null, $flags );
601 $pager = new LogPager( $loglist, $types, $user, $page, '', $conds );
602 if ( !$useRequestParams ) {
603 # Reset vars that may have been taken from the request
605 $pager->mDefaultLimit
= 50;
606 $pager->mOffset
= "";
607 $pager->mIsBackwards
= false;
610 if ( $param['useMaster'] ) {
611 $pager->mDb
= wfGetDB( DB_MASTER
);
613 if ( isset( $param['offset'] ) ) { # Tell pager to ignore WebRequest offset
614 $pager->setOffset( $param['offset'] );
618 $pager->mLimit
= $lim;
620 // Fetch the log rows and build the HTML if needed
621 $logBody = $pager->getBody();
622 $numRows = $pager->getNumRows();
628 $dir = $context->getLanguage()->getDir();
629 $lang = $context->getLanguage()->getHtmlCode();
631 $s = Xml
::openElement( 'div', [
632 'class' => "mw-warning-with-logexcerpt mw-content-$dir",
637 if ( count( $msgKey ) == 1 ) {
638 $s .= $context->msg( $msgKey[0] )->parseAsBlock();
639 } else { // Process additional arguments
641 array_shift( $args );
642 $s .= $context->msg( $msgKey[0], $args )->parseAsBlock();
645 $s .= $loglist->beginLogEventsList() .
647 $loglist->endLogEventsList();
648 } elseif ( $showIfEmpty ) {
649 $s = Html
::rawElement( 'div', [ 'class' => 'mw-warning-logempty' ],
650 $context->msg( 'logempty' )->parse() );
653 if ( $numRows > $pager->mLimit
) { # Show "Full log" link
655 if ( $page instanceof Title
) {
656 $urlParam['page'] = $page->getPrefixedDBkey();
657 } elseif ( $page != '' ) {
658 $urlParam['page'] = $page;
662 $urlParam['user'] = $user;
665 if ( !is_array( $types ) ) { # Make it an array, if it isn't
669 # If there is exactly one log type, we can link to Special:Log?type=foo
670 if ( count( $types ) == 1 ) {
671 $urlParam['type'] = $types[0];
674 if ( $extraUrlParams !== false ) {
675 $urlParam = array_merge( $urlParam, $extraUrlParams );
678 $s .= MediaWikiServices
::getInstance()->getLinkRenderer()->makeKnownLink(
679 SpecialPage
::getTitleFor( 'Log' ),
680 $context->msg( 'log-fulllog' )->text(),
686 if ( $logBody && $msgKey[0] ) {
690 if ( $wrap != '' ) { // Wrap message in html
691 $s = str_replace( '$1', $s, $wrap );
694 /* hook can return false, if we don't want the message to be emitted (Wikia BugId:7093) */
695 if ( Hooks
::run( 'LogEventsListShowLogExtract', [ &$s, $types, $page, $user, $param ] ) ) {
696 // $out can be either an OutputPage object or a String-by-reference
697 if ( $out instanceof OutputPage
) {
708 * SQL clause to skip forbidden log types for this user
710 * @param IDatabase $db
711 * @param string $audience Public/user
712 * @param User $user User to check, or null to use $wgUser
713 * @return string|bool String on success, false on failure.
715 public static function getExcludeClause( $db, $audience = 'public', User
$user = null ) {
716 global $wgLogRestrictions;
718 if ( $audience != 'public' && $user === null ) {
723 // Reset the array, clears extra "where" clauses when $par is used
726 // Don't show private logs to unprivileged users
727 foreach ( $wgLogRestrictions as $logType => $right ) {
728 if ( $audience == 'public' ||
!$user->isAllowed( $right ) ) {
729 $hiddenLogs[] = $logType;
732 if ( count( $hiddenLogs ) == 1 ) {
733 return 'log_type != ' . $db->addQuotes( $hiddenLogs[0] );
734 } elseif ( $hiddenLogs ) {
735 return 'log_type NOT IN (' . $db->makeList( $hiddenLogs ) . ')';