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;
156 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
157 $time = $wgLang->timeanddate( wfTimestamp(TS_MW
, $row->log_timestamp
), true );
159 if( self
::isDeleted($row,LogPage
::DELETED_USER
) ) {
160 $userLink = '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
162 $userLink = $this->skin
->userLink( $row->log_user
, $row->user_name
) .
163 $this->skin
->userToolLinks( $row->log_user
, $row->user_name
);
166 if( self
::isDeleted($row,LogPage
::DELETED_COMMENT
) ) {
167 $comment = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-comment') . '</span>';
169 $comment = $wgContLang->getDirMark() . $this->skin
->commentBlock( $row->log_comment
);
171 // Extract extra parameters
172 $paramArray = LogPage
::extractParams( $row->log_params
);
174 // Some user can hide log items and have review links
175 if( $wgUser->isAllowed( 'deleterevision' ) ) {
176 $del = $this->showhideLinks( $row ) . ' ';
178 // Add review links and such...
179 if( !($this->flags
& self
::NO_ACTION_LINK
) && !($row->log_deleted
& LogPage
::DELETED_ACTION
) ) {
180 if( $row->log_type
== 'move' && isset( $paramArray[0] ) && $wgUser->isAllowed( 'move' ) ) {
181 $destTitle = Title
::newFromText( $paramArray[0] );
183 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Movepage' ),
184 $this->message
['revertmove'],
185 'wpOldTitle=' . urlencode( $destTitle->getPrefixedDBkey() ) .
186 '&wpNewTitle=' . urlencode( $title->getPrefixedDBkey() ) .
187 '&wpReason=' . urlencode( wfMsgForContent( 'revertmove' ) ) .
188 '&wpMovetalk=0' ) . ')';
190 // Show undelete link
191 } else if( $row->log_action
== 'delete' && $wgUser->isAllowed( 'delete' ) ) {
192 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Undelete' ),
193 $this->message
['undeletelink'], 'target='. urlencode( $title->getPrefixedDBkey() ) ) . ')';
195 } else if( $row->log_action
== 'block' && $wgUser->isAllowed( 'block' ) ) {
196 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Ipblocklist' ),
197 $this->message
['unblocklink'],
198 'action=unblock&ip=' . urlencode( $row->log_title
) ) . ')';
199 // Show change protection link
200 } else if( ( $row->log_action
== 'protect' ||
$row->log_action
== 'modify' ) && $wgUser->isAllowed( 'protect' ) ) {
201 $revert = '(' . $this->skin
->makeKnownLinkObj( $title, $this->message
['protect_change'], 'action=unprotect' ) . ')';
203 } else if ( $row->log_action
== 'merge' ) {
204 $merge = SpecialPage
::getTitleFor( 'Mergehistory' );
205 $revert = '(' . $this->skin
->makeKnownLinkObj( $merge, $this->message
['revertmerge'],
207 array('target' => $paramArray[0], 'dest' => $title->getPrefixedText(), 'mergepoint' => $paramArray[1] )
210 // If an edit was hidden from a page give a review link to the history
211 } else if( $row->log_action
== 'revision' && $wgUser->isAllowed( 'deleterevision' ) && isset($paramArray[2]) ) {
212 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
213 // Different revision types use different URL params...
214 $subtype = isset($paramArray[2]) ?
$paramArray[1] : '';
215 // Link to each hidden object ID, $paramArray[1] is the url param. List if several...
216 $Ids = explode( ',', $paramArray[2] );
217 if( count($Ids) == 1 ) {
218 $revert = $this->skin
->makeKnownLinkObj( $revdel, $this->message
['revdel-restore'],
219 wfArrayToCGI( array('target' => $paramArray[0], $paramArray[1] => $Ids[0] ) ) );
221 $revert .= $this->message
['revdel-restore'].':';
222 foreach( $Ids as $n => $id ) {
223 $revert .= ' '.$this->skin
->makeKnownLinkObj( $revdel, '#'.($n+
1),
224 wfArrayToCGI( array('target' => $paramArray[0], $paramArray[1] => $id ) ) );
227 $revert = "($revert)";
228 // Hidden log items, give review link
229 } else if( $row->log_action
== 'event' && $wgUser->isAllowed( 'deleterevision' ) && isset($paramArray[0]) ) {
230 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
231 $revert .= $this->message
['revdel-restore'];
232 $Ids = explode( ',', $paramArray[0] );
233 // Link to each hidden object ID, $paramArray[1] is the url param. List if several...
234 if( count($Ids) == 1 ) {
235 $revert = $this->skin
->makeKnownLinkObj( $revdel, $this->message
['revdel-restore'],
236 wfArrayToCGI( array('logid' => $Ids[0] ) ) );
238 foreach( $Ids as $n => $id ) {
239 $revert .= $this->skin
->makeKnownLinkObj( $revdel, '#'.($n+
1),
240 wfArrayToCGI( array('logid' => $id ) ) );
243 $revert = "($revert)";
245 wfRunHooks( 'LogLine', array( $row->log_type
, $row->log_action
, $title, $paramArray,
246 &$comment, &$revert, $row->log_timestamp
) );
247 // wfDebug( "Invoked LogLine hook for " $row->log_type . ", " . $row->log_action . "\n" );
248 // Do nothing. The implementation is handled by the hook modifiying the passed-by-ref parameters.
252 if( self
::isDeleted($row,LogPage
::DELETED_ACTION
) ) {
253 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
255 $action = LogPage
::actionText( $row->log_type
, $row->log_action
, $title, $this->skin
, $paramArray, true );
258 return "<li>$del$time $userLink $action $comment $revert</li>\n";
265 private function showhideLinks( $row ) {
266 global $wgAllowLogDeletion;
268 if( !$wgAllowLogDeletion )
271 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
272 // If event was hidden from sysops
273 if( !self
::userCan( $row, LogPage
::DELETED_RESTRICTED
) ) {
274 $del = $this->message
['rev-delundel'];
275 } else if( $row->log_type
== 'suppress' ) {
276 // No one should be hiding from the oversight log
277 $del = $this->message
['rev-delundel'];
279 $del = $this->skin
->makeKnownLinkObj( $revdel, $this->message
['rev-delundel'], 'logid='.$row->log_id
);
280 // Bolden oversighted content
281 if( self
::isDeleted( $row, LogPage
::DELETED_RESTRICTED
) )
282 $del = "<strong>$del</strong>";
284 return "<tt>(<small>$del</small>)</tt>";
288 * Determine if the current user is allowed to view a particular
289 * field of this log row, if it's marked as deleted.
294 public static function userCan( $row, $field ) {
295 if( ( $row->log_deleted
& $field ) == $field ) {
297 $permission = ( $row->log_deleted
& LogPage
::DELETED_RESTRICTED
) == LogPage
::DELETED_RESTRICTED
300 wfDebug( "Checking for $permission due to $field match on $row->log_deleted\n" );
301 return $wgUser->isAllowed( $permission );
309 * @param int $field one of DELETED_* bitfield constants
312 public static function isDeleted( $row, $field ) {
313 return ($row->log_deleted
& $field) == $field;
317 * Quick function to show a short log extract
318 * @param OutputPage $out
319 * @param string $type
320 * @param string $page
321 * @param string $user
323 public static function showLogExtract( $out, $type='', $page='', $user='' ) {
325 # Insert list of top 50 or so items
326 $loglist = new LogEventsList( $wgUser->getSkin() );
327 $pager = new LogPager( $loglist, $type, $user, $page, '' );
328 $logBody = $pager->getBody();
331 $loglist->beginLogEventsList() .
333 $loglist->endLogEventsList()
336 $out->addWikiMsg( 'logempty' );
341 * SQL clause to skip forbidden log types for this user
342 * @param Database $db
343 * @returns mixed (string or false)
345 public static function getExcludeClause( $db ) {
346 global $wgLogRestrictions, $wgUser;
347 // Reset the array, clears extra "where" clauses when $par is used
348 $hiddenLogs = array();
349 // Don't show private logs to unprivileged users
350 foreach( $wgLogRestrictions as $logtype => $right ) {
351 if( !$wgUser->isAllowed($right) ) {
352 $safetype = $db->strencode( $logtype );
353 $hiddenLogs[] = $safetype;
356 if( count($hiddenLogs) == 1 ) {
357 return 'log_type != ' . $db->addQuotes( $hiddenLogs[0] );
358 } elseif( !empty( $hiddenLogs ) ) {
359 return 'log_type NOT IN (' . $db->makeList($hiddenLogs) . ')';
368 class LogPager
extends ReverseChronologicalPager
{
369 private $type = '', $user = '', $title = '', $pattern = '';
370 public $mLogEventsList;
373 * @param LogEventsList $loglist,
374 * @param string $type,
375 * @param string $user,
376 * @param string $page,
377 * @param string $pattern
378 * @param array $conds
380 function __construct( $loglist, $type='', $user='', $title='', $pattern='', $conds = array() ) {
381 parent
::__construct();
382 $this->mConds
= $conds;
384 $this->mLogEventsList
= $loglist;
386 $this->limitType( $type );
387 $this->limitUser( $user );
388 $this->limitTitle( $title, $pattern );
391 function getDefaultQuery() {
392 $query = parent
::getDefaultQuery();
393 $query['type'] = $this->type
;
398 * Set the log reader to return only entries of the given type.
399 * Type restrictions enforced here
400 * @param string $type A log type ('upload', 'delete', etc)
403 private function limitType( $type ) {
404 global $wgLogRestrictions, $wgUser;
405 // Don't even show header for private logs; don't recognize it...
406 if( isset($wgLogRestrictions[$type]) && !$wgUser->isAllowed($wgLogRestrictions[$type]) ) {
409 // Don't show private logs to unpriviledged users
410 $hideLogs = LogEventsList
::getExcludeClause( $this->mDb
);
411 if( $hideLogs !== false ) {
412 $this->mConds
[] = $hideLogs;
418 $this->mConds
['log_type'] = $type;
422 * Set the log reader to return only entries by the given user.
423 * @param string $name (In)valid user name
426 function limitUser( $name ) {
430 $usertitle = Title
::makeTitleSafe( NS_USER
, $name );
431 if( is_null($usertitle) ) {
434 $this->user
= $usertitle->getText();
435 /* Fetch userid at first, if known, provides awesome query plan afterwards */
436 $userid = User
::idFromName( $this->user
);
438 /* It should be nicer to abort query at all,
439 but for now it won't pass anywhere behind the optimizer */
440 $this->mConds
[] = "NULL";
442 $this->mConds
['log_user'] = $userid;
447 * Set the log reader to return only entries affecting the given page.
448 * (For the block and rights logs, this is a user page.)
449 * @param string $page Title name as text
452 function limitTitle( $page, $pattern ) {
455 $title = Title
::newFromText( $page );
456 if( strlen($page) == 0 ||
!$title instanceof Title
)
459 $this->title
= $title->getPrefixedText();
460 $this->pattern
= $pattern;
461 $ns = $title->getNamespace();
462 if( $pattern && !$wgMiserMode ) {
463 # use escapeLike to avoid expensive search patterns like 't%st%'
464 $safetitle = $this->mDb
->escapeLike( $title->getDBkey() );
465 $this->mConds
['log_namespace'] = $ns;
466 $this->mConds
[] = "log_title LIKE '$safetitle%'";
468 $this->mConds
['log_namespace'] = $ns;
469 $this->mConds
['log_title'] = $title->getDBkey();
473 function getQueryInfo() {
474 $this->mConds
[] = 'user_id = log_user';
475 # Hack this until live
476 global $wgAllowLogDeletion;
477 $log_id = $wgAllowLogDeletion ?
'log_id' : '0 AS log_id';
478 # Don't use the wrong logging index
479 if( $this->title ||
$this->pattern ||
$this->user
) {
480 $index = array( 'USE INDEX' => array( 'logging' => array('page_time','user_time') ) );
481 } else if( $this->type
) {
482 $index = array( 'USE INDEX' => array( 'logging' => 'type_time' ) );
484 $index = array( 'USE INDEX' => array( 'logging' => 'times' ) );
487 'tables' => array( 'logging', 'user' ),
488 'fields' => array( 'log_type', 'log_action', 'log_user', 'log_namespace', 'log_title',
489 'log_params', 'log_comment', $log_id, 'log_deleted', 'log_timestamp', 'user_name' ),
490 'conds' => $this->mConds
,
495 function getIndexField() {
496 return 'log_timestamp';
499 function getStartBody() {
500 wfProfileIn( __METHOD__
);
501 # Do a link batch query
503 while( $row = $this->mResult
->fetchObject() ) {
504 $lb->add( $row->log_namespace
, $row->log_title
);
505 $lb->addObj( Title
::makeTitleSafe( NS_USER
, $row->user_name
) );
506 $lb->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->user_name
) );
509 $this->mResult
->seek( 0 );
510 wfProfileOut( __METHOD__
);
514 function formatRow( $row ) {
515 return $this->mLogEventsList
->logLine( $row );
518 public function getType() {
522 public function getUser() {
526 public function getPage() {
530 public function getPattern() {
531 return $this->pattern
;
537 * @addtogroup SpecialPage
542 * @param WebRequest $request For internal use use a FauxRequest object to pass arbitrary parameters.
544 function __construct( $request ) {
547 $type = $request->getVal( 'type' );
548 $user = $request->getText( 'user' );
549 $title = $request->getText( 'page' );
550 $pattern = $request->getBool( 'pattern' );
552 $loglist = new LogEventsList( $wgUser->getSkin() );
553 $this->pager
= new LogPager( $loglist, $type, $user, $title, $pattern );
557 * Is there at least one row?
560 public function hasRows() {
561 return isset($this->pager
) ?
($this->pager
->getNumRows() > 0) : false;
567 * @addtogroup SpecialPage
570 const NO_ACTION_LINK
= 1;
572 * @var LogReader $reader
576 * @param LogReader &$reader where to get our data from
577 * @param integer $flags Bitwise combination of flags:
578 * LogEventsList::NO_ACTION_LINK Don't show restore/unblock/block links
580 function __construct( &$reader, $flags = 0 ) {
582 $this->reader
=& $reader;
583 $this->reader
->pager
->mLogEventsList
->flags
= $flags;
584 # Aliases for shorter code...
585 $this->pager
=& $this->reader
->pager
;
586 $this->logEventsList
=& $this->reader
->pager
->mLogEventsList
;
590 * Take over the whole output page in $wgOut with the log display.
592 public function show() {
594 # Set title and add header
595 $this->logEventsList
->showHeader( $wgOut, $pager->getType() );
597 $this->logEventsList
->showOptions( $wgOut, $this->pager
->getType(), $this->pager
->getUser(),
598 $this->pager
->getPage(), $this->pager
->getPattern() );
600 $logBody = $this->pager
->getBody();
603 $this->pager
->getNavigationBar() .
604 $this->logEventsList
->beginLogEventsList() .
606 $this->logEventsList
->endLogEventsList() .
607 $this->pager
->getNavigationBar()
610 $wgOut->addWikiMsg( 'logempty' );
615 * Output just the list of entries given by the linked LogReader,
616 * with extraneous UI elements. Use for displaying log fragments in
617 * another page (eg at Special:Undelete)
618 * @param OutputPage $out where to send output
620 public function showList( &$out ) {
621 $logBody = $this->pager
->getBody();
624 $this->logEventsList
->beginLogEventsList() .
626 $this->logEventsList
->endLogEventsList()
629 $out->addWikiMsg( 'logempty' );