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