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