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 );
140 public function beginLogEventsList() {
144 public function endLogEventsList() {
149 * @param Row $row a single row from the result set
150 * @return string Formatted HTML list item
153 public function logLine( $row ) {
154 global $wgLang, $wgUser, $wgContLang;
155 $skin = $wgUser->getSkin();
156 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
157 $time = $wgLang->timeanddate( wfTimestamp(TS_MW
, $row->log_timestamp
), true );
158 // Enter the existence or non-existence of this page into the link cache,
159 // for faster makeLinkObj() in LogPage::actionText()
160 $linkCache =& LinkCache
::singleton();
161 $linkCache->addLinkObj( $title );
163 if( self
::isDeleted($row,LogPage
::DELETED_USER
) ) {
164 $userLink = '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
166 $userLink = $this->skin
->userLink( $row->log_user
, $row->user_name
) .
167 $this->skin
->userToolLinksRedContribs( $row->log_user
, $row->user_name
);
170 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 $this->message
['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 $this->message
['undeletelink'], 'target='. urlencode( $title->getPrefixedDBkey() ) ) . ')';
199 } else if( $row->log_action
== 'block' && $wgUser->isAllowed( 'block' ) ) {
200 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Ipblocklist' ),
201 $this->message
['unblocklink'],
202 'action=unblock&ip=' . urlencode( $row->log_title
) ) . ')';
203 // Show change protection link
204 } else if( ( $row->log_action
== 'protect' ||
$row->log_action
== 'modify' ) && $wgUser->isAllowed( 'protect' ) ) {
205 $revert = '(' . $this->skin
->makeKnownLinkObj( $title, $this->message
['protect_change'], 'action=unprotect' ) . ')';
207 } else if ( $row->log_action
== 'merge' ) {
208 $merge = SpecialPage
::getTitleFor( 'Mergehistory' );
209 $revert = '(' . $this->skin
->makeKnownLinkObj( $merge, $this->message
['revertmerge'],
211 array('target' => $paramArray[0], 'dest' => $title->getPrefixedText(), 'mergepoint' => $paramArray[1] )
214 // If an edit was hidden from a page give a review link to the history
215 } else if( $row->log_action
== 'revision' && $wgUser->isAllowed( 'deleterevision' ) && isset($paramArray[2]) ) {
216 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
217 // Different revision types use different URL params...
218 $subtype = isset($paramArray[2]) ?
$paramArray[1] : '';
219 // Link to each hidden object ID, $paramArray[1] is the url param. List if several...
220 $Ids = explode( ',', $paramArray[2] );
221 if( count($Ids) == 1 ) {
222 $revert = $this->skin
->makeKnownLinkObj( $revdel, $this->message
['revdel-restore'],
223 wfArrayToCGI( array('target' => $paramArray[0], $paramArray[1] => $Ids[0] ) ) );
225 $revert .= $this->message
['revdel-restore'].':';
226 foreach( $Ids as $n => $id ) {
227 $revert .= ' '.$this->skin
->makeKnownLinkObj( $revdel, '#'.($n+
1),
228 wfArrayToCGI( array('target' => $paramArray[0], $paramArray[1] => $id ) ) );
231 $revert = "($revert)";
232 // Hidden log items, give review link
233 } else if( $row->log_action
== 'event' && $wgUser->isAllowed( 'deleterevision' ) && isset($paramArray[2]) ) {
234 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
235 $revert .= $this->message
['revdel-restore'];
236 $Ids = explode( ',', $paramArray[0] );
237 // Link to each hidden object ID, $paramArray[1] is the url param. List if several...
238 if( count($Ids) == 1 ) {
239 $revert = $this->skin
->makeKnownLinkObj( $revdel, $this->message
['revdel-restore'],
240 wfArrayToCGI( array('logid' => $Ids[0] ) ) );
242 foreach( $Ids as $n => $id ) {
243 $revert .= $this->skin
->makeKnownLinkObj( $revdel, '#'.($n+
1),
244 wfArrayToCGI( array('logid' => $id ) ) );
247 $revert = "($revert)";
249 wfRunHooks( 'LogLine', array( $row->log_type
, $row->log_action
, $title, $paramArray,
250 &$comment, &$revert, $row->log_timestamp
) );
251 // wfDebug( "Invoked LogLine hook for " $row->log_type . ", " . $row->log_action . "\n" );
252 // Do nothing. The implementation is handled by the hook modifiying the passed-by-ref parameters.
256 if( self
::isDeleted($row,LogPage
::DELETED_ACTION
) ) {
257 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
259 $action = LogPage
::actionText( $row->log_type
, $row->log_action
, $title, $this->skin
, $paramArray, true );
262 return "<li>$del$time $userLink $action $comment $revert</li>\n";
269 private function showhideLinks( $row ) {
270 global $wgAllowLogDeletion;
272 if( !$wgAllowLogDeletion )
275 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
276 // If event was hidden from sysops
277 if( !self
::userCan( $row, LogPage
::DELETED_RESTRICTED
) ) {
278 $del = $this->message
['rev-delundel'];
279 } else if( $row->log_type
== 'suppress' ) {
280 // No one should be hiding from the oversight log
281 $del = $this->message
['rev-delundel'];
283 $del = $this->skin
->makeKnownLinkObj( $revdel, $this->message
['rev-delundel'], 'logid='.$row->log_id
);
284 // Bolden oversighted content
285 if( self
::isDeleted( $row, LogPage
::DELETED_RESTRICTED
) )
286 $del = "<strong>$del</strong>";
288 return "<tt>(<small>$del</small>)</tt>";
292 * Determine if the current user is allowed to view a particular
293 * field of this log row, if it's marked as deleted.
298 public static function userCan( $row, $field ) {
299 if( ( $row->log_deleted
& $field ) == $field ) {
301 $permission = ( $row->log_deleted
& LogPage
::DELETED_RESTRICTED
) == LogPage
::DELETED_RESTRICTED
304 wfDebug( "Checking for $permission due to $field match on $row->log_deleted\n" );
305 return $wgUser->isAllowed( $permission );
313 * @param int $field one of DELETED_* bitfield constants
316 public static function isDeleted( $row, $field ) {
317 return ($row->log_deleted
& $field) == $field;
321 * Quick function to show a short log extract
322 * @param OutputPage $out
323 * @param string $type
324 * @param string $page
325 * @param string $user
327 public static function showLogExtract( $out, $type='', $page='', $user='' ) {
329 # Insert list of top 50 or so items
330 $loglist = new LogEventsList( $wgUser->getSkin() );
331 $pager = new LogPager( $loglist, $type, $user, $page, '' );
332 $logBody = $pager->getBody();
335 $loglist->beginLogEventsList() .
337 $loglist->endLogEventsList()
340 $out->addWikiMsg( 'logempty' );
345 * SQL clause to skip forbidden log types for this user
346 * @param Database $db
347 * @returns mixed (string or false)
349 public static function getExcludeClause( $db ) {
350 global $wgLogRestrictions, $wgUser;
351 // Reset the array, clears extra "where" clauses when $par is used
352 $hiddenLogs = array();
353 // Don't show private logs to unpriviledged users
354 foreach( $wgLogRestrictions as $logtype => $right ) {
355 if( !$wgUser->isAllowed($right) ) {
356 $safetype = $db->strencode( $logtype );
357 $hiddenLogs[] = "'$safetype'";
360 if( !empty($hiddenLogs) ) {
361 return 'log_type NOT IN(' . implode(',',$hiddenLogs) . ')';
370 class LogPager
extends ReverseChronologicalPager
{
371 private $type = '', $user = '', $title = '', $pattern = '';
372 public $mLogEventsList;
375 * @param LogEventsList $loglist,
376 * @param string $type,
377 * @param string $user,
378 * @param string $page,
379 * @param string $pattern
380 * @param array $conds
382 function __construct( $loglist, $type='', $user='', $title='', $pattern='', $conds = array() ) {
383 parent
::__construct();
384 $this->mConds
= $conds;
386 $this->mLogEventsList
= $loglist;
388 $this->limitType( $type );
389 $this->limitUser( $user );
390 $this->limitTitle( $title, $pattern );
394 * Set the log reader to return only entries of the given type.
395 * Type restrictions enforced here
396 * @param string $type A log type ('upload', 'delete', etc)
399 private function limitType( $type ) {
400 global $wgLogRestrictions, $wgUser;
401 // Don't even show header for private logs; don't recognize it...
402 if( isset($wgLogRestrictions[$type]) && !$wgUser->isAllowed($wgLogRestrictions[$type]) ) {
405 // Don't show private logs to unpriviledged users
406 $hideLogs = LogEventsList
::getExcludeClause( $this->mDb
);
407 if( $hideLogs !== false ) {
408 $this->mConds
[] = $hideLogs;
414 $this->mConds
['log_type'] = $type;
418 * Set the log reader to return only entries by the given user.
419 * @param string $name (In)valid user name
422 function limitUser( $name ) {
426 $usertitle = Title
::makeTitleSafe( NS_USER
, $name );
427 if( is_null($usertitle) ) {
430 $this->user
= $usertitle->getText();
431 /* Fetch userid at first, if known, provides awesome query plan afterwards */
432 $userid = User
::idFromName( $this->user
);
434 /* It should be nicer to abort query at all,
435 but for now it won't pass anywhere behind the optimizer */
436 $this->mConds
[] = "NULL";
438 $this->mConds
['log_user'] = $userid;
443 * Set the log reader to return only entries affecting the given page.
444 * (For the block and rights logs, this is a user page.)
445 * @param string $page Title name as text
448 function limitTitle( $page, $pattern ) {
451 $title = Title
::newFromText( $page );
452 if( strlen($page) == 0 ||
!$title instanceof Title
)
455 $this->title
= $title->getPrefixedText();
456 $this->pattern
= $pattern;
457 $ns = $title->getNamespace();
458 if( $pattern && !$wgMiserMode ) {
459 # use escapeLike to avoid expensive search patterns like 't%st%'
460 $safetitle = $this->mDb
->escapeLike( $title->getDBkey() );
461 $this->mConds
['log_namespace'] = $ns;
462 $this->mConds
[] = "log_title LIKE '$safetitle%'";
464 $this->mConds
['log_namespace'] = $ns;
465 $this->mConds
['log_title'] = $title->getDBkey();
469 function getQueryInfo() {
470 $this->mConds
[] = 'user_id = log_user';
471 # Hack this until live
472 global $wgAllowLogDeletion;
473 $log_id = $wgAllowLogDeletion ?
'log_id' : '0 AS log_id';
475 'tables' => array( 'logging', 'user' ),
476 'fields' => array( 'log_type', 'log_action', 'log_user', 'log_namespace', 'log_title',
477 'log_params', 'log_comment', $log_id, 'log_deleted', 'log_timestamp', 'user_name' ),
478 'conds' => $this->mConds
,
483 function getIndexField() {
484 return 'log_timestamp';
487 function formatRow( $row ) {
488 return $this->mLogEventsList
->logLine( $row );
491 public function getType() {
495 public function getUser() {
499 public function getPage() {
503 public function getPattern() {
504 return $this->pattern
;
510 * @addtogroup SpecialPage
515 * @param WebRequest $request For internal use use a FauxRequest object to pass arbitrary parameters.
517 function __construct( $request ) {
520 $type = $request->getVal( 'type' );
521 $user = $request->getText( 'user' );
522 $title = $request->getText( 'page' );
523 $pattern = $request->getBool( 'pattern' );
525 $loglist = new LogEventsList( $wgUser->getSkin() );
526 $this->pager
= new LogPager( $loglist, $type, $user, $title, $pattern );
530 * Is there at least one row?
533 public function hasRows() {
534 return isset($this->pager
) ?
($this->pager
->getNumRows() > 0) : false;
540 * @addtogroup SpecialPage
543 const NO_ACTION_LINK
= 1;
545 * @var LogReader $reader
549 * @param LogReader &$reader where to get our data from
550 * @param integer $flags Bitwise combination of flags:
551 * LogEventsList::NO_ACTION_LINK Don't show restore/unblock/block links
553 function __construct( &$reader, $flags = 0 ) {
555 $this->reader
=& $reader;
556 $this->reader
->pager
->mLogEventsList
->flags
= $flags;
557 # Aliases for shorter code...
558 $this->pager
=& $this->reader
->pager
;
559 $this->logEventsList
=& $this->reader
->pager
->mLogEventsList
;
563 * Take over the whole output page in $wgOut with the log display.
565 public function show() {
567 # Set title and add header
568 $this->logEventsList
->showHeader( $wgOut, $pager->getType() );
570 $this->logEventsList
->showOptions( $wgOut, $this->pager
->getType(), $this->pager
->getUser(),
571 $this->pager
->getPage(), $this->pager
->getPattern() );
573 $logBody = $this->pager
->getBody();
576 $this->pager
->getNavigationBar() .
577 $this->logEventsList
->beginLogEventsList() .
579 $this->logEventsList
->endLogEventsList() .
580 $this->pager
->getNavigationBar()
583 $wgOut->addWikiMsg( 'logempty' );
588 * Output just the list of entries given by the linked LogReader,
589 * with extraneous UI elements. Use for displaying log fragments in
590 * another page (eg at Special:Undelete)
591 * @param OutputPage $out where to send output
593 public function showList( &$out ) {
594 $logBody = $this->pager
->getBody();
597 $this->logEventsList
->beginLogEventsList() .
599 $this->logEventsList
->endLogEventsList()
602 $out->addWikiMsg( 'logempty' );