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