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