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;
161 $skin = $wgUser->getSkin();
162 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
163 $time = $wgLang->timeanddate( wfTimestamp(TS_MW
, $row->log_timestamp
), true );
164 // Enter the existence or non-existence of this page into the link cache,
165 // for faster makeLinkObj() in LogPage::actionText()
166 $linkCache =& LinkCache
::singleton();
167 $linkCache->addLinkObj( $title );
169 if( self
::isDeleted($row,LogPage
::DELETED_USER
) ) {
170 $userLink = '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
172 $userLink = $this->skin
->userLink( $row->log_user
, $row->user_name
) .
173 $this->skin
->userToolLinksRedContribs( $row->log_user
, $row->user_name
);
176 if( self
::isDeleted($row,LogPage
::DELETED_COMMENT
) ) {
177 $comment = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-comment') . '</span>';
179 $comment = $wgContLang->getDirMark() . $this->skin
->commentBlock( $row->log_comment
);
181 // Extract extra parameters
182 $paramArray = LogPage
::extractParams( $row->log_params
);
184 // Some user can hide log items and have review links
185 if( $wgUser->isAllowed( 'deleterevision' ) ) {
186 $del = $this->showhideLinks( $row ) . ' ';
188 // Add review links and such...
189 if( !($this->flags
& self
::NO_ACTION_LINK
) && !($row->log_deleted
& LogPage
::DELETED_ACTION
) ) {
190 if( $row->log_type
== 'move' && isset( $paramArray[0] ) && $wgUser->isAllowed( 'move' ) ) {
191 $destTitle = Title
::newFromText( $paramArray[0] );
193 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Movepage' ),
194 $this->message
['revertmove'],
195 'wpOldTitle=' . urlencode( $destTitle->getPrefixedDBkey() ) .
196 '&wpNewTitle=' . urlencode( $title->getPrefixedDBkey() ) .
197 '&wpReason=' . urlencode( wfMsgForContent( 'revertmove' ) ) .
198 '&wpMovetalk=0' ) . ')';
200 // Show undelete link
201 } else if( $row->log_action
== 'delete' && $wgUser->isAllowed( 'delete' ) ) {
202 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Undelete' ),
203 $this->message
['undeletelink'], 'target='. urlencode( $title->getPrefixedDBkey() ) ) . ')';
205 } else if( $row->log_action
== 'block' && $wgUser->isAllowed( 'block' ) ) {
206 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Ipblocklist' ),
207 $this->message
['unblocklink'],
208 'action=unblock&ip=' . urlencode( $row->log_title
) ) . ')';
209 // Show change protection link
210 } else if( ( $row->log_action
== 'protect' ||
$row->log_action
== 'modify' ) && $wgUser->isAllowed( 'protect' ) ) {
211 $revert = '(' . $this->skin
->makeKnownLinkObj( $title, $this->message
['protect_change'], 'action=unprotect' ) . ')';
213 } else if ( $row->log_action
== 'merge' ) {
214 $merge = SpecialPage
::getTitleFor( 'Mergehistory' );
215 $revert = '(' . $this->skin
->makeKnownLinkObj( $merge, $this->message
['revertmerge'],
217 array('target' => $paramArray[0], 'dest' => $title->getPrefixedText(), 'mergepoint' => $paramArray[1] )
220 // If an edit was hidden from a page give a review link to the history
221 } else if( $row->log_action
== 'revision' && $wgUser->isAllowed( 'deleterevision' ) && isset($paramArray[2]) ) {
222 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
223 // Different revision types use different URL params...
224 $subtype = isset($paramArray[2]) ?
$paramArray[1] : '';
225 // Link to each hidden object ID, $paramArray[1] is the url param. List if several...
226 $Ids = explode( ',', $paramArray[2] );
227 if( count($Ids) == 1 ) {
228 $revert = $this->skin
->makeKnownLinkObj( $revdel, $this->message
['revdel-restore'],
229 wfArrayToCGI( array('target' => $paramArray[0], $paramArray[1] => $Ids[0] ) ) );
231 $revert .= $this->message
['revdel-restore'].':';
232 foreach( $Ids as $n => $id ) {
233 $revert .= ' '.$this->skin
->makeKnownLinkObj( $revdel, '#'.($n+
1),
234 wfArrayToCGI( array('target' => $paramArray[0], $paramArray[1] => $id ) ) );
237 $revert = "($revert)";
238 // Hidden log items, give review link
239 } else if( $row->log_action
== 'event' && $wgUser->isAllowed( 'deleterevision' ) && isset($paramArray[0]) ) {
240 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
241 $revert .= $this->message
['revdel-restore'];
242 $Ids = explode( ',', $paramArray[0] );
243 // Link to each hidden object ID, $paramArray[1] is the url param. List if several...
244 if( count($Ids) == 1 ) {
245 $revert = $this->skin
->makeKnownLinkObj( $revdel, $this->message
['revdel-restore'],
246 wfArrayToCGI( array('logid' => $Ids[0] ) ) );
248 foreach( $Ids as $n => $id ) {
249 $revert .= $this->skin
->makeKnownLinkObj( $revdel, '#'.($n+
1),
250 wfArrayToCGI( array('logid' => $id ) ) );
253 $revert = "($revert)";
255 wfRunHooks( 'LogLine', array( $row->log_type
, $row->log_action
, $title, $paramArray,
256 &$comment, &$revert, $row->log_timestamp
) );
257 // wfDebug( "Invoked LogLine hook for " $row->log_type . ", " . $row->log_action . "\n" );
258 // Do nothing. The implementation is handled by the hook modifiying the passed-by-ref parameters.
262 if( self
::isDeleted($row,LogPage
::DELETED_ACTION
) ) {
263 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
265 $action = LogPage
::actionText( $row->log_type
, $row->log_action
, $title, $this->skin
, $paramArray, true );
268 global $wgDateGroupedLogs;
269 if ( $wgDateGroupedLogs ) {
270 $time = $wgLang->time( wfTimestamp(TS_MW
, $row->log_timestamp
), true );
271 $date = $wgLang->date( wfTimestamp(TS_MW
, $row->log_timestamp
), true );
272 $line = Xml
::tags('div', array( 'class' => 'mw-log-entry' ), "$del$time $userLink $action $comment $revert" );
274 static $lastdate = false;
275 if ( $date !== $lastdate ) {
277 return Xml
::element('h4', null, $date) . "\n" . $line;
282 return Xml
::tags('li', null, "$del$time $userLink $action $comment $revert" );
291 private function showhideLinks( $row ) {
292 global $wgAllowLogDeletion;
294 if( !$wgAllowLogDeletion )
297 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
298 // If event was hidden from sysops
299 if( !self
::userCan( $row, LogPage
::DELETED_RESTRICTED
) ) {
300 $del = $this->message
['rev-delundel'];
301 } else if( $row->log_type
== 'suppress' ) {
302 // No one should be hiding from the oversight log
303 $del = $this->message
['rev-delundel'];
305 $del = $this->skin
->makeKnownLinkObj( $revdel, $this->message
['rev-delundel'], 'logid='.$row->log_id
);
306 // Bolden oversighted content
307 if( self
::isDeleted( $row, LogPage
::DELETED_RESTRICTED
) )
308 $del = "<strong>$del</strong>";
310 return "<tt>(<small>$del</small>)</tt>";
314 * Determine if the current user is allowed to view a particular
315 * field of this log row, if it's marked as deleted.
320 public static function userCan( $row, $field ) {
321 if( ( $row->log_deleted
& $field ) == $field ) {
323 $permission = ( $row->log_deleted
& LogPage
::DELETED_RESTRICTED
) == LogPage
::DELETED_RESTRICTED
326 wfDebug( "Checking for $permission due to $field match on $row->log_deleted\n" );
327 return $wgUser->isAllowed( $permission );
335 * @param int $field one of DELETED_* bitfield constants
338 public static function isDeleted( $row, $field ) {
339 return ($row->log_deleted
& $field) == $field;
343 * Quick function to show a short log extract
344 * @param OutputPage $out
345 * @param string $type
346 * @param string $page
347 * @param string $user
349 public static function showLogExtract( $out, $type='', $page='', $user='' ) {
351 # Insert list of top 50 or so items
352 $loglist = new LogEventsList( $wgUser->getSkin() );
353 $pager = new LogPager( $loglist, $type, $user, $page, '' );
354 $logBody = $pager->getBody();
357 $loglist->beginLogEventsList() .
359 $loglist->endLogEventsList()
362 $out->addWikiMsg( 'logempty' );
367 * SQL clause to skip forbidden log types for this user
368 * @param Database $db
369 * @returns mixed (string or false)
371 public static function getExcludeClause( $db ) {
372 global $wgLogRestrictions, $wgUser;
373 // Reset the array, clears extra "where" clauses when $par is used
374 $hiddenLogs = array();
375 // Don't show private logs to unpriviledged users
376 foreach( $wgLogRestrictions as $logtype => $right ) {
377 if( !$wgUser->isAllowed($right) ) {
378 $safetype = $db->strencode( $logtype );
379 $hiddenLogs[] = "'$safetype'";
382 if( !empty($hiddenLogs) ) {
383 return 'log_type NOT IN(' . implode(',',$hiddenLogs) . ')';
392 class LogPager
extends ReverseChronologicalPager
{
393 private $type = '', $user = '', $title = '', $pattern = '';
394 public $mLogEventsList;
397 * @param LogEventsList $loglist,
398 * @param string $type,
399 * @param string $user,
400 * @param string $page,
401 * @param string $pattern
402 * @param array $conds
404 function __construct( $loglist, $type='', $user='', $title='', $pattern='', $conds = array() ) {
405 parent
::__construct();
406 $this->mConds
= $conds;
408 $this->mLogEventsList
= $loglist;
410 $this->limitType( $type );
411 $this->limitUser( $user );
412 $this->limitTitle( $title, $pattern );
416 * Set the log reader to return only entries of the given type.
417 * Type restrictions enforced here
418 * @param string $type A log type ('upload', 'delete', etc)
421 private function limitType( $type ) {
422 global $wgLogRestrictions, $wgUser;
423 // Don't even show header for private logs; don't recognize it...
424 if( isset($wgLogRestrictions[$type]) && !$wgUser->isAllowed($wgLogRestrictions[$type]) ) {
427 // Don't show private logs to unpriviledged users
428 $hideLogs = LogEventsList
::getExcludeClause( $this->mDb
);
429 if( $hideLogs !== false ) {
430 $this->mConds
[] = $hideLogs;
436 $this->mConds
['log_type'] = $type;
440 * Set the log reader to return only entries by the given user.
441 * @param string $name (In)valid user name
444 function limitUser( $name ) {
448 $usertitle = Title
::makeTitleSafe( NS_USER
, $name );
449 if( is_null($usertitle) ) {
452 $this->user
= $usertitle->getText();
453 /* Fetch userid at first, if known, provides awesome query plan afterwards */
454 $userid = User
::idFromName( $this->user
);
456 /* It should be nicer to abort query at all,
457 but for now it won't pass anywhere behind the optimizer */
458 $this->mConds
[] = "NULL";
460 $this->mConds
['log_user'] = $userid;
465 * Set the log reader to return only entries affecting the given page.
466 * (For the block and rights logs, this is a user page.)
467 * @param string $page Title name as text
470 function limitTitle( $page, $pattern ) {
473 $title = Title
::newFromText( $page );
474 if( strlen($page) == 0 ||
!$title instanceof Title
)
477 $this->title
= $title->getPrefixedText();
478 $this->pattern
= $pattern;
479 $ns = $title->getNamespace();
480 if( $pattern && !$wgMiserMode ) {
481 # use escapeLike to avoid expensive search patterns like 't%st%'
482 $safetitle = $this->mDb
->escapeLike( $title->getDBkey() );
483 $this->mConds
['log_namespace'] = $ns;
484 $this->mConds
[] = "log_title LIKE '$safetitle%'";
486 $this->mConds
['log_namespace'] = $ns;
487 $this->mConds
['log_title'] = $title->getDBkey();
491 function getQueryInfo() {
492 $this->mConds
[] = 'user_id = log_user';
493 # Hack this until live
494 global $wgAllowLogDeletion;
495 $log_id = $wgAllowLogDeletion ?
'log_id' : '0 AS log_id';
496 # Don't use the wrong logging index
497 if( $this->title ||
$this->pattern
) {
498 $index = array( 'USE INDEX' => array( 'logging' => 'page_time' ) );
499 } else if( $this->user
) {
500 $index = array( 'USE INDEX' => array( 'logging' => 'user_time' ) );
501 } else if( $this->type
) {
502 $index = array( 'USE INDEX' => array( 'logging' => 'type_time' ) );
504 $index = array( 'USE INDEX' => array( 'logging' => 'times' ) );
507 'tables' => array( 'logging', 'user' ),
508 'fields' => array( 'log_type', 'log_action', 'log_user', 'log_namespace', 'log_title',
509 'log_params', 'log_comment', $log_id, 'log_deleted', 'log_timestamp', 'user_name' ),
510 'conds' => $this->mConds
,
515 function getIndexField() {
516 return 'log_timestamp';
519 function formatRow( $row ) {
520 return $this->mLogEventsList
->logLine( $row );
523 public function getType() {
527 public function getUser() {
531 public function getPage() {
535 public function getPattern() {
536 return $this->pattern
;
542 * @addtogroup SpecialPage
547 * @param WebRequest $request For internal use use a FauxRequest object to pass arbitrary parameters.
549 function __construct( $request ) {
552 $type = $request->getVal( 'type' );
553 $user = $request->getText( 'user' );
554 $title = $request->getText( 'page' );
555 $pattern = $request->getBool( 'pattern' );
557 $loglist = new LogEventsList( $wgUser->getSkin() );
558 $this->pager
= new LogPager( $loglist, $type, $user, $title, $pattern );
562 * Is there at least one row?
565 public function hasRows() {
566 return isset($this->pager
) ?
($this->pager
->getNumRows() > 0) : false;
572 * @addtogroup SpecialPage
575 const NO_ACTION_LINK
= 1;
577 * @var LogReader $reader
581 * @param LogReader &$reader where to get our data from
582 * @param integer $flags Bitwise combination of flags:
583 * LogEventsList::NO_ACTION_LINK Don't show restore/unblock/block links
585 function __construct( &$reader, $flags = 0 ) {
587 $this->reader
=& $reader;
588 $this->reader
->pager
->mLogEventsList
->flags
= $flags;
589 # Aliases for shorter code...
590 $this->pager
=& $this->reader
->pager
;
591 $this->logEventsList
=& $this->reader
->pager
->mLogEventsList
;
595 * Take over the whole output page in $wgOut with the log display.
597 public function show() {
599 # Set title and add header
600 $this->logEventsList
->showHeader( $wgOut, $pager->getType() );
602 $this->logEventsList
->showOptions( $wgOut, $this->pager
->getType(), $this->pager
->getUser(),
603 $this->pager
->getPage(), $this->pager
->getPattern() );
605 $logBody = $this->pager
->getBody();
608 $this->pager
->getNavigationBar() .
609 $this->logEventsList
->beginLogEventsList() .
611 $this->logEventsList
->endLogEventsList() .
612 $this->pager
->getNavigationBar()
615 $wgOut->addWikiMsg( 'logempty' );
620 * Output just the list of entries given by the linked LogReader,
621 * with extraneous UI elements. Use for displaying log fragments in
622 * another page (eg at Special:Undelete)
623 * @param OutputPage $out where to send output
625 public function showList( &$out ) {
626 $logBody = $this->pager
->getBody();
629 $this->logEventsList
->beginLogEventsList() .
631 $this->logEventsList
->endLogEventsList()
634 $out->addWikiMsg( 'logempty' );