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