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
;
27 use Wikimedia\Rdbms\IDatabase
;
29 class LogEventsList
extends ContextSource
{
30 const NO_ACTION_LINK
= 1;
31 const NO_EXTRA_USER_LINKS
= 2;
32 const USE_CHECKBOXES
= 4;
39 protected $mDefaultQuery;
44 protected $showTagEditUI;
49 protected $allowedActions = null;
53 * The first two parameters used to be $skin and $out, but now only a context
54 * is needed, that's why there's a second unused parameter.
56 * @param IContextSource|Skin $context Context to use; formerly it was
57 * a Skin object. Use of Skin is deprecated.
58 * @param null $unused Unused; used to be an OutputPage object.
59 * @param int $flags Can be a combination of self::NO_ACTION_LINK,
60 * self::NO_EXTRA_USER_LINKS or self::USE_CHECKBOXES.
62 public function __construct( $context, $unused = null, $flags = 0 ) {
63 if ( $context instanceof IContextSource
) {
64 $this->setContext( $context );
66 // Old parameters, $context should be a Skin object
67 $this->setContext( $context->getContext() );
70 $this->flags
= $flags;
71 $this->showTagEditUI
= ChangeTags
::showTagEditingUI( $this->getUser() );
75 * Show options for the log list
77 * @param array|string $types
80 * @param string $pattern
81 * @param int $year Year
82 * @param int $month Month
83 * @param array $filter
84 * @param string $tagFilter Tag to select by default
85 * @param string $action
87 public function showOptions( $types = [], $user = '', $page = '', $pattern = '', $year = 0,
88 $month = 0, $filter = null, $tagFilter = '', $action = null
90 global $wgScript, $wgMiserMode;
92 $title = SpecialPage
::getTitleFor( 'Log' );
94 // For B/C, we take strings, but make sure they are converted...
95 $types = ( $types === '' ) ?
[] : (array)$types;
97 $tagSelector = ChangeTags
::buildTagFilterSelector( $tagFilter, false, $this->getContext() );
99 $html = Html
::hidden( 'title', $title->getPrefixedDBkey() );
102 $html .= $this->getTypeMenu( $types ) . "\n";
103 $html .= $this->getUserInput( $user ) . "\n";
104 $html .= $this->getTitleInput( $page ) . "\n";
105 $html .= $this->getExtraInputs( $types ) . "\n";
107 // Title pattern, if allowed
108 if ( !$wgMiserMode ) {
109 $html .= $this->getTitlePattern( $pattern ) . "\n";
113 $html .= Xml
::tags( 'p', null, Xml
::dateMenu( (int)$year, (int)$month ) );
116 if ( $tagSelector ) {
117 $html .= Xml
::tags( 'p', null, implode( ' ', $tagSelector ) );
122 $html .= Xml
::tags( 'p', null, $this->getFilterLinks( $filter ) );
126 if ( $action !== null ) {
127 $html .= Xml
::tags( 'p', null, $this->getActionSelector( $types, $action ) );
131 $html .= Xml
::submitButton( $this->msg( 'logeventslist-submit' )->text() );
134 $html = Xml
::fieldset( $this->msg( 'log' )->text(), $html );
137 $html = Xml
::tags( 'form', [ 'action' => $wgScript, 'method' => 'get' ], $html );
139 $this->getOutput()->addHTML( $html );
143 * @param array $filter
144 * @return string Formatted HTML
146 private function getFilterLinks( $filter ) {
148 $messages = [ $this->msg( 'show' )->text(), $this->msg( 'hide' )->text() ];
149 // Option value -> message mapping
151 $hiddens = ''; // keep track for "go" button
152 $linkRenderer = MediaWikiServices
::getInstance()->getLinkRenderer();
153 foreach ( $filter as $type => $val ) {
154 // Should the below assignment be outside the foreach?
155 // Then it would have to be copied. Not certain what is more expensive.
156 $query = $this->getDefaultQuery();
157 $queryKey = "hide_{$type}_log";
159 $hideVal = 1 - intval( $val );
160 $query[$queryKey] = $hideVal;
162 $link = $linkRenderer->makeKnownLink(
169 // Message: log-show-hide-patrol
170 $links[$type] = $this->msg( "log-show-hide-{$type}" )->rawParams( $link )->escaped();
171 $hiddens .= Html
::hidden( "hide_{$type}_log", $val ) . "\n";
175 return '<small>' . $this->getLanguage()->pipeList( $links ) . '</small>' . $hiddens;
178 private function getDefaultQuery() {
179 if ( !isset( $this->mDefaultQuery
) ) {
180 $this->mDefaultQuery
= $this->getRequest()->getQueryValues();
181 unset( $this->mDefaultQuery
['title'] );
182 unset( $this->mDefaultQuery
['dir'] );
183 unset( $this->mDefaultQuery
['offset'] );
184 unset( $this->mDefaultQuery
['limit'] );
185 unset( $this->mDefaultQuery
['order'] );
186 unset( $this->mDefaultQuery
['month'] );
187 unset( $this->mDefaultQuery
['year'] );
190 return $this->mDefaultQuery
;
194 * @param array $queryTypes
195 * @return string Formatted HTML
197 private function getTypeMenu( $queryTypes ) {
198 $queryType = count( $queryTypes ) == 1 ?
$queryTypes[0] : '';
199 $selector = $this->getTypeSelector();
200 $selector->setDefault( $queryType );
202 return $selector->getHTML();
206 * Returns log page selector.
210 public function getTypeSelector() {
211 $typesByName = []; // Temporary array
212 // First pass to load the log names
213 foreach ( LogPage
::validTypes() as $type ) {
214 $page = new LogPage( $type );
215 $restriction = $page->getRestriction();
216 if ( $this->getUser()->isAllowed( $restriction ) ) {
217 $typesByName[$type] = $page->getName()->text();
221 // Second pass to sort by name
222 asort( $typesByName );
224 // Always put "All public logs" on top
225 $public = $typesByName[''];
226 unset( $typesByName[''] );
227 $typesByName = [ '' => $public ] +
$typesByName;
229 $select = new XmlSelect( 'type' );
230 foreach ( $typesByName as $type => $name ) {
231 $select->addOption( $name, $type );
238 * @param string $user
239 * @return string Formatted HTML
241 private function getUserInput( $user ) {
242 $label = Xml
::inputLabel(
243 $this->msg( 'specialloguserlabel' )->text(),
248 [ 'class' => 'mw-autocomplete-user' ]
251 return '<span class="mw-input-with-label">' . $label . '</span>';
255 * @param string $title
256 * @return string Formatted HTML
258 private function getTitleInput( $title ) {
259 $label = Xml
::inputLabel(
260 $this->msg( 'speciallogtitlelabel' )->text(),
267 return '<span class="mw-input-with-label">' . $label . '</span>';
271 * @param string $pattern
272 * @return string Checkbox
274 private function getTitlePattern( $pattern ) {
275 return '<span class="mw-input-with-label">' .
276 Xml
::checkLabel( $this->msg( 'log-title-wildcard' )->text(), 'pattern', 'pattern', $pattern ) .
281 * @param array $types
284 private function getExtraInputs( $types ) {
285 if ( count( $types ) == 1 ) {
286 if ( $types[0] == 'suppress' ) {
287 $offender = $this->getRequest()->getVal( 'offender' );
288 $user = User
::newFromName( $offender, false );
289 if ( !$user ||
( $user->getId() == 0 && !IP
::isIPAddress( $offender ) ) ) {
290 $offender = ''; // Blank field if invalid
292 return Xml
::inputLabel( $this->msg( 'revdelete-offender' )->text(), 'offender',
293 'mw-log-offender', 20, $offender );
295 // Allow extensions to add their own extra inputs
297 Hooks
::run( 'LogEventsListGetExtraInputs', [ $types[0], $this, &$input ] );
306 * Drop down menu for selection of actions that can be used to filter the log
307 * @param array $types
308 * @param string $action
312 private function getActionSelector( $types, $action ) {
313 if ( $this->allowedActions
=== null ||
!count( $this->allowedActions
) ) {
317 $html .= Xml
::label( wfMessage( 'log-action-filter-' . $types[0] )->text(),
318 'action-filter-' .$types[0] ) . "\n";
319 $select = new XmlSelect( 'subtype' );
320 $select->addOption( wfMessage( 'log-action-filter-all' )->text(), '' );
321 foreach ( $this->allowedActions
as $value ) {
322 $msgKey = 'log-action-filter-' . $types[0] . '-' . $value;
323 $select->addOption( wfMessage( $msgKey )->text(), $value );
325 $select->setDefault( $action );
326 $html .= $select->getHTML();
331 * Sets the action types allowed for log filtering
332 * To one action type may correspond several log_actions
333 * @param array $actions
336 public function setAllowedActions( $actions ) {
337 $this->allowedActions
= $actions;
343 public function beginLogEventsList() {
350 public function endLogEventsList() {
355 * @param stdClass $row A single row from the result set
356 * @return string Formatted HTML list item
358 public function logLine( $row ) {
359 $entry = DatabaseLogEntry
::newFromRow( $row );
360 $formatter = LogFormatter
::newFromEntry( $entry );
361 $formatter->setContext( $this->getContext() );
362 $formatter->setShowUserToolLinks( !( $this->flags
& self
::NO_EXTRA_USER_LINKS
) );
364 $time = htmlspecialchars( $this->getLanguage()->userTimeAndDate(
365 $entry->getTimestamp(), $this->getUser() ) );
367 $action = $formatter->getActionText();
369 if ( $this->flags
& self
::NO_ACTION_LINK
) {
372 $revert = $formatter->getActionLinks();
373 if ( $revert != '' ) {
374 $revert = '<span class="mw-logevent-actionlink">' . $revert . '</span>';
378 $comment = $formatter->getComment();
380 // Some user can hide log items and have review links
381 $del = $this->getShowHideLinks( $row );
384 list( $tagDisplay, $newClasses ) = ChangeTags
::formatSummaryRow(
389 $classes = array_merge(
390 [ 'mw-logline-' . $entry->getType() ],
394 return Html
::rawElement( 'li', [ 'class' => $classes ],
395 "$del $time $action $comment $revert $tagDisplay" ) . "\n";
399 * @param stdClass $row Row
402 private function getShowHideLinks( $row ) {
403 // We don't want to see the links and
404 if ( $this->flags
== self
::NO_ACTION_LINK
) {
408 $user = $this->getUser();
410 // If change tag editing is available to this user, return the checkbox
411 if ( $this->flags
& self
::USE_CHECKBOXES
&& $this->showTagEditUI
) {
415 [ 'name' => 'ids[' . $row->log_id
. ']' ]
419 // no one can hide items from the suppress log.
420 if ( $row->log_type
== 'suppress' ) {
425 // Don't show useless checkbox to people who cannot hide log entries
426 if ( $user->isAllowed( 'deletedhistory' ) ) {
427 $canHide = $user->isAllowed( 'deletelogentry' );
428 $canViewSuppressedOnly = $user->isAllowed( 'viewsuppressed' ) &&
429 !$user->isAllowed( 'suppressrevision' );
430 $entryIsSuppressed = self
::isDeleted( $row, LogPage
::DELETED_RESTRICTED
);
431 $canViewThisSuppressedEntry = $canViewSuppressedOnly && $entryIsSuppressed;
432 if ( $row->log_deleted ||
$canHide ) {
433 // Show checkboxes instead of links.
434 if ( $canHide && $this->flags
& self
::USE_CHECKBOXES
&& !$canViewThisSuppressedEntry ) {
435 // If event was hidden from sysops
436 if ( !self
::userCan( $row, LogPage
::DELETED_RESTRICTED
, $user ) ) {
437 $del = Xml
::check( 'deleterevisions', false, [ 'disabled' => 'disabled' ] );
442 [ 'name' => 'ids[' . $row->log_id
. ']' ]
446 // If event was hidden from sysops
447 if ( !self
::userCan( $row, LogPage
::DELETED_RESTRICTED
, $user ) ) {
448 $del = Linker
::revDeleteLinkDisabled( $canHide );
451 'target' => SpecialPage
::getTitleFor( 'Log', $row->log_type
)->getPrefixedDBkey(),
453 'ids' => $row->log_id
,
455 $del = Linker
::revDeleteLink(
458 $canHide && !$canViewThisSuppressedEntry
469 * @param stdClass $row Row
470 * @param string|array $type
471 * @param string|array $action
472 * @param string $right
475 public static function typeAction( $row, $type, $action, $right = '' ) {
476 $match = is_array( $type ) ?
477 in_array( $row->log_type
, $type ) : $row->log_type
== $type;
479 $match = is_array( $action ) ?
480 in_array( $row->log_action
, $action ) : $row->log_action
== $action;
481 if ( $match && $right ) {
483 $match = $wgUser->isAllowed( $right );
491 * Determine if the current user is allowed to view a particular
492 * field of this log row, if it's marked as deleted.
494 * @param stdClass $row Row
496 * @param User $user User to check, or null to use $wgUser
499 public static function userCan( $row, $field, User
$user = null ) {
500 return self
::userCanBitfield( $row->log_deleted
, $field, $user );
504 * Determine if the current user is allowed to view a particular
505 * field of this log row, if it's marked as deleted.
507 * @param int $bitfield Current field
509 * @param User $user User to check, or null to use $wgUser
512 public static function userCanBitfield( $bitfield, $field, User
$user = null ) {
513 if ( $bitfield & $field ) {
514 if ( $user === null ) {
518 if ( $bitfield & LogPage
::DELETED_RESTRICTED
) {
519 $permissions = [ 'suppressrevision', 'viewsuppressed' ];
521 $permissions = [ 'deletedhistory' ];
523 $permissionlist = implode( ', ', $permissions );
524 wfDebug( "Checking for $permissionlist due to $field match on $bitfield\n" );
525 return call_user_func_array( [ $user, 'isAllowedAny' ], $permissions );
531 * @param stdClass $row Row
532 * @param int $field One of DELETED_* bitfield constants
535 public static function isDeleted( $row, $field ) {
536 return ( $row->log_deleted
& $field ) == $field;
540 * Show log extract. Either with text and a box (set $msgKey) or without (don't set $msgKey)
542 * @param OutputPage|string $out By-reference
543 * @param string|array $types Log types to show
544 * @param string|Title $page The page title to show log entries for
545 * @param string $user The user who made the log entries
546 * @param array $param Associative Array with the following additional options:
547 * - lim Integer Limit of items to show, default is 50
548 * - conds Array Extra conditions for the query
549 * (e.g. 'log_action != ' . $dbr->addQuotes( 'revision' ))
550 * - showIfEmpty boolean Set to false if you don't want any output in case the loglist is empty
551 * if set to true (default), "No matching items in log" is displayed if loglist is empty
552 * - msgKey Array If you want a nice box with a message, set this to the key of the message.
553 * First element is the message key, additional optional elements are parameters for the key
554 * that are processed with wfMessage
555 * - offset Set to overwrite offset parameter in WebRequest
556 * set to '' to unset offset
557 * - wrap String Wrap the message in html (usually something like "<div ...>$1</div>").
558 * - flags Integer display flags (NO_ACTION_LINK,NO_EXTRA_USER_LINKS)
559 * - useRequestParams boolean Set true to use Pager-related parameters in the WebRequest
560 * - useMaster boolean Use master DB
561 * - extraUrlParams array|bool Additional url parameters for "full log" link (if it is shown)
562 * @return int Number of total log items (not limited by $lim)
564 public static function showLogExtract(
565 &$out, $types = [], $page = '', $user = '', $param = []
567 $defaultParameters = [
570 'showIfEmpty' => true,
574 'useRequestParams' => false,
575 'useMaster' => false,
576 'extraUrlParams' => false,
578 # The + operator appends elements of remaining keys from the right
579 # handed array to the left handed, whereas duplicated keys are NOT overwritten.
580 $param +
= $defaultParameters;
581 # Convert $param array to individual variables
582 $lim = $param['lim'];
583 $conds = $param['conds'];
584 $showIfEmpty = $param['showIfEmpty'];
585 $msgKey = $param['msgKey'];
586 $wrap = $param['wrap'];
587 $flags = $param['flags'];
588 $extraUrlParams = $param['extraUrlParams'];
590 $useRequestParams = $param['useRequestParams'];
591 if ( !is_array( $msgKey ) ) {
592 $msgKey = [ $msgKey ];
595 if ( $out instanceof OutputPage
) {
596 $context = $out->getContext();
598 $context = RequestContext
::getMain();
601 # Insert list of top 50 (or top $lim) items
602 $loglist = new LogEventsList( $context, null, $flags );
603 $pager = new LogPager( $loglist, $types, $user, $page, '', $conds );
604 if ( !$useRequestParams ) {
605 # Reset vars that may have been taken from the request
607 $pager->mDefaultLimit
= 50;
608 $pager->mOffset
= "";
609 $pager->mIsBackwards
= false;
612 if ( $param['useMaster'] ) {
613 $pager->mDb
= wfGetDB( DB_MASTER
);
615 if ( isset( $param['offset'] ) ) { # Tell pager to ignore WebRequest offset
616 $pager->setOffset( $param['offset'] );
620 $pager->mLimit
= $lim;
622 // Fetch the log rows and build the HTML if needed
623 $logBody = $pager->getBody();
624 $numRows = $pager->getNumRows();
630 $dir = $context->getLanguage()->getDir();
631 $lang = $context->getLanguage()->getHtmlCode();
633 $s = Xml
::openElement( 'div', [
634 'class' => "mw-warning-with-logexcerpt mw-content-$dir",
639 if ( count( $msgKey ) == 1 ) {
640 $s .= $context->msg( $msgKey[0] )->parseAsBlock();
641 } else { // Process additional arguments
643 array_shift( $args );
644 $s .= $context->msg( $msgKey[0], $args )->parseAsBlock();
647 $s .= $loglist->beginLogEventsList() .
649 $loglist->endLogEventsList();
650 } elseif ( $showIfEmpty ) {
651 $s = Html
::rawElement( 'div', [ 'class' => 'mw-warning-logempty' ],
652 $context->msg( 'logempty' )->parse() );
655 if ( $numRows > $pager->mLimit
) { # Show "Full log" link
657 if ( $page instanceof Title
) {
658 $urlParam['page'] = $page->getPrefixedDBkey();
659 } elseif ( $page != '' ) {
660 $urlParam['page'] = $page;
664 $urlParam['user'] = $user;
667 if ( !is_array( $types ) ) { # Make it an array, if it isn't
671 # If there is exactly one log type, we can link to Special:Log?type=foo
672 if ( count( $types ) == 1 ) {
673 $urlParam['type'] = $types[0];
676 if ( $extraUrlParams !== false ) {
677 $urlParam = array_merge( $urlParam, $extraUrlParams );
680 $s .= MediaWikiServices
::getInstance()->getLinkRenderer()->makeKnownLink(
681 SpecialPage
::getTitleFor( 'Log' ),
682 $context->msg( 'log-fulllog' )->text(),
688 if ( $logBody && $msgKey[0] ) {
692 if ( $wrap != '' ) { // Wrap message in html
693 $s = str_replace( '$1', $s, $wrap );
696 /* hook can return false, if we don't want the message to be emitted (Wikia BugId:7093) */
697 if ( Hooks
::run( 'LogEventsListShowLogExtract', [ &$s, $types, $page, $user, $param ] ) ) {
698 // $out can be either an OutputPage object or a String-by-reference
699 if ( $out instanceof OutputPage
) {
710 * SQL clause to skip forbidden log types for this user
712 * @param IDatabase $db
713 * @param string $audience Public/user
714 * @param User $user User to check, or null to use $wgUser
715 * @return string|bool String on success, false on failure.
717 public static function getExcludeClause( $db, $audience = 'public', User
$user = null ) {
718 global $wgLogRestrictions;
720 if ( $audience != 'public' && $user === null ) {
725 // Reset the array, clears extra "where" clauses when $par is used
728 // Don't show private logs to unprivileged users
729 foreach ( $wgLogRestrictions as $logType => $right ) {
730 if ( $audience == 'public' ||
!$user->isAllowed( $right ) ) {
731 $hiddenLogs[] = $logType;
734 if ( count( $hiddenLogs ) == 1 ) {
735 return 'log_type != ' . $db->addQuotes( $hiddenLogs[0] );
736 } elseif ( $hiddenLogs ) {
737 return 'log_type NOT IN (' . $db->makeList( $hiddenLogs ) . ')';