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 foreach( explode(' ', 'viewpagelogs revhistory filehist rev-delundel' ) as $msg ) {
39 $this->message
[$msg] = wfMsgExt( $msg, array( 'escape') );
45 * Set page title and show header for this log type
46 * @param OutputPage $out where to send output
49 public function showHeader( $out, $type ) {
50 if( LogPage
::isLogType( $type ) ) {
51 $out->setPageTitle( LogPage
::logName( $type ) );
52 $out->addWikiText( LogPage
::logHeader( $type ) );
57 * Show options for the log list
58 * @param OutputPage $out where to send output
59 * @param string $type,
60 * @param string $user,
61 * @param string $page,
62 * @param string $pattern
64 public function showOptions( $out, $type, $user, $page, $pattern ) {
65 global $wgScript, $wgMiserMode;
66 $action = htmlspecialchars( $wgScript );
67 $title = SpecialPage
::getTitleFor( 'Log' );
68 $special = htmlspecialchars( $title->getPrefixedDBkey() );
69 $out->addHTML( "<form action=\"$action\" method=\"get\"><fieldset>" .
70 Xml
::element( 'legend', array(), wfMsg( 'log' ) ) .
71 Xml
::hidden( 'title', $special ) . "\n" .
72 $this->getTypeMenu( $type ) . "\n" .
73 $this->getUserInput( $user ) . "\n" .
74 $this->getTitleInput( $page ) . "\n" .
75 ( !$wgMiserMode ?
($this->getTitlePattern( $pattern )."\n") : "" ) .
76 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
77 "</fieldset></form>" );
81 * @return string Formatted HTML
83 private function getTypeMenu( $queryType ) {
84 global $wgLogRestrictions, $wgUser;
86 $out = "<select name='type'>\n";
88 $validTypes = LogPage
::validTypes();
89 $m = array(); // Temporary array
91 // First pass to load the log names
92 foreach( $validTypes as $type ) {
93 $text = LogPage
::logName( $type );
97 // Second pass to sort by name
100 // Third pass generates sorted XHTML content
101 foreach( $m as $text => $type ) {
102 $selected = ($type == $queryType);
104 if ( isset($wgLogRestrictions[$type]) ) {
105 if ( $wgUser->isAllowed( $wgLogRestrictions[$type] ) ) {
106 $out .= Xml
::option( $text, $type, $selected ) . "\n";
109 $out .= Xml
::option( $text, $type, $selected ) . "\n";
118 * @return string Formatted HTML
120 private function getUserInput( $user ) {
121 return Xml
::inputLabel( wfMsg( 'specialloguserlabel' ), 'user', 'user', 12, $user );
125 * @return string Formatted HTML
127 private function getTitleInput( $title ) {
128 return Xml
::inputLabel( wfMsg( 'speciallogtitlelabel' ), 'page', 'page', 20, $title );
132 * @return boolean Checkbox
134 private function getTitlePattern( $pattern ) {
135 return Xml
::checkLabel( wfMsg( 'log-title-wildcard' ), 'pattern', 'pattern', $pattern );
138 public function beginLogEventsList() {
142 public function endLogEventsList() {
147 * @param Row $row a single row from the result set
148 * @return string Formatted HTML list item
151 public function logLine( $row ) {
152 global $wgLang, $wgUser, $wgContLang;
153 $skin = $wgUser->getSkin();
154 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
155 $time = $wgLang->timeanddate( wfTimestamp(TS_MW
, $row->log_timestamp
), true );
156 // Enter the existence or non-existence of this page into the link cache,
157 // for faster makeLinkObj() in LogPage::actionText()
158 $linkCache =& LinkCache
::singleton();
159 $linkCache->addLinkObj( $title );
161 if( self
::isDeleted($row,LogPage
::DELETED_USER
) ) {
162 $userLink = '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
164 $userLink = $this->skin
->userLink( $row->log_user
, $row->user_name
) .
165 $this->skin
->userToolLinksRedContribs( $row->log_user
, $row->user_name
);
168 if( $row->log_action
== 'create2' ) {
169 $comment = ''; // Suppress from old account creations, useless and can contain incorrect links
170 } else if( self
::isDeleted($row,LogPage
::DELETED_COMMENT
) ) {
171 $comment = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-comment') . '</span>';
173 $comment = $wgContLang->getDirMark() . $this->skin
->commentBlock( $row->log_comment
);
175 // Extract extra parameters
176 $paramArray = LogPage
::extractParams( $row->log_params
);
178 // Some user can hide log items and have review links
179 if( $wgUser->isAllowed( 'deleterevision' ) ) {
180 $del = $this->showhideLinks( $row ) . ' ';
182 // Add review links and such...
183 if( !($this->flags
& self
::NO_ACTION_LINK
) && !($row->log_deleted
& LogPage
::DELETED_ACTION
) ) {
184 if( $row->log_type
== 'move' && isset( $paramArray[0] ) && $wgUser->isAllowed( 'move' ) ) {
185 $destTitle = Title
::newFromText( $paramArray[0] );
187 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Movepage' ),
188 wfMsg( 'revertmove' ),
189 'wpOldTitle=' . urlencode( $destTitle->getPrefixedDBkey() ) .
190 '&wpNewTitle=' . urlencode( $title->getPrefixedDBkey() ) .
191 '&wpReason=' . urlencode( wfMsgForContent( 'revertmove' ) ) .
192 '&wpMovetalk=0' ) . ')';
194 // Show undelete link
195 } else if( $row->log_action
== 'delete' && $wgUser->isAllowed( 'delete' ) ) {
196 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Undelete' ),
197 wfMsg( 'undeletelink' ) ,
198 'target='. urlencode( $title->getPrefixedDBkey() ) ) . ')';
200 } else if( $row->log_action
== 'block' && $wgUser->isAllowed( 'block' ) ) {
201 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Ipblocklist' ),
202 wfMsg( 'unblocklink' ),
203 'action=unblock&ip=' . urlencode( $row->log_title
) ) . ')';
204 // Show change protection link
205 } else if( ( $row->log_action
== 'protect' ||
$row->log_action
== 'modify' ) && $wgUser->isAllowed( 'protect' ) ) {
206 $revert = '(' . $this->skin
->makeKnownLinkObj( $title, wfMsg( 'protect_change' ), 'action=unprotect' ) . ')';
208 } else if ( $row->log_action
== 'merge' ) {
209 $merge = SpecialPage
::getTitleFor( 'Mergehistory' );
210 $revert = '(' . $this->skin
->makeKnownLinkObj( $merge, wfMsg('revertmerge'),
212 array('target' => $paramArray[0], 'dest' => $title->getPrefixedText(), 'mergepoint' => $paramArray[1] )
215 // If an edit was hidden from a page give a review link to the history
216 } else if( $row->log_action
== 'revision' && $wgUser->isAllowed( 'deleterevision' ) ) {
217 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
218 // Different revision types use different URL params...
219 $subtype = isset($paramArray[2]) ?
$paramArray[1] : '';
220 // Link to each hidden object ID, $paramArray[1] is the url param. List if several...
221 $Ids = explode( ',', $paramArray[2] );
222 if( count($Ids) == 1 ) {
223 $revert = $this->skin
->makeKnownLinkObj( $revdel, wfMsgHtml('revdel-restore'),
224 wfArrayToCGI( array('target' => $paramArray[0], $paramArray[1] => $Ids[0] ) ) );
226 $revert .= wfMsgHtml('revdel-restore').':';
227 foreach( $Ids as $n => $id ) {
228 $revert .= ' '.$this->skin
->makeKnownLinkObj( $revdel, '#'.($n+
1),
229 wfArrayToCGI( array('target' => $paramArray[0], $paramArray[1] => $id ) ) );
232 $revert = "($revert)";
233 // Hidden log items, give review link
234 } else if( $row->log_action
== 'event' && $wgUser->isAllowed( 'deleterevision' ) ) {
235 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
236 $revert .= wfMsgHtml('revdel-restore');
237 $Ids = explode( ',', $paramArray[0] );
238 // Link to each hidden object ID, $paramArray[1] is the url param. List if several...
239 if( count($Ids) == 1 ) {
240 $revert = $this->skin
->makeKnownLinkObj( $revdel, wfMsgHtml('revdel-restore'),
241 wfArrayToCGI( array('logid' => $Ids[0] ) ) );
243 foreach( $Ids as $n => $id ) {
244 $revert .= $this->skin
->makeKnownLinkObj( $revdel, '#'.($n+
1),
245 wfArrayToCGI( array('logid' => $id ) ) );
248 $revert = "($revert)";
250 wfRunHooks( 'LogLine', array( $row->log_type
, $row->log_action
, $title, $paramArray,
251 &$comment, &$revert, $row->log_timestamp
) );
252 // wfDebug( "Invoked LogLine hook for " $row->log_type . ", " . $row->log_action . "\n" );
253 // Do nothing. The implementation is handled by the hook modifiying the passed-by-ref parameters.
257 if( $row->log_deleted
& LogPage
::DELETED_ACTION
) {
258 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
260 $action = LogPage
::actionText( $row->log_type
, $row->log_action
, $title, $this->skin
, $paramArray, true );
263 return "<li>$del$time $userLink $action $comment $revert</li>\n";
270 private function showhideLinks( $row ) {
271 global $wgAllowLogDeletion;
273 if( !$wgAllowLogDeletion )
276 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
277 // If event was hidden from sysops
278 if( !self
::userCan( $row, LogPage
::DELETED_RESTRICTED
) ) {
279 $del = $this->message
['rev-delundel'];
280 } else if( $row->log_type
== 'suppress' ) {
281 // No one should be hiding from the oversight log
282 $del = $this->message
['rev-delundel'];
284 $del = $this->skin
->makeKnownLinkObj( $revdel, $this->message
['rev-delundel'], 'logid='.$row->log_id
);
285 // Bolden oversighted content
286 if( self
::isDeleted( $row, LogPage
::DELETED_RESTRICTED
) )
287 $del = "<strong>$del</strong>";
289 return "<tt>(<small>$del</small>)</tt>";
293 * SQL clause to skip forbidden log types for this user
294 * @param Database $db
295 * @returns mixed (string or false)
297 public static function getExcludeClause( $db ) {
298 global $wgLogRestrictions, $wgUser;
299 // Reset the array, clears extra "where" clauses when $par is used
300 $hiddenLogs = array();
301 // Don't show private logs to unpriviledged users
302 foreach( $wgLogRestrictions as $logtype => $right ) {
303 if( !$wgUser->isAllowed($right) ) {
304 $safetype = $db->strencode( $logtype );
305 $hiddenLogs[] = "'$safetype'";
308 if( !empty($hiddenLogs) ) {
309 return 'log_type NOT IN(' . implode(',',$hiddenLogs) . ')';
315 * Determine if the current user is allowed to view a particular
316 * field of this log row, if it's marked as deleted.
321 public static function userCan( $row, $field ) {
322 if( ( $row->log_deleted
& $field ) == $field ) {
324 $permission = ( $row->log_deleted
& LogPage
::DELETED_RESTRICTED
) == LogPage
::DELETED_RESTRICTED
327 wfDebug( "Checking for $permission due to $field match on $row->log_deleted\n" );
328 return $wgUser->isAllowed( $permission );
336 * @param int $field one of DELETED_* bitfield constants
339 public static function isDeleted( $row, $field ) {
340 return ($row->log_deleted
& $field) == $field;
347 class LogPager
extends ReverseChronologicalPager
{
348 private $type = '', $user = '', $title = '', $pattern = '';
349 public $mLogEventsList;
352 * @param LogEventsList $loglist,
353 * @param string $type,
354 * @param string $user,
355 * @param string $page,
356 * @param string $pattern
357 * @param array $conds
359 function __construct( $loglist, $type, $user, $title, $pattern, $conds = array() ) {
360 parent
::__construct();
361 $this->mConds
= $conds;
363 $this->mLogEventsList
= $loglist;
365 $this->limitType( $type );
366 $this->limitUser( $user );
367 $this->limitTitle( $title, $pattern );
371 * Set the log reader to return only entries of the given type.
372 * Type restrictions enforced here
373 * @param string $type A log type ('upload', 'delete', etc)
376 private function limitType( $type ) {
377 global $wgLogRestrictions, $wgUser;
378 // Don't even show header for private logs; don't recognize it...
379 if( isset($wgLogRestrictions[$type]) && !$wgUser->isAllowed($wgLogRestrictions[$type]) ) {
382 // Don't show private logs to unpriviledged users
383 $hideLogs = LogEventsList
::getExcludeClause( $this->mDb
);
384 if( $hideLogs !== false ) {
385 $this->mConds
[] = $hideLogs;
391 $this->mConds
['log_type'] = $type;
395 * Set the log reader to return only entries by the given user.
396 * @param string $name (In)valid user name
399 function limitUser( $name ) {
403 $usertitle = Title
::makeTitleSafe( NS_USER
, $name );
404 if( is_null($usertitle) ) {
407 $this->user
= $usertitle->getText();
408 /* Fetch userid at first, if known, provides awesome query plan afterwards */
409 $userid = User
::idFromName( $this->user
);
411 /* It should be nicer to abort query at all,
412 but for now it won't pass anywhere behind the optimizer */
413 $this->mConds
[] = "NULL";
415 $this->mConds
['log_user'] = $userid;
420 * Set the log reader to return only entries affecting the given page.
421 * (For the block and rights logs, this is a user page.)
422 * @param string $page Title name as text
425 function limitTitle( $page, $pattern ) {
428 $title = Title
::newFromText( $page );
429 if( strlen($page) == 0 ||
!$title instanceof Title
)
432 $this->title
= $title->getPrefixedText();
433 $this->pattern
= $pattern;
434 $ns = $title->getNamespace();
435 if( $pattern && !$wgMiserMode ) {
436 # use escapeLike to avoid expensive search patterns like 't%st%'
437 $safetitle = $this->mDb
->escapeLike( $title->getDBkey() );
438 $this->mConds
['log_namespace'] = $ns;
439 $this->mConds
[] = "log_title LIKE '$safetitle%'";
441 $this->mConds
['log_namespace'] = $ns;
442 $this->mConds
['log_title'] = $title->getDBkey();
446 function getQueryInfo() {
447 $this->mConds
[] = 'user_id = log_user';
448 # Hack this until live
449 global $wgAllowLogDeletion;
450 $log_id = $wgAllowLogDeletion ?
'log_id' : '0 AS log_id';
452 'tables' => array( 'logging', 'user' ),
453 'fields' => array( 'log_type', 'log_action', 'log_user', 'log_namespace', 'log_title',
454 'log_params', 'log_comment', $log_id, 'log_deleted', 'log_timestamp', 'user_name' ),
455 'conds' => $this->mConds
,
460 function getIndexField() {
461 return 'log_timestamp';
464 function formatRow( $row ) {
465 return $this->mLogEventsList
->logLine( $row );
468 public function getType() {
472 public function getUser() {
476 public function getPage() {
480 public function getPattern() {
481 return $this->pattern
;
487 * @addtogroup SpecialPage
490 var $db, $joinClauses, $whereClauses;
491 var $type = '', $user = '', $title = null, $pattern = false;
493 * @param WebRequest $request For internal use use a FauxRequest object to pass arbitrary parameters.
495 function __construct( $request ) {
498 $type = $request->getVal( 'type' );
499 $user = $request->getText( 'user' );
500 $title = $request->getText( 'page' );
501 $pattern = $request->getBool( 'pattern' );
503 $loglist = new LogEventsList( $wgUser->getSkin() );
504 $this->pager
= new LogPager( $loglist, $type, $user, $title, $pattern );
508 * Is there at least one row?
511 public function hasRows() {
512 return isset($this->pager
) ?
($this->pager
->getNumRows() > 0) : false;
518 * @addtogroup SpecialPage
521 const NO_ACTION_LINK
= 1;
523 * @var LogReader $reader
530 * @param LogReader &$reader where to get our data from
531 * @param integer $flags Bitwise combination of flags:
532 * LogEventsList::NO_ACTION_LINK Don't show restore/unblock/block links
534 function __construct( &$reader, $flags = 0 ) {
536 $this->skin
= $wgUser->getSkin();
537 $this->reader
=& $reader;
538 $this->reader
->pager
->mLogEventsList
->flags
= $flags;
539 # Aliases for shorter code...
540 $this->pager
=& $this->reader
->pager
;
541 $this->logEventsList
=& $this->reader
->pager
->mLogEventsList
;
545 * Take over the whole output page in $wgOut with the log display.
547 public function show() {
549 # Set title and add header
550 $this->logEventsList
->showHeader( $wgOut, $pager->getType() );
552 $this->logEventsList
->showOptions( $wgOut, $this->pager
->getType(), $this->pager
->getUser(),
553 $this->pager
->getPage(), $this->pager
->getPattern() );
555 $logBody = $this->pager
->getBody();
558 $this->pager
->getNavigationBar() .
559 $this->logEventsList
->beginLogEventsList() .
561 $this->logEventsList
->endLogEventsList() .
562 $this->pager
->getNavigationBar()
565 $wgOut->addWikiMsg( 'logempty' );
570 * Output just the list of entries given by the linked LogReader,
571 * with extraneous UI elements. Use for displaying log fragments in
572 * another page (eg at Special:Undelete)
573 * @param OutputPage $out where to send output
575 public function showList( &$out ) {
576 $logBody = $this->pager
->getBody();
579 $this->logEventsList
->beginLogEventsList() .
581 $this->logEventsList
->endLogEventsList()
584 $out->addWikiMsg( 'logempty' );