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