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