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;
25 function __construct( &$skin, $flags = 0 ) {
27 $this->flags
= $flags;
28 $this->preCacheMessages();
32 * As we use the same small set of messages in various methods and that
33 * they are called often, we call them once and save them in $this->message
35 private function preCacheMessages() {
36 // Precache various messages
37 if( !isset( $this->message
) ) {
38 $messages = 'revertmerge protect_change unblocklink revertmove undeletelink revdel-restore rev-delundel';
39 foreach( explode(' ', $messages ) as $msg ) {
40 $this->message
[$msg] = wfMsgExt( $msg, array( 'escape') );
46 * Set page title and show header for this log type
47 * @param OutputPage $out where to send output
50 public function showHeader( $out, $type ) {
51 if( LogPage
::isLogType( $type ) ) {
52 $out->setPageTitle( LogPage
::logName( $type ) );
53 $out->addWikiText( LogPage
::logHeader( $type ) );
58 * Show options for the log list
59 * @param OutputPage $out where to send output
60 * @param string $type,
61 * @param string $user,
62 * @param string $page,
63 * @param string $pattern
65 public function showOptions( $out, $type, $user, $page, $pattern ) {
66 global $wgScript, $wgMiserMode;
67 $action = htmlspecialchars( $wgScript );
68 $title = SpecialPage
::getTitleFor( 'Log' );
69 $special = htmlspecialchars( $title->getPrefixedDBkey() );
70 $out->addHTML( "<form action=\"$action\" method=\"get\"><fieldset>" .
71 Xml
::element( 'legend', array(), wfMsg( 'log' ) ) .
72 Xml
::hidden( 'title', $special ) . "\n" .
73 $this->getTypeMenu( $type ) . "\n" .
74 $this->getUserInput( $user ) . "\n" .
75 $this->getTitleInput( $page ) . "\n" .
76 ( !$wgMiserMode ?
($this->getTitlePattern( $pattern )."\n") : "" ) .
77 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
78 "</fieldset></form>" );
82 * @return string Formatted HTML
84 private function getTypeMenu( $queryType ) {
85 global $wgLogRestrictions, $wgUser;
87 $out = "<select name='type'>\n";
89 $validTypes = LogPage
::validTypes();
90 $m = array(); // Temporary array
92 // First pass to load the log names
93 foreach( $validTypes as $type ) {
94 $text = LogPage
::logName( $type );
98 // Second pass to sort by name
101 // Third pass generates sorted XHTML content
102 foreach( $m as $text => $type ) {
103 $selected = ($type == $queryType);
105 if ( isset($wgLogRestrictions[$type]) ) {
106 if ( $wgUser->isAllowed( $wgLogRestrictions[$type] ) ) {
107 $out .= Xml
::option( $text, $type, $selected ) . "\n";
110 $out .= Xml
::option( $text, $type, $selected ) . "\n";
119 * @return string Formatted HTML
121 private function getUserInput( $user ) {
122 return Xml
::inputLabel( wfMsg( 'specialloguserlabel' ), 'user', 'user', 12, $user );
126 * @return string Formatted HTML
128 private function getTitleInput( $title ) {
129 return Xml
::inputLabel( wfMsg( 'speciallogtitlelabel' ), 'page', 'page', 20, $title );
133 * @return boolean Checkbox
135 private function getTitlePattern( $pattern ) {
136 return Xml
::checkLabel( wfMsg( 'log-title-wildcard' ), 'pattern', 'pattern', $pattern );
139 public function beginLogEventsList() {
143 public function endLogEventsList() {
148 * @param Row $row a single row from the result set
149 * @return string Formatted HTML list item
152 public function logLine( $row ) {
153 global $wgLang, $wgUser, $wgContLang;
154 $skin = $wgUser->getSkin();
155 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
156 $time = $wgLang->timeanddate( wfTimestamp(TS_MW
, $row->log_timestamp
), true );
157 // Enter the existence or non-existence of this page into the link cache,
158 // for faster makeLinkObj() in LogPage::actionText()
159 $linkCache =& LinkCache
::singleton();
160 $linkCache->addLinkObj( $title );
162 if( self
::isDeleted($row,LogPage
::DELETED_USER
) ) {
163 $userLink = '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
165 $userLink = $this->skin
->userLink( $row->log_user
, $row->user_name
) .
166 $this->skin
->userToolLinksRedContribs( $row->log_user
, $row->user_name
);
169 if( self
::isDeleted($row,LogPage
::DELETED_COMMENT
) ) {
170 $comment = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-comment') . '</span>';
172 $comment = $wgContLang->getDirMark() . $this->skin
->commentBlock( $row->log_comment
);
174 // Extract extra parameters
175 $paramArray = LogPage
::extractParams( $row->log_params
);
177 // Some user can hide log items and have review links
178 if( $wgUser->isAllowed( 'deleterevision' ) ) {
179 $del = $this->showhideLinks( $row ) . ' ';
181 // Add review links and such...
182 if( !($this->flags
& self
::NO_ACTION_LINK
) && !($row->log_deleted
& LogPage
::DELETED_ACTION
) ) {
183 if( $row->log_type
== 'move' && isset( $paramArray[0] ) && $wgUser->isAllowed( 'move' ) ) {
184 $destTitle = Title
::newFromText( $paramArray[0] );
186 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Movepage' ),
187 $this->message
['revertmove'],
188 'wpOldTitle=' . urlencode( $destTitle->getPrefixedDBkey() ) .
189 '&wpNewTitle=' . urlencode( $title->getPrefixedDBkey() ) .
190 '&wpReason=' . urlencode( wfMsgForContent( 'revertmove' ) ) .
191 '&wpMovetalk=0' ) . ')';
193 // Show undelete link
194 } else if( $row->log_action
== 'delete' && $wgUser->isAllowed( 'delete' ) ) {
195 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Undelete' ),
196 $this->message
['undeletelink'], 'target='. urlencode( $title->getPrefixedDBkey() ) ) . ')';
198 } else if( $row->log_action
== 'block' && $wgUser->isAllowed( 'block' ) ) {
199 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Ipblocklist' ),
200 $this->message
['unblocklink'],
201 'action=unblock&ip=' . urlencode( $row->log_title
) ) . ')';
202 // Show change protection link
203 } else if( ( $row->log_action
== 'protect' ||
$row->log_action
== 'modify' ) && $wgUser->isAllowed( 'protect' ) ) {
204 $revert = '(' . $this->skin
->makeKnownLinkObj( $title, $this->message
['protect_change'], 'action=unprotect' ) . ')';
206 } else if ( $row->log_action
== 'merge' ) {
207 $merge = SpecialPage
::getTitleFor( 'Mergehistory' );
208 $revert = '(' . $this->skin
->makeKnownLinkObj( $merge, $this->message
['revertmerge'],
210 array('target' => $paramArray[0], 'dest' => $title->getPrefixedText(), 'mergepoint' => $paramArray[1] )
213 // If an edit was hidden from a page give a review link to the history
214 } else if( $row->log_action
== 'revision' && $wgUser->isAllowed( 'deleterevision' ) ) {
215 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
216 // Different revision types use different URL params...
217 $subtype = isset($paramArray[2]) ?
$paramArray[1] : '';
218 // Link to each hidden object ID, $paramArray[1] is the url param. List if several...
219 $Ids = explode( ',', $paramArray[2] );
220 if( count($Ids) == 1 ) {
221 $revert = $this->skin
->makeKnownLinkObj( $revdel, $this->message
['revdel-restore'],
222 wfArrayToCGI( array('target' => $paramArray[0], $paramArray[1] => $Ids[0] ) ) );
224 $revert .= $this->message
['revdel-restore'].':';
225 foreach( $Ids as $n => $id ) {
226 $revert .= ' '.$this->skin
->makeKnownLinkObj( $revdel, '#'.($n+
1),
227 wfArrayToCGI( array('target' => $paramArray[0], $paramArray[1] => $id ) ) );
230 $revert = "($revert)";
231 // Hidden log items, give review link
232 } else if( $row->log_action
== 'event' && $wgUser->isAllowed( 'deleterevision' ) ) {
233 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
234 $revert .= $this->message
['revdel-restore'];
235 $Ids = explode( ',', $paramArray[0] );
236 // Link to each hidden object ID, $paramArray[1] is the url param. List if several...
237 if( count($Ids) == 1 ) {
238 $revert = $this->skin
->makeKnownLinkObj( $revdel, $this->message
['revdel-restore'],
239 wfArrayToCGI( array('logid' => $Ids[0] ) ) );
241 foreach( $Ids as $n => $id ) {
242 $revert .= $this->skin
->makeKnownLinkObj( $revdel, '#'.($n+
1),
243 wfArrayToCGI( array('logid' => $id ) ) );
246 $revert = "($revert)";
248 wfRunHooks( 'LogLine', array( $row->log_type
, $row->log_action
, $title, $paramArray,
249 &$comment, &$revert, $row->log_timestamp
) );
250 // wfDebug( "Invoked LogLine hook for " $row->log_type . ", " . $row->log_action . "\n" );
251 // Do nothing. The implementation is handled by the hook modifiying the passed-by-ref parameters.
255 if( $row->log_deleted
& LogPage
::DELETED_ACTION
) {
256 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
258 $action = LogPage
::actionText( $row->log_type
, $row->log_action
, $title, $this->skin
, $paramArray, true );
261 return "<li>$del$time $userLink $action $comment $revert</li>\n";
268 private function showhideLinks( $row ) {
269 global $wgAllowLogDeletion;
271 if( !$wgAllowLogDeletion )
274 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
275 // If event was hidden from sysops
276 if( !self
::userCan( $row, LogPage
::DELETED_RESTRICTED
) ) {
277 $del = $this->message
['rev-delundel'];
278 } else if( $row->log_type
== 'suppress' ) {
279 // No one should be hiding from the oversight log
280 $del = $this->message
['rev-delundel'];
282 $del = $this->skin
->makeKnownLinkObj( $revdel, $this->message
['rev-delundel'], 'logid='.$row->log_id
);
283 // Bolden oversighted content
284 if( self
::isDeleted( $row, LogPage
::DELETED_RESTRICTED
) )
285 $del = "<strong>$del</strong>";
287 return "<tt>(<small>$del</small>)</tt>";
291 * SQL clause to skip forbidden log types for this user
292 * @param Database $db
293 * @returns mixed (string or false)
295 public static function getExcludeClause( $db ) {
296 global $wgLogRestrictions, $wgUser;
297 // Reset the array, clears extra "where" clauses when $par is used
298 $hiddenLogs = array();
299 // Don't show private logs to unpriviledged users
300 foreach( $wgLogRestrictions as $logtype => $right ) {
301 if( !$wgUser->isAllowed($right) ) {
302 $safetype = $db->strencode( $logtype );
303 $hiddenLogs[] = "'$safetype'";
306 if( !empty($hiddenLogs) ) {
307 return 'log_type NOT IN(' . implode(',',$hiddenLogs) . ')';
313 * Determine if the current user is allowed to view a particular
314 * field of this log row, if it's marked as deleted.
319 public static function userCan( $row, $field ) {
320 if( ( $row->log_deleted
& $field ) == $field ) {
322 $permission = ( $row->log_deleted
& LogPage
::DELETED_RESTRICTED
) == LogPage
::DELETED_RESTRICTED
325 wfDebug( "Checking for $permission due to $field match on $row->log_deleted\n" );
326 return $wgUser->isAllowed( $permission );
334 * @param int $field one of DELETED_* bitfield constants
337 public static function isDeleted( $row, $field ) {
338 return ($row->log_deleted
& $field) == $field;
345 class LogPager
extends ReverseChronologicalPager
{
346 private $type = '', $user = '', $title = '', $pattern = '';
347 public $mLogEventsList;
350 * @param LogEventsList $loglist,
351 * @param string $type,
352 * @param string $user,
353 * @param string $page,
354 * @param string $pattern
355 * @param array $conds
357 function __construct( $loglist, $type, $user, $title, $pattern, $conds = array() ) {
358 parent
::__construct();
359 $this->mConds
= $conds;
361 $this->mLogEventsList
= $loglist;
363 $this->limitType( $type );
364 $this->limitUser( $user );
365 $this->limitTitle( $title, $pattern );
369 * Set the log reader to return only entries of the given type.
370 * Type restrictions enforced here
371 * @param string $type A log type ('upload', 'delete', etc)
374 private function limitType( $type ) {
375 global $wgLogRestrictions, $wgUser;
376 // Don't even show header for private logs; don't recognize it...
377 if( isset($wgLogRestrictions[$type]) && !$wgUser->isAllowed($wgLogRestrictions[$type]) ) {
380 // Don't show private logs to unpriviledged users
381 $hideLogs = LogEventsList
::getExcludeClause( $this->mDb
);
382 if( $hideLogs !== false ) {
383 $this->mConds
[] = $hideLogs;
389 $this->mConds
['log_type'] = $type;
393 * Set the log reader to return only entries by the given user.
394 * @param string $name (In)valid user name
397 function limitUser( $name ) {
401 $usertitle = Title
::makeTitleSafe( NS_USER
, $name );
402 if( is_null($usertitle) ) {
405 $this->user
= $usertitle->getText();
406 /* Fetch userid at first, if known, provides awesome query plan afterwards */
407 $userid = User
::idFromName( $this->user
);
409 /* It should be nicer to abort query at all,
410 but for now it won't pass anywhere behind the optimizer */
411 $this->mConds
[] = "NULL";
413 $this->mConds
['log_user'] = $userid;
418 * Set the log reader to return only entries affecting the given page.
419 * (For the block and rights logs, this is a user page.)
420 * @param string $page Title name as text
423 function limitTitle( $page, $pattern ) {
426 $title = Title
::newFromText( $page );
427 if( strlen($page) == 0 ||
!$title instanceof Title
)
430 $this->title
= $title->getPrefixedText();
431 $this->pattern
= $pattern;
432 $ns = $title->getNamespace();
433 if( $pattern && !$wgMiserMode ) {
434 # use escapeLike to avoid expensive search patterns like 't%st%'
435 $safetitle = $this->mDb
->escapeLike( $title->getDBkey() );
436 $this->mConds
['log_namespace'] = $ns;
437 $this->mConds
[] = "log_title LIKE '$safetitle%'";
439 $this->mConds
['log_namespace'] = $ns;
440 $this->mConds
['log_title'] = $title->getDBkey();
444 function getQueryInfo() {
445 $this->mConds
[] = 'user_id = log_user';
446 # Hack this until live
447 global $wgAllowLogDeletion;
448 $log_id = $wgAllowLogDeletion ?
'log_id' : '0 AS log_id';
450 'tables' => array( 'logging', 'user' ),
451 'fields' => array( 'log_type', 'log_action', 'log_user', 'log_namespace', 'log_title',
452 'log_params', 'log_comment', $log_id, 'log_deleted', 'log_timestamp', 'user_name' ),
453 'conds' => $this->mConds
,
458 function getIndexField() {
459 return 'log_timestamp';
462 function formatRow( $row ) {
463 return $this->mLogEventsList
->logLine( $row );
466 public function getType() {
470 public function getUser() {
474 public function getPage() {
478 public function getPattern() {
479 return $this->pattern
;
485 * @addtogroup SpecialPage
488 var $db, $joinClauses, $whereClauses;
489 var $type = '', $user = '', $title = null, $pattern = false;
491 * @param WebRequest $request For internal use use a FauxRequest object to pass arbitrary parameters.
493 function __construct( $request ) {
496 $type = $request->getVal( 'type' );
497 $user = $request->getText( 'user' );
498 $title = $request->getText( 'page' );
499 $pattern = $request->getBool( 'pattern' );
501 $loglist = new LogEventsList( $wgUser->getSkin() );
502 $this->pager
= new LogPager( $loglist, $type, $user, $title, $pattern );
506 * Is there at least one row?
509 public function hasRows() {
510 return isset($this->pager
) ?
($this->pager
->getNumRows() > 0) : false;
516 * @addtogroup SpecialPage
519 const NO_ACTION_LINK
= 1;
521 * @var LogReader $reader
528 * @param LogReader &$reader where to get our data from
529 * @param integer $flags Bitwise combination of flags:
530 * LogEventsList::NO_ACTION_LINK Don't show restore/unblock/block links
532 function __construct( &$reader, $flags = 0 ) {
534 $this->skin
= $wgUser->getSkin();
535 $this->reader
=& $reader;
536 $this->reader
->pager
->mLogEventsList
->flags
= $flags;
537 # Aliases for shorter code...
538 $this->pager
=& $this->reader
->pager
;
539 $this->logEventsList
=& $this->reader
->pager
->mLogEventsList
;
543 * Take over the whole output page in $wgOut with the log display.
545 public function show() {
547 # Set title and add header
548 $this->logEventsList
->showHeader( $wgOut, $pager->getType() );
550 $this->logEventsList
->showOptions( $wgOut, $this->pager
->getType(), $this->pager
->getUser(),
551 $this->pager
->getPage(), $this->pager
->getPattern() );
553 $logBody = $this->pager
->getBody();
556 $this->pager
->getNavigationBar() .
557 $this->logEventsList
->beginLogEventsList() .
559 $this->logEventsList
->endLogEventsList() .
560 $this->pager
->getNavigationBar()
563 $wgOut->addWikiMsg( 'logempty' );
568 * Output just the list of entries given by the linked LogReader,
569 * with extraneous UI elements. Use for displaying log fragments in
570 * another page (eg at Special:Undelete)
571 * @param OutputPage $out where to send output
573 public function showList( &$out ) {
574 $logBody = $this->pager
->getBody();
577 $this->logEventsList
->beginLogEventsList() .
579 $this->logEventsList
->endLogEventsList()
582 $out->addWikiMsg( 'logempty' );