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