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