In Database::getLag(): Add some more I/O thread states.
[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
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;
569 # Insert list of top 50 or so items
570 $loglist = new LogEventsList( $wgUser->getSkin(), $out, 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 $out->addHTML(
576 $loglist->beginLogEventsList() .
577 $logBody .
578 $loglist->endLogEventsList()
579 );
580 } else {
581 $out->addWikiMsg( 'logempty' );
582 }
583 return $pager->getNumRows();
584 }
585
586 /**
587 * SQL clause to skip forbidden log types for this user
588 * @param $db Database
589 * @param $audience string, public/user
590 * @return mixed (string or false)
591 */
592 public static function getExcludeClause( $db, $audience = 'public' ) {
593 global $wgLogRestrictions, $wgUser;
594 // Reset the array, clears extra "where" clauses when $par is used
595 $hiddenLogs = array();
596 // Don't show private logs to unprivileged users
597 foreach( $wgLogRestrictions as $logType => $right ) {
598 if( $audience == 'public' || !$wgUser->isAllowed($right) ) {
599 $safeType = $db->strencode( $logType );
600 $hiddenLogs[] = $safeType;
601 }
602 }
603 if( count($hiddenLogs) == 1 ) {
604 return 'log_type != ' . $db->addQuotes( $hiddenLogs[0] );
605 } elseif( $hiddenLogs ) {
606 return 'log_type NOT IN (' . $db->makeList($hiddenLogs) . ')';
607 }
608 return false;
609 }
610 }
611
612 /**
613 * @ingroup Pager
614 */
615 class LogPager extends ReverseChronologicalPager {
616 private $types = array(), $user = '', $title = '', $pattern = '';
617 private $typeCGI = '';
618 public $mLogEventsList;
619
620 /**
621 * constructor
622 * @param $list LogEventsList
623 * @param $types String or Array
624 * @param $user String
625 * @param $title String
626 * @param $pattern String
627 * @param $conds Array
628 * @param $year Integer
629 * @param $month Integer
630 */
631 public function __construct( $list, $types = array(), $user = '', $title = '', $pattern = '',
632 $conds = array(), $year = false, $month = false, $tagFilter = '' )
633 {
634 parent::__construct();
635 $this->mConds = $conds;
636
637 $this->mLogEventsList = $list;
638
639 $this->limitType( $types ); // also excludes hidden types
640 $this->limitUser( $user );
641 $this->limitTitle( $title, $pattern );
642 $this->getDateCond( $year, $month );
643 $this->mTagFilter = $tagFilter;
644 }
645
646 public function getDefaultQuery() {
647 $query = parent::getDefaultQuery();
648 $query['type'] = $this->typeCGI; // arrays won't work here
649 $query['user'] = $this->user;
650 $query['month'] = $this->mMonth;
651 $query['year'] = $this->mYear;
652 return $query;
653 }
654
655 // Call ONLY after calling $this->limitType() already!
656 public function getFilterParams() {
657 global $wgFilterLogTypes, $wgUser, $wgRequest;
658 $filters = array();
659 if( count($this->types) ) {
660 return $filters;
661 }
662 foreach( $wgFilterLogTypes as $type => $default ) {
663 // Avoid silly filtering
664 if( $type !== 'patrol' || $wgUser->useNPPatrol() ) {
665 $hide = $wgRequest->getInt( "hide_{$type}_log", $default );
666 $filters[$type] = $hide;
667 if( $hide )
668 $this->mConds[] = 'log_type != ' . $this->mDb->addQuotes( $type );
669 }
670 }
671 return $filters;
672 }
673
674 /**
675 * Set the log reader to return only entries of the given type.
676 * Type restrictions enforced here
677 * @param $types String or array: Log types ('upload', 'delete', etc);
678 * empty string means no restriction
679 */
680 private function limitType( $types ) {
681 global $wgLogRestrictions, $wgUser;
682 // If $types is not an array, make it an array
683 $types = ($types === '') ? array() : (array)$types;
684 // Don't even show header for private logs; don't recognize it...
685 foreach ( $types as $type ) {
686 if( isset( $wgLogRestrictions[$type] ) && !$wgUser->isAllowed($wgLogRestrictions[$type]) ) {
687 $types = array_diff( $types, array( $type ) );
688 }
689 }
690 // Don't show private logs to unprivileged users.
691 // Also, only show them upon specific request to avoid suprises.
692 $audience = $types ? 'user' : 'public';
693 $hideLogs = LogEventsList::getExcludeClause( $this->mDb, $audience );
694 if( $hideLogs !== false ) {
695 $this->mConds[] = $hideLogs;
696 }
697 if( count($types) ) {
698 $this->types = $types;
699 $this->mConds['log_type'] = $types;
700 // Set typeCGI; used in url param for paging
701 if( count($types) == 1 ) $this->typeCGI = $types[0];
702 }
703 }
704
705 /**
706 * Set the log reader to return only entries by the given user.
707 * @param $name String: (In)valid user name
708 */
709 private function limitUser( $name ) {
710 if( $name == '' ) {
711 return false;
712 }
713 $usertitle = Title::makeTitleSafe( NS_USER, $name );
714 if( is_null($usertitle) ) {
715 return false;
716 }
717 /* Fetch userid at first, if known, provides awesome query plan afterwards */
718 $userid = User::idFromName( $name );
719 if( !$userid ) {
720 /* It should be nicer to abort query at all,
721 but for now it won't pass anywhere behind the optimizer */
722 $this->mConds[] = "NULL";
723 } else {
724 global $wgUser;
725 $this->mConds['log_user'] = $userid;
726 // Paranoia: avoid brute force searches (bug 17342)
727 if( !$wgUser->isAllowed( 'deleterevision' ) ) {
728 $this->mConds[] = $this->mDb->bitAnd('log_deleted', LogPage::DELETED_USER) . ' = 0';
729 } else if( !$wgUser->isAllowed( 'suppressrevision' ) ) {
730 $this->mConds[] = $this->mDb->bitAnd('log_deleted', LogPage::SUPPRESSED_USER) .
731 ' != ' . LogPage::SUPPRESSED_USER;
732 }
733 $this->user = $usertitle->getText();
734 }
735 }
736
737 /**
738 * Set the log reader to return only entries affecting the given page.
739 * (For the block and rights logs, this is a user page.)
740 * @param $page String: Title name as text
741 * @param $pattern String
742 */
743 private function limitTitle( $page, $pattern ) {
744 global $wgMiserMode, $wgUser;
745
746 $title = Title::newFromText( $page );
747 if( strlen($page) == 0 || !$title instanceof Title )
748 return false;
749
750 $this->title = $title->getPrefixedText();
751 $ns = $title->getNamespace();
752 # Using the (log_namespace, log_title, log_timestamp) index with a
753 # range scan (LIKE) on the first two parts, instead of simple equality,
754 # makes it unusable for sorting. Sorted retrieval using another index
755 # would be possible, but then we might have to scan arbitrarily many
756 # nodes of that index. Therefore, we need to avoid this if $wgMiserMode
757 # is on.
758 #
759 # This is not a problem with simple title matches, because then we can
760 # use the page_time index. That should have no more than a few hundred
761 # log entries for even the busiest pages, so it can be safely scanned
762 # in full to satisfy an impossible condition on user or similar.
763 if( $pattern && !$wgMiserMode ) {
764 # use escapeLike to avoid expensive search patterns like 't%st%'
765 $safetitle = $this->mDb->escapeLike( $title->getDBkey() );
766 $this->mConds['log_namespace'] = $ns;
767 $this->mConds[] = "log_title LIKE '$safetitle%'";
768 $this->pattern = $pattern;
769 } else {
770 $this->mConds['log_namespace'] = $ns;
771 $this->mConds['log_title'] = $title->getDBkey();
772 }
773 // Paranoia: avoid brute force searches (bug 17342)
774 if( !$wgUser->isAllowed( 'deleterevision' ) ) {
775 $this->mConds[] = $this->mDb->bitAnd('log_deleted', LogPage::DELETED_ACTION) . ' = 0';
776 } else if( !$wgUser->isAllowed( 'suppressrevision' ) ) {
777 $this->mConds[] = $this->mDb->bitAnd('log_deleted', LogPage::SUPPRESSED_ACTION) .
778 ' != ' . LogPage::SUPPRESSED_ACTION;
779 }
780 }
781
782 public function getQueryInfo() {
783 $tables = array( 'logging', 'user' );
784 $this->mConds[] = 'user_id = log_user';
785 $groupBy = false;
786 # Add log_search table if there are conditions on it
787 if( array_key_exists('ls_field',$this->mConds) ) {
788 $tables[] = 'log_search';
789 $index = array( 'log_search' => 'ls_field_val', 'logging' => 'PRIMARY' );
790 $groupBy = 'ls_log_id';
791 # Don't use the wrong logging index
792 } else if( $this->title || $this->pattern || $this->user ) {
793 $index = array( 'logging' => array('page_time','user_time') );
794 } else if( $this->types ) {
795 $index = array( 'logging' => 'type_time' );
796 } else {
797 $index = array( 'logging' => 'times' );
798 }
799 $options = array( 'USE INDEX' => $index );
800 # Don't show duplicate rows when using log_search
801 if( $groupBy ) $options['GROUP BY'] = $groupBy;
802 $info = array(
803 'tables' => $tables,
804 'fields' => array( 'log_type', 'log_action', 'log_user', 'log_namespace',
805 'log_title', 'log_params', 'log_comment', 'log_id', 'log_deleted',
806 'log_timestamp', 'user_name', 'user_editcount' ),
807 'conds' => $this->mConds,
808 'options' => $options,
809 'join_conds' => array(
810 'user' => array( 'INNER JOIN', 'user_id=log_user' ),
811 'log_search' => array( 'INNER JOIN', 'ls_log_id=log_id' )
812 )
813 );
814 # Add ChangeTags filter query
815 ChangeTags::modifyDisplayQuery( $info['tables'], $info['fields'], $info['conds'],
816 $info['join_conds'], $info['options'], $this->mTagFilter );
817
818 return $info;
819 }
820
821 function getIndexField() {
822 return 'log_timestamp';
823 }
824
825 public function getStartBody() {
826 wfProfileIn( __METHOD__ );
827 # Do a link batch query
828 if( $this->getNumRows() > 0 ) {
829 $lb = new LinkBatch;
830 while( $row = $this->mResult->fetchObject() ) {
831 $lb->add( $row->log_namespace, $row->log_title );
832 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
833 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
834 }
835 $lb->execute();
836 $this->mResult->seek( 0 );
837 }
838 wfProfileOut( __METHOD__ );
839 return '';
840 }
841
842 public function formatRow( $row ) {
843 return $this->mLogEventsList->logLine( $row );
844 }
845
846 public function getType() {
847 return $this->types;
848 }
849
850 public function getUser() {
851 return $this->user;
852 }
853
854 public function getPage() {
855 return $this->title;
856 }
857
858 public function getPattern() {
859 return $this->pattern;
860 }
861
862 public function getYear() {
863 return $this->mYear;
864 }
865
866 public function getMonth() {
867 return $this->mMonth;
868 }
869
870 public function getTagFilter() {
871 return $this->mTagFilter;
872 }
873
874 public function doQuery() {
875 // Workaround MySQL optimizer bug
876 $this->mDb->setBigSelects();
877 parent::doQuery();
878 $this->mDb->setBigSelects( 'default' );
879 }
880 }
881
882 /**
883 * @deprecated
884 * @ingroup SpecialPage
885 */
886 class LogReader {
887 var $pager;
888 /**
889 * @param $request WebRequest: for internal use use a FauxRequest object to pass arbitrary parameters.
890 */
891 function __construct( $request ) {
892 global $wgUser, $wgOut;
893 wfDeprecated(__METHOD__);
894 # Get parameters
895 $type = $request->getVal( 'type' );
896 $user = $request->getText( 'user' );
897 $title = $request->getText( 'page' );
898 $pattern = $request->getBool( 'pattern' );
899 $year = $request->getIntOrNull( 'year' );
900 $month = $request->getIntOrNull( 'month' );
901 $tagFilter = $request->getVal( 'tagfilter' );
902 # Don't let the user get stuck with a certain date
903 $skip = $request->getText( 'offset' ) || $request->getText( 'dir' ) == 'prev';
904 if( $skip ) {
905 $year = '';
906 $month = '';
907 }
908 # Use new list class to output results
909 $loglist = new LogEventsList( $wgUser->getSkin(), $wgOut, 0 );
910 $this->pager = new LogPager( $loglist, $type, $user, $title, $pattern, $year, $month, $tagFilter );
911 }
912
913 /**
914 * Is there at least one row?
915 * @return bool
916 */
917 public function hasRows() {
918 return isset($this->pager) ? ($this->pager->getNumRows() > 0) : false;
919 }
920 }
921
922 /**
923 * @deprecated
924 * @ingroup SpecialPage
925 */
926 class LogViewer {
927 const NO_ACTION_LINK = 1;
928
929 /**
930 * LogReader object
931 */
932 var $reader;
933
934 /**
935 * @param &$reader LogReader: where to get our data from
936 * @param $flags Integer: Bitwise combination of flags:
937 * LogEventsList::NO_ACTION_LINK Don't show restore/unblock/block links
938 */
939 function __construct( &$reader, $flags = 0 ) {
940 wfDeprecated(__METHOD__);
941 $this->reader =& $reader;
942 $this->reader->pager->mLogEventsList->flags = $flags;
943 # Aliases for shorter code...
944 $this->pager =& $this->reader->pager;
945 $this->list =& $this->reader->pager->mLogEventsList;
946 }
947
948 /**
949 * Take over the whole output page in $wgOut with the log display.
950 */
951 public function show() {
952 # Set title and add header
953 $this->list->showHeader( $pager->getType() );
954 # Show form options
955 $this->list->showOptions( $this->pager->getType(), $this->pager->getUser(), $this->pager->getPage(),
956 $this->pager->getPattern(), $this->pager->getYear(), $this->pager->getMonth() );
957 # Insert list
958 $logBody = $this->pager->getBody();
959 if( $logBody ) {
960 $wgOut->addHTML(
961 $this->pager->getNavigationBar() .
962 $this->list->beginLogEventsList() .
963 $logBody .
964 $this->list->endLogEventsList() .
965 $this->pager->getNavigationBar()
966 );
967 } else {
968 $wgOut->addWikiMsg( 'logempty' );
969 }
970 }
971
972 /**
973 * Output just the list of entries given by the linked LogReader,
974 * with extraneous UI elements. Use for displaying log fragments in
975 * another page (eg at Special:Undelete)
976 * @param $out OutputPage: where to send output
977 */
978 public function showList( &$out ) {
979 $logBody = $this->pager->getBody();
980 if( $logBody ) {
981 $out->addHTML(
982 $this->list->beginLogEventsList() .
983 $logBody .
984 $this->list->endLogEventsList()
985 );
986 } else {
987 $out->addWikiMsg( 'logempty' );
988 }
989 }
990 }