3 * Contain classes to list log entries
5 * Copyright © 2004 Brion Vibber <brion@pobox.com>, 2008 Aaron Schulz
6 * http://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 $context IContextSource Context to use; formerly it was Skin object.
44 * @param $unused void Unused; used to be an OutputPage object.
45 * @param int $flags flags; can be a combinaison of self::NO_ACTION_LINK,
46 * self::NO_EXTRA_USER_LINKS or self::USE_REVDEL_CHECKBOXES.
48 public function __construct( $context, $unused = null, $flags = 0 ) {
49 if ( $context instanceof IContextSource
) {
50 $this->setContext( $context );
52 // Old parameters, $context should be a Skin object
53 $this->setContext( $context->getContext() );
56 $this->flags
= $flags;
60 * Deprecated alias for getTitle(); do not use.
62 * @deprecated in 1.20; use getTitle() instead.
63 * @return Title object
65 public function getDisplayTitle() {
66 return $this->getTitle();
70 * Set page title and show header for this log type
74 public function showHeader( $type ) {
75 wfDeprecated( __METHOD__
, '1.19' );
76 // If only one log type is used, then show a special message...
77 $headerType = count( $type ) == 1 ?
$type[0] : '';
78 $out = $this->getOutput();
79 if ( LogPage
::isLogType( $headerType ) ) {
80 $page = new LogPage( $headerType );
81 $out->setPageTitle( $page->getName()->text() );
82 $out->addHTML( $page->getDescription()->parseAsBlock() );
84 $out->addHTML( $this->msg( 'alllogstext' )->parse() );
89 * Show options for the log list
91 * @param string $types or Array
94 * @param $pattern String
95 * @param $year Integer: year
96 * @param $month Integer: month
97 * @param $filter: array
98 * @param $tagFilter: array?
100 public function showOptions( $types = array(), $user = '', $page = '', $pattern = '', $year = '',
101 $month = '', $filter = null, $tagFilter = '' ) {
102 global $wgScript, $wgMiserMode;
104 $title = SpecialPage
::getTitleFor( 'Log' );
106 // For B/C, we take strings, but make sure they are converted...
107 $types = ( $types === '' ) ?
array() : (array)$types;
109 $tagSelector = ChangeTags
::buildTagFilterSelector( $tagFilter );
111 $html = Html
::hidden( 'title', $title->getPrefixedDBkey() );
114 $html .= $this->getTypeMenu( $types ) . "\n";
115 $html .= $this->getUserInput( $user ) . "\n";
116 $html .= $this->getTitleInput( $page ) . "\n";
117 $html .= $this->getExtraInputs( $types ) . "\n";
119 // Title pattern, if allowed
120 if ( !$wgMiserMode ) {
121 $html .= $this->getTitlePattern( $pattern ) . "\n";
125 $html .= Xml
::tags( 'p', null, Xml
::dateMenu( $year, $month ) );
128 if ( $tagSelector ) {
129 $html .= Xml
::tags( 'p', null, implode( ' ', $tagSelector ) );
134 $html .= Xml
::tags( 'p', null, $this->getFilterLinks( $filter ) );
138 $html .= Xml
::submitButton( $this->msg( 'allpagessubmit' )->text() );
141 $html = Xml
::fieldset( $this->msg( 'log' )->text(), $html );
144 $html = Xml
::tags( 'form', array( 'action' => $wgScript, 'method' => 'get' ), $html );
146 $this->getOutput()->addHTML( $html );
150 * @param $filter Array
151 * @return String: Formatted HTML
153 private function getFilterLinks( $filter ) {
155 $messages = array( $this->msg( 'show' )->escaped(), $this->msg( 'hide' )->escaped() );
156 // Option value -> message mapping
158 $hiddens = ''; // keep track for "go" button
159 foreach ( $filter as $type => $val ) {
160 // Should the below assignment be outside the foreach?
161 // Then it would have to be copied. Not certain what is more expensive.
162 $query = $this->getDefaultQuery();
163 $queryKey = "hide_{$type}_log";
165 $hideVal = 1 - intval( $val );
166 $query[$queryKey] = $hideVal;
168 $link = Linker
::linkKnown(
175 $links[$type] = $this->msg( "log-show-hide-{$type}" )->rawParams( $link )->escaped();
176 $hiddens .= Html
::hidden( "hide_{$type}_log", $val ) . "\n";
179 return '<small>' . $this->getLanguage()->pipeList( $links ) . '</small>' . $hiddens;
182 private function getDefaultQuery() {
183 if ( !isset( $this->mDefaultQuery
) ) {
184 $this->mDefaultQuery
= $this->getRequest()->getQueryValues();
185 unset( $this->mDefaultQuery
['title'] );
186 unset( $this->mDefaultQuery
['dir'] );
187 unset( $this->mDefaultQuery
['offset'] );
188 unset( $this->mDefaultQuery
['limit'] );
189 unset( $this->mDefaultQuery
['order'] );
190 unset( $this->mDefaultQuery
['month'] );
191 unset( $this->mDefaultQuery
['year'] );
193 return $this->mDefaultQuery
;
197 * @param $queryTypes Array
198 * @return String: Formatted HTML
200 private function getTypeMenu( $queryTypes ) {
201 $queryType = count( $queryTypes ) == 1 ?
$queryTypes[0] : '';
202 $selector = $this->getTypeSelector();
203 $selector->setDefault( $queryType );
204 return $selector->getHtml();
208 * Returns log page selector.
212 public function getTypeSelector() {
213 $typesByName = array(); // Temporary array
214 // First pass to load the log names
215 foreach ( LogPage
::validTypes() as $type ) {
216 $page = new LogPage( $type );
217 $restriction = $page->getRestriction();
218 if ( $this->getUser()->isAllowed( $restriction ) ) {
219 $typesByName[$type] = $page->getName()->text();
223 // Second pass to sort by name
224 asort( $typesByName );
226 // Always put "All public logs" on top
227 $public = $typesByName[''];
228 unset( $typesByName[''] );
229 $typesByName = array( '' => $public ) +
$typesByName;
231 $select = new XmlSelect( 'type' );
232 foreach ( $typesByName as $type => $name ) {
233 $select->addOption( $name, $type );
240 * @param $user String
241 * @return String: Formatted HTML
243 private function getUserInput( $user ) {
244 return '<span style="white-space: nowrap">' .
245 Xml
::inputLabel( $this->msg( 'specialloguserlabel' )->text(), 'user', 'mw-log-user', 15, $user ) .
250 * @param $title String
251 * @return String: Formatted HTML
253 private function getTitleInput( $title ) {
254 return '<span style="white-space: nowrap">' .
255 Xml
::inputLabel( $this->msg( 'speciallogtitlelabel' )->text(), 'page', 'mw-log-page', 20, $title ) .
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 ) .
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 );
289 public function beginLogEventsList() {
296 public function endLogEventsList() {
301 * @param $row Row: a single row from the result set
302 * @return String: Formatted HTML list item
304 public function logLine( $row ) {
305 $entry = DatabaseLogEntry
::newFromRow( $row );
306 $formatter = LogFormatter
::newFromEntry( $entry );
307 $formatter->setContext( $this->getContext() );
308 $formatter->setShowUserToolLinks( !( $this->flags
& self
::NO_EXTRA_USER_LINKS
) );
310 $time = htmlspecialchars( $this->getLanguage()->userTimeAndDate(
311 $entry->getTimestamp(), $this->getUser() ) );
313 $action = $formatter->getActionText();
315 if ( $this->flags
& self
::NO_ACTION_LINK
) {
318 $revert = $formatter->getActionLinks();
319 if ( $revert != '' ) {
320 $revert = '<span class="mw-logevent-actionlink">' . $revert . '</span>';
324 $comment = $formatter->getComment();
326 // Some user can hide log items and have review links
327 $del = $this->getShowHideLinks( $row );
330 list( $tagDisplay, $newClasses ) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'logevent' );
331 $classes = array_merge(
332 array( 'mw-logline-' . $entry->getType() ),
336 return Html
::rawElement( 'li', array( 'class' => $classes ),
337 "$del $time $action $comment $revert $tagDisplay" ) . "\n";
344 private function getShowHideLinks( $row ) {
345 if ( ( $this->flags
== self
::NO_ACTION_LINK
) // we don't want to see the links
346 ||
$row->log_type
== 'suppress' ) { // no one can hide items from the suppress log
350 $user = $this->getUser();
351 // Don't show useless checkbox to people who cannot hide log entries
352 if ( $user->isAllowed( 'deletedhistory' ) ) {
353 $canHide = $user->isAllowed( 'deletelogentry' );
354 if ( $row->log_deleted ||
$canHide ) {
355 if ( $canHide && $this->flags
& self
::USE_REVDEL_CHECKBOXES
) { // Show checkboxes instead of links.
356 if ( !self
::userCan( $row, LogPage
::DELETED_RESTRICTED
, $user ) ) { // If event was hidden from sysops
357 $del = Xml
::check( 'deleterevisions', false, array( 'disabled' => 'disabled' ) );
359 $del = Xml
::check( 'showhiderevisions', false, array( 'name' => 'ids[' . $row->log_id
. ']' ) );
362 if ( !self
::userCan( $row, LogPage
::DELETED_RESTRICTED
, $user ) ) { // If event was hidden from sysops
363 $del = Linker
::revDeleteLinkDisabled( $canHide );
366 'target' => SpecialPage
::getTitleFor( 'Log', $row->log_type
)->getPrefixedDBkey(),
368 'ids' => $row->log_id
,
370 $del = Linker
::revDeleteLink( $query, self
::isDeleted( $row, LogPage
::DELETED_RESTRICTED
), $canHide );
380 * @param $type Mixed: string/array
381 * @param $action Mixed: string/array
382 * @param $right string
385 public static function typeAction( $row, $type, $action, $right = '' ) {
386 $match = is_array( $type ) ?
387 in_array( $row->log_type
, $type ) : $row->log_type
== $type;
389 $match = is_array( $action ) ?
390 in_array( $row->log_action
, $action ) : $row->log_action
== $action;
391 if ( $match && $right ) {
393 $match = $wgUser->isAllowed( $right );
400 * Determine if the current user is allowed to view a particular
401 * field of this log row, if it's marked as deleted.
404 * @param $field Integer
405 * @param $user User object to check, or null to use $wgUser
408 public static function userCan( $row, $field, User
$user = null ) {
409 return self
::userCanBitfield( $row->log_deleted
, $field, $user );
413 * Determine if the current user is allowed to view a particular
414 * field of this log row, if it's marked as deleted.
416 * @param $bitfield Integer (current field)
417 * @param $field Integer
418 * @param $user User object to check, or null to use $wgUser
421 public static function userCanBitfield( $bitfield, $field, User
$user = null ) {
422 if ( $bitfield & $field ) {
423 if ( $bitfield & LogPage
::DELETED_RESTRICTED
) {
424 $permission = 'suppressrevision';
426 $permission = 'deletedhistory';
428 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
429 if ( $user === null ) {
433 return $user->isAllowed( $permission );
441 * @param $field Integer: one of DELETED_* bitfield constants
444 public static function isDeleted( $row, $field ) {
445 return ( $row->log_deleted
& $field ) == $field;
449 * Show log extract. Either with text and a box (set $msgKey) or without (don't set $msgKey)
451 * @param $out OutputPage|String-by-reference
452 * @param string|array $types Log types to show
453 * @param string|Title $page The page title to show log entries for
454 * @param string $user The user who made the log entries
455 * @param array $param Associative Array with the following additional options:
456 * - lim Integer Limit of items to show, default is 50
457 * - conds Array Extra conditions for the query (e.g. "log_action != 'revision'")
458 * - showIfEmpty boolean Set to false if you don't want any output in case the loglist is empty
459 * if set to true (default), "No matching items in log" is displayed if loglist is empty
460 * - msgKey Array If you want a nice box with a message, set this to the key of the message.
461 * First element is the message key, additional optional elements are parameters for the key
462 * that are processed with wfMessage
463 * - offset Set to overwrite offset parameter in WebRequest
464 * set to '' to unset offset
465 * - wrap String Wrap the message in html (usually something like "<div ...>$1</div>").
466 * - flags Integer display flags (NO_ACTION_LINK,NO_EXTRA_USER_LINKS)
467 * - useRequestParams boolean Set true to use Pager-related parameters in the WebRequest
468 * @return Integer Number of total log items (not limited by $lim)
470 public static function showLogExtract(
471 &$out, $types = array(), $page = '', $user = '', $param = array()
473 $defaultParameters = array(
476 'showIfEmpty' => true,
477 'msgKey' => array( '' ),
480 'useRequestParams' => false,
482 # The + operator appends elements of remaining keys from the right
483 # handed array to the left handed, whereas duplicated keys are NOT overwritten.
484 $param +
= $defaultParameters;
485 # Convert $param array to individual variables
486 $lim = $param['lim'];
487 $conds = $param['conds'];
488 $showIfEmpty = $param['showIfEmpty'];
489 $msgKey = $param['msgKey'];
490 $wrap = $param['wrap'];
491 $flags = $param['flags'];
492 $useRequestParams = $param['useRequestParams'];
493 if ( !is_array( $msgKey ) ) {
494 $msgKey = array( $msgKey );
497 if ( $out instanceof OutputPage
) {
498 $context = $out->getContext();
500 $context = RequestContext
::getMain();
503 # Insert list of top 50 (or top $lim) items
504 $loglist = new LogEventsList( $context, null, $flags );
505 $pager = new LogPager( $loglist, $types, $user, $page, '', $conds );
506 if ( !$useRequestParams ) {
507 # Reset vars that may have been taken from the request
509 $pager->mDefaultLimit
= 50;
510 $pager->mOffset
= "";
511 $pager->mIsBackwards
= false;
513 if ( isset( $param['offset'] ) ) { # Tell pager to ignore WebRequest offset
514 $pager->setOffset( $param['offset'] );
517 $pager->mLimit
= $lim;
519 $logBody = $pager->getBody();
523 $s = '<div class="mw-warning-with-logexcerpt">';
525 if ( count( $msgKey ) == 1 ) {
526 $s .= $context->msg( $msgKey[0] )->parseAsBlock();
527 } else { // Process additional arguments
529 array_shift( $args );
530 $s .= $context->msg( $msgKey[0], $args )->parseAsBlock();
533 $s .= $loglist->beginLogEventsList() .
535 $loglist->endLogEventsList();
536 } elseif ( $showIfEmpty ) {
537 $s = Html
::rawElement( 'div', array( 'class' => 'mw-warning-logempty' ),
538 $context->msg( 'logempty' )->parse() );
540 if ( $pager->getNumRows() > $pager->mLimit
) { # Show "Full log" link
542 if ( $page instanceof Title
) {
543 $urlParam['page'] = $page->getPrefixedDBkey();
544 } elseif ( $page != '' ) {
545 $urlParam['page'] = $page;
548 $urlParam['user'] = $user;
550 if ( !is_array( $types ) ) { # Make it an array, if it isn't
551 $types = array( $types );
553 # If there is exactly one log type, we can link to Special:Log?type=foo
554 if ( count( $types ) == 1 ) {
555 $urlParam['type'] = $types[0];
558 SpecialPage
::getTitleFor( 'Log' ),
559 $context->msg( 'log-fulllog' )->escaped(),
564 if ( $logBody && $msgKey[0] ) {
568 if ( $wrap != '' ) { // Wrap message in html
569 $s = str_replace( '$1', $s, $wrap );
572 /* hook can return false, if we don't want the message to be emitted (Wikia BugId:7093) */
573 if ( wfRunHooks( 'LogEventsListShowLogExtract', array( &$s, $types, $page, $user, $param ) ) ) {
574 // $out can be either an OutputPage object or a String-by-reference
575 if ( $out instanceof OutputPage
) {
582 return $pager->getNumRows();
586 * SQL clause to skip forbidden log types for this user
588 * @param $db DatabaseBase
589 * @param $audience string, public/user
590 * @param $user User object to check, or null to use $wgUser
591 * @return Mixed: string or false
593 public static function getExcludeClause( $db, $audience = 'public', User
$user = null ) {
594 global $wgLogRestrictions;
596 if ( $audience != 'public' && $user === null ) {
601 // Reset the array, clears extra "where" clauses when $par is used
602 $hiddenLogs = array();
604 // Don't show private logs to unprivileged users
605 foreach ( $wgLogRestrictions as $logType => $right ) {
606 if ( $audience == 'public' ||
!$user->isAllowed( $right ) ) {
607 $hiddenLogs[] = $logType;
610 if ( count( $hiddenLogs ) == 1 ) {
611 return 'log_type != ' . $db->addQuotes( $hiddenLogs[0] );
612 } elseif ( $hiddenLogs ) {
613 return 'log_type NOT IN (' . $db->makeList( $hiddenLogs ) . ')';