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 class LogEventsList
extends ContextSource
{
27 const NO_ACTION_LINK
= 1;
28 const NO_EXTRA_USER_LINKS
= 2;
29 const USE_REVDEL_CHECKBOXES
= 4;
36 protected $mDefaultQuery;
40 * The first two parameters used to be $skin and $out, but now only a context
41 * is needed, that's why there's a second unused parameter.
43 * @param IContextSource|Skin $context Context to use; formerly it was
44 * a Skin object. Use of Skin is deprecated.
45 * @param null $unused Unused; used to be an OutputPage object.
46 * @param int $flags Can be a combination of self::NO_ACTION_LINK,
47 * self::NO_EXTRA_USER_LINKS or self::USE_REVDEL_CHECKBOXES.
49 public function __construct( $context, $unused = null, $flags = 0 ) {
50 if ( $context instanceof IContextSource
) {
51 $this->setContext( $context );
53 // Old parameters, $context should be a Skin object
54 $this->setContext( $context->getContext() );
57 $this->flags
= $flags;
61 * Deprecated alias for getTitle(); do not use.
63 * @deprecated since 1.20; use getTitle() instead.
66 public function getDisplayTitle() {
67 wfDeprecated( __METHOD__
, '1.20' );
68 return $this->getTitle();
72 * Show options for the log list
74 * @param array|string $types
77 * @param string $pattern
78 * @param int $year Year
79 * @param int $month Month
80 * @param array $filter
81 * @param string $tagFilter Tag to select by default
83 public function showOptions( $types = array(), $user = '', $page = '', $pattern = '', $year = 0,
84 $month = 0, $filter = null, $tagFilter = ''
86 global $wgScript, $wgMiserMode;
88 $title = SpecialPage
::getTitleFor( 'Log' );
90 // For B/C, we take strings, but make sure they are converted...
91 $types = ( $types === '' ) ?
array() : (array)$types;
93 $tagSelector = ChangeTags
::buildTagFilterSelector( $tagFilter );
95 $html = Html
::hidden( 'title', $title->getPrefixedDBkey() );
98 $html .= $this->getTypeMenu( $types ) . "\n";
99 $html .= $this->getUserInput( $user ) . "\n";
100 $html .= $this->getTitleInput( $page ) . "\n";
101 $html .= $this->getExtraInputs( $types ) . "\n";
103 // Title pattern, if allowed
104 if ( !$wgMiserMode ) {
105 $html .= $this->getTitlePattern( $pattern ) . "\n";
109 $html .= Xml
::tags( 'p', null, Xml
::dateMenu( (int)$year, (int)$month ) );
112 if ( $tagSelector ) {
113 $html .= Xml
::tags( 'p', null, implode( ' ', $tagSelector ) );
118 $html .= Xml
::tags( 'p', null, $this->getFilterLinks( $filter ) );
122 $html .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() );
125 $html = Xml
::fieldset( $this->msg( 'log' )->text(), $html );
128 $html = Xml
::tags( 'form', array( 'action' => $wgScript, 'method' => 'get' ), $html );
130 $this->getOutput()->addHTML( $html );
134 * @param array $filter
135 * @return string Formatted HTML
137 private function getFilterLinks( $filter ) {
139 $messages = array( $this->msg( 'show' )->escaped(), $this->msg( 'hide' )->escaped() );
140 // Option value -> message mapping
142 $hiddens = ''; // keep track for "go" button
143 foreach ( $filter as $type => $val ) {
144 // Should the below assignment be outside the foreach?
145 // Then it would have to be copied. Not certain what is more expensive.
146 $query = $this->getDefaultQuery();
147 $queryKey = "hide_{$type}_log";
149 $hideVal = 1 - intval( $val );
150 $query[$queryKey] = $hideVal;
152 $link = Linker
::linkKnown(
159 // Message: log-show-hide-patrol
160 $links[$type] = $this->msg( "log-show-hide-{$type}" )->rawParams( $link )->escaped();
161 $hiddens .= Html
::hidden( "hide_{$type}_log", $val ) . "\n";
165 return '<small>' . $this->getLanguage()->pipeList( $links ) . '</small>' . $hiddens;
168 private function getDefaultQuery() {
169 if ( !isset( $this->mDefaultQuery
) ) {
170 $this->mDefaultQuery
= $this->getRequest()->getQueryValues();
171 unset( $this->mDefaultQuery
['title'] );
172 unset( $this->mDefaultQuery
['dir'] );
173 unset( $this->mDefaultQuery
['offset'] );
174 unset( $this->mDefaultQuery
['limit'] );
175 unset( $this->mDefaultQuery
['order'] );
176 unset( $this->mDefaultQuery
['month'] );
177 unset( $this->mDefaultQuery
['year'] );
180 return $this->mDefaultQuery
;
184 * @param array $queryTypes
185 * @return string Formatted HTML
187 private function getTypeMenu( $queryTypes ) {
188 $queryType = count( $queryTypes ) == 1 ?
$queryTypes[0] : '';
189 $selector = $this->getTypeSelector();
190 $selector->setDefault( $queryType );
192 return $selector->getHtml();
196 * Returns log page selector.
200 public function getTypeSelector() {
201 $typesByName = array(); // Temporary array
202 // First pass to load the log names
203 foreach ( LogPage
::validTypes() as $type ) {
204 $page = new LogPage( $type );
205 $restriction = $page->getRestriction();
206 if ( $this->getUser()->isAllowed( $restriction ) ) {
207 $typesByName[$type] = $page->getName()->text();
211 // Second pass to sort by name
212 asort( $typesByName );
214 // Always put "All public logs" on top
215 $public = $typesByName[''];
216 unset( $typesByName[''] );
217 $typesByName = array( '' => $public ) +
$typesByName;
219 $select = new XmlSelect( 'type' );
220 foreach ( $typesByName as $type => $name ) {
221 $select->addOption( $name, $type );
228 * @param string $user
229 * @return string Formatted HTML
231 private function getUserInput( $user ) {
232 $label = Xml
::inputLabel(
233 $this->msg( 'specialloguserlabel' )->text(),
240 return '<span style="white-space: nowrap">' . $label . '</span>';
244 * @param string $title
245 * @return string Formatted HTML
247 private function getTitleInput( $title ) {
248 $label = Xml
::inputLabel(
249 $this->msg( 'speciallogtitlelabel' )->text(),
256 return '<span style="white-space: nowrap">' . $label . '</span>';
260 * @param string $pattern
261 * @return string Checkbox
263 private function getTitlePattern( $pattern ) {
264 return '<span style="white-space: nowrap">' .
265 Xml
::checkLabel( $this->msg( 'log-title-wildcard' )->text(), 'pattern', 'pattern', $pattern ) .
270 * @param array $types
273 private function getExtraInputs( $types ) {
274 $offender = $this->getRequest()->getVal( 'offender' );
275 $user = User
::newFromName( $offender, false );
276 if ( !$user ||
( $user->getId() == 0 && !IP
::isIPAddress( $offender ) ) ) {
277 $offender = ''; // Blank field if invalid
279 if ( count( $types ) == 1 && $types[0] == 'suppress' ) {
280 return Xml
::inputLabel( $this->msg( 'revdelete-offender' )->text(), 'offender',
281 'mw-log-offender', 20, $offender );
290 public function beginLogEventsList() {
297 public function endLogEventsList() {
302 * @param stdClass $row A single row from the result set
303 * @return string Formatted HTML list item
305 public function logLine( $row ) {
306 $entry = DatabaseLogEntry
::newFromRow( $row );
307 $formatter = LogFormatter
::newFromEntry( $entry );
308 $formatter->setContext( $this->getContext() );
309 $formatter->setShowUserToolLinks( !( $this->flags
& self
::NO_EXTRA_USER_LINKS
) );
311 $time = htmlspecialchars( $this->getLanguage()->userTimeAndDate(
312 $entry->getTimestamp(), $this->getUser() ) );
314 $action = $formatter->getActionText();
316 if ( $this->flags
& self
::NO_ACTION_LINK
) {
319 $revert = $formatter->getActionLinks();
320 if ( $revert != '' ) {
321 $revert = '<span class="mw-logevent-actionlink">' . $revert . '</span>';
325 $comment = $formatter->getComment();
327 // Some user can hide log items and have review links
328 $del = $this->getShowHideLinks( $row );
331 list( $tagDisplay, $newClasses ) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'logevent' );
332 $classes = array_merge(
333 array( 'mw-logline-' . $entry->getType() ),
337 return Html
::rawElement( 'li', array( 'class' => $classes ),
338 "$del $time $action $comment $revert $tagDisplay" ) . "\n";
342 * @param stdClass $row Row
345 private function getShowHideLinks( $row ) {
346 // We don't want to see the links and
347 // no one can hide items from the suppress log.
348 if ( ( $this->flags
== self
::NO_ACTION_LINK
)
349 ||
$row->log_type
== 'suppress'
354 $user = $this->getUser();
355 // Don't show useless checkbox to people who cannot hide log entries
356 if ( $user->isAllowed( 'deletedhistory' ) ) {
357 $canHide = $user->isAllowed( 'deletelogentry' );
358 $canViewSuppressedOnly = $user->isAllowed( 'viewsuppressed' ) &&
359 !$user->isAllowed( 'suppressrevision' );
360 $entryIsSuppressed = self
::isDeleted( $row, LogPage
::DELETED_RESTRICTED
);
361 $canViewThisSuppressedEntry = $canViewSuppressedOnly && $entryIsSuppressed;
362 if ( $row->log_deleted ||
$canHide ) {
363 // Show checkboxes instead of links.
364 if ( $canHide && $this->flags
& self
::USE_REVDEL_CHECKBOXES
&& !$canViewThisSuppressedEntry ) {
365 // If event was hidden from sysops
366 if ( !self
::userCan( $row, LogPage
::DELETED_RESTRICTED
, $user ) ) {
367 $del = Xml
::check( 'deleterevisions', false, array( 'disabled' => 'disabled' ) );
372 array( 'name' => 'ids[' . $row->log_id
. ']' )
376 // If event was hidden from sysops
377 if ( !self
::userCan( $row, LogPage
::DELETED_RESTRICTED
, $user ) ) {
378 $del = Linker
::revDeleteLinkDisabled( $canHide );
381 'target' => SpecialPage
::getTitleFor( 'Log', $row->log_type
)->getPrefixedDBkey(),
383 'ids' => $row->log_id
,
385 $del = Linker
::revDeleteLink(
388 $canHide && !$canViewThisSuppressedEntry
399 * @param stdClass $row Row
400 * @param string|array $type
401 * @param string|array $action
402 * @param string $right
405 public static function typeAction( $row, $type, $action, $right = '' ) {
406 $match = is_array( $type ) ?
407 in_array( $row->log_type
, $type ) : $row->log_type
== $type;
409 $match = is_array( $action ) ?
410 in_array( $row->log_action
, $action ) : $row->log_action
== $action;
411 if ( $match && $right ) {
413 $match = $wgUser->isAllowed( $right );
421 * Determine if the current user is allowed to view a particular
422 * field of this log row, if it's marked as deleted.
424 * @param stdClass $row Row
426 * @param User $user User to check, or null to use $wgUser
429 public static function userCan( $row, $field, User
$user = null ) {
430 return self
::userCanBitfield( $row->log_deleted
, $field, $user );
434 * Determine if the current user is allowed to view a particular
435 * field of this log row, if it's marked as deleted.
437 * @param int $bitfield Current field
439 * @param User $user User to check, or null to use $wgUser
442 public static function userCanBitfield( $bitfield, $field, User
$user = null ) {
443 if ( $bitfield & $field ) {
444 if ( $user === null ) {
448 if ( $bitfield & LogPage
::DELETED_RESTRICTED
) {
449 $permissions = array( 'suppressrevision', 'viewsuppressed' );
451 $permissions = array( 'deletedhistory' );
453 $permissionlist = implode( ', ', $permissions );
454 wfDebug( "Checking for $permissionlist due to $field match on $bitfield\n" );
455 return call_user_func_array( array( $user, 'isAllowedAny' ), $permissions );
461 * @param stdClass $row Row
462 * @param int $field One of DELETED_* bitfield constants
465 public static function isDeleted( $row, $field ) {
466 return ( $row->log_deleted
& $field ) == $field;
470 * Show log extract. Either with text and a box (set $msgKey) or without (don't set $msgKey)
472 * @param OutputPage|string $out By-reference
473 * @param string|array $types Log types to show
474 * @param string|Title $page The page title to show log entries for
475 * @param string $user The user who made the log entries
476 * @param array $param Associative Array with the following additional options:
477 * - lim Integer Limit of items to show, default is 50
478 * - conds Array Extra conditions for the query (e.g. "log_action != 'revision'")
479 * - showIfEmpty boolean Set to false if you don't want any output in case the loglist is empty
480 * if set to true (default), "No matching items in log" is displayed if loglist is empty
481 * - msgKey Array If you want a nice box with a message, set this to the key of the message.
482 * First element is the message key, additional optional elements are parameters for the key
483 * that are processed with wfMessage
484 * - offset Set to overwrite offset parameter in WebRequest
485 * set to '' to unset offset
486 * - wrap String Wrap the message in html (usually something like "<div ...>$1</div>").
487 * - flags Integer display flags (NO_ACTION_LINK,NO_EXTRA_USER_LINKS)
488 * - useRequestParams boolean Set true to use Pager-related parameters in the WebRequest
489 * - useMaster boolean Use master DB
490 * @return int Number of total log items (not limited by $lim)
492 public static function showLogExtract(
493 &$out, $types = array(), $page = '', $user = '', $param = array()
495 $defaultParameters = array(
498 'showIfEmpty' => true,
499 'msgKey' => array( '' ),
502 'useRequestParams' => false,
503 'useMaster' => false,
505 # The + operator appends elements of remaining keys from the right
506 # handed array to the left handed, whereas duplicated keys are NOT overwritten.
507 $param +
= $defaultParameters;
508 # Convert $param array to individual variables
509 $lim = $param['lim'];
510 $conds = $param['conds'];
511 $showIfEmpty = $param['showIfEmpty'];
512 $msgKey = $param['msgKey'];
513 $wrap = $param['wrap'];
514 $flags = $param['flags'];
515 $useRequestParams = $param['useRequestParams'];
516 if ( !is_array( $msgKey ) ) {
517 $msgKey = array( $msgKey );
520 if ( $out instanceof OutputPage
) {
521 $context = $out->getContext();
523 $context = RequestContext
::getMain();
526 # Insert list of top 50 (or top $lim) items
527 $loglist = new LogEventsList( $context, null, $flags );
528 $pager = new LogPager( $loglist, $types, $user, $page, '', $conds );
529 if ( !$useRequestParams ) {
530 # Reset vars that may have been taken from the request
532 $pager->mDefaultLimit
= 50;
533 $pager->mOffset
= "";
534 $pager->mIsBackwards
= false;
537 if ( $param['useMaster'] ) {
538 $pager->mDb
= wfGetDB( DB_MASTER
);
540 if ( isset( $param['offset'] ) ) { # Tell pager to ignore WebRequest offset
541 $pager->setOffset( $param['offset'] );
545 $pager->mLimit
= $lim;
548 $logBody = $pager->getBody();
553 $dir = $context->getLanguage()->getDir();
554 $lang = $context->getLanguage()->getCode();
556 $s = Xml
::openElement( 'div', array(
557 'class' => "mw-warning-with-logexcerpt mw-content-$dir",
562 if ( count( $msgKey ) == 1 ) {
563 $s .= $context->msg( $msgKey[0] )->parseAsBlock();
564 } else { // Process additional arguments
566 array_shift( $args );
567 $s .= $context->msg( $msgKey[0], $args )->parseAsBlock();
570 $s .= $loglist->beginLogEventsList() .
572 $loglist->endLogEventsList();
573 } elseif ( $showIfEmpty ) {
574 $s = Html
::rawElement( 'div', array( 'class' => 'mw-warning-logempty' ),
575 $context->msg( 'logempty' )->parse() );
578 if ( $pager->getNumRows() > $pager->mLimit
) { # Show "Full log" link
580 if ( $page instanceof Title
) {
581 $urlParam['page'] = $page->getPrefixedDBkey();
582 } elseif ( $page != '' ) {
583 $urlParam['page'] = $page;
587 $urlParam['user'] = $user;
590 if ( !is_array( $types ) ) { # Make it an array, if it isn't
591 $types = array( $types );
594 # If there is exactly one log type, we can link to Special:Log?type=foo
595 if ( count( $types ) == 1 ) {
596 $urlParam['type'] = $types[0];
600 SpecialPage
::getTitleFor( 'Log' ),
601 $context->msg( 'log-fulllog' )->escaped(),
607 if ( $logBody && $msgKey[0] ) {
611 if ( $wrap != '' ) { // Wrap message in html
612 $s = str_replace( '$1', $s, $wrap );
615 /* hook can return false, if we don't want the message to be emitted (Wikia BugId:7093) */
616 if ( wfRunHooks( 'LogEventsListShowLogExtract', array( &$s, $types, $page, $user, $param ) ) ) {
617 // $out can be either an OutputPage object or a String-by-reference
618 if ( $out instanceof OutputPage
) {
625 return $pager->getNumRows();
629 * SQL clause to skip forbidden log types for this user
631 * @param DatabaseBase $db
632 * @param string $audience Public/user
633 * @param User $user User to check, or null to use $wgUser
634 * @return string|bool String on success, false on failure.
636 public static function getExcludeClause( $db, $audience = 'public', User
$user = null ) {
637 global $wgLogRestrictions;
639 if ( $audience != 'public' && $user === null ) {
644 // Reset the array, clears extra "where" clauses when $par is used
645 $hiddenLogs = array();
647 // Don't show private logs to unprivileged users
648 foreach ( $wgLogRestrictions as $logType => $right ) {
649 if ( $audience == 'public' ||
!$user->isAllowed( $right ) ) {
650 $hiddenLogs[] = $logType;
653 if ( count( $hiddenLogs ) == 1 ) {
654 return 'log_type != ' . $db->addQuotes( $hiddenLogs[0] );
655 } elseif ( $hiddenLogs ) {
656 return 'log_type NOT IN (' . $db->makeList( $hiddenLogs ) . ')';