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