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