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