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