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;
27 function __construct( &$skin, &$out, $flags = 0 ) {
30 $this->flags
= $flags;
31 $this->preCacheMessages();
35 * As we use the same small set of messages in various methods and that
36 * they are called often, we call them once and save them in $this->message
38 private function preCacheMessages() {
39 // Precache various messages
40 if( !isset( $this->message
) ) {
41 $messages = 'revertmerge protect_change unblocklink revertmove undeletelink revdel-restore rev-delundel';
42 foreach( explode(' ', $messages ) as $msg ) {
43 $this->message
[$msg] = wfMsgExt( $msg, array( 'escape') );
49 * Set page title and show header for this log type
52 public function showHeader( $type ) {
53 if( LogPage
::isLogType( $type ) ) {
54 $this->out
->setPageTitle( LogPage
::logName( $type ) );
55 $this->out
->addHtml( LogPage
::logHeader( $type ) );
60 * Show options for the log list
61 * @param string $type,
62 * @param string $user,
63 * @param string $page,
64 * @param string $pattern
68 public function showOptions( $type='', $user='', $page='', $pattern='', $year='', $month='' ) {
69 global $wgScript, $wgMiserMode;
70 $action = htmlspecialchars( $wgScript );
71 $title = SpecialPage
::getTitleFor( 'Log' );
72 $special = htmlspecialchars( $title->getPrefixedDBkey() );
74 $this->out
->addHTML( "<form action=\"$action\" method=\"get\"><fieldset>" .
75 Xml
::element( 'legend', array(), wfMsg( 'log' ) ) .
76 Xml
::hidden( 'title', $special ) . "\n" .
77 $this->getTypeMenu( $type ) . "\n" .
78 $this->getUserInput( $user ) . "\n" .
79 $this->getTitleInput( $page ) . "\n" .
80 ( !$wgMiserMode ?
($this->getTitlePattern( $pattern )."\n") : "" ) .
81 "<p>" . $this->getDateMenu( $year, $month ) . "\n" .
82 Xml
::submitButton( wfMsg( 'allpagessubmit' ) ) . "</p>\n" .
83 "</fieldset></form>" );
87 * @return string Formatted HTML
88 * @param string $queryType
90 private function getTypeMenu( $queryType ) {
91 global $wgLogRestrictions, $wgUser;
93 $html = "<select name='type'>\n";
95 $validTypes = LogPage
::validTypes();
96 $m = array(); // Temporary array
98 // First pass to load the log names
99 foreach( $validTypes as $type ) {
100 $text = LogPage
::logName( $type );
104 // Second pass to sort by name
107 // Third pass generates sorted XHTML content
108 foreach( $m as $text => $type ) {
109 $selected = ($type == $queryType);
111 if ( isset($wgLogRestrictions[$type]) ) {
112 if ( $wgUser->isAllowed( $wgLogRestrictions[$type] ) ) {
113 $html .= Xml
::option( $text, $type, $selected ) . "\n";
116 $html .= Xml
::option( $text, $type, $selected ) . "\n";
120 $html .= '</select>';
125 * @return string Formatted HTML
126 * @param string $user
128 private function getUserInput( $user ) {
129 return Xml
::inputLabel( wfMsg( 'specialloguserlabel' ), 'user', 'user', 15, $user );
133 * @return string Formatted HTML
134 * @param string $title
136 private function getTitleInput( $title ) {
137 return Xml
::inputLabel( wfMsg( 'speciallogtitlelabel' ), 'page', 'page', 20, $title );
141 * @return string Formatted HTML
145 private function getDateMenu( $year, $month ) {
146 # Offset overrides year/month selection
147 if( $month && $month !== -1 ) {
148 $encMonth = intval( $month );
153 $encYear = intval( $year );
154 } else if( $encMonth ) {
155 $thisMonth = intval( gmdate( 'n' ) );
156 $thisYear = intval( gmdate( 'Y' ) );
157 if( intval($encMonth) > $thisMonth ) {
160 $encYear = $thisYear;
164 return Xml
::label( wfMsg( 'year' ), 'year' ) . ' '.
165 Xml
::input( 'year', 4, $encYear, array('id' => 'year', 'maxlength' => 4) ) .
167 Xml
::label( wfMsg( 'month' ), 'month' ) . ' '.
168 Xml
::monthSelector( $encMonth, -1 );
172 * @return boolean Checkbox
174 private function getTitlePattern( $pattern ) {
175 return '<span style="white-space: nowrap">' .
176 Xml
::checkLabel( wfMsg( 'log-title-wildcard' ), 'pattern', 'pattern', $pattern ) .
180 public function beginLogEventsList() {
184 public function endLogEventsList() {
189 * @param Row $row a single row from the result set
190 * @return string Formatted HTML list item
193 public function logLine( $row ) {
194 global $wgLang, $wgUser, $wgContLang;
196 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
197 $time = $wgLang->timeanddate( wfTimestamp(TS_MW
, $row->log_timestamp
), true );
199 if( self
::isDeleted($row,LogPage
::DELETED_USER
) ) {
200 $userLink = '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
202 $userLink = $this->skin
->userLink( $row->log_user
, $row->user_name
) .
203 $this->skin
->userToolLinks( $row->log_user
, $row->user_name
, true, 0, $row->user_editcount
);
206 if( self
::isDeleted($row,LogPage
::DELETED_COMMENT
) ) {
207 $comment = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-comment') . '</span>';
209 $comment = $wgContLang->getDirMark() . $this->skin
->commentBlock( $row->log_comment
);
211 // Extract extra parameters
212 $paramArray = LogPage
::extractParams( $row->log_params
);
214 // Some user can hide log items and have review links
215 if( $wgUser->isAllowed( 'deleterevision' ) ) {
216 $del = $this->showhideLinks( $row ) . ' ';
218 // Add review links and such...
219 if( !($this->flags
& self
::NO_ACTION_LINK
) && !($row->log_deleted
& LogPage
::DELETED_ACTION
) ) {
220 if( self
::typeAction($row,'move','move') && isset( $paramArray[0] ) && $wgUser->isAllowed( 'move' ) ) {
221 $destTitle = Title
::newFromText( $paramArray[0] );
223 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Movepage' ),
224 $this->message
['revertmove'],
225 'wpOldTitle=' . urlencode( $destTitle->getPrefixedDBkey() ) .
226 '&wpNewTitle=' . urlencode( $title->getPrefixedDBkey() ) .
227 '&wpReason=' . urlencode( wfMsgForContent( 'revertmove' ) ) .
228 '&wpMovetalk=0' ) . ')';
230 // Show undelete link
231 } else if( self
::typeAction($row,'delete','delete') && $wgUser->isAllowed( 'delete' ) ) {
232 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Undelete' ),
233 $this->message
['undeletelink'], 'target='. urlencode( $title->getPrefixedDBkey() ) ) . ')';
235 } else if( self
::typeAction($row,'block','block') && $wgUser->isAllowed( 'block' ) ) {
236 $revert = '(' . $this->skin
->makeKnownLinkObj( SpecialPage
::getTitleFor( 'Ipblocklist' ),
237 $this->message
['unblocklink'],
238 'action=unblock&ip=' . urlencode( $row->log_title
) ) . ')';
239 // Show change protection link
240 } else if( self
::typeAction($row,'protect','modify') && $wgUser->isAllowed( 'protect' ) ) {
241 $revert = '(' . $this->skin
->makeKnownLinkObj( $title, $this->message
['protect_change'], 'action=unprotect' ) . ')';
243 } else if ( self
::typeAction($row,'merge','merge') ) {
244 $merge = SpecialPage
::getTitleFor( 'Mergehistory' );
245 $revert = '(' . $this->skin
->makeKnownLinkObj( $merge, $this->message
['revertmerge'],
247 array('target' => $paramArray[0], 'dest' => $title->getPrefixedText(), 'mergepoint' => $paramArray[1] )
250 // If an edit was hidden from a page give a review link to the history
251 } else if( self
::typeAction($row,array('delete','suppress'),'revision') && $wgUser->isAllowed( 'deleterevision' ) ) {
252 if( isset($paramArray[2]) ) {
253 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
254 // Different revision types use different URL params...
255 $subtype = isset($paramArray[2]) ?
$paramArray[1] : '';
256 // Link to each hidden object ID, $paramArray[1] is the url param. List if several...
257 $Ids = explode( ',', $paramArray[2] );
258 if( count($Ids) == 1 ) {
259 $revert = $this->skin
->makeKnownLinkObj( $revdel, $this->message
['revdel-restore'],
260 wfArrayToCGI( array('target' => $paramArray[0], $paramArray[1] => $Ids[0] ) ) );
262 $revert .= $this->message
['revdel-restore'].':';
263 foreach( $Ids as $n => $id ) {
264 $revert .= ' '.$this->skin
->makeKnownLinkObj( $revdel, '#'.($n+
1),
265 wfArrayToCGI( array('target' => $paramArray[0], $paramArray[1] => $id ) ) );
268 $revert = "($revert)";
270 // Hidden log items, give review link
271 } else if( self
::typeAction($row,array('delete','suppress'),'event') && $wgUser->isAllowed( 'deleterevision' ) ) {
272 if( isset($paramArray[0]) ) {
273 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
274 $revert .= $this->message
['revdel-restore'];
275 $Ids = explode( ',', $paramArray[0] );
276 // Link to each hidden object ID, $paramArray[1] is the url param. List if several...
277 if( count($Ids) == 1 ) {
278 $revert = $this->skin
->makeKnownLinkObj( $revdel, $this->message
['revdel-restore'],
279 wfArrayToCGI( array('logid' => $Ids[0] ) ) );
281 foreach( $Ids as $n => $id ) {
282 $revert .= $this->skin
->makeKnownLinkObj( $revdel, '#'.($n+
1),
283 wfArrayToCGI( array('logid' => $id ) ) );
286 $revert = "($revert)";
289 wfRunHooks( 'LogLine', array( $row->log_type
, $row->log_action
, $title, $paramArray,
290 &$comment, &$revert, $row->log_timestamp
) );
291 // wfDebug( "Invoked LogLine hook for " $row->log_type . ", " . $row->log_action . "\n" );
292 // Do nothing. The implementation is handled by the hook modifiying the passed-by-ref parameters.
296 if( self
::isDeleted($row,LogPage
::DELETED_ACTION
) ) {
297 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
299 $action = LogPage
::actionText( $row->log_type
, $row->log_action
, $title, $this->skin
, $paramArray, true );
302 return "<li>$del$time $userLink $action $comment $revert</li>\n";
309 private function showhideLinks( $row ) {
310 global $wgAllowLogDeletion;
312 if( !$wgAllowLogDeletion )
315 $revdel = SpecialPage
::getTitleFor( 'Revisiondelete' );
316 // If event was hidden from sysops
317 if( !self
::userCan( $row, LogPage
::DELETED_RESTRICTED
) ) {
318 $del = $this->message
['rev-delundel'];
319 } else if( $row->log_type
== 'suppress' ) {
320 // No one should be hiding from the oversight log
321 $del = $this->message
['rev-delundel'];
323 $del = $this->skin
->makeKnownLinkObj( $revdel, $this->message
['rev-delundel'], 'logid='.$row->log_id
);
324 // Bolden oversighted content
325 if( self
::isDeleted( $row, LogPage
::DELETED_RESTRICTED
) )
326 $del = "<strong>$del</strong>";
328 return "<tt>(<small>$del</small>)</tt>";
333 * @param mixed $type (string/array)
334 * @param string $action
337 public static function typeAction( $row, $type, $action ) {
338 if( is_array($type) ) {
339 return ( in_array($row->log_type
,$type) && $row->log_action
== $action );
341 return ( $row->log_type
== $type && $row->log_action
== $action );
346 * Determine if the current user is allowed to view a particular
347 * field of this log row, if it's marked as deleted.
352 public static function userCan( $row, $field ) {
353 if( ( $row->log_deleted
& $field ) == $field ) {
355 $permission = ( $row->log_deleted
& LogPage
::DELETED_RESTRICTED
) == LogPage
::DELETED_RESTRICTED
358 wfDebug( "Checking for $permission due to $field match on $row->log_deleted\n" );
359 return $wgUser->isAllowed( $permission );
367 * @param int $field one of DELETED_* bitfield constants
370 public static function isDeleted( $row, $field ) {
371 return ($row->log_deleted
& $field) == $field;
375 * Quick function to show a short log extract
376 * @param OutputPage $out
377 * @param string $type
378 * @param string $page
379 * @param string $user
381 public static function showLogExtract( $out, $type='', $page='', $user='' ) {
383 # Insert list of top 50 or so items
384 $loglist = new LogEventsList( $wgUser->getSkin(), $out, 0 );
385 $pager = new LogPager( $loglist, $type, $user, $page, '' );
386 $logBody = $pager->getBody();
389 $loglist->beginLogEventsList() .
391 $loglist->endLogEventsList()
394 $out->addWikiMsg( 'logempty' );
399 * SQL clause to skip forbidden log types for this user
400 * @param Database $db
401 * @returns mixed (string or false)
403 public static function getExcludeClause( $db ) {
404 global $wgLogRestrictions, $wgUser;
405 // Reset the array, clears extra "where" clauses when $par is used
406 $hiddenLogs = array();
407 // Don't show private logs to unprivileged users
408 foreach( $wgLogRestrictions as $logtype => $right ) {
409 if( !$wgUser->isAllowed($right) ) {
410 $safetype = $db->strencode( $logtype );
411 $hiddenLogs[] = $safetype;
414 if( count($hiddenLogs) == 1 ) {
415 return 'log_type != ' . $db->addQuotes( $hiddenLogs[0] );
416 } elseif( !empty( $hiddenLogs ) ) {
417 return 'log_type NOT IN (' . $db->makeList($hiddenLogs) . ')';
426 class LogPager
extends ReverseChronologicalPager
{
427 private $type = '', $user = '', $title = '', $pattern = '', $year = '', $month = '';
428 public $mLogEventsList;
431 * @param LogEventsList $loglist,
432 * @param string $type,
433 * @param string $user,
434 * @param string $page,
435 * @param string $pattern
436 * @param array $conds
438 function __construct( $list, $type='', $user='', $title='', $pattern='', $conds=array(), $y=false, $m=false ) {
439 parent
::__construct();
440 $this->mConds
= $conds;
442 $this->mLogEventsList
= $list;
444 $this->limitType( $type );
445 $this->limitUser( $user );
446 $this->limitTitle( $title, $pattern );
447 $this->limitDate( $y, $m );
450 function getDefaultQuery() {
451 $query = parent
::getDefaultQuery();
452 $query['type'] = $this->type
;
453 $query['month'] = $this->month
;
454 $query['year'] = $this->year
;
459 * Set the log reader to return only entries of the given type.
460 * Type restrictions enforced here
461 * @param string $type A log type ('upload', 'delete', etc)
464 private function limitType( $type ) {
465 global $wgLogRestrictions, $wgUser;
466 // Don't even show header for private logs; don't recognize it...
467 if( isset($wgLogRestrictions[$type]) && !$wgUser->isAllowed($wgLogRestrictions[$type]) ) {
470 // Don't show private logs to unpriviledged users
471 $hideLogs = LogEventsList
::getExcludeClause( $this->mDb
);
472 if( $hideLogs !== false ) {
473 $this->mConds
[] = $hideLogs;
479 $this->mConds
['log_type'] = $type;
483 * Set the log reader to return only entries by the given user.
484 * @param string $name (In)valid user name
487 function limitUser( $name ) {
491 $usertitle = Title
::makeTitleSafe( NS_USER
, $name );
492 if( is_null($usertitle) ) {
495 /* Fetch userid at first, if known, provides awesome query plan afterwards */
496 $userid = User
::idFromName( $name );
498 /* It should be nicer to abort query at all,
499 but for now it won't pass anywhere behind the optimizer */
500 $this->mConds
[] = "NULL";
502 $this->mConds
['log_user'] = $userid;
503 $this->user
= $usertitle->getText();
508 * Set the log reader to return only entries affecting the given page.
509 * (For the block and rights logs, this is a user page.)
510 * @param string $page Title name as text
513 function limitTitle( $page, $pattern ) {
516 $title = Title
::newFromText( $page );
517 if( strlen($page) == 0 ||
!$title instanceof Title
)
520 $this->title
= $title->getPrefixedText();
521 $ns = $title->getNamespace();
522 if( $pattern && !$wgMiserMode ) {
523 # use escapeLike to avoid expensive search patterns like 't%st%'
524 $safetitle = $this->mDb
->escapeLike( $title->getDBkey() );
525 $this->mConds
['log_namespace'] = $ns;
526 $this->mConds
[] = "log_title LIKE '$safetitle%'";
527 $this->pattern
= $pattern;
529 $this->mConds
['log_namespace'] = $ns;
530 $this->mConds
['log_title'] = $title->getDBkey();
535 * Set the log reader to return only entries from given date.
540 function limitDate( $year, $month ) {
541 $year = intval($year);
542 $month = intval($month);
544 $this->year
= ($year > 0 && $year < 10000) ?
$year : '';
545 $this->month
= ($month > 0 && $month < 13) ?
$month : '';
547 if( $this->year ||
$this->month
) {
548 // Assume this year if only a month is given
550 $year_start = $this->year
;
552 $year_start = substr( wfTimestampNow(), 0, 4 );
553 $thisMonth = gmdate( 'n' );
554 if( $this->month
> $thisMonth ) {
555 // Future contributions aren't supposed to happen. :)
561 $month_end = str_pad($this->month +
1, 2, '0', STR_PAD_LEFT
);
562 $year_end = $year_start;
565 $year_end = $year_start +
1;
567 $ts_end = str_pad($year_end . $month_end, 14, '0' );
569 $this->mOffset
= $ts_end;
573 function getQueryInfo() {
574 $this->mConds
[] = 'user_id = log_user';
575 # Hack this until live
576 global $wgAllowLogDeletion;
577 $log_id = $wgAllowLogDeletion ?
'log_id' : '0 AS log_id';
578 # Don't use the wrong logging index
579 if( $this->title ||
$this->pattern ||
$this->user
) {
580 $index = array( 'USE INDEX' => array( 'logging' => array('page_time','user_time') ) );
581 } else if( $this->type
) {
582 $index = array( 'USE INDEX' => array( 'logging' => 'type_time' ) );
584 $index = array( 'USE INDEX' => array( 'logging' => 'times' ) );
587 'tables' => array( 'logging', 'user' ),
588 'fields' => array( 'log_type', 'log_action', 'log_user', 'log_namespace', 'log_title', 'log_params',
589 'log_comment', $log_id, 'log_deleted', 'log_timestamp', 'user_name', 'user_editcount' ),
590 'conds' => $this->mConds
,
595 function getIndexField() {
596 return 'log_timestamp';
599 function getStartBody() {
600 wfProfileIn( __METHOD__
);
601 # Do a link batch query
602 if( $this->getNumRows() > 0 ) {
604 while( $row = $this->mResult
->fetchObject() ) {
605 $lb->add( $row->log_namespace
, $row->log_title
);
606 $lb->addObj( Title
::makeTitleSafe( NS_USER
, $row->user_name
) );
607 $lb->addObj( Title
::makeTitleSafe( NS_USER_TALK
, $row->user_name
) );
610 $this->mResult
->seek( 0 );
612 wfProfileOut( __METHOD__
);
616 function formatRow( $row ) {
617 return $this->mLogEventsList
->logLine( $row );
620 public function getType() {
624 public function getUser() {
628 public function getPage() {
632 public function getPattern() {
633 return $this->pattern
;
636 public function getYear() {
640 public function getMonth() {
647 * @addtogroup SpecialPage
652 * @param WebRequest $request For internal use use a FauxRequest object to pass arbitrary parameters.
654 function __construct( $request ) {
655 global $wgUser, $wgOut;
657 $type = $request->getVal( 'type' );
658 $user = $request->getText( 'user' );
659 $title = $request->getText( 'page' );
660 $pattern = $request->getBool( 'pattern' );
661 $y = $request->getIntOrNull( 'year' );
662 $m = $request->getIntOrNull( 'month' );
663 # Don't let the user get stuck with a certain date
664 $skip = $request->getText( 'offset' ) ||
$request->getText( 'dir' ) == 'prev';
669 # Use new list class to output results
670 $loglist = new LogEventsList( $wgUser->getSkin(), $wgOut, 0 );
671 $this->pager
= new LogPager( $loglist, $type, $user, $title, $pattern, $y, $m );
675 * Is there at least one row?
678 public function hasRows() {
679 return isset($this->pager
) ?
($this->pager
->getNumRows() > 0) : false;
685 * @addtogroup SpecialPage
688 const NO_ACTION_LINK
= 1;
690 * @var LogReader $reader
694 * @param LogReader &$reader where to get our data from
695 * @param integer $flags Bitwise combination of flags:
696 * LogEventsList::NO_ACTION_LINK Don't show restore/unblock/block links
698 function __construct( &$reader, $flags = 0 ) {
700 $this->reader
=& $reader;
701 $this->reader
->pager
->mLogEventsList
->flags
= $flags;
702 # Aliases for shorter code...
703 $this->pager
=& $this->reader
->pager
;
704 $this->list =& $this->reader
->pager
->mLogEventsList
;
708 * Take over the whole output page in $wgOut with the log display.
710 public function show() {
711 # Set title and add header
712 $this->list->showHeader( $pager->getType() );
714 $this->list->showOptions( $this->pager
->getType(), $this->pager
->getUser(), $this->pager
->getPage(),
715 $this->pager
->getPattern(), $this->pager
->getYear(), $this->pager
->getMonth() );
717 $logBody = $this->pager
->getBody();
720 $this->pager
->getNavigationBar() .
721 $this->list->beginLogEventsList() .
723 $this->list->endLogEventsList() .
724 $this->pager
->getNavigationBar()
727 $wgOut->addWikiMsg( 'logempty' );
732 * Output just the list of entries given by the linked LogReader,
733 * with extraneous UI elements. Use for displaying log fragments in
734 * another page (eg at Special:Undelete)
735 * @param OutputPage $out where to send output
737 public function showList( &$out ) {
738 $logBody = $this->pager
->getBody();
741 $this->list->beginLogEventsList() .
743 $this->list->endLogEventsList()
746 $out->addWikiMsg( 'logempty' );