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