Patched jquery-1.4.2 to not crash in IE7 when a style property is set with 'null...
[mediawiki.git] / includes / LogEventsList.php
blob045644d6f7b17bafd19cdacb213a81444452e086
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>, 2008 Aaron Schulz
3 # http://www.mediawiki.org/
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License along
16 # with this program; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # http://www.gnu.org/copyleft/gpl.html
20 class LogEventsList {
21 const NO_ACTION_LINK = 1;
22 const NO_EXTRA_USER_LINKS = 2;
24 private $skin;
25 private $out;
26 public $flags;
28 public function __construct( $skin, $out, $flags = 0 ) {
29 $this->skin = $skin;
30 $this->out = $out;
31 $this->flags = $flags;
32 $this->preCacheMessages();
35 /**
36 * As we use the same small set of messages in various methods and that
37 * they are called often, we call them once and save them in $this->message
39 private function preCacheMessages() {
40 // Precache various messages
41 if( !isset( $this->message ) ) {
42 $messages = array( 'revertmerge', 'protect_change', 'unblocklink', 'change-blocklink',
43 'revertmove', 'undeletelink', 'undeleteviewlink', 'revdel-restore', 'hist', 'diff',
44 'pipe-separator' );
45 foreach( $messages as $msg ) {
46 $this->message[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
51 /**
52 * Set page title and show header for this log type
53 * @param $type Array
55 public function showHeader( $type ) {
56 // If only one log type is used, then show a special message...
57 $headerType = (count($type) == 1) ? $type[0] : '';
58 if( LogPage::isLogType( $headerType ) ) {
59 $this->out->setPageTitle( LogPage::logName( $headerType ) );
60 $this->out->addHTML( LogPage::logHeader( $headerType ) );
61 } else {
62 $this->out->addHTML( wfMsgExt('alllogstext',array('parseinline')) );
66 /**
67 * Show options for the log list
68 * @param $types string or Array
69 * @param $user String
70 * @param $page String
71 * @param $pattern String
72 * @param $year Integer: year
73 * @param $month Integer: month
74 * @param $filter: array
75 * @param $tagFilter: array?
77 public function showOptions( $types=array(), $user='', $page='', $pattern='', $year='',
78 $month = '', $filter = null, $tagFilter='' )
80 global $wgScript, $wgMiserMode;
82 $action = $wgScript;
83 $title = SpecialPage::getTitleFor( 'Log' );
84 $special = $title->getPrefixedDBkey();
86 // For B/C, we take strings, but make sure they are converted...
87 $types = ($types === '') ? array() : (array)$types;
89 $tagSelector = ChangeTags::buildTagFilterSelector( $tagFilter );
91 $html = '';
92 $html .= Xml::hidden( 'title', $special );
94 // Basic selectors
95 $html .= $this->getTypeMenu( $types ) . "\n";
96 $html .= $this->getUserInput( $user ) . "\n";
97 $html .= $this->getTitleInput( $page ) . "\n";
98 $html .= $this->getExtraInputs( $types ) . "\n";
100 // Title pattern, if allowed
101 if (!$wgMiserMode) {
102 $html .= $this->getTitlePattern( $pattern ) . "\n";
105 // date menu
106 $html .= Xml::tags( 'p', null, Xml::dateMenu( $year, $month ) );
108 // Tag filter
109 if ($tagSelector) {
110 $html .= Xml::tags( 'p', null, implode( '&nbsp;', $tagSelector ) );
113 // Filter links
114 if ($filter) {
115 $html .= Xml::tags( 'p', null, $this->getFilterLinks( $filter ) );
118 // Submit button
119 $html .= Xml::submitButton( wfMsg( 'allpagessubmit' ) );
121 // Fieldset
122 $html = Xml::fieldset( wfMsg( 'log' ), $html );
124 // Form wrapping
125 $html = Xml::tags( 'form', array( 'action' => $action, 'method' => 'get' ), $html );
127 $this->out->addHTML( $html );
131 * @param $filter Array
132 * @return String: Formatted HTML
134 private function getFilterLinks( $filter ) {
135 global $wgTitle, $wgLang;
136 // show/hide links
137 $messages = array( wfMsgHtml( 'show' ), wfMsgHtml( 'hide' ) );
138 // Option value -> message mapping
139 $links = array();
140 $hiddens = ''; // keep track for "go" button
141 foreach( $filter as $type => $val ) {
142 // Should the below assignment be outside the foreach?
143 // Then it would have to be copied. Not certain what is more expensive.
144 $query = $this->getDefaultQuery();
145 $queryKey = "hide_{$type}_log";
147 $hideVal = 1 - intval($val);
148 $query[$queryKey] = $hideVal;
150 $link = $this->skin->link(
151 $wgTitle,
152 $messages[$hideVal],
153 array(),
154 $query,
155 array( 'known', 'noclasses' )
158 $links[$type] = wfMsgHtml( "log-show-hide-{$type}", $link );
159 $hiddens .= Xml::hidden( "hide_{$type}_log", $val ) . "\n";
161 // Build links
162 return '<small>'.$wgLang->pipeList( $links ) . '</small>' . $hiddens;
165 private function getDefaultQuery() {
166 if ( !isset( $this->mDefaultQuery ) ) {
167 $this->mDefaultQuery = $_GET;
168 unset( $this->mDefaultQuery['title'] );
169 unset( $this->mDefaultQuery['dir'] );
170 unset( $this->mDefaultQuery['offset'] );
171 unset( $this->mDefaultQuery['limit'] );
172 unset( $this->mDefaultQuery['order'] );
173 unset( $this->mDefaultQuery['month'] );
174 unset( $this->mDefaultQuery['year'] );
176 return $this->mDefaultQuery;
180 * @param $queryTypes Array
181 * @return String: Formatted HTML
183 private function getTypeMenu( $queryTypes ) {
184 global $wgLogRestrictions, $wgUser;
186 $html = "<select name='type'>\n";
188 $validTypes = LogPage::validTypes();
189 $typesByName = array(); // Temporary array
191 // First pass to load the log names
192 foreach( $validTypes as $type ) {
193 $text = LogPage::logName( $type );
194 $typesByName[$type] = $text;
197 // Second pass to sort by name
198 asort($typesByName);
200 // Note the query type
201 $queryType = count($queryTypes) == 1 ? $queryTypes[0] : '';
203 // Always put "All public logs" on top
204 if ( isset( $typesByName[''] ) ) {
205 $all = $typesByName[''];
206 unset( $typesByName[''] );
207 $typesByName = array( '' => $all ) + $typesByName;
210 // Third pass generates sorted XHTML content
211 foreach( $typesByName as $type => $text ) {
212 $selected = ($type == $queryType);
213 // Restricted types
214 if ( isset($wgLogRestrictions[$type]) ) {
215 if ( $wgUser->isAllowed( $wgLogRestrictions[$type] ) ) {
216 $html .= Xml::option( $text, $type, $selected ) . "\n";
218 } else {
219 $html .= Xml::option( $text, $type, $selected ) . "\n";
223 $html .= '</select>';
224 return $html;
228 * @param $user String
229 * @return String: Formatted HTML
231 private function getUserInput( $user ) {
232 return '<span style="white-space: nowrap">' .
233 Xml::inputLabel( wfMsg( 'specialloguserlabel' ), 'user', 'mw-log-user', 15, $user ) .
234 '</span>';
238 * @param $title String
239 * @return String: Formatted HTML
241 private function getTitleInput( $title ) {
242 return '<span style="white-space: nowrap">' .
243 Xml::inputLabel( wfMsg( 'speciallogtitlelabel' ), 'page', 'mw-log-page', 20, $title ) .
244 '</span>';
248 * @return boolean Checkbox
250 private function getTitlePattern( $pattern ) {
251 return '<span style="white-space: nowrap">' .
252 Xml::checkLabel( wfMsg( 'log-title-wildcard' ), 'pattern', 'pattern', $pattern ) .
253 '</span>';
256 private function getExtraInputs( $types ) {
257 global $wgRequest;
258 $offender = $wgRequest->getVal('offender');
259 $user = User::newFromName( $offender, false );
260 if( !$user || ($user->getId() == 0 && !IP::isIPAddress($offender) ) ) {
261 $offender = ''; // Blank field if invalid
263 if( count($types) == 1 && $types[0] == 'suppress' ) {
264 return Xml::inputLabel( wfMsg('revdelete-offender'), 'offender',
265 'mw-log-offender', 20, $offender );
267 return '';
270 public function beginLogEventsList() {
271 return "<ul>\n";
274 public function endLogEventsList() {
275 return "</ul>\n";
279 * @param $row Row: a single row from the result set
280 * @return String: Formatted HTML list item
282 public function logLine( $row ) {
283 $classes = array( 'mw-logline-' . $row->log_type );
284 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
285 // Log time
286 $time = $this->logTimestamp( $row );
287 // User links
288 $userLink = $this->logUserLinks( $row );
289 // Extract extra parameters
290 $paramArray = LogPage::extractParams( $row->log_params );
291 // Event description
292 $action = $this->logAction( $row, $title, $paramArray );
293 // Log comment
294 $comment = $this->logComment( $row );
295 // Add review/revert links and such...
296 $revert = $this->logActionLinks( $row, $title, $paramArray, $comment );
298 // Some user can hide log items and have review links
299 $del = $this->getShowHideLinks( $row );
300 if( $del != '' ) $del .= ' ';
302 // Any tags...
303 list( $tagDisplay, $newClasses ) = ChangeTags::formatSummaryRow( $row->ts_tags, 'logevent' );
304 $classes = array_merge( $classes, $newClasses );
306 return Xml::tags( 'li', array( "class" => implode( ' ', $classes ) ),
307 $del . "$time $userLink $action $comment $revert $tagDisplay" ) . "\n";
310 private function logTimestamp( $row ) {
311 global $wgLang;
312 $time = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->log_timestamp ), true );
313 return htmlspecialchars( $time );
316 private function logUserLinks( $row ) {
317 $userLinks = '';
318 if( self::isDeleted( $row, LogPage::DELETED_USER ) ) {
319 $userLinks = '<span class="history-deleted">' .
320 wfMsgHtml( 'rev-deleted-user' ) . '</span>';
321 } else {
322 $userLinks = $this->skin->userLink( $row->log_user, $row->user_name );
323 // Talk|Contribs links...
324 if( !( $this->flags & self::NO_EXTRA_USER_LINKS ) ) {
325 $userLinks .= $this->skin->userToolLinks(
326 $row->log_user, $row->user_name, true, 0, $row->user_editcount );
329 return $userLinks;
332 private function logAction( $row, $title, $paramArray ) {
333 $action = '';
334 if( self::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
335 $action = '<span class="history-deleted">' .
336 wfMsgHtml( 'rev-deleted-event' ) . '</span>';
337 } else {
338 $action = LogPage::actionText(
339 $row->log_type, $row->log_action, $title, $this->skin, $paramArray, true );
341 return $action;
344 private function logComment( $row ) {
345 global $wgContLang;
346 $comment = '';
347 if( self::isDeleted( $row, LogPage::DELETED_COMMENT ) ) {
348 $comment = '<span class="history-deleted">' .
349 wfMsgHtml( 'rev-deleted-comment' ) . '</span>';
350 } else {
351 $comment = $wgContLang->getDirMark() .
352 $this->skin->commentBlock( $row->log_comment );
354 return $comment;
357 // @TODO: split up!
358 private function logActionLinks( $row, $title, $paramArray, &$comment ) {
359 global $wgUser, $wgLang;
360 if( ( $this->flags & self::NO_ACTION_LINK ) // we don't want to see the action
361 || self::isDeleted( $row, LogPage::DELETED_ACTION ) ) // action is hidden
363 return '';
365 $revert = '';
366 if( self::typeAction( $row, 'move', 'move', 'move' ) && !empty( $paramArray[0] ) ) {
367 $destTitle = Title::newFromText( $paramArray[0] );
368 if( $destTitle ) {
369 $revert = '(' . $this->skin->link(
370 SpecialPage::getTitleFor( 'Movepage' ),
371 $this->message['revertmove'],
372 array(),
373 array(
374 'wpOldTitle' => $destTitle->getPrefixedDBkey(),
375 'wpNewTitle' => $title->getPrefixedDBkey(),
376 'wpReason' => wfMsgForContent( 'revertmove' ),
377 'wpMovetalk' => 0
379 array( 'known', 'noclasses' )
380 ) . ')';
382 // Show undelete link
383 } else if( self::typeAction( $row, array( 'delete', 'suppress' ), 'delete', 'deletedhistory' ) ) {
384 if( !$wgUser->isAllowed( 'undelete' ) ) {
385 $viewdeleted = $this->message['undeleteviewlink'];
386 } else {
387 $viewdeleted = $this->message['undeletelink'];
389 $revert = '(' . $this->skin->link(
390 SpecialPage::getTitleFor( 'Undelete' ),
391 $viewdeleted,
392 array(),
393 array( 'target' => $title->getPrefixedDBkey() ),
394 array( 'known', 'noclasses' )
395 ) . ')';
396 // Show unblock/change block link
397 } else if( self::typeAction( $row, array( 'block', 'suppress' ), array( 'block', 'reblock' ), 'block' ) ) {
398 $revert = '(' .
399 $this->skin->link(
400 SpecialPage::getTitleFor( 'Ipblocklist' ),
401 $this->message['unblocklink'],
402 array(),
403 array(
404 'action' => 'unblock',
405 'ip' => $row->log_title
407 'known'
409 $this->message['pipe-separator'] .
410 $this->skin->link(
411 SpecialPage::getTitleFor( 'Blockip', $row->log_title ),
412 $this->message['change-blocklink'],
413 array(),
414 array(),
415 'known'
417 ')';
418 // Show change protection link
419 } else if( self::typeAction( $row, 'protect', array( 'modify', 'protect', 'unprotect' ) ) ) {
420 $revert .= ' (' .
421 $this->skin->link( $title,
422 $this->message['hist'],
423 array(),
424 array(
425 'action' => 'history',
426 'offset' => $row->log_timestamp
429 if( $wgUser->isAllowed( 'protect' ) ) {
430 $revert .= $this->message['pipe-separator'] .
431 $this->skin->link( $title,
432 $this->message['protect_change'],
433 array(),
434 array( 'action' => 'protect' ),
435 'known' );
437 $revert .= ')';
438 // Show unmerge link
439 } else if( self::typeAction( $row, 'merge', 'merge', 'mergehistory' ) ) {
440 $revert = '(' . $this->skin->link(
441 SpecialPage::getTitleFor( 'MergeHistory' ),
442 $this->message['revertmerge'],
443 array(),
444 array(
445 'target' => $paramArray[0],
446 'dest' => $title->getPrefixedDBkey(),
447 'mergepoint' => $paramArray[1]
449 array( 'known', 'noclasses' )
450 ) . ')';
451 // If an edit was hidden from a page give a review link to the history
452 } else if( self::typeAction( $row, array( 'delete', 'suppress' ), 'revision', 'deletedhistory' ) ) {
453 if( count($paramArray) >= 2 ) {
454 // Different revision types use different URL params...
455 $key = $paramArray[0];
456 // $paramArray[1] is a CSV of the IDs
457 $Ids = explode( ',', $paramArray[1] );
458 $query = $paramArray[1];
459 $revert = array();
460 // Diff link for single rev deletions
461 if( count($Ids) == 1 ) {
462 // Live revision diffs...
463 if( in_array( $key, array( 'oldid', 'revision' ) ) ) {
464 $revert[] = $this->skin->link(
465 $title,
466 $this->message['diff'],
467 array(),
468 array(
469 'diff' => intval( $Ids[0] ),
470 'unhide' => 1
472 array( 'known', 'noclasses' )
474 // Deleted revision diffs...
475 } else if( in_array( $key, array( 'artimestamp','archive' ) ) ) {
476 $revert[] = $this->skin->link(
477 SpecialPage::getTitleFor( 'Undelete' ),
478 $this->message['diff'],
479 array(),
480 array(
481 'target' => $title->getPrefixedDBKey(),
482 'diff' => 'prev',
483 'timestamp' => $Ids[0]
485 array( 'known', 'noclasses' )
489 // View/modify link...
490 $revert[] = $this->skin->link(
491 SpecialPage::getTitleFor( 'Revisiondelete' ),
492 $this->message['revdel-restore'],
493 array(),
494 array(
495 'target' => $title->getPrefixedText(),
496 'type' => $key,
497 'ids' => $query
499 array( 'known', 'noclasses' )
501 // Pipe links
502 $revert = wfMsg( 'parentheses', $wgLang->pipeList( $revert ) );
504 // Hidden log items, give review link
505 } else if( self::typeAction( $row, array( 'delete', 'suppress' ), 'event', 'deletedhistory' ) ) {
506 if( count($paramArray) >= 1 ) {
507 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
508 // $paramArray[1] is a CSV of the IDs
509 $Ids = explode( ',', $paramArray[0] );
510 $query = $paramArray[0];
511 // Link to each hidden object ID, $paramArray[1] is the url param
512 $revert = '(' . $this->skin->link(
513 $revdel,
514 $this->message['revdel-restore'],
515 array(),
516 array(
517 'target' => $title->getPrefixedText(),
518 'type' => 'logging',
519 'ids' => $query
521 array( 'known', 'noclasses' )
522 ) . ')';
524 // Self-created users
525 } else if( self::typeAction( $row, 'newusers', 'create2' ) ) {
526 if( isset( $paramArray[0] ) ) {
527 $revert = $this->skin->userToolLinks( $paramArray[0], $title->getDBkey(), true );
528 } else {
529 # Fall back to a blue contributions link
530 $revert = $this->skin->userToolLinks( 1, $title->getDBkey() );
532 $ts = wfTimestamp( TS_UNIX, $row->log_timestamp );
533 if( $ts < '20080129000000' ) {
534 # Suppress $comment from old entries (before 2008-01-29),
535 # not needed and can contain incorrect links
536 $comment = '';
538 // Do nothing. The implementation is handled by the hook modifiying the passed-by-ref parameters.
539 } else {
540 wfRunHooks( 'LogLine', array( $row->log_type, $row->log_action, $title, $paramArray,
541 &$comment, &$revert, $row->log_timestamp ) );
543 if( $revert != '' ) {
544 $revert = '<span class="mw-logevent-actionlink">' . $revert . '</span>';
546 return $revert;
550 * @param $row Row
551 * @return string
553 private function getShowHideLinks( $row ) {
554 global $wgUser;
555 if( ( $this->flags & self::NO_ACTION_LINK ) // we don't want to see the links
556 || $row->log_type == 'suppress' ) // no one can hide items from the suppress log
558 return '';
560 $del = '';
561 // Don't show useless link to people who cannot hide revisions
562 if( $wgUser->isAllowed( 'deletedhistory' ) ) {
563 if( $row->log_deleted || $wgUser->isAllowed( 'deleterevision' ) ) {
564 $canHide = $wgUser->isAllowed( 'deleterevision' );
565 // If event was hidden from sysops
566 if( !self::userCan( $row, LogPage::DELETED_RESTRICTED ) ) {
567 $del = $this->skin->revDeleteLinkDisabled( $canHide );
568 } else {
569 $target = SpecialPage::getTitleFor( 'Log', $row->log_type );
570 $query = array(
571 'target' => $target->getPrefixedDBkey(),
572 'type' => 'logging',
573 'ids' => $row->log_id,
575 $del = $this->skin->revDeleteLink( $query,
576 self::isDeleted( $row, LogPage::DELETED_RESTRICTED ), $canHide );
580 return $del;
584 * @param $row Row
585 * @param $type Mixed: string/array
586 * @param $action Mixed: string/array
587 * @param $right string
588 * @return bool
590 public static function typeAction( $row, $type, $action, $right='' ) {
591 $match = is_array($type) ?
592 in_array( $row->log_type, $type ) : $row->log_type == $type;
593 if( $match ) {
594 $match = is_array( $action ) ?
595 in_array( $row->log_action, $action ) : $row->log_action == $action;
596 if( $match && $right ) {
597 global $wgUser;
598 $match = $wgUser->isAllowed( $right );
601 return $match;
605 * Determine if the current user is allowed to view a particular
606 * field of this log row, if it's marked as deleted.
607 * @param $row Row
608 * @param $field Integer
609 * @return Boolean
611 public static function userCan( $row, $field ) {
612 return self::userCanBitfield( $row->log_deleted, $field );
616 * Determine if the current user is allowed to view a particular
617 * field of this log row, if it's marked as deleted.
618 * @param $bitfield Integer (current field)
619 * @param $field Integer
620 * @return Boolean
622 public static function userCanBitfield( $bitfield, $field ) {
623 if( $bitfield & $field ) {
624 global $wgUser;
625 $permission = '';
626 if ( $bitfield & LogPage::DELETED_RESTRICTED ) {
627 $permission = 'suppressrevision';
628 } else {
629 $permission = 'deletedhistory';
631 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
632 return $wgUser->isAllowed( $permission );
633 } else {
634 return true;
639 * @param $row Row
640 * @param $field Integer: one of DELETED_* bitfield constants
641 * @return Boolean
643 public static function isDeleted( $row, $field ) {
644 return ( $row->log_deleted & $field ) == $field;
648 * Show log extract. Either with text and a box (set $msgKey) or without (don't set $msgKey)
649 * @param $out OutputPage or String-by-reference
650 * @param $types String or Array
651 * @param $page String The page title to show log entries for
652 * @param $user String The user who made the log entries
653 * @param $param Associative Array with the following additional options:
654 * - lim Integer Limit of items to show, default is 50
655 * - conds Array Extra conditions for the query (e.g. "log_action != 'revision'")
656 * - showIfEmpty boolean Set to false if you don't want any output in case the loglist is empty
657 * if set to true (default), "No matching items in log" is displayed if loglist is empty
658 * - msgKey Array If you want a nice box with a message, set this to the key of the message.
659 * First element is the message key, additional optional elements are parameters for the key
660 * that are processed with wgMsgExt and option 'parse'
661 * - offset Set to overwrite offset parameter in $wgRequest
662 * set to '' to unset offset
663 * - wrap String Wrap the message in html (usually something like "<div ...>$1</div>").
664 * - flags Integer display flags (NO_ACTION_LINK,NO_EXTRA_USER_LINKS)
665 * @return Integer Number of total log items (not limited by $lim)
667 public static function showLogExtract(
668 &$out, $types=array(), $page='', $user='', $param = array()
670 global $wgUser, $wgOut;
671 $defaultParameters = array(
672 'lim' => 25,
673 'conds' => array(),
674 'showIfEmpty' => true,
675 'msgKey' => array(''),
676 'wrap' => "$1",
677 'flags' => 0
679 # The + operator appends elements of remaining keys from the right
680 # handed array to the left handed, whereas duplicated keys are NOT overwritten.
681 $param += $defaultParameters;
682 # Convert $param array to individual variables
683 $lim = $param['lim'];
684 $conds = $param['conds'];
685 $showIfEmpty = $param['showIfEmpty'];
686 $msgKey = $param['msgKey'];
687 $wrap = $param['wrap'];
688 $flags = $param['flags'];
689 if ( !is_array( $msgKey ) ) {
690 $msgKey = array( $msgKey );
692 # Insert list of top 50 (or top $lim) items
693 $loglist = new LogEventsList( $wgUser->getSkin(), $wgOut, $flags );
694 $pager = new LogPager( $loglist, $types, $user, $page, '', $conds );
695 if ( isset( $param['offset'] ) ) { # Tell pager to ignore $wgRequest offset
696 $pager->setOffset( $param['offset'] );
698 if( $lim > 0 ) $pager->mLimit = $lim;
699 $logBody = $pager->getBody();
700 $s = '';
701 if( $logBody ) {
702 if ( $msgKey[0] ) {
703 $s = '<div class="mw-warning-with-logexcerpt">';
705 if ( count( $msgKey ) == 1 ) {
706 $s .= wfMsgExt( $msgKey[0], array( 'parse' ) );
707 } else { // Process additional arguments
708 $args = $msgKey;
709 array_shift( $args );
710 $s .= wfMsgExt( $msgKey[0], array( 'parse' ), $args );
713 $s .= $loglist->beginLogEventsList() .
714 $logBody .
715 $loglist->endLogEventsList();
716 } else {
717 if ( $showIfEmpty )
718 $s = Html::rawElement( 'div', array( 'class' => 'mw-warning-logempty' ),
719 wfMsgExt( 'logempty', array( 'parseinline' ) ) );
721 if( $pager->getNumRows() > $pager->mLimit ) { # Show "Full log" link
722 $urlParam = array();
723 if ( $page != '')
724 $urlParam['page'] = $page;
725 if ( $user != '')
726 $urlParam['user'] = $user;
727 if ( !is_array( $types ) ) # Make it an array, if it isn't
728 $types = array( $types );
729 # If there is exactly one log type, we can link to Special:Log?type=foo
730 if ( count( $types ) == 1 )
731 $urlParam['type'] = $types[0];
732 $s .= $wgUser->getSkin()->link(
733 SpecialPage::getTitleFor( 'Log' ),
734 wfMsgHtml( 'log-fulllog' ),
735 array(),
736 $urlParam
739 if ( $logBody && $msgKey[0] ) {
740 $s .= '</div>';
743 if ( $wrap!='' ) { // Wrap message in html
744 $s = str_replace( '$1', $s, $wrap );
747 // $out can be either an OutputPage object or a String-by-reference
748 if( $out instanceof OutputPage ){
749 $out->addHTML( $s );
750 } else {
751 $out = $s;
753 return $pager->getNumRows();
757 * SQL clause to skip forbidden log types for this user
758 * @param $db Database
759 * @param $audience string, public/user
760 * @return mixed (string or false)
762 public static function getExcludeClause( $db, $audience = 'public' ) {
763 global $wgLogRestrictions, $wgUser;
764 // Reset the array, clears extra "where" clauses when $par is used
765 $hiddenLogs = array();
766 // Don't show private logs to unprivileged users
767 foreach( $wgLogRestrictions as $logType => $right ) {
768 if( $audience == 'public' || !$wgUser->isAllowed($right) ) {
769 $safeType = $db->strencode( $logType );
770 $hiddenLogs[] = $safeType;
773 if( count($hiddenLogs) == 1 ) {
774 return 'log_type != ' . $db->addQuotes( $hiddenLogs[0] );
775 } elseif( $hiddenLogs ) {
776 return 'log_type NOT IN (' . $db->makeList($hiddenLogs) . ')';
778 return false;
783 * @ingroup Pager
785 class LogPager extends ReverseChronologicalPager {
786 private $types = array(), $user = '', $title = '', $pattern = '';
787 private $typeCGI = '';
788 public $mLogEventsList;
791 * constructor
792 * @param $list LogEventsList
793 * @param $types String or Array log types to show
794 * @param $user String The user who made the log entries
795 * @param $title String The page title the log entries are for
796 * @param $pattern String Do a prefix search rather than an exact title match
797 * @param $conds Array Extra conditions for the query
798 * @param $year Integer The year to start from
799 * @param $month Integer The month to start from
801 public function __construct( $list, $types = array(), $user = '', $title = '', $pattern = '',
802 $conds = array(), $year = false, $month = false, $tagFilter = '' )
804 parent::__construct();
805 $this->mConds = $conds;
807 $this->mLogEventsList = $list;
809 $this->limitType( $types ); // also excludes hidden types
810 $this->limitUser( $user );
811 $this->limitTitle( $title, $pattern );
812 $this->getDateCond( $year, $month );
813 $this->mTagFilter = $tagFilter;
816 public function getDefaultQuery() {
817 $query = parent::getDefaultQuery();
818 $query['type'] = $this->typeCGI; // arrays won't work here
819 $query['user'] = $this->user;
820 $query['month'] = $this->mMonth;
821 $query['year'] = $this->mYear;
822 return $query;
825 // Call ONLY after calling $this->limitType() already!
826 public function getFilterParams() {
827 global $wgFilterLogTypes, $wgUser, $wgRequest;
828 $filters = array();
829 if( count($this->types) ) {
830 return $filters;
832 foreach( $wgFilterLogTypes as $type => $default ) {
833 // Avoid silly filtering
834 if( $type !== 'patrol' || $wgUser->useNPPatrol() ) {
835 $hide = $wgRequest->getInt( "hide_{$type}_log", $default );
836 $filters[$type] = $hide;
837 if( $hide )
838 $this->mConds[] = 'log_type != ' . $this->mDb->addQuotes( $type );
841 return $filters;
845 * Set the log reader to return only entries of the given type.
846 * Type restrictions enforced here
847 * @param $types String or array: Log types ('upload', 'delete', etc);
848 * empty string means no restriction
850 private function limitType( $types ) {
851 global $wgLogRestrictions, $wgUser;
852 // If $types is not an array, make it an array
853 $types = ($types === '') ? array() : (array)$types;
854 // Don't even show header for private logs; don't recognize it...
855 foreach ( $types as $type ) {
856 if( isset( $wgLogRestrictions[$type] )
857 && !$wgUser->isAllowed($wgLogRestrictions[$type])
859 $types = array_diff( $types, array( $type ) );
862 $this->types = $types;
863 // Don't show private logs to unprivileged users.
864 // Also, only show them upon specific request to avoid suprises.
865 $audience = $types ? 'user' : 'public';
866 $hideLogs = LogEventsList::getExcludeClause( $this->mDb, $audience );
867 if( $hideLogs !== false ) {
868 $this->mConds[] = $hideLogs;
870 if( count($types) ) {
871 $this->mConds['log_type'] = $types;
872 // Set typeCGI; used in url param for paging
873 if( count($types) == 1 ) $this->typeCGI = $types[0];
878 * Set the log reader to return only entries by the given user.
879 * @param $name String: (In)valid user name
881 private function limitUser( $name ) {
882 if( $name == '' ) {
883 return false;
885 $usertitle = Title::makeTitleSafe( NS_USER, $name );
886 if( is_null($usertitle) ) {
887 return false;
889 /* Fetch userid at first, if known, provides awesome query plan afterwards */
890 $userid = User::idFromName( $name );
891 if( !$userid ) {
892 /* It should be nicer to abort query at all,
893 but for now it won't pass anywhere behind the optimizer */
894 $this->mConds[] = "NULL";
895 } else {
896 global $wgUser;
897 $this->mConds['log_user'] = $userid;
898 // Paranoia: avoid brute force searches (bug 17342)
899 if( !$wgUser->isAllowed( 'deletedhistory' ) ) {
900 $this->mConds[] = $this->mDb->bitAnd('log_deleted', LogPage::DELETED_USER) . ' = 0';
901 } else if( !$wgUser->isAllowed( 'suppressrevision' ) ) {
902 $this->mConds[] = $this->mDb->bitAnd('log_deleted', LogPage::SUPPRESSED_USER) .
903 ' != ' . LogPage::SUPPRESSED_USER;
905 $this->user = $usertitle->getText();
910 * Set the log reader to return only entries affecting the given page.
911 * (For the block and rights logs, this is a user page.)
912 * @param $page String: Title name as text
913 * @param $pattern String
915 private function limitTitle( $page, $pattern ) {
916 global $wgMiserMode, $wgUser;
918 $title = Title::newFromText( $page );
919 if( strlen( $page ) == 0 || !$title instanceof Title )
920 return false;
922 $this->title = $title->getPrefixedText();
923 $ns = $title->getNamespace();
924 $db = $this->mDb;
926 # Using the (log_namespace, log_title, log_timestamp) index with a
927 # range scan (LIKE) on the first two parts, instead of simple equality,
928 # makes it unusable for sorting. Sorted retrieval using another index
929 # would be possible, but then we might have to scan arbitrarily many
930 # nodes of that index. Therefore, we need to avoid this if $wgMiserMode
931 # is on.
933 # This is not a problem with simple title matches, because then we can
934 # use the page_time index. That should have no more than a few hundred
935 # log entries for even the busiest pages, so it can be safely scanned
936 # in full to satisfy an impossible condition on user or similar.
937 if( $pattern && !$wgMiserMode ) {
938 $this->mConds['log_namespace'] = $ns;
939 $this->mConds[] = 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() );
940 $this->pattern = $pattern;
941 } else {
942 $this->mConds['log_namespace'] = $ns;
943 $this->mConds['log_title'] = $title->getDBkey();
945 // Paranoia: avoid brute force searches (bug 17342)
946 if( !$wgUser->isAllowed( 'deletedhistory' ) ) {
947 $this->mConds[] = $db->bitAnd('log_deleted', LogPage::DELETED_ACTION) . ' = 0';
948 } else if( !$wgUser->isAllowed( 'suppressrevision' ) ) {
949 $this->mConds[] = $db->bitAnd('log_deleted', LogPage::SUPPRESSED_ACTION) .
950 ' != ' . LogPage::SUPPRESSED_ACTION;
954 public function getQueryInfo() {
955 global $wgOut;
956 $tables = array( 'logging', 'user' );
957 $this->mConds[] = 'user_id = log_user';
958 $index = array();
959 $options = array();
960 # Add log_search table if there are conditions on it
961 if( array_key_exists('ls_field',$this->mConds) ) {
962 $tables[] = 'log_search';
963 $index['log_search'] = 'ls_field_val';
964 $index['logging'] = 'PRIMARY';
965 $options[] = 'DISTINCT';
966 # Avoid usage of the wrong index by limiting
967 # the choices of available indexes. This mainly
968 # avoids site-breaking filesorts.
969 } else if( $this->title || $this->pattern || $this->user ) {
970 $index['logging'] = array( 'page_time', 'user_time' );
971 if( count($this->types) == 1 ) {
972 $index['logging'][] = 'log_user_type_time';
974 } else if( count($this->types) == 1 ) {
975 $index['logging'] = 'type_time';
976 } else {
977 $index['logging'] = 'times';
979 $options['USE INDEX'] = $index;
980 # Don't show duplicate rows when using log_search
981 $info = array(
982 'tables' => $tables,
983 'fields' => array( 'log_type', 'log_action', 'log_user', 'log_namespace',
984 'log_title', 'log_params', 'log_comment', 'log_id', 'log_deleted',
985 'log_timestamp', 'user_name', 'user_editcount' ),
986 'conds' => $this->mConds,
987 'options' => $options,
988 'join_conds' => array(
989 'user' => array( 'INNER JOIN', 'user_id=log_user' ),
990 'log_search' => array( 'INNER JOIN', 'ls_log_id=log_id' )
993 # Add ChangeTags filter query
994 ChangeTags::modifyDisplayQuery( $info['tables'], $info['fields'], $info['conds'],
995 $info['join_conds'], $info['options'], $this->mTagFilter );
996 return $info;
999 function getIndexField() {
1000 return 'log_timestamp';
1003 public function getStartBody() {
1004 wfProfileIn( __METHOD__ );
1005 # Do a link batch query
1006 if( $this->getNumRows() > 0 ) {
1007 $lb = new LinkBatch;
1008 while( $row = $this->mResult->fetchObject() ) {
1009 $lb->add( $row->log_namespace, $row->log_title );
1010 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
1011 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
1013 $lb->execute();
1014 $this->mResult->seek( 0 );
1016 wfProfileOut( __METHOD__ );
1017 return '';
1020 public function formatRow( $row ) {
1021 return $this->mLogEventsList->logLine( $row );
1024 public function getType() {
1025 return $this->types;
1028 public function getUser() {
1029 return $this->user;
1032 public function getPage() {
1033 return $this->title;
1036 public function getPattern() {
1037 return $this->pattern;
1040 public function getYear() {
1041 return $this->mYear;
1044 public function getMonth() {
1045 return $this->mMonth;
1048 public function getTagFilter() {
1049 return $this->mTagFilter;
1052 public function doQuery() {
1053 // Workaround MySQL optimizer bug
1054 $this->mDb->setBigSelects();
1055 parent::doQuery();
1056 $this->mDb->setBigSelects( 'default' );
1061 * @deprecated
1062 * @ingroup SpecialPage
1064 class LogReader {
1065 var $pager;
1067 * @param $request WebRequest: for internal use use a FauxRequest object to pass arbitrary parameters.
1069 function __construct( $request ) {
1070 global $wgUser, $wgOut;
1071 wfDeprecated(__METHOD__);
1072 # Get parameters
1073 $type = $request->getVal( 'type' );
1074 $user = $request->getText( 'user' );
1075 $title = $request->getText( 'page' );
1076 $pattern = $request->getBool( 'pattern' );
1077 $year = $request->getIntOrNull( 'year' );
1078 $month = $request->getIntOrNull( 'month' );
1079 $tagFilter = $request->getVal( 'tagfilter' );
1080 # Don't let the user get stuck with a certain date
1081 $skip = $request->getText( 'offset' ) || $request->getText( 'dir' ) == 'prev';
1082 if( $skip ) {
1083 $year = '';
1084 $month = '';
1086 # Use new list class to output results
1087 $loglist = new LogEventsList( $wgUser->getSkin(), $wgOut, 0 );
1088 $this->pager = new LogPager( $loglist, $type, $user, $title, $pattern, $year, $month, $tagFilter );
1092 * Is there at least one row?
1093 * @return bool
1095 public function hasRows() {
1096 return isset($this->pager) ? ($this->pager->getNumRows() > 0) : false;
1101 * @deprecated
1102 * @ingroup SpecialPage
1104 class LogViewer {
1105 const NO_ACTION_LINK = 1;
1108 * LogReader object
1110 var $reader;
1113 * @param &$reader LogReader: where to get our data from
1114 * @param $flags Integer: Bitwise combination of flags:
1115 * LogEventsList::NO_ACTION_LINK Don't show restore/unblock/block links
1117 function __construct( &$reader, $flags = 0 ) {
1118 wfDeprecated(__METHOD__);
1119 $this->reader =& $reader;
1120 $this->reader->pager->mLogEventsList->flags = $flags;
1121 # Aliases for shorter code...
1122 $this->pager =& $this->reader->pager;
1123 $this->list =& $this->reader->pager->mLogEventsList;
1127 * Take over the whole output page in $wgOut with the log display.
1129 public function show() {
1130 # Set title and add header
1131 $this->list->showHeader( $pager->getType() );
1132 # Show form options
1133 $this->list->showOptions( $this->pager->getType(), $this->pager->getUser(), $this->pager->getPage(),
1134 $this->pager->getPattern(), $this->pager->getYear(), $this->pager->getMonth() );
1135 # Insert list
1136 $logBody = $this->pager->getBody();
1137 if( $logBody ) {
1138 $wgOut->addHTML(
1139 $this->pager->getNavigationBar() .
1140 $this->list->beginLogEventsList() .
1141 $logBody .
1142 $this->list->endLogEventsList() .
1143 $this->pager->getNavigationBar()
1145 } else {
1146 $wgOut->addWikiMsg( 'logempty' );
1151 * Output just the list of entries given by the linked LogReader,
1152 * with extraneous UI elements. Use for displaying log fragments in
1153 * another page (eg at Special:Undelete)
1154 * @param $out OutputPage: where to send output
1156 public function showList( &$out ) {
1157 $logBody = $this->pager->getBody();
1158 if( $logBody ) {
1159 $out->addHTML(
1160 $this->list->beginLogEventsList() .
1161 $logBody .
1162 $this->list->endLogEventsList()
1164 } else {
1165 $out->addWikiMsg( 'logempty' );