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