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
27 const NO_ACTION_LINK
= 1;
28 const NO_EXTRA_USER_LINKS
= 2;
29 const USE_REVDEL_CHECKBOXES
= 4;
50 protected $mDefaultQuery;
52 public function __construct( $skin, $out, $flags = 0 ) {
55 $this->flags
= $flags;
56 $this->preCacheMessages();
60 * As we use the same small set of messages in various methods and that
61 * they are called often, we call them once and save them in $this->message
63 private function preCacheMessages() {
64 // Precache various messages
65 if( !isset( $this->message
) ) {
66 $messages = array( 'revertmerge', 'protect_change', 'unblocklink', 'change-blocklink',
67 'revertmove', 'undeletelink', 'undeleteviewlink', 'revdel-restore', 'hist', 'diff',
68 'pipe-separator', 'revdel-restore-deleted', 'revdel-restore-visible' );
69 foreach( $messages as $msg ) {
70 $this->message
[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
76 * Set page title and show header for this log type
80 public function showHeader( $type ) {
81 wfDeprecated( __METHOD__
, '1.19' );
82 // If only one log type is used, then show a special message...
83 $headerType = (count($type) == 1) ?
$type[0] : '';
84 if( LogPage
::isLogType( $headerType ) ) {
85 $page = new LogPage( $headerType );
86 $this->out
->setPageTitle( $page->getName()->text() );
87 $this->out
->addHTML( $page->getDescription()->parseAsBlock() );
89 $this->out
->addHTML( wfMsgExt('alllogstext',array('parseinline')) );
94 * Show options for the log list
96 * @param $types string or Array
99 * @param $pattern String
100 * @param $year Integer: year
101 * @param $month Integer: month
102 * @param $filter: array
103 * @param $tagFilter: array?
105 public function showOptions( $types=array(), $user='', $page='', $pattern='', $year='',
106 $month = '', $filter = null, $tagFilter='' ) {
107 global $wgScript, $wgMiserMode;
109 $title = SpecialPage
::getTitleFor( 'Log' );
111 // For B/C, we take strings, but make sure they are converted...
112 $types = ($types === '') ?
array() : (array)$types;
114 $tagSelector = ChangeTags
::buildTagFilterSelector( $tagFilter );
116 $html = Html
::hidden( 'title', $title->getPrefixedDBkey() );
119 $html .= $this->getTypeMenu( $types ) . "\n";
120 $html .= $this->getUserInput( $user ) . "\n";
121 $html .= $this->getTitleInput( $page ) . "\n";
122 $html .= $this->getExtraInputs( $types ) . "\n";
124 // Title pattern, if allowed
126 $html .= $this->getTitlePattern( $pattern ) . "\n";
130 $html .= Xml
::tags( 'p', null, Xml
::dateMenu( $year, $month ) );
134 $html .= Xml
::tags( 'p', null, implode( ' ', $tagSelector ) );
139 $html .= Xml
::tags( 'p', null, $this->getFilterLinks( $filter ) );
143 $html .= Xml
::submitButton( wfMsg( 'allpagessubmit' ) );
146 $html = Xml
::fieldset( wfMsg( 'log' ), $html );
149 $html = Xml
::tags( 'form', array( 'action' => $wgScript, 'method' => 'get' ), $html );
151 $this->out
->addHTML( $html );
155 * @param $filter Array
156 * @return String: Formatted HTML
158 private function getFilterLinks( $filter ) {
161 $messages = array( wfMsgHtml( 'show' ), wfMsgHtml( 'hide' ) );
162 // Option value -> message mapping
164 $hiddens = ''; // keep track for "go" button
165 foreach( $filter as $type => $val ) {
166 // Should the below assignment be outside the foreach?
167 // Then it would have to be copied. Not certain what is more expensive.
168 $query = $this->getDefaultQuery();
169 $queryKey = "hide_{$type}_log";
171 $hideVal = 1 - intval($val);
172 $query[$queryKey] = $hideVal;
174 $link = Linker
::linkKnown(
175 $this->getDisplayTitle(),
181 $links[$type] = wfMsgHtml( "log-show-hide-{$type}", $link );
182 $hiddens .= Html
::hidden( "hide_{$type}_log", $val ) . "\n";
185 return '<small>'.$wgLang->pipeList( $links ) . '</small>' . $hiddens;
188 private function getDefaultQuery() {
191 if ( !isset( $this->mDefaultQuery
) ) {
192 $this->mDefaultQuery
= $wgRequest->getQueryValues();
193 unset( $this->mDefaultQuery
['title'] );
194 unset( $this->mDefaultQuery
['dir'] );
195 unset( $this->mDefaultQuery
['offset'] );
196 unset( $this->mDefaultQuery
['limit'] );
197 unset( $this->mDefaultQuery
['order'] );
198 unset( $this->mDefaultQuery
['month'] );
199 unset( $this->mDefaultQuery
['year'] );
201 return $this->mDefaultQuery
;
205 * Get the Title object of the page the links should point to.
206 * This is NOT the Title of the page the entries should be restricted to.
208 * @return Title object
210 public function getDisplayTitle() {
211 return $this->out
->getTitle();
214 public function getContext() {
215 return $this->out
->getContext();
219 * @param $queryTypes Array
220 * @return String: Formatted HTML
222 private function getTypeMenu( $queryTypes ) {
223 $queryType = count($queryTypes) == 1 ?
$queryTypes[0] : '';
224 $selector = $this->getTypeSelector();
225 $selector->setDefault( $queryType );
226 return $selector->getHtml();
230 * Returns log page selector.
234 public function getTypeSelector() {
237 $typesByName = array(); // Temporary array
238 // First pass to load the log names
239 foreach( LogPage
::validTypes() as $type ) {
240 $page = new LogPage( $type );
241 $restriction = $page->getRestriction();
242 if ( $wgUser->isAllowed( $restriction ) ) {
243 $typesByName[$type] = $page->getName()->text();
247 // Second pass to sort by name
250 // Always put "All public logs" on top
251 $public = $typesByName[''];
252 unset( $typesByName[''] );
253 $typesByName = array( '' => $public ) +
$typesByName;
255 $select = new XmlSelect( 'type' );
256 foreach( $typesByName as $type => $name ) {
257 $select->addOption( $name, $type );
264 * @param $user String
265 * @return String: Formatted HTML
267 private function getUserInput( $user ) {
268 return '<span style="white-space: nowrap">' .
269 Xml
::inputLabel( wfMsg( 'specialloguserlabel' ), 'user', 'mw-log-user', 15, $user ) .
274 * @param $title String
275 * @return String: Formatted HTML
277 private function getTitleInput( $title ) {
278 return '<span style="white-space: nowrap">' .
279 Xml
::inputLabel( wfMsg( 'speciallogtitlelabel' ), 'page', 'mw-log-page', 20, $title ) .
285 * @return string Checkbox
287 private function getTitlePattern( $pattern ) {
288 return '<span style="white-space: nowrap">' .
289 Xml
::checkLabel( wfMsg( 'log-title-wildcard' ), 'pattern', 'pattern', $pattern ) .
297 private function getExtraInputs( $types ) {
299 $offender = $wgRequest->getVal('offender');
300 $user = User
::newFromName( $offender, false );
301 if( !$user ||
($user->getId() == 0 && !IP
::isIPAddress($offender) ) ) {
302 $offender = ''; // Blank field if invalid
304 if( count($types) == 1 && $types[0] == 'suppress' ) {
305 return Xml
::inputLabel( wfMsg('revdelete-offender'), 'offender',
306 'mw-log-offender', 20, $offender );
314 public function beginLogEventsList() {
321 public function endLogEventsList() {
326 * @param $row Row: a single row from the result set
327 * @return String: Formatted HTML list item
329 public function logLine( $row ) {
330 $entry = DatabaseLogEntry
::newFromRow( $row );
331 $formatter = LogFormatter
::newFromEntry( $entry );
332 $formatter->setShowUserToolLinks( !( $this->flags
& self
::NO_EXTRA_USER_LINKS
) );
334 $action = $formatter->getActionText();
335 $comment = $formatter->getComment();
337 $classes = array( 'mw-logline-' . $entry->getType() );
338 $title = $entry->getTarget();
339 $time = $this->logTimestamp( $entry );
341 // Extract extra parameters
342 $paramArray = LogPage
::extractParams( $row->log_params
);
343 // Add review/revert links and such...
344 $revert = $this->logActionLinks( $row, $title, $paramArray, $comment );
346 // Some user can hide log items and have review links
347 $del = $this->getShowHideLinks( $row );
348 if( $del != '' ) $del .= ' ';
351 list( $tagDisplay, $newClasses ) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'logevent' );
352 $classes = array_merge( $classes, $newClasses );
354 return Xml
::tags( 'li', array( "class" => implode( ' ', $classes ) ),
355 $del . "$time $action $comment $revert $tagDisplay" ) . "\n";
358 private function logTimestamp( LogEntry
$entry ) {
360 $time = $wgLang->timeanddate( wfTimestamp( TS_MW
, $entry->getTimestamp() ), true );
361 return htmlspecialchars( $time );
368 * @param Title $title
369 * @param Array $paramArray
373 private function logActionLinks( $row, $title, $paramArray, &$comment ) {
375 if( ( $this->flags
& self
::NO_ACTION_LINK
) // we don't want to see the action
376 || self
::isDeleted( $row, LogPage
::DELETED_ACTION
) ) // action is hidden
381 if( self
::typeAction( $row, 'move', 'move', 'move' ) && !empty( $paramArray[0] ) ) {
382 $destTitle = Title
::newFromText( $paramArray[0] );
384 $revert = Linker
::linkKnown(
385 SpecialPage
::getTitleFor( 'Movepage' ),
386 $this->message
['revertmove'],
389 'wpOldTitle' => $destTitle->getPrefixedDBkey(),
390 'wpNewTitle' => $title->getPrefixedDBkey(),
391 'wpReason' => wfMsgForContent( 'revertmove' ),
395 $revert = wfMessage( 'parentheses' )->rawParams( $revert )->escaped();
397 // Show undelete link
398 } elseif( self
::typeAction( $row, array( 'delete', 'suppress' ), 'delete', 'deletedhistory' ) ) {
399 if( !$wgUser->isAllowed( 'undelete' ) ) {
400 $viewdeleted = $this->message
['undeleteviewlink'];
402 $viewdeleted = $this->message
['undeletelink'];
404 $revert = Linker
::linkKnown(
405 SpecialPage
::getTitleFor( 'Undelete' ),
408 array( 'target' => $title->getPrefixedDBkey() )
410 $revert = wfMessage( 'parentheses' )->rawParams( $revert )->escaped();
411 // Show unblock/change block link
412 } elseif( self
::typeAction( $row, array( 'block', 'suppress' ), array( 'block', 'reblock' ), 'block' ) ) {
413 $revert = Linker
::linkKnown(
414 SpecialPage
::getTitleFor( 'Unblock', $row->log_title
),
415 $this->message
['unblocklink']
417 $this->message
['pipe-separator'] .
419 SpecialPage
::getTitleFor( 'Block', $row->log_title
),
420 $this->message
['change-blocklink']
422 $revert = wfMessage( 'parentheses' )->rawParams( $revert )->escaped();
423 // Show change protection link
424 } elseif( self
::typeAction( $row, 'protect', array( 'modify', 'protect', 'unprotect' ) ) ) {
425 $revert .= Linker
::link( $title,
426 $this->message
['hist'],
429 'action' => 'history',
430 'offset' => $row->log_timestamp
433 if( $wgUser->isAllowed( 'protect' ) ) {
434 $revert .= $this->message
['pipe-separator'] .
435 Linker
::link( $title,
436 $this->message
['protect_change'],
438 array( 'action' => 'protect' ),
441 $revert = ' ' . wfMessage( 'parentheses' )->rawParams( $revert )->escaped();
443 } elseif( self
::typeAction( $row, 'merge', 'merge', 'mergehistory' ) ) {
444 $revert = Linker
::linkKnown(
445 SpecialPage
::getTitleFor( 'MergeHistory' ),
446 $this->message
['revertmerge'],
449 'target' => $paramArray[0],
450 'dest' => $title->getPrefixedDBkey(),
451 'mergepoint' => $paramArray[1]
454 $revert = wfMessage( 'parentheses' )->rawParams( $revert )->escaped();
455 // If an edit was hidden from a page give a review link to the history
456 } elseif( self
::typeAction( $row, array( 'delete', 'suppress' ), 'revision', 'deletedhistory' ) ) {
457 $revert = RevisionDeleter
::getLogLinks( $title, $paramArray,
459 // Hidden log items, give review link
460 } elseif( self
::typeAction( $row, array( 'delete', 'suppress' ), 'event', 'deletedhistory' ) ) {
461 if( count($paramArray) >= 1 ) {
462 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
463 // $paramArray[1] is a CSV of the IDs
464 $query = $paramArray[0];
465 // Link to each hidden object ID, $paramArray[1] is the url param
466 $revert = Linker
::linkKnown(
468 $this->message
['revdel-restore'],
471 'target' => $title->getPrefixedText(),
476 $revert = wfMessage( 'parentheses' )->rawParams( $revert )->escaped();
478 // Do nothing. The implementation is handled by the hook modifiying the passed-by-ref parameters.
480 wfRunHooks( 'LogLine', array( $row->log_type
, $row->log_action
, $title, $paramArray,
481 &$comment, &$revert, $row->log_timestamp
) );
483 if( $revert != '' ) {
484 $revert = '<span class="mw-logevent-actionlink">' . $revert . '</span>';
493 private function getShowHideLinks( $row ) {
495 if( ( $this->flags
== self
::NO_ACTION_LINK
) // we don't want to see the links
496 ||
$row->log_type
== 'suppress' ) { // no one can hide items from the suppress log
500 // Don't show useless checkbox to people who cannot hide revisions
501 if( $wgUser->isAllowed( 'deletedhistory' ) ) {
502 if( $row->log_deleted ||
$wgUser->isAllowed( 'deleterevision' ) ) {
503 $canHide = $wgUser->isAllowed( 'deleterevision' );
504 if ( $this->flags
& self
::USE_REVDEL_CHECKBOXES
) { // Show checkboxes instead of links.
505 if ( !self
::userCan( $row, LogPage
::DELETED_RESTRICTED
) ) { // If event was hidden from sysops
506 $del = Xml
::check( 'deleterevisions', false, array( 'disabled' => 'disabled' ) );
508 $del = Xml
::check( 'showhiderevisions', false, array( 'name' => 'ids[' . $row->log_id
. ']' ) );
511 if ( !self
::userCan( $row, LogPage
::DELETED_RESTRICTED
) ) { // If event was hidden from sysops
512 $del = Linker
::revDeleteLinkDisabled( $canHide );
515 'target' => SpecialPage
::getTitleFor( 'Log', $row->log_type
)->getPrefixedDBkey(),
517 'ids' => $row->log_id
,
519 $del = Linker
::revDeleteLink( $query, self
::isDeleted( $row, LogPage
::DELETED_RESTRICTED
), $canHide );
529 * @param $type Mixed: string/array
530 * @param $action Mixed: string/array
531 * @param $right string
534 public static function typeAction( $row, $type, $action, $right='' ) {
535 $match = is_array($type) ?
536 in_array( $row->log_type
, $type ) : $row->log_type
== $type;
538 $match = is_array( $action ) ?
539 in_array( $row->log_action
, $action ) : $row->log_action
== $action;
540 if( $match && $right ) {
542 $match = $wgUser->isAllowed( $right );
549 * Determine if the current user is allowed to view a particular
550 * field of this log row, if it's marked as deleted.
553 * @param $field Integer
554 * @param $user User object to check, or null to use $wgUser
557 public static function userCan( $row, $field, User
$user = null ) {
558 return self
::userCanBitfield( $row->log_deleted
, $field, $user );
562 * Determine if the current user is allowed to view a particular
563 * field of this log row, if it's marked as deleted.
565 * @param $bitfield Integer (current field)
566 * @param $field Integer
567 * @param $user User object to check, or null to use $wgUser
570 public static function userCanBitfield( $bitfield, $field, User
$user = null ) {
571 if( $bitfield & $field ) {
572 if ( $bitfield & LogPage
::DELETED_RESTRICTED
) {
573 $permission = 'suppressrevision';
575 $permission = 'deletedhistory';
577 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
578 if ( $user === null ) {
582 return $user->isAllowed( $permission );
590 * @param $field Integer: one of DELETED_* bitfield constants
593 public static function isDeleted( $row, $field ) {
594 return ( $row->log_deleted
& $field ) == $field;
598 * Show log extract. Either with text and a box (set $msgKey) or without (don't set $msgKey)
600 * @param $out OutputPage|String-by-reference
601 * @param $types String|Array Log types to show
602 * @param $page String|Title The page title to show log entries for
603 * @param $user String The user who made the log entries
604 * @param $param array Associative Array with the following additional options:
605 * - lim Integer Limit of items to show, default is 50
606 * - conds Array Extra conditions for the query (e.g. "log_action != 'revision'")
607 * - showIfEmpty boolean Set to false if you don't want any output in case the loglist is empty
608 * if set to true (default), "No matching items in log" is displayed if loglist is empty
609 * - msgKey Array If you want a nice box with a message, set this to the key of the message.
610 * First element is the message key, additional optional elements are parameters for the key
611 * that are processed with wfMsgExt and option 'parse'
612 * - offset Set to overwrite offset parameter in $wgRequest
613 * set to '' to unset offset
614 * - wrap String Wrap the message in html (usually something like "<div ...>$1</div>").
615 * - flags Integer display flags (NO_ACTION_LINK,NO_EXTRA_USER_LINKS)
616 * @return Integer Number of total log items (not limited by $lim)
618 public static function showLogExtract(
619 &$out, $types=array(), $page='', $user='', $param = array()
621 $defaultParameters = array(
624 'showIfEmpty' => true,
625 'msgKey' => array(''),
629 # The + operator appends elements of remaining keys from the right
630 # handed array to the left handed, whereas duplicated keys are NOT overwritten.
631 $param +
= $defaultParameters;
632 # Convert $param array to individual variables
633 $lim = $param['lim'];
634 $conds = $param['conds'];
635 $showIfEmpty = $param['showIfEmpty'];
636 $msgKey = $param['msgKey'];
637 $wrap = $param['wrap'];
638 $flags = $param['flags'];
639 if ( !is_array( $msgKey ) ) {
640 $msgKey = array( $msgKey );
643 if ( $out instanceof OutputPage
) {
644 $context = $out->getContext();
646 $context = RequestContext
::getMain();
649 # Insert list of top 50 (or top $lim) items
650 $loglist = new LogEventsList( $context->getSkin(), $context->getOutput(), $flags );
651 $pager = new LogPager( $loglist, $types, $user, $page, '', $conds );
652 if ( isset( $param['offset'] ) ) { # Tell pager to ignore $wgRequest offset
653 $pager->setOffset( $param['offset'] );
655 if( $lim > 0 ) $pager->mLimit
= $lim;
656 $logBody = $pager->getBody();
660 $s = '<div class="mw-warning-with-logexcerpt">';
662 if ( count( $msgKey ) == 1 ) {
663 $s .= wfMsgExt( $msgKey[0], array( 'parse' ) );
664 } else { // Process additional arguments
666 array_shift( $args );
667 $s .= wfMsgExt( $msgKey[0], array( 'parse' ), $args );
670 $s .= $loglist->beginLogEventsList() .
672 $loglist->endLogEventsList();
674 if ( $showIfEmpty ) {
675 $s = Html
::rawElement( 'div', array( 'class' => 'mw-warning-logempty' ),
676 wfMsgExt( 'logempty', array( 'parseinline' ) ) );
679 if( $pager->getNumRows() > $pager->mLimit
) { # Show "Full log" link
681 if ( $page instanceof Title
) {
682 $urlParam['page'] = $page->getPrefixedDBkey();
683 } elseif ( $page != '' ) {
684 $urlParam['page'] = $page;
687 $urlParam['user'] = $user;
688 if ( !is_array( $types ) ) # Make it an array, if it isn't
689 $types = array( $types );
690 # If there is exactly one log type, we can link to Special:Log?type=foo
691 if ( count( $types ) == 1 )
692 $urlParam['type'] = $types[0];
694 SpecialPage
::getTitleFor( 'Log' ),
695 wfMsgHtml( 'log-fulllog' ),
700 if ( $logBody && $msgKey[0] ) {
704 if ( $wrap != '' ) { // Wrap message in html
705 $s = str_replace( '$1', $s, $wrap );
708 /* hook can return false, if we don't want the message to be emitted (Wikia BugId:7093) */
709 if ( wfRunHooks( 'LogEventsListShowLogExtract', array( &$s, $types, $page, $user, $param ) ) ) {
710 // $out can be either an OutputPage object or a String-by-reference
711 if ( $out instanceof OutputPage
){
718 return $pager->getNumRows();
722 * SQL clause to skip forbidden log types for this user
724 * @param $db DatabaseBase
725 * @param $audience string, public/user
726 * @return Mixed: string or false
728 public static function getExcludeClause( $db, $audience = 'public' ) {
729 global $wgLogRestrictions, $wgUser;
730 // Reset the array, clears extra "where" clauses when $par is used
731 $hiddenLogs = array();
732 // Don't show private logs to unprivileged users
733 foreach( $wgLogRestrictions as $logType => $right ) {
734 if( $audience == 'public' ||
!$wgUser->isAllowed($right) ) {
735 $safeType = $db->strencode( $logType );
736 $hiddenLogs[] = $safeType;
739 if( count($hiddenLogs) == 1 ) {
740 return 'log_type != ' . $db->addQuotes( $hiddenLogs[0] );
741 } elseif( $hiddenLogs ) {
742 return 'log_type NOT IN (' . $db->makeList($hiddenLogs) . ')';