2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>, 2008 Aaron Schulz
3 # http://www.mediawiki.org/
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.
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.
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
21 const NO_ACTION_LINK
= 1;
27 function __construct( $skin, $out, $flags = 0 ) {
30 $this->flags
= $flags;
31 $this->preCacheMessages();
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
38 private function preCacheMessages() {
39 // Precache various messages
40 if( !isset( $this->message
) ) {
41 $messages = 'revertmerge protect_change unblocklink revertmove undeletelink revdel-restore rev-delundel hist';
42 foreach( explode(' ', $messages ) as $msg ) {
43 $this->message
[$msg] = wfMsgExt( $msg, array( 'escape') );
49 * Set page title and show header for this log type
52 public function showHeader( $type ) {
53 if( LogPage
::isLogType( $type ) ) {
54 $this->out
->setPageTitle( LogPage
::logName( $type ) );
55 $this->out
->addHtml( LogPage
::logHeader( $type ) );
60 * Show options for the log list
61 * @param string $type,
62 * @param string $user,
63 * @param string $page,
64 * @param string $pattern
68 public function showOptions( $type='', $user='', $page='', $pattern='', $year='', $month='' ) {
69 global $wgScript, $wgMiserMode;
70 $action = htmlspecialchars( $wgScript );
71 $title = SpecialPage
::getTitleFor( 'Log' );
72 $special = htmlspecialchars( $title->getPrefixedDBkey() );
74 $this->out
->addHTML( "<form action=\"$action\" method=\"get\"><fieldset>" .
75 Xml
::element( 'legend', array(), wfMsg( 'log' ) ) .
76 Xml
::hidden( 'title', $special ) . "\n" .
77 $this->getTypeMenu( $type ) . "\n" .
78 $this->getUserInput( $user ) . "\n" .
79 $this->getTitleInput( $page ) . "\n" .
80 ( !$wgMiserMode ?
($this->getTitlePattern( $pattern )."\n") : "" ) .
81 "<p>" . $this->getDateMenu( $year, $month ) . "\n" .
82 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . "</p>\n" .
83 "</fieldset></form>" );
87 * @return string Formatted HTML
88 * @param string $queryType
90 private function getTypeMenu( $queryType ) {
91 global $wgLogRestrictions, $wgUser;
93 $html = "<select name='type'>\n";
95 $validTypes = LogPage
::validTypes();
96 $m = array(); // Temporary array
98 // First pass to load the log names
99 foreach( $validTypes as $type ) {
100 $text = LogPage
::logName( $type );
104 // Second pass to sort by name
107 // Third pass generates sorted XHTML content
108 foreach( $m as $text => $type ) {
109 $selected = ($type == $queryType);
111 if ( isset($wgLogRestrictions[$type]) ) {
112 if ( $wgUser->isAllowed( $wgLogRestrictions[$type] ) ) {
113 $html .= Xml
::option( $text, $type, $selected ) . "\n";
116 $html .= Xml
::option( $text, $type, $selected ) . "\n";
120 $html .= '</select>';
125 * @return string Formatted HTML
126 * @param string $user
128 private function getUserInput( $user ) {
129 return Xml
::inputLabel( wfMsg( 'specialloguserlabel' ), 'user', 'user', 15, $user );
133 * @return string Formatted HTML
134 * @param string $title
136 private function getTitleInput( $title ) {
137 return Xml
::inputLabel( wfMsg( 'speciallogtitlelabel' ), 'page', 'page', 20, $title );
141 * @return string Formatted HTML
145 private function getDateMenu( $year, $month ) {
146 # Offset overrides year/month selection
147 if( $month && $month !== -1 ) {
148 $encMonth = intval( $month );
153 $encYear = intval( $year );
154 } else if( $encMonth ) {
155 $thisMonth = intval( gmdate( 'n' ) );
156 $thisYear = intval( gmdate( 'Y' ) );
157 if( intval($encMonth) > $thisMonth ) {
160 $encYear = $thisYear;
164 return Xml
::label( wfMsg( 'year' ), 'year' ) . ' '.
165 Xml
::input( 'year', 4, $encYear, array('id' => 'year', 'maxlength' => 4) ) .
167 Xml
::label( wfMsg( 'month' ), 'month' ) . ' '.
168 Xml
::monthSelector( $encMonth, -1 );
172 * @return boolean Checkbox
174 private function getTitlePattern( $pattern ) {
175 return '<span style="white-space: nowrap">' .
176 Xml
::checkLabel( wfMsg( 'log-title-wildcard' ), 'pattern', 'pattern', $pattern ) .
180 public function beginLogEventsList() {
184 public function endLogEventsList() {
189 * @param Row $row a single row from the result set
190 * @return string Formatted HTML list item
193 public function logLine( $row ) {
194 global $wgLang, $wgUser, $wgContLang;
196 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
197 $time = $wgLang->timeanddate( wfTimestamp(TS_MW
, $row->log_timestamp
), true );
199 if( self
::isDeleted($row,LogPage
::DELETED_USER
) ) {
200 $userLink = '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
202 $userLink = $this->skin
->userLink( $row->log_user
, $row->user_name
) .
203 $this->skin
->userToolLinks( $row->log_user
, $row->user_name
, true, 0, $row->user_editcount
);
206 if( self
::isDeleted($row,LogPage
::DELETED_COMMENT
) ) {
207 $comment = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-comment') . '</span>';
209 $comment = $wgContLang->getDirMark() . $this->skin
->commentBlock( $row->log_comment
);
211 // Extract extra parameters
212 $paramArray = LogPage
::extractParams( $row->log_params
);
214 // Some user can hide log items and have review links
215 if( $wgUser->isAllowed( 'deleterevision' ) ) {
216 $del = $this->showhideLinks( $row ) . ' ';
218 // Add review links and such...
219 if( ($this->flags
& self
::NO_ACTION_LINK
) ||
($row->log_deleted
& LogPage
::DELETED_ACTION
) ) {
220 // Action text is suppressed...
221 } else if( self
::typeAction($row,'move','move') && !empty($paramArray[0]) && $wgUser->isAllowed( 'move' ) ) {
222 $destTitle = Title
::newFromText( $paramArray[0] );
224 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Movepage' ),
225 $this->message
['revertmove'],
226 'wpOldTitle=' . urlencode( $destTitle->getPrefixedDBkey() ) .
227 '&wpNewTitle=' . urlencode( $title->getPrefixedDBkey() ) .
228 '&wpReason=' . urlencode( wfMsgForContent( 'revertmove' ) ) .
229 '&wpMovetalk=0' ) . ')';
231 // Show undelete link
232 } else if( self
::typeAction($row,array('delete','suppress'),'delete') && $wgUser->isAllowed( 'delete' ) ) {
233 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Undelete' ),
234 $this->message
['undeletelink'], 'target='. urlencode( $title->getPrefixedDBkey() ) ) . ')';
236 } else if( self
::typeAction($row,array('block','suppress'),'block') && $wgUser->isAllowed( 'block' ) ) {
237 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Ipblocklist' ),
238 $this->message
['unblocklink'],
239 'action=unblock&ip=' . urlencode( $row->log_title
) ) . ')';
240 // Show change protection link
241 } else if( self
::typeAction($row,'protect',array('modify','protect','unprotect')) ) {
242 $revert = '(' . $this->skin
->makeKnownLinkObj( $title, $this->message
['hist'],
243 'action=history&offset=' . urlencode($row->log_timestamp
) ) . ')';
244 if( $wgUser->isAllowed('protect') && $row->log_action
!= 'unprotect' ) {
245 $revert .= ' (' . $this->skin
->makeKnownLinkObj( $title, $this->message
['protect_change'],
246 'action=unprotect' ) . ')';
249 } else if ( self
::typeAction($row,'merge','merge') ) {
250 $merge = SpecialPage
::getTitleFor( 'Mergehistory' );
251 $revert = '(' . $this->skin
->makeKnownLinkObj( $merge, $this->message
['revertmerge'],
252 wfArrayToCGI( array('target' => $paramArray[0], 'dest' => $title->getPrefixedDBkey(),
253 'mergepoint' => $paramArray[1] ) ) ) . ')';
254 // If an edit was hidden from a page give a review link to the history
255 } else if( self
::typeAction($row,array('delete','suppress'),'revision') && $wgUser->isAllowed( 'deleterevision' ) ) {
256 if( count($paramArray) == 2 ) {
257 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
258 // Different revision types use different URL params...
259 $key = $paramArray[0];
260 // Link to each hidden object ID, $paramArray[1] is the url param
261 $Ids = explode( ',', $paramArray[1] );
263 foreach( $Ids as $n => $id ) {
264 $revParams .= '&' . urlencode($key) . '[]=' . urlencode($id);
266 $revert = '(' . $this->skin
->makeKnownLinkObj( $revdel, $this->message
['revdel-restore'],
267 'target=' . $title->getPrefixedUrl() . $revParams ) . ')';
269 // Hidden log items, give review link
270 } else if( self
::typeAction($row,array('delete','suppress'),'event') && $wgUser->isAllowed( 'deleterevision' ) ) {
271 if( count($paramArray) == 1 ) {
272 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
273 $Ids = explode( ',', $paramArray[0] );
274 // Link to each hidden object ID, $paramArray[1] is the url param
276 foreach( $Ids as $n => $id ) {
277 $logParams .= '&logid[]=' . intval($id);
279 $revert = '(' . $this->skin
->makeKnownLinkObj( $revdel, $this->message
['revdel-restore'],
280 'target=' . $title->getPrefixedUrl() . $logParams ) . ')';
283 wfRunHooks( 'LogLine', array( $row->log_type
, $row->log_action
, $title, $paramArray,
284 &$comment, &$revert, $row->log_timestamp
) );
285 // Do nothing. The implementation is handled by the hook modifiying the passed-by-ref parameters.
288 if( self
::isDeleted($row,LogPage
::DELETED_ACTION
) ) {
289 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
291 $action = LogPage
::actionText( $row->log_type
, $row->log_action
, $title, $this->skin
, $paramArray, true );
294 return "<li>$del$time $userLink $action $comment $revert</li>\n";
301 private function showhideLinks( $row ) {
302 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
303 // If event was hidden from sysops
304 if( !self
::userCan( $row, LogPage
::DELETED_RESTRICTED
) ) {
305 $del = $this->message
['rev-delundel'];
306 } else if( $row->log_type
== 'suppress' ) {
307 // No one should be hiding from the oversight log
308 $del = $this->message
['rev-delundel'];
310 $target = SpecialPage
::getTitleFor( 'Log', $row->log_type
);
311 $del = $this->skin
->makeKnownLinkObj( $revdel, $this->message
['rev-delundel'],
312 'target=' . $target->getPrefixedUrl() . '&logid='.$row->log_id
);
313 // Bolden oversighted content
314 if( self
::isDeleted( $row, LogPage
::DELETED_RESTRICTED
) )
315 $del = "<strong>$del</strong>";
317 return "<tt>(<small>$del</small>)</tt>";
322 * @param mixed $type (string/array)
323 * @param mixed $action (string/array)
326 public static function typeAction( $row, $type, $action ) {
327 $match = is_array($type) ?
in_array($row->log_type
,$type) : $row->log_type
== $type;
329 $match = is_array($action) ?
in_array($row->log_action
,$action) : $row->log_action
== $action;
335 * Determine if the current user is allowed to view a particular
336 * field of this log row, if it's marked as deleted.
341 public static function userCan( $row, $field ) {
342 if( ( $row->log_deleted
& $field ) == $field ) {
344 $permission = ( $row->log_deleted
& LogPage
::DELETED_RESTRICTED
) == LogPage
::DELETED_RESTRICTED
347 wfDebug( "Checking for $permission due to $field match on $row->log_deleted\n" );
348 return $wgUser->isAllowed( $permission );
356 * @param int $field one of DELETED_* bitfield constants
359 public static function isDeleted( $row, $field ) {
360 return ($row->log_deleted
& $field) == $field;
364 * Quick function to show a short log extract
365 * @param OutputPage $out
366 * @param string $type
367 * @param string $page
368 * @param string $user
370 public static function showLogExtract( $out, $type='', $page='', $user='', $limit = NULL ) {
372 # Insert list of top 50 or so items
373 $loglist = new LogEventsList( $wgUser->getSkin(), $out, 0 );
374 $pager = new LogPager( $loglist, $type, $user, $page, '' );
375 if( $limit ) $pager->mLimit
= $limit;
376 $logBody = $pager->getBody();
379 $loglist->beginLogEventsList() .
381 $loglist->endLogEventsList()
384 $out->addWikiMsg( 'logempty' );
389 * SQL clause to skip forbidden log types for this user
390 * @param Database $db
391 * @returns mixed (string or false)
393 public static function getExcludeClause( $db ) {
394 global $wgLogRestrictions, $wgUser;
395 // Reset the array, clears extra "where" clauses when $par is used
396 $hiddenLogs = array();
397 // Don't show private logs to unprivileged users
398 foreach( $wgLogRestrictions as $logtype => $right ) {
399 if( !$wgUser->isAllowed($right) ) {
400 $safetype = $db->strencode( $logtype );
401 $hiddenLogs[] = $safetype;
404 if( count($hiddenLogs) == 1 ) {
405 return 'log_type != ' . $db->addQuotes( $hiddenLogs[0] );
406 } elseif( !empty( $hiddenLogs ) ) {
407 return 'log_type NOT IN (' . $db->makeList($hiddenLogs) . ')';
416 class LogPager
extends ReverseChronologicalPager
{
417 private $type = '', $user = '', $title = '', $pattern = '';
418 public $mLogEventsList;
421 * @param LogEventsList $loglist,
422 * @param string $type,
423 * @param string $user,
424 * @param string $page,
425 * @param string $pattern
426 * @param array $conds
428 function __construct( $list, $type='', $user='', $title='', $pattern='', $conds=array(), $y=false, $m=false ) {
429 parent
::__construct();
430 $this->mConds
= $conds;
432 $this->mLogEventsList
= $list;
434 $this->limitType( $type );
435 $this->limitUser( $user );
436 $this->limitTitle( $title, $pattern );
437 $this->getDateCond( $y, $m );
440 function getDefaultQuery() {
441 $query = parent
::getDefaultQuery();
442 $query['type'] = $this->type
;
443 $query['month'] = $this->mMonth
;
444 $query['year'] = $this->mYear
;
449 * Set the log reader to return only entries of the given type.
450 * Type restrictions enforced here
451 * @param string $type A log type ('upload', 'delete', etc)
454 private function limitType( $type ) {
455 global $wgLogRestrictions, $wgUser;
456 // Don't even show header for private logs; don't recognize it...
457 if( isset($wgLogRestrictions[$type]) && !$wgUser->isAllowed($wgLogRestrictions[$type]) ) {
460 // Don't show private logs to unpriviledged users
461 $hideLogs = LogEventsList
::getExcludeClause( $this->mDb
);
462 if( $hideLogs !== false ) {
463 $this->mConds
[] = $hideLogs;
469 $this->mConds
['log_type'] = $type;
473 * Set the log reader to return only entries by the given user.
474 * @param string $name (In)valid user name
477 function limitUser( $name ) {
481 $usertitle = Title
::makeTitleSafe( NS_USER
, $name );
482 if( is_null($usertitle) ) {
485 /* Fetch userid at first, if known, provides awesome query plan afterwards */
486 $userid = User
::idFromName( $name );
488 /* It should be nicer to abort query at all,
489 but for now it won't pass anywhere behind the optimizer */
490 $this->mConds
[] = "NULL";
492 $this->mConds
['log_user'] = $userid;
493 $this->user
= $usertitle->getText();
498 * Set the log reader to return only entries affecting the given page.
499 * (For the block and rights logs, this is a user page.)
500 * @param string $page Title name as text
503 function limitTitle( $page, $pattern ) {
506 $title = Title
::newFromText( $page );
507 if( strlen($page) == 0 ||
!$title instanceof Title
)
510 $this->title
= $title->getPrefixedText();
511 $ns = $title->getNamespace();
512 # Using the (log_namespace, log_title, log_timestamp) index with a
513 # range scan (LIKE) on the first two parts, instead of simple equality,
514 # makes it unusable for sorting. Sorted retrieval using another index
515 # would be possible, but then we might have to scan arbitrarily many
516 # nodes of that index. Therefore, we need to avoid this if $wgMiserMode
519 # This is not a problem with simple title matches, because then we can
520 # use the page_time index. That should have no more than a few hundred
521 # log entries for even the busiest pages, so it can be safely scanned
522 # in full to satisfy an impossible condition on user or similar.
523 if( $pattern && !$wgMiserMode ) {
524 # use escapeLike to avoid expensive search patterns like 't%st%'
525 $safetitle = $this->mDb
->escapeLike( $title->getDBkey() );
526 $this->mConds
['log_namespace'] = $ns;
527 $this->mConds
[] = "log_title LIKE '$safetitle%'";
528 $this->pattern
= $pattern;
530 $this->mConds
['log_namespace'] = $ns;
531 $this->mConds
['log_title'] = $title->getDBkey();
535 function getQueryInfo() {
536 $this->mConds
[] = 'user_id = log_user';
537 # Don't use the wrong logging index
538 if( $this->title ||
$this->pattern ||
$this->user
) {
539 $index = array( 'USE INDEX' => array( 'logging' => array('page_time','user_time') ) );
540 } else if( $this->type
) {
541 $index = array( 'USE INDEX' => array( 'logging' => 'type_time' ) );
543 $index = array( 'USE INDEX' => array( 'logging' => 'times' ) );
546 'tables' => array( 'logging', 'user' ),
547 'fields' => array( 'log_type', 'log_action', 'log_user', 'log_namespace', 'log_title', 'log_params',
548 'log_comment', 'log_id', 'log_deleted', 'log_timestamp', 'user_name', 'user_editcount' ),
549 'conds' => $this->mConds
,
554 function getIndexField() {
555 return 'log_timestamp';
558 function getStartBody() {
559 wfProfileIn( __METHOD__
);
560 # Do a link batch query
561 if( $this->getNumRows() > 0 ) {
563 while( $row = $this->mResult
->fetchObject() ) {
564 $lb->add( $row->log_namespace
, $row->log_title
);
565 $lb->addObj( Title
::makeTitleSafe( NS_USER
, $row->user_name
) );
566 $lb->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->user_name
) );
569 $this->mResult
->seek( 0 );
571 wfProfileOut( __METHOD__
);
575 function formatRow( $row ) {
576 return $this->mLogEventsList
->logLine( $row );
579 public function getType() {
583 public function getUser() {
587 public function getPage() {
591 public function getPattern() {
592 return $this->pattern
;
595 public function getYear() {
599 public function getMonth() {
600 return $this->mMonth
;
606 * @ingroup SpecialPage
611 * @param WebRequest $request For internal use use a FauxRequest object to pass arbitrary parameters.
613 function __construct( $request ) {
614 global $wgUser, $wgOut;
616 $type = $request->getVal( 'type' );
617 $user = $request->getText( 'user' );
618 $title = $request->getText( 'page' );
619 $pattern = $request->getBool( 'pattern' );
620 $y = $request->getIntOrNull( 'year' );
621 $m = $request->getIntOrNull( 'month' );
622 # Don't let the user get stuck with a certain date
623 $skip = $request->getText( 'offset' ) ||
$request->getText( 'dir' ) == 'prev';
628 # Use new list class to output results
629 $loglist = new LogEventsList( $wgUser->getSkin(), $wgOut, 0 );
630 $this->pager
= new LogPager( $loglist, $type, $user, $title, $pattern, $y, $m );
634 * Is there at least one row?
637 public function hasRows() {
638 return isset($this->pager
) ?
($this->pager
->getNumRows() > 0) : false;
644 * @ingroup SpecialPage
647 const NO_ACTION_LINK
= 1;
649 * @var LogReader $reader
653 * @param LogReader &$reader where to get our data from
654 * @param integer $flags Bitwise combination of flags:
655 * LogEventsList::NO_ACTION_LINK Don't show restore/unblock/block links
657 function __construct( &$reader, $flags = 0 ) {
659 $this->reader
=& $reader;
660 $this->reader
->pager
->mLogEventsList
->flags
= $flags;
661 # Aliases for shorter code...
662 $this->pager
=& $this->reader
->pager
;
663 $this->list =& $this->reader
->pager
->mLogEventsList
;
667 * Take over the whole output page in $wgOut with the log display.
669 public function show() {
670 # Set title and add header
671 $this->list->showHeader( $pager->getType() );
673 $this->list->showOptions( $this->pager
->getType(), $this->pager
->getUser(), $this->pager
->getPage(),
674 $this->pager
->getPattern(), $this->pager
->getYear(), $this->pager
->getMonth() );
676 $logBody = $this->pager
->getBody();
679 $this->pager
->getNavigationBar() .
680 $this->list->beginLogEventsList() .
682 $this->list->endLogEventsList() .
683 $this->pager
->getNavigationBar()
686 $wgOut->addWikiMsg( 'logempty' );
691 * Output just the list of entries given by the linked LogReader,
692 * with extraneous UI elements. Use for displaying log fragments in
693 * another page (eg at Special:Undelete)
694 * @param OutputPage $out where to send output
696 public function showList( &$out ) {
697 $logBody = $this->pager
->getBody();
700 $this->list->beginLogEventsList() .
702 $this->list->endLogEventsList()
705 $out->addWikiMsg( 'logempty' );