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