Update deprecated method calls
[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 $Ids = explode( ',', $paramArray[0] );
464 $query = $paramArray[0];
465 // Link to each hidden object ID, $paramArray[1] is the url param
466 $revert = '(' . $this->skin->link(
467 $revdel,
468 $this->message['revdel-restore'],
469 array(),
470 array(
471 'target' => $title->getPrefixedText(),
472 'type' => 'logging',
473 'ids' => $query
474 ),
475 array( 'known', 'noclasses' )
476 ) . ')';
477 }
478 // Self-created users
479 } else if( self::typeAction( $row, 'newusers', 'create2' ) ) {
480 if( isset( $paramArray[0] ) ) {
481 $revert = $this->skin->userToolLinks( $paramArray[0], $title->getDBkey(), true );
482 } else {
483 # Fall back to a blue contributions link
484 $revert = $this->skin->userToolLinks( 1, $title->getDBkey() );
485 }
486 $ts = wfTimestamp( TS_UNIX, $row->log_timestamp );
487 if( $ts < '20080129000000' ) {
488 # Suppress $comment from old entries (before 2008-01-29),
489 # not needed and can contain incorrect links
490 $comment = '';
491 }
492 // Do nothing. The implementation is handled by the hook modifiying the passed-by-ref parameters.
493 } else {
494 wfRunHooks( 'LogLine', array( $row->log_type, $row->log_action, $title, $paramArray,
495 &$comment, &$revert, $row->log_timestamp ) );
496 }
497 if( $revert != '' ) {
498 $revert = '<span class="mw-logevent-actionlink">' . $revert . '</span>';
499 }
500 return $revert;
501 }
502
503 /**
504 * @param $row Row
505 * @return string
506 */
507 private function getShowHideLinks( $row ) {
508 global $wgUser;
509 if( ( $this->flags & self::NO_ACTION_LINK ) // we don't want to see the links
510 || $row->log_type == 'suppress' ) // no one can hide items from the suppress log
511 {
512 return '';
513 }
514 $del = '';
515 // Don't show useless link to people who cannot hide revisions
516 if( $wgUser->isAllowed( 'deletedhistory' ) ) {
517 if( $row->log_deleted || $wgUser->isAllowed( 'deleterevision' ) ) {
518 $canHide = $wgUser->isAllowed( 'deleterevision' );
519 // If event was hidden from sysops
520 if( !self::userCan( $row, LogPage::DELETED_RESTRICTED ) ) {
521 $del = $this->skin->revDeleteLinkDisabled( $canHide );
522 } else {
523 $target = SpecialPage::getTitleFor( 'Log', $row->log_type );
524 $query = array(
525 'target' => $target->getPrefixedDBkey(),
526 'type' => 'logging',
527 'ids' => $row->log_id,
528 );
529 $del = $this->skin->revDeleteLink( $query,
530 self::isDeleted( $row, LogPage::DELETED_RESTRICTED ), $canHide );
531 }
532 }
533 }
534 return $del;
535 }
536
537 /**
538 * @param $row Row
539 * @param $type Mixed: string/array
540 * @param $action Mixed: string/array
541 * @param $right string
542 * @return Boolean
543 */
544 public static function typeAction( $row, $type, $action, $right='' ) {
545 $match = is_array($type) ?
546 in_array( $row->log_type, $type ) : $row->log_type == $type;
547 if( $match ) {
548 $match = is_array( $action ) ?
549 in_array( $row->log_action, $action ) : $row->log_action == $action;
550 if( $match && $right ) {
551 global $wgUser;
552 $match = $wgUser->isAllowed( $right );
553 }
554 }
555 return $match;
556 }
557
558 /**
559 * Determine if the current user is allowed to view a particular
560 * field of this log row, if it's marked as deleted.
561 *
562 * @param $row Row
563 * @param $field Integer
564 * @return Boolean
565 */
566 public static function userCan( $row, $field ) {
567 return self::userCanBitfield( $row->log_deleted, $field );
568 }
569
570 /**
571 * Determine if the current user is allowed to view a particular
572 * field of this log row, if it's marked as deleted.
573 *
574 * @param $bitfield Integer (current field)
575 * @param $field Integer
576 * @return Boolean
577 */
578 public static function userCanBitfield( $bitfield, $field ) {
579 if( $bitfield & $field ) {
580 global $wgUser;
581
582 if ( $bitfield & LogPage::DELETED_RESTRICTED ) {
583 $permission = 'suppressrevision';
584 } else {
585 $permission = 'deletedhistory';
586 }
587 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
588 return $wgUser->isAllowed( $permission );
589 } else {
590 return true;
591 }
592 }
593
594 /**
595 * @param $row Row
596 * @param $field Integer: one of DELETED_* bitfield constants
597 * @return Boolean
598 */
599 public static function isDeleted( $row, $field ) {
600 return ( $row->log_deleted & $field ) == $field;
601 }
602
603 /**
604 * Show log extract. Either with text and a box (set $msgKey) or without (don't set $msgKey)
605 *
606 * @param $out OutputPage or String-by-reference
607 * @param $types String or Array
608 * @param $page String The page title to show log entries for
609 * @param $user String The user who made the log entries
610 * @param $param Associative Array with the following additional options:
611 * - lim Integer Limit of items to show, default is 50
612 * - conds Array Extra conditions for the query (e.g. "log_action != 'revision'")
613 * - showIfEmpty boolean Set to false if you don't want any output in case the loglist is empty
614 * if set to true (default), "No matching items in log" is displayed if loglist is empty
615 * - msgKey Array If you want a nice box with a message, set this to the key of the message.
616 * First element is the message key, additional optional elements are parameters for the key
617 * that are processed with wgMsgExt and option 'parse'
618 * - offset Set to overwrite offset parameter in $wgRequest
619 * set to '' to unset offset
620 * - wrap String Wrap the message in html (usually something like "<div ...>$1</div>").
621 * - flags Integer display flags (NO_ACTION_LINK,NO_EXTRA_USER_LINKS)
622 * @return Integer Number of total log items (not limited by $lim)
623 */
624 public static function showLogExtract(
625 &$out, $types=array(), $page='', $user='', $param = array()
626 ) {
627 global $wgUser, $wgOut;
628 $defaultParameters = array(
629 'lim' => 25,
630 'conds' => array(),
631 'showIfEmpty' => true,
632 'msgKey' => array(''),
633 'wrap' => "$1",
634 'flags' => 0
635 );
636 # The + operator appends elements of remaining keys from the right
637 # handed array to the left handed, whereas duplicated keys are NOT overwritten.
638 $param += $defaultParameters;
639 # Convert $param array to individual variables
640 $lim = $param['lim'];
641 $conds = $param['conds'];
642 $showIfEmpty = $param['showIfEmpty'];
643 $msgKey = $param['msgKey'];
644 $wrap = $param['wrap'];
645 $flags = $param['flags'];
646 if ( !is_array( $msgKey ) ) {
647 $msgKey = array( $msgKey );
648 }
649 # Insert list of top 50 (or top $lim) items
650 $loglist = new LogEventsList( $wgUser->getSkin(), $wgOut, $flags );
651 $pager = new LogPager( $loglist, $types, $user, $page, '', $conds );
652 if ( isset( $param['offset'] ) ) { # Tell pager to ignore $wgRequest offset
653 $pager->setOffset( $param['offset'] );
654 }
655 if( $lim > 0 ) $pager->mLimit = $lim;
656 $logBody = $pager->getBody();
657 $s = '';
658 if( $logBody ) {
659 if ( $msgKey[0] ) {
660 $s = '<div class="mw-warning-with-logexcerpt">';
661
662 if ( count( $msgKey ) == 1 ) {
663 $s .= wfMsgExt( $msgKey[0], array( 'parse' ) );
664 } else { // Process additional arguments
665 $args = $msgKey;
666 array_shift( $args );
667 $s .= wfMsgExt( $msgKey[0], array( 'parse' ), $args );
668 }
669 }
670 $s .= $loglist->beginLogEventsList() .
671 $logBody .
672 $loglist->endLogEventsList();
673 } else {
674 if ( $showIfEmpty )
675 $s = Html::rawElement( 'div', array( 'class' => 'mw-warning-logempty' ),
676 wfMsgExt( 'logempty', array( 'parseinline' ) ) );
677 }
678 if( $pager->getNumRows() > $pager->mLimit ) { # Show "Full log" link
679 $urlParam = array();
680 if ( $page != '')
681 $urlParam['page'] = $page;
682 if ( $user != '')
683 $urlParam['user'] = $user;
684 if ( !is_array( $types ) ) # Make it an array, if it isn't
685 $types = array( $types );
686 # If there is exactly one log type, we can link to Special:Log?type=foo
687 if ( count( $types ) == 1 )
688 $urlParam['type'] = $types[0];
689 $s .= $wgUser->getSkin()->link(
690 SpecialPage::getTitleFor( 'Log' ),
691 wfMsgHtml( 'log-fulllog' ),
692 array(),
693 $urlParam
694 );
695 }
696 if ( $logBody && $msgKey[0] ) {
697 $s .= '</div>';
698 }
699
700 if ( $wrap!='' ) { // Wrap message in html
701 $s = str_replace( '$1', $s, $wrap );
702 }
703
704 // $out can be either an OutputPage object or a String-by-reference
705 if( $out instanceof OutputPage ){
706 $out->addHTML( $s );
707 } else {
708 $out = $s;
709 }
710 return $pager->getNumRows();
711 }
712
713 /**
714 * SQL clause to skip forbidden log types for this user
715 *
716 * @param $db Database
717 * @param $audience string, public/user
718 * @return Mixed: string or false
719 */
720 public static function getExcludeClause( $db, $audience = 'public' ) {
721 global $wgLogRestrictions, $wgUser;
722 // Reset the array, clears extra "where" clauses when $par is used
723 $hiddenLogs = array();
724 // Don't show private logs to unprivileged users
725 foreach( $wgLogRestrictions as $logType => $right ) {
726 if( $audience == 'public' || !$wgUser->isAllowed($right) ) {
727 $safeType = $db->strencode( $logType );
728 $hiddenLogs[] = $safeType;
729 }
730 }
731 if( count($hiddenLogs) == 1 ) {
732 return 'log_type != ' . $db->addQuotes( $hiddenLogs[0] );
733 } elseif( $hiddenLogs ) {
734 return 'log_type NOT IN (' . $db->makeList($hiddenLogs) . ')';
735 }
736 return false;
737 }
738 }
739
740 /**
741 * @ingroup Pager
742 */
743 class LogPager extends ReverseChronologicalPager {
744 private $types = array(), $user = '', $title = '', $pattern = '';
745 private $typeCGI = '';
746 public $mLogEventsList;
747
748 /**
749 * Constructor
750 *
751 * @param $list LogEventsList
752 * @param $types String or Array: log types to show
753 * @param $user String: the user who made the log entries
754 * @param $title String: the page title the log entries are for
755 * @param $pattern String: do a prefix search rather than an exact title match
756 * @param $conds Array: extra conditions for the query
757 * @param $year Integer: the year to start from
758 * @param $month Integer: the month to start from
759 * @param $tagFilter String: tag
760 */
761 public function __construct( $list, $types = array(), $user = '', $title = '', $pattern = '',
762 $conds = array(), $year = false, $month = false, $tagFilter = '' )
763 {
764 parent::__construct();
765 $this->mConds = $conds;
766
767 $this->mLogEventsList = $list;
768
769 $this->limitType( $types ); // also excludes hidden types
770 $this->limitUser( $user );
771 $this->limitTitle( $title, $pattern );
772 $this->getDateCond( $year, $month );
773 $this->mTagFilter = $tagFilter;
774 }
775
776 public function getDefaultQuery() {
777 $query = parent::getDefaultQuery();
778 $query['type'] = $this->typeCGI; // arrays won't work here
779 $query['user'] = $this->user;
780 $query['month'] = $this->mMonth;
781 $query['year'] = $this->mYear;
782 return $query;
783 }
784
785 // Call ONLY after calling $this->limitType() already!
786 public function getFilterParams() {
787 global $wgFilterLogTypes, $wgUser, $wgRequest;
788 $filters = array();
789 if( count($this->types) ) {
790 return $filters;
791 }
792 foreach( $wgFilterLogTypes as $type => $default ) {
793 // Avoid silly filtering
794 if( $type !== 'patrol' || $wgUser->useNPPatrol() ) {
795 $hide = $wgRequest->getInt( "hide_{$type}_log", $default );
796 $filters[$type] = $hide;
797 if( $hide )
798 $this->mConds[] = 'log_type != ' . $this->mDb->addQuotes( $type );
799 }
800 }
801 return $filters;
802 }
803
804 /**
805 * Set the log reader to return only entries of the given type.
806 * Type restrictions enforced here
807 *
808 * @param $types String or array: Log types ('upload', 'delete', etc);
809 * empty string means no restriction
810 */
811 private function limitType( $types ) {
812 global $wgLogRestrictions, $wgUser;
813 // If $types is not an array, make it an array
814 $types = ($types === '') ? array() : (array)$types;
815 // Don't even show header for private logs; don't recognize it...
816 foreach ( $types as $type ) {
817 if( isset( $wgLogRestrictions[$type] )
818 && !$wgUser->isAllowed($wgLogRestrictions[$type])
819 ) {
820 $types = array_diff( $types, array( $type ) );
821 }
822 }
823 $this->types = $types;
824 // Don't show private logs to unprivileged users.
825 // Also, only show them upon specific request to avoid suprises.
826 $audience = $types ? 'user' : 'public';
827 $hideLogs = LogEventsList::getExcludeClause( $this->mDb, $audience );
828 if( $hideLogs !== false ) {
829 $this->mConds[] = $hideLogs;
830 }
831 if( count($types) ) {
832 $this->mConds['log_type'] = $types;
833 // Set typeCGI; used in url param for paging
834 if( count($types) == 1 ) $this->typeCGI = $types[0];
835 }
836 }
837
838 /**
839 * Set the log reader to return only entries by the given user.
840 *
841 * @param $name String: (In)valid user name
842 */
843 private function limitUser( $name ) {
844 if( $name == '' ) {
845 return false;
846 }
847 $usertitle = Title::makeTitleSafe( NS_USER, $name );
848 if( is_null($usertitle) ) {
849 return false;
850 }
851 /* Fetch userid at first, if known, provides awesome query plan afterwards */
852 $userid = User::idFromName( $name );
853 if( !$userid ) {
854 /* It should be nicer to abort query at all,
855 but for now it won't pass anywhere behind the optimizer */
856 $this->mConds[] = "NULL";
857 } else {
858 global $wgUser;
859 $this->mConds['log_user'] = $userid;
860 // Paranoia: avoid brute force searches (bug 17342)
861 if( !$wgUser->isAllowed( 'deletedhistory' ) ) {
862 $this->mConds[] = $this->mDb->bitAnd('log_deleted', LogPage::DELETED_USER) . ' = 0';
863 } else if( !$wgUser->isAllowed( 'suppressrevision' ) ) {
864 $this->mConds[] = $this->mDb->bitAnd('log_deleted', LogPage::SUPPRESSED_USER) .
865 ' != ' . LogPage::SUPPRESSED_USER;
866 }
867 $this->user = $usertitle->getText();
868 }
869 }
870
871 /**
872 * Set the log reader to return only entries affecting the given page.
873 * (For the block and rights logs, this is a user page.)
874 *
875 * @param $page String: Title name as text
876 * @param $pattern String
877 */
878 private function limitTitle( $page, $pattern ) {
879 global $wgMiserMode, $wgUser;
880
881 $title = Title::newFromText( $page );
882 if( strlen( $page ) == 0 || !$title instanceof Title )
883 return false;
884
885 $this->title = $title->getPrefixedText();
886 $ns = $title->getNamespace();
887 $db = $this->mDb;
888
889 # Using the (log_namespace, log_title, log_timestamp) index with a
890 # range scan (LIKE) on the first two parts, instead of simple equality,
891 # makes it unusable for sorting. Sorted retrieval using another index
892 # would be possible, but then we might have to scan arbitrarily many
893 # nodes of that index. Therefore, we need to avoid this if $wgMiserMode
894 # is on.
895 #
896 # This is not a problem with simple title matches, because then we can
897 # use the page_time index. That should have no more than a few hundred
898 # log entries for even the busiest pages, so it can be safely scanned
899 # in full to satisfy an impossible condition on user or similar.
900 if( $pattern && !$wgMiserMode ) {
901 $this->mConds['log_namespace'] = $ns;
902 $this->mConds[] = 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() );
903 $this->pattern = $pattern;
904 } else {
905 $this->mConds['log_namespace'] = $ns;
906 $this->mConds['log_title'] = $title->getDBkey();
907 }
908 // Paranoia: avoid brute force searches (bug 17342)
909 if( !$wgUser->isAllowed( 'deletedhistory' ) ) {
910 $this->mConds[] = $db->bitAnd('log_deleted', LogPage::DELETED_ACTION) . ' = 0';
911 } else if( !$wgUser->isAllowed( 'suppressrevision' ) ) {
912 $this->mConds[] = $db->bitAnd('log_deleted', LogPage::SUPPRESSED_ACTION) .
913 ' != ' . LogPage::SUPPRESSED_ACTION;
914 }
915 }
916
917 public function getQueryInfo() {
918 $tables = array( 'logging', 'user' );
919 $this->mConds[] = 'user_id = log_user';
920 $index = array();
921 $options = array();
922 # Add log_search table if there are conditions on it
923 if( array_key_exists('ls_field',$this->mConds) ) {
924 $tables[] = 'log_search';
925 $index['log_search'] = 'ls_field_val';
926 $index['logging'] = 'PRIMARY';
927 $options[] = 'DISTINCT';
928 # Avoid usage of the wrong index by limiting
929 # the choices of available indexes. This mainly
930 # avoids site-breaking filesorts.
931 } else if( $this->title || $this->pattern || $this->user ) {
932 $index['logging'] = array( 'page_time', 'user_time' );
933 if( count($this->types) == 1 ) {
934 $index['logging'][] = 'log_user_type_time';
935 }
936 } else if( count($this->types) == 1 ) {
937 $index['logging'] = 'type_time';
938 } else {
939 $index['logging'] = 'times';
940 }
941 $options['USE INDEX'] = $index;
942 # Don't show duplicate rows when using log_search
943 $info = array(
944 'tables' => $tables,
945 'fields' => array( 'log_type', 'log_action', 'log_user', 'log_namespace',
946 'log_title', 'log_params', 'log_comment', 'log_id', 'log_deleted',
947 'log_timestamp', 'user_name', 'user_editcount' ),
948 'conds' => $this->mConds,
949 'options' => $options,
950 'join_conds' => array(
951 'user' => array( 'INNER JOIN', 'user_id=log_user' ),
952 'log_search' => array( 'INNER JOIN', 'ls_log_id=log_id' )
953 )
954 );
955 # Add ChangeTags filter query
956 ChangeTags::modifyDisplayQuery( $info['tables'], $info['fields'], $info['conds'],
957 $info['join_conds'], $info['options'], $this->mTagFilter );
958 return $info;
959 }
960
961 function getIndexField() {
962 return 'log_timestamp';
963 }
964
965 public function getStartBody() {
966 wfProfileIn( __METHOD__ );
967 # Do a link batch query
968 if( $this->getNumRows() > 0 ) {
969 $lb = new LinkBatch;
970 foreach ( $this->mResult as $row ) {
971 $lb->add( $row->log_namespace, $row->log_title );
972 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
973 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
974 }
975 $lb->execute();
976 $this->mResult->seek( 0 );
977 }
978 wfProfileOut( __METHOD__ );
979 return '';
980 }
981
982 public function formatRow( $row ) {
983 return $this->mLogEventsList->logLine( $row );
984 }
985
986 public function getType() {
987 return $this->types;
988 }
989
990 public function getUser() {
991 return $this->user;
992 }
993
994 public function getPage() {
995 return $this->title;
996 }
997
998 public function getPattern() {
999 return $this->pattern;
1000 }
1001
1002 public function getYear() {
1003 return $this->mYear;
1004 }
1005
1006 public function getMonth() {
1007 return $this->mMonth;
1008 }
1009
1010 public function getTagFilter() {
1011 return $this->mTagFilter;
1012 }
1013
1014 public function doQuery() {
1015 // Workaround MySQL optimizer bug
1016 $this->mDb->setBigSelects();
1017 parent::doQuery();
1018 $this->mDb->setBigSelects( 'default' );
1019 }
1020 }
1021
1022 /**
1023 * @deprecated
1024 * @ingroup SpecialPage
1025 */
1026 class LogReader {
1027 var $pager;
1028
1029 /**
1030 * @param $request WebRequest: for internal use use a FauxRequest object to pass arbitrary parameters.
1031 */
1032 function __construct( $request ) {
1033 global $wgUser, $wgOut;
1034 wfDeprecated(__METHOD__);
1035 # Get parameters
1036 $type = $request->getVal( 'type' );
1037 $user = $request->getText( 'user' );
1038 $title = $request->getText( 'page' );
1039 $pattern = $request->getBool( 'pattern' );
1040 $year = $request->getIntOrNull( 'year' );
1041 $month = $request->getIntOrNull( 'month' );
1042 $tagFilter = $request->getVal( 'tagfilter' );
1043 # Don't let the user get stuck with a certain date
1044 $skip = $request->getText( 'offset' ) || $request->getText( 'dir' ) == 'prev';
1045 if( $skip ) {
1046 $year = '';
1047 $month = '';
1048 }
1049 # Use new list class to output results
1050 $loglist = new LogEventsList( $wgUser->getSkin(), $wgOut, 0 );
1051 $this->pager = new LogPager( $loglist, $type, $user, $title, $pattern, $year, $month, $tagFilter );
1052 }
1053
1054 /**
1055 * Is there at least one row?
1056 * @return bool
1057 */
1058 public function hasRows() {
1059 return isset($this->pager) ? ($this->pager->getNumRows() > 0) : false;
1060 }
1061 }
1062
1063 /**
1064 * @deprecated
1065 * @ingroup SpecialPage
1066 */
1067 class LogViewer {
1068 const NO_ACTION_LINK = 1;
1069
1070 /**
1071 * LogReader object
1072 */
1073 var $reader;
1074
1075 /**
1076 * @param &$reader LogReader: where to get our data from
1077 * @param $flags Integer: Bitwise combination of flags:
1078 * LogEventsList::NO_ACTION_LINK Don't show restore/unblock/block links
1079 */
1080 function __construct( &$reader, $flags = 0 ) {
1081 wfDeprecated(__METHOD__);
1082 $this->reader =& $reader;
1083 $this->reader->pager->mLogEventsList->flags = $flags;
1084 # Aliases for shorter code...
1085 $this->pager =& $this->reader->pager;
1086 $this->list =& $this->reader->pager->mLogEventsList;
1087 }
1088
1089 /**
1090 * Take over the whole output page in $wgOut with the log display.
1091 */
1092 public function show() {
1093 global $wgOut;
1094 # Set title and add header
1095 $this->list->showHeader( $pager->getType() );
1096 # Show form options
1097 $this->list->showOptions( $this->pager->getType(), $this->pager->getUser(), $this->pager->getPage(),
1098 $this->pager->getPattern(), $this->pager->getYear(), $this->pager->getMonth() );
1099 # Insert list
1100 $logBody = $this->pager->getBody();
1101 if( $logBody ) {
1102 $wgOut->addHTML(
1103 $this->pager->getNavigationBar() .
1104 $this->list->beginLogEventsList() .
1105 $logBody .
1106 $this->list->endLogEventsList() .
1107 $this->pager->getNavigationBar()
1108 );
1109 } else {
1110 $wgOut->addWikiMsg( 'logempty' );
1111 }
1112 }
1113
1114 /**
1115 * Output just the list of entries given by the linked LogReader,
1116 * with extraneous UI elements. Use for displaying log fragments in
1117 * another page (eg at Special:Undelete)
1118 *
1119 * @param $out OutputPage: where to send output
1120 */
1121 public function showList( &$out ) {
1122 $logBody = $this->pager->getBody();
1123 if( $logBody ) {
1124 $out->addHTML(
1125 $this->list->beginLogEventsList() .
1126 $logBody .
1127 $this->list->endLogEventsList()
1128 );
1129 } else {
1130 $out->addWikiMsg( 'logempty' );
1131 }
1132 }
1133 }