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