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' ) && !empty( $paramArray ) ) {
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' ) ) {
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
326 public static function showLogExtract( $out, $type='', $page='', $user='' ) {
328 # Insert list of top 50 or so items
329 $loglist = new LogEventsList( $wgUser->getSkin() );
330 $pager = new LogPager( $loglist, $type, $user, $page, '' );
331 $logBody = $pager->getBody();
334 $loglist->beginLogEventsList() .
336 $loglist->endLogEventsList()
339 $out->addWikiMsg( 'logempty' );
344 * SQL clause to skip forbidden log types for this user
345 * @param Database $db
346 * @returns mixed (string or false)
348 public static function getExcludeClause( $db ) {
349 global $wgLogRestrictions, $wgUser;
350 // Reset the array, clears extra "where" clauses when $par is used
351 $hiddenLogs = array();
352 // Don't show private logs to unpriviledged users
353 foreach( $wgLogRestrictions as $logtype => $right ) {
354 if( !$wgUser->isAllowed($right) ) {
355 $safetype = $db->strencode( $logtype );
356 $hiddenLogs[] = "'$safetype'";
359 if( !empty($hiddenLogs) ) {
360 return 'log_type NOT IN(' . implode(',',$hiddenLogs) . ')';
369 class LogPager
extends ReverseChronologicalPager
{
370 private $type = '', $user = '', $title = '', $pattern = '';
371 public $mLogEventsList;
374 * @param LogEventsList $loglist,
375 * @param string $type,
376 * @param string $user,
377 * @param string $page,
378 * @param string $pattern
379 * @param array $conds
381 function __construct( $loglist, $type='', $user='', $title='', $pattern='', $conds = array() ) {
382 parent
::__construct();
383 $this->mConds
= $conds;
385 $this->mLogEventsList
= $loglist;
387 $this->limitType( $type );
388 $this->limitUser( $user );
389 $this->limitTitle( $title, $pattern );
393 * Set the log reader to return only entries of the given type.
394 * Type restrictions enforced here
395 * @param string $type A log type ('upload', 'delete', etc)
398 private function limitType( $type ) {
399 global $wgLogRestrictions, $wgUser;
400 // Don't even show header for private logs; don't recognize it...
401 if( isset($wgLogRestrictions[$type]) && !$wgUser->isAllowed($wgLogRestrictions[$type]) ) {
404 // Don't show private logs to unpriviledged users
405 $hideLogs = LogEventsList
::getExcludeClause( $this->mDb
);
406 if( $hideLogs !== false ) {
407 $this->mConds
[] = $hideLogs;
413 $this->mConds
['log_type'] = $type;
417 * Set the log reader to return only entries by the given user.
418 * @param string $name (In)valid user name
421 function limitUser( $name ) {
425 $usertitle = Title
::makeTitleSafe( NS_USER
, $name );
426 if( is_null($usertitle) ) {
429 $this->user
= $usertitle->getText();
430 /* Fetch userid at first, if known, provides awesome query plan afterwards */
431 $userid = User
::idFromName( $this->user
);
433 /* It should be nicer to abort query at all,
434 but for now it won't pass anywhere behind the optimizer */
435 $this->mConds
[] = "NULL";
437 $this->mConds
['log_user'] = $userid;
442 * Set the log reader to return only entries affecting the given page.
443 * (For the block and rights logs, this is a user page.)
444 * @param string $page Title name as text
447 function limitTitle( $page, $pattern ) {
450 $title = Title
::newFromText( $page );
451 if( strlen($page) == 0 ||
!$title instanceof Title
)
454 $this->title
= $title->getPrefixedText();
455 $this->pattern
= $pattern;
456 $ns = $title->getNamespace();
457 if( $pattern && !$wgMiserMode ) {
458 # use escapeLike to avoid expensive search patterns like 't%st%'
459 $safetitle = $this->mDb
->escapeLike( $title->getDBkey() );
460 $this->mConds
['log_namespace'] = $ns;
461 $this->mConds
[] = "log_title LIKE '$safetitle%'";
463 $this->mConds
['log_namespace'] = $ns;
464 $this->mConds
['log_title'] = $title->getDBkey();
468 function getQueryInfo() {
469 $this->mConds
[] = 'user_id = log_user';
470 # Hack this until live
471 global $wgAllowLogDeletion;
472 $log_id = $wgAllowLogDeletion ?
'log_id' : '0 AS log_id';
474 'tables' => array( 'logging', 'user' ),
475 'fields' => array( 'log_type', 'log_action', 'log_user', 'log_namespace', 'log_title',
476 'log_params', 'log_comment', $log_id, 'log_deleted', 'log_timestamp', 'user_name' ),
477 'conds' => $this->mConds
,
482 function getIndexField() {
483 return 'log_timestamp';
486 function formatRow( $row ) {
487 return $this->mLogEventsList
->logLine( $row );
490 public function getType() {
494 public function getUser() {
498 public function getPage() {
502 public function getPattern() {
503 return $this->pattern
;
509 * @addtogroup SpecialPage
514 * @param WebRequest $request For internal use use a FauxRequest object to pass arbitrary parameters.
516 function __construct( $request ) {
519 $type = $request->getVal( 'type' );
520 $user = $request->getText( 'user' );
521 $title = $request->getText( 'page' );
522 $pattern = $request->getBool( 'pattern' );
524 $loglist = new LogEventsList( $wgUser->getSkin() );
525 $this->pager
= new LogPager( $loglist, $type, $user, $title, $pattern );
529 * Is there at least one row?
532 public function hasRows() {
533 return isset($this->pager
) ?
($this->pager
->getNumRows() > 0) : false;
539 * @addtogroup SpecialPage
542 const NO_ACTION_LINK
= 1;
544 * @var LogReader $reader
548 * @param LogReader &$reader where to get our data from
549 * @param integer $flags Bitwise combination of flags:
550 * LogEventsList::NO_ACTION_LINK Don't show restore/unblock/block links
552 function __construct( &$reader, $flags = 0 ) {
554 $this->reader
=& $reader;
555 $this->reader
->pager
->mLogEventsList
->flags
= $flags;
556 # Aliases for shorter code...
557 $this->pager
=& $this->reader
->pager
;
558 $this->logEventsList
=& $this->reader
->pager
->mLogEventsList
;
562 * Take over the whole output page in $wgOut with the log display.
564 public function show() {
566 # Set title and add header
567 $this->logEventsList
->showHeader( $wgOut, $pager->getType() );
569 $this->logEventsList
->showOptions( $wgOut, $this->pager
->getType(), $this->pager
->getUser(),
570 $this->pager
->getPage(), $this->pager
->getPattern() );
572 $logBody = $this->pager
->getBody();
575 $this->pager
->getNavigationBar() .
576 $this->logEventsList
->beginLogEventsList() .
578 $this->logEventsList
->endLogEventsList() .
579 $this->pager
->getNavigationBar()
582 $wgOut->addWikiMsg( 'logempty' );
587 * Output just the list of entries given by the linked LogReader,
588 * with extraneous UI elements. Use for displaying log fragments in
589 * another page (eg at Special:Undelete)
590 * @param OutputPage $out where to send output
592 public function showList( &$out ) {
593 $logBody = $this->pager
->getBody();
596 $this->logEventsList
->beginLogEventsList() .
598 $this->logEventsList
->endLogEventsList()
601 $out->addWikiMsg( 'logempty' );