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