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;
26 function __construct( &$skin, $flags = 0 ) {
28 $this->flags
= $flags;
29 $this->preCacheMessages();
33 * As we use the same small set of messages in various methods and that
34 * they are called often, we call them once and save them in $this->message
36 private function preCacheMessages() {
37 // Precache various messages
38 if( !isset( $this->message
) ) {
39 $messages = 'revertmerge protect_change unblocklink revertmove undeletelink revdel-restore rev-delundel';
40 foreach( explode(' ', $messages ) as $msg ) {
41 $this->message
[$msg] = wfMsgExt( $msg, array( 'escape') );
47 * Set page title and show header for this log type
48 * @param OutputPage $out where to send output
51 public function showHeader( $out, $type ) {
52 if( LogPage
::isLogType( $type ) ) {
53 $out->setPageTitle( LogPage
::logName( $type ) );
54 $out->addWikiText( LogPage
::logHeader( $type ) );
59 * Show options for the log list
60 * @param OutputPage $out where to send output
61 * @param string $type,
62 * @param string $user,
63 * @param string $page,
64 * @param string $pattern
66 public function showOptions( $out, $type, $user, $page, $pattern ) {
67 global $wgScript, $wgMiserMode;
68 $action = htmlspecialchars( $wgScript );
69 $title = SpecialPage
::getTitleFor( 'Log' );
70 $special = htmlspecialchars( $title->getPrefixedDBkey() );
71 $out->addHTML( "<form action=\"$action\" method=\"get\"><fieldset>" .
72 Xml
::element( 'legend', array(), wfMsg( 'log' ) ) .
73 Xml
::hidden( 'title', $special ) . "\n" .
74 $this->getTypeMenu( $type ) . "\n" .
75 $this->getUserInput( $user ) . "\n" .
76 $this->getTitleInput( $page ) . "\n" .
77 ( !$wgMiserMode ?
($this->getTitlePattern( $pattern )."\n") : "" ) .
78 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
79 "</fieldset></form>" );
83 * @return string Formatted HTML
85 private function getTypeMenu( $queryType ) {
86 global $wgLogRestrictions, $wgUser;
88 $out = "<select name='type'>\n";
90 $validTypes = LogPage
::validTypes();
91 $m = array(); // Temporary array
93 // First pass to load the log names
94 foreach( $validTypes as $type ) {
95 $text = LogPage
::logName( $type );
99 // Second pass to sort by name
102 // Third pass generates sorted XHTML content
103 foreach( $m as $text => $type ) {
104 $selected = ($type == $queryType);
106 if ( isset($wgLogRestrictions[$type]) ) {
107 if ( $wgUser->isAllowed( $wgLogRestrictions[$type] ) ) {
108 $out .= Xml
::option( $text, $type, $selected ) . "\n";
111 $out .= Xml
::option( $text, $type, $selected ) . "\n";
120 * @return string Formatted HTML
122 private function getUserInput( $user ) {
123 return Xml
::inputLabel( wfMsg( 'specialloguserlabel' ), 'user', 'user', 12, $user );
127 * @return string Formatted HTML
129 private function getTitleInput( $title ) {
130 return Xml
::inputLabel( wfMsg( 'speciallogtitlelabel' ), 'page', 'page', 20, $title );
134 * @return boolean Checkbox
136 private function getTitlePattern( $pattern ) {
137 return Xml
::checkLabel( wfMsg( 'log-title-wildcard' ), 'pattern', 'pattern', $pattern );
141 protected function getWrapperElement() {
142 global $wgDateGroupedLogs;
143 return $wgDateGroupedLogs ?
'div' : 'ul';
146 public function beginLogEventsList() {
147 return Xml
::openElement( $this->getWrapperElement() ) . "\n";
150 public function endLogEventsList() {
151 return Xml
::closeElement( $this->getWrapperElement() ) . "\n";
155 * @param Row $row a single row from the result set
156 * @return string Formatted HTML list item
159 public function logLine( $row ) {
160 global $wgLang, $wgUser, $wgContLang;
162 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
163 $time = $wgLang->timeanddate( wfTimestamp(TS_MW
, $row->log_timestamp
), true );
165 if( self
::isDeleted($row,LogPage
::DELETED_USER
) ) {
166 $userLink = '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
168 $userLink = $this->skin
->userLink( $row->log_user
, $row->user_name
);
169 $this->skin
->userToolLinks( $row->log_user
, $row->user_name
);
172 if( self
::isDeleted($row,LogPage
::DELETED_COMMENT
) ) {
173 $comment = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-comment') . '</span>';
175 $comment = $wgContLang->getDirMark() . $this->skin
->commentBlock( $row->log_comment
);
177 // Extract extra parameters
178 $paramArray = LogPage
::extractParams( $row->log_params
);
180 // Some user can hide log items and have review links
181 if( $wgUser->isAllowed( 'deleterevision' ) ) {
182 $del = $this->showhideLinks( $row ) . ' ';
184 // Add review links and such...
185 if( !($this->flags
& self
::NO_ACTION_LINK
) && !($row->log_deleted
& LogPage
::DELETED_ACTION
) ) {
186 if( $row->log_type
== 'move' && isset( $paramArray[0] ) && $wgUser->isAllowed( 'move' ) ) {
187 $destTitle = Title
::newFromText( $paramArray[0] );
189 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Movepage' ),
190 $this->message
['revertmove'],
191 'wpOldTitle=' . urlencode( $destTitle->getPrefixedDBkey() ) .
192 '&wpNewTitle=' . urlencode( $title->getPrefixedDBkey() ) .
193 '&wpReason=' . urlencode( wfMsgForContent( 'revertmove' ) ) .
194 '&wpMovetalk=0' ) . ')';
196 // Show undelete link
197 } else if( $row->log_action
== 'delete' && $wgUser->isAllowed( 'delete' ) ) {
198 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Undelete' ),
199 $this->message
['undeletelink'], 'target='. urlencode( $title->getPrefixedDBkey() ) ) . ')';
201 } else if( $row->log_action
== 'block' && $wgUser->isAllowed( 'block' ) ) {
202 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Ipblocklist' ),
203 $this->message
['unblocklink'],
204 'action=unblock&ip=' . urlencode( $row->log_title
) ) . ')';
205 // Show change protection link
206 } else if( ( $row->log_action
== 'protect' ||
$row->log_action
== 'modify' ) && $wgUser->isAllowed( 'protect' ) ) {
207 $revert = '(' . $this->skin
->makeKnownLinkObj( $title, $this->message
['protect_change'], 'action=unprotect' ) . ')';
209 } else if ( $row->log_action
== 'merge' ) {
210 $merge = SpecialPage
::getTitleFor( 'Mergehistory' );
211 $revert = '(' . $this->skin
->makeKnownLinkObj( $merge, $this->message
['revertmerge'],
213 array('target' => $paramArray[0], 'dest' => $title->getPrefixedText(), 'mergepoint' => $paramArray[1] )
216 // If an edit was hidden from a page give a review link to the history
217 } else if( $row->log_action
== 'revision' && $wgUser->isAllowed( 'deleterevision' ) && isset($paramArray[2]) ) {
218 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
219 // Different revision types use different URL params...
220 $subtype = isset($paramArray[2]) ?
$paramArray[1] : '';
221 // Link to each hidden object ID, $paramArray[1] is the url param. List if several...
222 $Ids = explode( ',', $paramArray[2] );
223 if( count($Ids) == 1 ) {
224 $revert = $this->skin
->makeKnownLinkObj( $revdel, $this->message
['revdel-restore'],
225 wfArrayToCGI( array('target' => $paramArray[0], $paramArray[1] => $Ids[0] ) ) );
227 $revert .= $this->message
['revdel-restore'].':';
228 foreach( $Ids as $n => $id ) {
229 $revert .= ' '.$this->skin
->makeKnownLinkObj( $revdel, '#'.($n+
1),
230 wfArrayToCGI( array('target' => $paramArray[0], $paramArray[1] => $id ) ) );
233 $revert = "($revert)";
234 // Hidden log items, give review link
235 } else if( $row->log_action
== 'event' && $wgUser->isAllowed( 'deleterevision' ) && isset($paramArray[0]) ) {
236 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
237 $revert .= $this->message
['revdel-restore'];
238 $Ids = explode( ',', $paramArray[0] );
239 // Link to each hidden object ID, $paramArray[1] is the url param. List if several...
240 if( count($Ids) == 1 ) {
241 $revert = $this->skin
->makeKnownLinkObj( $revdel, $this->message
['revdel-restore'],
242 wfArrayToCGI( array('logid' => $Ids[0] ) ) );
244 foreach( $Ids as $n => $id ) {
245 $revert .= $this->skin
->makeKnownLinkObj( $revdel, '#'.($n+
1),
246 wfArrayToCGI( array('logid' => $id ) ) );
249 $revert = "($revert)";
251 wfRunHooks( 'LogLine', array( $row->log_type
, $row->log_action
, $title, $paramArray,
252 &$comment, &$revert, $row->log_timestamp
) );
253 // wfDebug( "Invoked LogLine hook for " $row->log_type . ", " . $row->log_action . "\n" );
254 // Do nothing. The implementation is handled by the hook modifiying the passed-by-ref parameters.
258 if( self
::isDeleted($row,LogPage
::DELETED_ACTION
) ) {
259 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
261 $action = LogPage
::actionText( $row->log_type
, $row->log_action
, $title, $this->skin
, $paramArray, true );
264 global $wgDateGroupedLogs;
265 if ( $wgDateGroupedLogs ) {
266 $time = $wgLang->time( wfTimestamp(TS_MW
, $row->log_timestamp
), true );
267 $date = $wgLang->date( wfTimestamp(TS_MW
, $row->log_timestamp
), true );
268 $line = Xml
::tags('div', array( 'class' => 'mw-log-entry' ), "$del$time $userLink $action $comment $revert" );
270 static $lastdate = false;
271 if ( $date !== $lastdate ) {
273 return Xml
::element('h4', null, $date) . "\n" . $line;
278 return Xml
::tags('li', null, "$del$time $userLink $action $comment $revert" );
287 private function showhideLinks( $row ) {
288 global $wgAllowLogDeletion;
290 if( !$wgAllowLogDeletion )
293 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
294 // If event was hidden from sysops
295 if( !self
::userCan( $row, LogPage
::DELETED_RESTRICTED
) ) {
296 $del = $this->message
['rev-delundel'];
297 } else if( $row->log_type
== 'suppress' ) {
298 // No one should be hiding from the oversight log
299 $del = $this->message
['rev-delundel'];
301 $del = $this->skin
->makeKnownLinkObj( $revdel, $this->message
['rev-delundel'], 'logid='.$row->log_id
);
302 // Bolden oversighted content
303 if( self
::isDeleted( $row, LogPage
::DELETED_RESTRICTED
) )
304 $del = "<strong>$del</strong>";
306 return "<tt>(<small>$del</small>)</tt>";
310 * Determine if the current user is allowed to view a particular
311 * field of this log row, if it's marked as deleted.
316 public static function userCan( $row, $field ) {
317 if( ( $row->log_deleted
& $field ) == $field ) {
319 $permission = ( $row->log_deleted
& LogPage
::DELETED_RESTRICTED
) == LogPage
::DELETED_RESTRICTED
322 wfDebug( "Checking for $permission due to $field match on $row->log_deleted\n" );
323 return $wgUser->isAllowed( $permission );
331 * @param int $field one of DELETED_* bitfield constants
334 public static function isDeleted( $row, $field ) {
335 return ($row->log_deleted
& $field) == $field;
339 * Quick function to show a short log extract
340 * @param OutputPage $out
341 * @param string $type
342 * @param string $page
343 * @param string $user
345 public static function showLogExtract( $out, $type='', $page='', $user='' ) {
347 # Insert list of top 50 or so items
348 $loglist = new LogEventsList( $wgUser->getSkin() );
349 $pager = new LogPager( $loglist, $type, $user, $page, '' );
350 $logBody = $pager->getBody();
353 $loglist->beginLogEventsList() .
355 $loglist->endLogEventsList()
358 $out->addWikiMsg( 'logempty' );
363 * SQL clause to skip forbidden log types for this user
364 * @param Database $db
365 * @returns mixed (string or false)
367 public static function getExcludeClause( $db ) {
368 global $wgLogRestrictions, $wgUser;
369 // Reset the array, clears extra "where" clauses when $par is used
370 $hiddenLogs = array();
371 // Don't show private logs to unprivileged users
372 foreach( $wgLogRestrictions as $logtype => $right ) {
373 if( !$wgUser->isAllowed($right) ) {
374 $safetype = $db->strencode( $logtype );
375 $hiddenLogs[] = $safetype;
378 if( count($hiddenLogs) == 1 ) {
379 return 'log_type != ' . $db->addQuotes( $hiddenLogs[0] );
380 } elseif( !empty( $hiddenLogs ) ) {
381 return 'log_type NOT IN (' . $db->makeList($hiddenLogs) . ')';
390 class LogPager
extends ReverseChronologicalPager
{
391 private $type = '', $user = '', $title = '', $pattern = '';
392 public $mLogEventsList;
395 * @param LogEventsList $loglist,
396 * @param string $type,
397 * @param string $user,
398 * @param string $page,
399 * @param string $pattern
400 * @param array $conds
402 function __construct( $loglist, $type='', $user='', $title='', $pattern='', $conds = array() ) {
403 parent
::__construct();
404 $this->mConds
= $conds;
406 $this->mLogEventsList
= $loglist;
408 $this->limitType( $type );
409 $this->limitUser( $user );
410 $this->limitTitle( $title, $pattern );
414 * Set the log reader to return only entries of the given type.
415 * Type restrictions enforced here
416 * @param string $type A log type ('upload', 'delete', etc)
419 private function limitType( $type ) {
420 global $wgLogRestrictions, $wgUser;
421 // Don't even show header for private logs; don't recognize it...
422 if( isset($wgLogRestrictions[$type]) && !$wgUser->isAllowed($wgLogRestrictions[$type]) ) {
425 // Don't show private logs to unpriviledged users
426 $hideLogs = LogEventsList
::getExcludeClause( $this->mDb
);
427 if( $hideLogs !== false ) {
428 $this->mConds
[] = $hideLogs;
434 $this->mConds
['log_type'] = $type;
438 * Set the log reader to return only entries by the given user.
439 * @param string $name (In)valid user name
442 function limitUser( $name ) {
446 $usertitle = Title
::makeTitleSafe( NS_USER
, $name );
447 if( is_null($usertitle) ) {
450 $this->user
= $usertitle->getText();
451 /* Fetch userid at first, if known, provides awesome query plan afterwards */
452 $userid = User
::idFromName( $this->user
);
454 /* It should be nicer to abort query at all,
455 but for now it won't pass anywhere behind the optimizer */
456 $this->mConds
[] = "NULL";
458 $this->mConds
['log_user'] = $userid;
463 * Set the log reader to return only entries affecting the given page.
464 * (For the block and rights logs, this is a user page.)
465 * @param string $page Title name as text
468 function limitTitle( $page, $pattern ) {
471 $title = Title
::newFromText( $page );
472 if( strlen($page) == 0 ||
!$title instanceof Title
)
475 $this->title
= $title->getPrefixedText();
476 $this->pattern
= $pattern;
477 $ns = $title->getNamespace();
478 if( $pattern && !$wgMiserMode ) {
479 # use escapeLike to avoid expensive search patterns like 't%st%'
480 $safetitle = $this->mDb
->escapeLike( $title->getDBkey() );
481 $this->mConds
['log_namespace'] = $ns;
482 $this->mConds
[] = "log_title LIKE '$safetitle%'";
484 $this->mConds
['log_namespace'] = $ns;
485 $this->mConds
['log_title'] = $title->getDBkey();
489 function getQueryInfo() {
490 $this->mConds
[] = 'user_id = log_user';
491 # Hack this until live
492 global $wgAllowLogDeletion;
493 $log_id = $wgAllowLogDeletion ?
'log_id' : '0 AS log_id';
494 # Don't use the wrong logging index
495 if( $this->title ||
$this->pattern ||
$this->user
) {
496 $index = array( 'USE INDEX' => array( 'logging' => array('page_time','user_time') ) );
497 } else if( $this->type
) {
498 $index = array( 'USE INDEX' => array( 'logging' => 'type_time' ) );
500 $index = array( 'USE INDEX' => array( 'logging' => 'times' ) );
503 'tables' => array( 'logging', 'user' ),
504 'fields' => array( 'log_type', 'log_action', 'log_user', 'log_namespace', 'log_title',
505 'log_params', 'log_comment', $log_id, 'log_deleted', 'log_timestamp', 'user_name' ),
506 'conds' => $this->mConds
,
511 function getIndexField() {
512 return 'log_timestamp';
515 function getStartBody() {
516 wfProfileIn( __METHOD__
);
517 # Do a link batch query
519 while( $row = $this->mResult
->fetchObject() ) {
520 $lb->add( $row->log_namespace
, $row->log_title
);
521 $lb->addObj( Title
::makeTitleSafe( NS_USER
, $row->user_name
) );
522 $lb->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->user_name
) );
525 $this->mResult
->seek( 0 );
526 wfProfileOut( __METHOD__
);
530 function formatRow( $row ) {
531 return $this->mLogEventsList
->logLine( $row );
534 public function getType() {
538 public function getUser() {
542 public function getPage() {
546 public function getPattern() {
547 return $this->pattern
;
553 * @addtogroup SpecialPage
558 * @param WebRequest $request For internal use use a FauxRequest object to pass arbitrary parameters.
560 function __construct( $request ) {
563 $type = $request->getVal( 'type' );
564 $user = $request->getText( 'user' );
565 $title = $request->getText( 'page' );
566 $pattern = $request->getBool( 'pattern' );
568 $loglist = new LogEventsList( $wgUser->getSkin() );
569 $this->pager
= new LogPager( $loglist, $type, $user, $title, $pattern );
573 * Is there at least one row?
576 public function hasRows() {
577 return isset($this->pager
) ?
($this->pager
->getNumRows() > 0) : false;
583 * @addtogroup SpecialPage
586 const NO_ACTION_LINK
= 1;
588 * @var LogReader $reader
592 * @param LogReader &$reader where to get our data from
593 * @param integer $flags Bitwise combination of flags:
594 * LogEventsList::NO_ACTION_LINK Don't show restore/unblock/block links
596 function __construct( &$reader, $flags = 0 ) {
598 $this->reader
=& $reader;
599 $this->reader
->pager
->mLogEventsList
->flags
= $flags;
600 # Aliases for shorter code...
601 $this->pager
=& $this->reader
->pager
;
602 $this->logEventsList
=& $this->reader
->pager
->mLogEventsList
;
606 * Take over the whole output page in $wgOut with the log display.
608 public function show() {
610 # Set title and add header
611 $this->logEventsList
->showHeader( $wgOut, $pager->getType() );
613 $this->logEventsList
->showOptions( $wgOut, $this->pager
->getType(), $this->pager
->getUser(),
614 $this->pager
->getPage(), $this->pager
->getPattern() );
616 $logBody = $this->pager
->getBody();
619 $this->pager
->getNavigationBar() .
620 $this->logEventsList
->beginLogEventsList() .
622 $this->logEventsList
->endLogEventsList() .
623 $this->pager
->getNavigationBar()
626 $wgOut->addWikiMsg( 'logempty' );
631 * Output just the list of entries given by the linked LogReader,
632 * with extraneous UI elements. Use for displaying log fragments in
633 * another page (eg at Special:Undelete)
634 * @param OutputPage $out where to send output
636 public function showList( &$out ) {
637 $logBody = $this->pager
->getBody();
640 $this->logEventsList
->beginLogEventsList() .
642 $this->logEventsList
->endLogEventsList()
645 $out->addWikiMsg( 'logempty' );