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;
49 protected $mDefaultQuery;
51 public function __construct( $skin, $out, $flags = 0 ) {
54 $this->flags
= $flags;
55 $this->preCacheMessages();
59 * As we use the same small set of messages in various methods and that
60 * they are called often, we call them once and save them in $this->message
62 private function preCacheMessages() {
63 // Precache various messages
64 if( !isset( $this->message
) ) {
65 $messages = array( 'revertmerge', 'protect_change', 'unblocklink', 'change-blocklink',
66 'revertmove', 'undeletelink', 'undeleteviewlink', 'revdel-restore', 'hist', 'diff',
67 'pipe-separator', 'revdel-restore-deleted', 'revdel-restore-visible' );
68 foreach( $messages as $msg ) {
69 $this->message
[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
75 * Set page title and show header for this log type
79 public function showHeader( $type ) {
80 wfDeprecated( __METHOD__
);
81 // If only one log type is used, then show a special message...
82 $headerType = (count($type) == 1) ?
$type[0] : '';
83 if( LogPage
::isLogType( $headerType ) ) {
84 $page = new LogPage( $headerType );
85 $this->out
->setPageTitle( $page->getName()->text() );
86 $this->out
->addHTML( $page->getDescription()->parseAsBlock() );
88 $this->out
->addHTML( wfMsgExt('alllogstext',array('parseinline')) );
93 * Show options for the log list
95 * @param $types string or Array
98 * @param $pattern String
99 * @param $year Integer: year
100 * @param $month Integer: month
101 * @param $filter: array
102 * @param $tagFilter: array?
104 public function showOptions( $types=array(), $user='', $page='', $pattern='', $year='',
105 $month = '', $filter = null, $tagFilter='' ) {
106 global $wgScript, $wgMiserMode;
109 $title = SpecialPage
::getTitleFor( 'Log' );
110 $special = $title->getPrefixedDBkey();
112 // For B/C, we take strings, but make sure they are converted...
113 $types = ($types === '') ?
array() : (array)$types;
115 $tagSelector = ChangeTags
::buildTagFilterSelector( $tagFilter );
117 $html = Html
::hidden( 'title', $special );
120 $html .= $this->getTypeMenu( $types ) . "\n";
121 $html .= $this->getUserInput( $user ) . "\n";
122 $html .= $this->getTitleInput( $page ) . "\n";
123 $html .= $this->getExtraInputs( $types ) . "\n";
125 // Title pattern, if allowed
127 $html .= $this->getTitlePattern( $pattern ) . "\n";
131 $html .= Xml
::tags( 'p', null, Xml
::dateMenu( $year, $month ) );
135 $html .= Xml
::tags( 'p', null, implode( ' ', $tagSelector ) );
140 $html .= Xml
::tags( 'p', null, $this->getFilterLinks( $filter ) );
144 $html .= Xml
::submitButton( wfMsg( 'allpagessubmit' ) );
147 $html = Xml
::fieldset( wfMsg( 'log' ), $html );
150 $html = Xml
::tags( 'form', array( 'action' => $action, 'method' => 'get' ), $html );
152 $this->out
->addHTML( $html );
156 * @param $filter Array
157 * @return String: Formatted HTML
159 private function getFilterLinks( $filter ) {
162 $messages = array( wfMsgHtml( 'show' ), wfMsgHtml( 'hide' ) );
163 // Option value -> message mapping
165 $hiddens = ''; // keep track for "go" button
166 foreach( $filter as $type => $val ) {
167 // Should the below assignment be outside the foreach?
168 // Then it would have to be copied. Not certain what is more expensive.
169 $query = $this->getDefaultQuery();
170 $queryKey = "hide_{$type}_log";
172 $hideVal = 1 - intval($val);
173 $query[$queryKey] = $hideVal;
175 $link = Linker
::link(
176 $this->getDisplayTitle(),
180 array( 'known', 'noclasses' )
183 $links[$type] = wfMsgHtml( "log-show-hide-{$type}", $link );
184 $hiddens .= Html
::hidden( "hide_{$type}_log", $val ) . "\n";
187 return '<small>'.$wgLang->pipeList( $links ) . '</small>' . $hiddens;
190 private function getDefaultQuery() {
193 if ( !isset( $this->mDefaultQuery
) ) {
194 $this->mDefaultQuery
= $wgRequest->getQueryValues();
195 unset( $this->mDefaultQuery
['title'] );
196 unset( $this->mDefaultQuery
['dir'] );
197 unset( $this->mDefaultQuery
['offset'] );
198 unset( $this->mDefaultQuery
['limit'] );
199 unset( $this->mDefaultQuery
['order'] );
200 unset( $this->mDefaultQuery
['month'] );
201 unset( $this->mDefaultQuery
['year'] );
203 return $this->mDefaultQuery
;
207 * Get the Title object of the page the links should point to.
208 * This is NOT the Title of the page the entries should be restricted to.
210 * @return Title object
212 public function getDisplayTitle() {
213 return $this->out
->getTitle();
216 public function getContext() {
217 return $this->out
->getContext();
221 * @param $queryTypes Array
222 * @return String: Formatted HTML
224 private function getTypeMenu( $queryTypes ) {
225 $queryType = count($queryTypes) == 1 ?
$queryTypes[0] : '';
226 $selector = $this->getTypeSelector();
227 $selector->setDefault( $queryType );
228 return $selector->getHtml();
232 * Returns log page selector.
233 * @param $default string The default selection
237 public function getTypeSelector() {
240 $typesByName = array(); // Temporary array
241 // First pass to load the log names
242 foreach( LogPage
::validTypes() as $type ) {
243 $page = new LogPage( $type );
244 $restriction = $page->getRestriction();
245 if ( $wgUser->isAllowed( $restriction ) ) {
246 $typesByName[$type] = $page->getName()->text();
250 // Second pass to sort by name
253 // Always put "All public logs" on top
254 $public = $typesByName[''];
255 unset( $typesByName[''] );
256 $typesByName = array( '' => $public ) +
$typesByName;
258 $select = new XmlSelect( 'type' );
259 foreach( $typesByName as $type => $name ) {
260 $select->addOption( $name, $type );
267 * @param $user String
268 * @return String: Formatted HTML
270 private function getUserInput( $user ) {
271 return '<span style="white-space: nowrap">' .
272 Xml
::inputLabel( wfMsg( 'specialloguserlabel' ), 'user', 'mw-log-user', 15, $user ) .
277 * @param $title String
278 * @return String: Formatted HTML
280 private function getTitleInput( $title ) {
281 return '<span style="white-space: nowrap">' .
282 Xml
::inputLabel( wfMsg( 'speciallogtitlelabel' ), 'page', 'mw-log-page', 20, $title ) .
288 * @return string Checkbox
290 private function getTitlePattern( $pattern ) {
291 return '<span style="white-space: nowrap">' .
292 Xml
::checkLabel( wfMsg( 'log-title-wildcard' ), 'pattern', 'pattern', $pattern ) .
300 private function getExtraInputs( $types ) {
302 $offender = $wgRequest->getVal('offender');
303 $user = User
::newFromName( $offender, false );
304 if( !$user ||
($user->getId() == 0 && !IP
::isIPAddress($offender) ) ) {
305 $offender = ''; // Blank field if invalid
307 if( count($types) == 1 && $types[0] == 'suppress' ) {
308 return Xml
::inputLabel( wfMsg('revdelete-offender'), 'offender',
309 'mw-log-offender', 20, $offender );
317 public function beginLogEventsList() {
324 public function endLogEventsList() {
329 * @param $row Row: a single row from the result set
330 * @return String: Formatted HTML list item
332 public function logLine( $row ) {
333 $entry = DatabaseLogEntry
::newFromRow( $row );
334 $formatter = LogFormatter
::newFromEntry( $entry );
335 $formatter->setShowUserToolLinks( !( $this->flags
& self
::NO_EXTRA_USER_LINKS
) );
337 $action = $formatter->getActionText();
338 $comment = $formatter->getComment();
340 $classes = array( 'mw-logline-' . $entry->getType() );
341 $title = $entry->getTarget();
342 $time = $this->logTimestamp( $entry );
344 // Extract extra parameters
345 $paramArray = LogPage
::extractParams( $row->log_params
);
346 // Add review/revert links and such...
347 $revert = $this->logActionLinks( $row, $title, $paramArray, $comment );
349 // Some user can hide log items and have review links
350 $del = $this->getShowHideLinks( $row );
351 if( $del != '' ) $del .= ' ';
354 list( $tagDisplay, $newClasses ) = ChangeTags
::formatSummaryRow( $row->ts_tags
, 'logevent' );
355 $classes = array_merge( $classes, $newClasses );
357 return Xml
::tags( 'li', array( "class" => implode( ' ', $classes ) ),
358 $del . "$time $action $comment $revert $tagDisplay" ) . "\n";
361 private function logTimestamp( LogEntry
$entry ) {
363 $time = $wgLang->timeanddate( wfTimestamp( TS_MW
, $entry->getTimestamp() ), true );
364 return htmlspecialchars( $time );
371 * @param Title $title
372 * @param Array $paramArray
376 private function logActionLinks( $row, $title, $paramArray, &$comment ) {
378 if( ( $this->flags
& self
::NO_ACTION_LINK
) // we don't want to see the action
379 || self
::isDeleted( $row, LogPage
::DELETED_ACTION
) ) // action is hidden
384 if( self
::typeAction( $row, 'move', 'move', 'move' ) && !empty( $paramArray[0] ) ) {
385 $destTitle = Title
::newFromText( $paramArray[0] );
387 $revert = '(' . Linker
::link(
388 SpecialPage
::getTitleFor( 'Movepage' ),
389 $this->message
['revertmove'],
392 'wpOldTitle' => $destTitle->getPrefixedDBkey(),
393 'wpNewTitle' => $title->getPrefixedDBkey(),
394 'wpReason' => wfMsgForContent( 'revertmove' ),
397 array( 'known', 'noclasses' )
400 // Show undelete link
401 } elseif( self
::typeAction( $row, array( 'delete', 'suppress' ), 'delete', 'deletedhistory' ) ) {
402 if( !$wgUser->isAllowed( 'undelete' ) ) {
403 $viewdeleted = $this->message
['undeleteviewlink'];
405 $viewdeleted = $this->message
['undeletelink'];
407 $revert = '(' . Linker
::link(
408 SpecialPage
::getTitleFor( 'Undelete' ),
411 array( 'target' => $title->getPrefixedDBkey() ),
412 array( 'known', 'noclasses' )
414 // Show unblock/change block link
415 } elseif( self
::typeAction( $row, array( 'block', 'suppress' ), array( 'block', 'reblock' ), 'block' ) ) {
418 SpecialPage
::getTitleFor( 'Unblock', $row->log_title
),
419 $this->message
['unblocklink'],
424 $this->message
['pipe-separator'] .
426 SpecialPage
::getTitleFor( 'Block', $row->log_title
),
427 $this->message
['change-blocklink'],
433 // Show change protection link
434 } elseif( self
::typeAction( $row, 'protect', array( 'modify', 'protect', 'unprotect' ) ) ) {
436 Linker
::link( $title,
437 $this->message
['hist'],
440 'action' => 'history',
441 'offset' => $row->log_timestamp
444 if( $wgUser->isAllowed( 'protect' ) ) {
445 $revert .= $this->message
['pipe-separator'] .
446 Linker
::link( $title,
447 $this->message
['protect_change'],
449 array( 'action' => 'protect' ),
454 } elseif( self
::typeAction( $row, 'merge', 'merge', 'mergehistory' ) ) {
455 $revert = '(' . Linker
::link(
456 SpecialPage
::getTitleFor( 'MergeHistory' ),
457 $this->message
['revertmerge'],
460 'target' => $paramArray[0],
461 'dest' => $title->getPrefixedDBkey(),
462 'mergepoint' => $paramArray[1]
464 array( 'known', 'noclasses' )
466 // If an edit was hidden from a page give a review link to the history
467 } elseif( self
::typeAction( $row, array( 'delete', 'suppress' ), 'revision', 'deletedhistory' ) ) {
468 $revert = RevisionDeleter
::getLogLinks( $title, $paramArray,
469 $this->skin
, $this->message
);
470 // Hidden log items, give review link
471 } elseif( self
::typeAction( $row, array( 'delete', 'suppress' ), 'event', 'deletedhistory' ) ) {
472 if( count($paramArray) >= 1 ) {
473 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
474 // $paramArray[1] is a CSV of the IDs
475 $query = $paramArray[0];
476 // Link to each hidden object ID, $paramArray[1] is the url param
477 $revert = '(' . Linker
::link(
479 $this->message
['revdel-restore'],
482 'target' => $title->getPrefixedText(),
486 array( 'known', 'noclasses' )
489 // Self-created users
490 } elseif( self
::typeAction( $row, 'newusers', 'create2' ) ) {
491 if( isset( $paramArray[0] ) ) {
492 $revert = Linker
::userToolLinks( $paramArray[0], $title->getDBkey(), true );
494 # Fall back to a blue contributions link
495 $revert = Linker
::userToolLinks( 1, $title->getDBkey() );
497 if( wfTimestamp( TS_MW
, $row->log_timestamp
) < '20080129000000' ) {
498 # Suppress $comment from old entries (before 2008-01-29),
499 # not needed and can contain incorrect links
502 // Do nothing. The implementation is handled by the hook modifiying the passed-by-ref parameters.
504 wfRunHooks( 'LogLine', array( $row->log_type
, $row->log_action
, $title, $paramArray,
505 &$comment, &$revert, $row->log_timestamp
) );
507 if( $revert != '' ) {
508 $revert = '<span class="mw-logevent-actionlink">' . $revert . '</span>';
517 private function getShowHideLinks( $row ) {
519 if( ( $this->flags
& self
::NO_ACTION_LINK
) // we don't want to see the links
520 ||
$row->log_type
== 'suppress' ) { // no one can hide items from the suppress log
524 // Don't show useless link to people who cannot hide revisions
525 if( $wgUser->isAllowed( 'deletedhistory' ) && !$wgUser->isBlocked() ) {
526 if( $row->log_deleted ||
$wgUser->isAllowed( 'deleterevision' ) ) {
527 $canHide = $wgUser->isAllowed( 'deleterevision' );
528 // If event was hidden from sysops
529 if( !self
::userCan( $row, LogPage
::DELETED_RESTRICTED
) ) {
530 $del = Linker
::revDeleteLinkDisabled( $canHide );
532 $target = SpecialPage
::getTitleFor( 'Log', $row->log_type
);
534 'target' => $target->getPrefixedDBkey(),
536 'ids' => $row->log_id
,
538 $del = Linker
::revDeleteLink( $query,
539 self
::isDeleted( $row, LogPage
::DELETED_RESTRICTED
), $canHide );
548 * @param $type Mixed: string/array
549 * @param $action Mixed: string/array
550 * @param $right string
553 public static function typeAction( $row, $type, $action, $right='' ) {
554 $match = is_array($type) ?
555 in_array( $row->log_type
, $type ) : $row->log_type
== $type;
557 $match = is_array( $action ) ?
558 in_array( $row->log_action
, $action ) : $row->log_action
== $action;
559 if( $match && $right ) {
561 $match = $wgUser->isAllowed( $right );
568 * Determine if the current user is allowed to view a particular
569 * field of this log row, if it's marked as deleted.
572 * @param $field Integer
575 public static function userCan( $row, $field ) {
576 return self
::userCanBitfield( $row->log_deleted
, $field );
580 * Determine if the current user is allowed to view a particular
581 * field of this log row, if it's marked as deleted.
583 * @param $bitfield Integer (current field)
584 * @param $field Integer
587 public static function userCanBitfield( $bitfield, $field ) {
588 if( $bitfield & $field ) {
591 if ( $bitfield & LogPage
::DELETED_RESTRICTED
) {
592 $permission = 'suppressrevision';
594 $permission = 'deletedhistory';
596 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
597 return $wgUser->isAllowed( $permission );
605 * @param $field Integer: one of DELETED_* bitfield constants
608 public static function isDeleted( $row, $field ) {
609 return ( $row->log_deleted
& $field ) == $field;
613 * Show log extract. Either with text and a box (set $msgKey) or without (don't set $msgKey)
615 * @param $out OutputPage|String-by-reference
616 * @param $types String or Array
617 * @param $page String The page title to show log entries for
618 * @param $user String The user who made the log entries
619 * @param $param Associative Array with the following additional options:
620 * - lim Integer Limit of items to show, default is 50
621 * - conds Array Extra conditions for the query (e.g. "log_action != 'revision'")
622 * - showIfEmpty boolean Set to false if you don't want any output in case the loglist is empty
623 * if set to true (default), "No matching items in log" is displayed if loglist is empty
624 * - msgKey Array If you want a nice box with a message, set this to the key of the message.
625 * First element is the message key, additional optional elements are parameters for the key
626 * that are processed with wgMsgExt and option 'parse'
627 * - offset Set to overwrite offset parameter in $wgRequest
628 * set to '' to unset offset
629 * - wrap String Wrap the message in html (usually something like "<div ...>$1</div>").
630 * - flags Integer display flags (NO_ACTION_LINK,NO_EXTRA_USER_LINKS)
631 * @return Integer Number of total log items (not limited by $lim)
633 public static function showLogExtract(
634 &$out, $types=array(), $page='', $user='', $param = array()
636 $defaultParameters = array(
639 'showIfEmpty' => true,
640 'msgKey' => array(''),
644 # The + operator appends elements of remaining keys from the right
645 # handed array to the left handed, whereas duplicated keys are NOT overwritten.
646 $param +
= $defaultParameters;
647 # Convert $param array to individual variables
648 $lim = $param['lim'];
649 $conds = $param['conds'];
650 $showIfEmpty = $param['showIfEmpty'];
651 $msgKey = $param['msgKey'];
652 $wrap = $param['wrap'];
653 $flags = $param['flags'];
654 if ( !is_array( $msgKey ) ) {
655 $msgKey = array( $msgKey );
658 if ( $out instanceof OutputPage
) {
659 $context = $out->getContext();
661 $context = RequestContext
::getMain();
664 # Insert list of top 50 (or top $lim) items
665 $loglist = new LogEventsList( $context->getSkin(), $context->getOutput(), $flags );
666 $pager = new LogPager( $loglist, $types, $user, $page, '', $conds );
667 if ( isset( $param['offset'] ) ) { # Tell pager to ignore $wgRequest offset
668 $pager->setOffset( $param['offset'] );
670 if( $lim > 0 ) $pager->mLimit
= $lim;
671 $logBody = $pager->getBody();
675 $s = '<div class="mw-warning-with-logexcerpt">';
677 if ( count( $msgKey ) == 1 ) {
678 $s .= wfMsgExt( $msgKey[0], array( 'parse' ) );
679 } else { // Process additional arguments
681 array_shift( $args );
682 $s .= wfMsgExt( $msgKey[0], array( 'parse' ), $args );
685 $s .= $loglist->beginLogEventsList() .
687 $loglist->endLogEventsList();
690 $s = Html
::rawElement( 'div', array( 'class' => 'mw-warning-logempty' ),
691 wfMsgExt( 'logempty', array( 'parseinline' ) ) );
693 if( $pager->getNumRows() > $pager->mLimit
) { # Show "Full log" link
696 $urlParam['page'] = $page;
698 $urlParam['user'] = $user;
699 if ( !is_array( $types ) ) # Make it an array, if it isn't
700 $types = array( $types );
701 # If there is exactly one log type, we can link to Special:Log?type=foo
702 if ( count( $types ) == 1 )
703 $urlParam['type'] = $types[0];
705 SpecialPage
::getTitleFor( 'Log' ),
706 wfMsgHtml( 'log-fulllog' ),
711 if ( $logBody && $msgKey[0] ) {
715 if ( $wrap!='' ) { // Wrap message in html
716 $s = str_replace( '$1', $s, $wrap );
719 /* hook can return false, if we don't want the message to be emitted (Wikia BugId:7093) */
720 if ( !wfRunHooks( 'LogEventsListShowLogExtract', array( &$s, $types, $page, $user, $param ) ) ) {
721 return $pager->getNumRows();
724 // $out can be either an OutputPage object or a String-by-reference
725 if( $out instanceof OutputPage
){
730 return $pager->getNumRows();
734 * SQL clause to skip forbidden log types for this user
736 * @param $db Database
737 * @param $audience string, public/user
738 * @return Mixed: string or false
740 public static function getExcludeClause( $db, $audience = 'public' ) {
741 global $wgLogRestrictions, $wgUser;
742 // Reset the array, clears extra "where" clauses when $par is used
743 $hiddenLogs = array();
744 // Don't show private logs to unprivileged users
745 foreach( $wgLogRestrictions as $logType => $right ) {
746 if( $audience == 'public' ||
!$wgUser->isAllowed($right) ) {
747 $safeType = $db->strencode( $logType );
748 $hiddenLogs[] = $safeType;
751 if( count($hiddenLogs) == 1 ) {
752 return 'log_type != ' . $db->addQuotes( $hiddenLogs[0] );
753 } elseif( $hiddenLogs ) {
754 return 'log_type NOT IN (' . $db->makeList($hiddenLogs) . ')';
763 class LogPager
extends ReverseChronologicalPager
{
764 private $types = array(), $performer = '', $title = '', $pattern = '';
765 private $typeCGI = '';
766 public $mLogEventsList;
771 * @param $list LogEventsList
772 * @param $types String or Array: log types to show
773 * @param $performer String: the user who made the log entries
774 * @param $title String or Title: the page title the log entries are for
775 * @param $pattern String: do a prefix search rather than an exact title match
776 * @param $conds Array: extra conditions for the query
777 * @param $year Integer: the year to start from
778 * @param $month Integer: the month to start from
779 * @param $tagFilter String: tag
781 public function __construct( $list, $types = array(), $performer = '', $title = '', $pattern = '',
782 $conds = array(), $year = false, $month = false, $tagFilter = '' ) {
783 parent
::__construct( $list->getContext() );
784 $this->mConds
= $conds;
786 $this->mLogEventsList
= $list;
788 $this->limitType( $types ); // also excludes hidden types
789 $this->limitPerformer( $performer );
790 $this->limitTitle( $title, $pattern );
791 $this->getDateCond( $year, $month );
792 $this->mTagFilter
= $tagFilter;
795 public function getDefaultQuery() {
796 $query = parent
::getDefaultQuery();
797 $query['type'] = $this->typeCGI
; // arrays won't work here
798 $query['user'] = $this->performer
;
799 $query['month'] = $this->mMonth
;
800 $query['year'] = $this->mYear
;
804 // Call ONLY after calling $this->limitType() already!
805 public function getFilterParams() {
806 global $wgFilterLogTypes;
808 if( count($this->types
) ) {
811 foreach( $wgFilterLogTypes as $type => $default ) {
812 // Avoid silly filtering
813 if( $type !== 'patrol' ||
$this->getUser()->useNPPatrol() ) {
814 $hide = $this->getRequest()->getInt( "hide_{$type}_log", $default );
815 $filters[$type] = $hide;
817 $this->mConds
[] = 'log_type != ' . $this->mDb
->addQuotes( $type );
824 * Set the log reader to return only entries of the given type.
825 * Type restrictions enforced here
827 * @param $types String or array: Log types ('upload', 'delete', etc);
828 * empty string means no restriction
830 private function limitType( $types ) {
831 global $wgLogRestrictions;
832 // If $types is not an array, make it an array
833 $types = ($types === '') ?
array() : (array)$types;
834 // Don't even show header for private logs; don't recognize it...
835 foreach ( $types as $type ) {
836 if( isset( $wgLogRestrictions[$type] )
837 && !$this->getUser()->isAllowed($wgLogRestrictions[$type])
839 $types = array_diff( $types, array( $type ) );
842 $this->types
= $types;
843 // Don't show private logs to unprivileged users.
844 // Also, only show them upon specific request to avoid suprises.
845 $audience = $types ?
'user' : 'public';
846 $hideLogs = LogEventsList
::getExcludeClause( $this->mDb
, $audience );
847 if( $hideLogs !== false ) {
848 $this->mConds
[] = $hideLogs;
850 if( count($types) ) {
851 $this->mConds
['log_type'] = $types;
852 // Set typeCGI; used in url param for paging
853 if( count($types) == 1 ) $this->typeCGI
= $types[0];
858 * Set the log reader to return only entries by the given user.
860 * @param $name String: (In)valid user name
862 private function limitPerformer( $name ) {
866 $usertitle = Title
::makeTitleSafe( NS_USER
, $name );
867 if( is_null($usertitle) ) {
870 /* Fetch userid at first, if known, provides awesome query plan afterwards */
871 $userid = User
::idFromName( $name );
873 /* It should be nicer to abort query at all,
874 but for now it won't pass anywhere behind the optimizer */
875 $this->mConds
[] = "NULL";
877 $this->mConds
['log_user'] = $userid;
878 // Paranoia: avoid brute force searches (bug 17342)
879 $user = $this->getUser();
880 if( !$user->isAllowed( 'deletedhistory' ) ||
$user->isBlocked() ) {
881 $this->mConds
[] = $this->mDb
->bitAnd('log_deleted', LogPage
::DELETED_USER
) . ' = 0';
882 } elseif( !$user->isAllowed( 'suppressrevision' ) ||
$user->isBlocked() ) {
883 $this->mConds
[] = $this->mDb
->bitAnd('log_deleted', LogPage
::SUPPRESSED_USER
) .
884 ' != ' . LogPage
::SUPPRESSED_USER
;
886 $this->performer
= $usertitle->getText();
891 * Set the log reader to return only entries affecting the given page.
892 * (For the block and rights logs, this is a user page.)
894 * @param $page String or Title object: Title name
895 * @param $pattern String
897 private function limitTitle( $page, $pattern ) {
900 if ( $page instanceof Title
) {
903 $title = Title
::newFromText( $page );
904 if( strlen( $page ) == 0 ||
!$title instanceof Title
) {
909 $this->title
= $title->getPrefixedText();
910 $ns = $title->getNamespace();
913 # Using the (log_namespace, log_title, log_timestamp) index with a
914 # range scan (LIKE) on the first two parts, instead of simple equality,
915 # makes it unusable for sorting. Sorted retrieval using another index
916 # would be possible, but then we might have to scan arbitrarily many
917 # nodes of that index. Therefore, we need to avoid this if $wgMiserMode
920 # This is not a problem with simple title matches, because then we can
921 # use the page_time index. That should have no more than a few hundred
922 # log entries for even the busiest pages, so it can be safely scanned
923 # in full to satisfy an impossible condition on user or similar.
924 if( $pattern && !$wgMiserMode ) {
925 $this->mConds
['log_namespace'] = $ns;
926 $this->mConds
[] = 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() );
927 $this->pattern
= $pattern;
929 $this->mConds
['log_namespace'] = $ns;
930 $this->mConds
['log_title'] = $title->getDBkey();
932 // Paranoia: avoid brute force searches (bug 17342)
933 $user = $this->getUser();
934 if( !$user->isAllowed( 'deletedhistory' ) ||
$user->isBlocked() ) {
935 $this->mConds
[] = $db->bitAnd('log_deleted', LogPage
::DELETED_ACTION
) . ' = 0';
936 } elseif( !$user->isAllowed( 'suppressrevision' ) ||
$user->isBlocked() ) {
937 $this->mConds
[] = $db->bitAnd('log_deleted', LogPage
::SUPPRESSED_ACTION
) .
938 ' != ' . LogPage
::SUPPRESSED_ACTION
;
943 * Constructs the most part of the query. Extra conditions are sprinkled in
944 * all over this class.
947 public function getQueryInfo() {
948 $basic = DatabaseLogEntry
::getSelectQueryData();
950 $tables = $basic['tables'];
951 $fields = $basic['fields'];
952 $conds = $basic['conds'];
953 $options = $basic['options'];
954 $joins = $basic['join_conds'];
957 # Add log_search table if there are conditions on it.
958 # This filters the results to only include log rows that have
959 # log_search records with the specified ls_field and ls_value values.
960 if( array_key_exists( 'ls_field', $this->mConds
) ) {
961 $tables[] = 'log_search';
962 $index['log_search'] = 'ls_field_val';
963 $index['logging'] = 'PRIMARY';
964 if ( !$this->hasEqualsClause( 'ls_field' )
965 ||
!$this->hasEqualsClause( 'ls_value' ) )
967 # Since (ls_field,ls_value,ls_logid) is unique, if the condition is
968 # to match a specific (ls_field,ls_value) tuple, then there will be
969 # no duplicate log rows. Otherwise, we need to remove the duplicates.
970 $options[] = 'DISTINCT';
972 # Avoid usage of the wrong index by limiting
973 # the choices of available indexes. This mainly
974 # avoids site-breaking filesorts.
975 } elseif( $this->title ||
$this->pattern ||
$this->performer
) {
976 $index['logging'] = array( 'page_time', 'user_time' );
977 if( count($this->types
) == 1 ) {
978 $index['logging'][] = 'log_user_type_time';
980 } elseif( count($this->types
) == 1 ) {
981 $index['logging'] = 'type_time';
983 $index['logging'] = 'times';
985 $options['USE INDEX'] = $index;
986 # Don't show duplicate rows when using log_search
987 $joins['log_search'] = array( 'INNER JOIN', 'ls_log_id=log_id' );
992 'conds' => array_merge( $conds, $this->mConds
),
993 'options' => $options,
994 'join_conds' => $joins,
996 # Add ChangeTags filter query
997 ChangeTags
::modifyDisplayQuery( $info['tables'], $info['fields'], $info['conds'],
998 $info['join_conds'], $info['options'], $this->mTagFilter
);
1002 // Checks if $this->mConds has $field matched to a *single* value
1003 protected function hasEqualsClause( $field ) {
1005 array_key_exists( $field, $this->mConds
) &&
1006 ( !is_array( $this->mConds
[$field] ) ||
count( $this->mConds
[$field] ) == 1 )
1010 function getIndexField() {
1011 return 'log_timestamp';
1014 public function getStartBody() {
1015 wfProfileIn( __METHOD__
);
1016 # Do a link batch query
1017 if( $this->getNumRows() > 0 ) {
1018 $lb = new LinkBatch
;
1019 foreach ( $this->mResult
as $row ) {
1020 $lb->add( $row->log_namespace
, $row->log_title
);
1021 $lb->addObj( Title
::makeTitleSafe( NS_USER
, $row->user_name
) );
1022 $lb->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->user_name
) );
1025 $this->mResult
->seek( 0 );
1027 wfProfileOut( __METHOD__
);
1031 public function formatRow( $row ) {
1032 return $this->mLogEventsList
->logLine( $row );
1035 public function getType() {
1036 return $this->types
;
1042 public function getPerformer() {
1043 return $this->performer
;
1049 public function getPage() {
1050 return $this->title
;
1053 public function getPattern() {
1054 return $this->pattern
;
1057 public function getYear() {
1058 return $this->mYear
;
1061 public function getMonth() {
1062 return $this->mMonth
;
1065 public function getTagFilter() {
1066 return $this->mTagFilter
;
1069 public function doQuery() {
1070 // Workaround MySQL optimizer bug
1071 $this->mDb
->setBigSelects();
1073 $this->mDb
->setBigSelects( 'default' );