type/time
[lhc/web/wiklou.git] / includes / LogEventsList.php
1 <?php
2 # Copyright (C) 2004 Brion Vibber <brion@pobox.com>, 2008 Aaron Schulz
3 # http://www.mediawiki.org/
4 #
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.
9 #
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.
14 #
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
19
20 class LogEventsList {
21 const NO_ACTION_LINK = 1;
22
23 private $skin;
24 public $flags;
25
26 function __construct( &$skin, $flags = 0 ) {
27 $this->skin =& $skin;
28 $this->flags = $flags;
29 $this->preCacheMessages();
30 }
31
32 /**
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
35 */
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') );
42 }
43 }
44 }
45
46 /**
47 * Set page title and show header for this log type
48 * @param OutputPage $out where to send output
49 * @param strin $type
50 */
51 public function showHeader( $out, $type ) {
52 if( LogPage::isLogType( $type ) ) {
53 $out->setPageTitle( LogPage::logName( $type ) );
54 $out->addWikiText( LogPage::logHeader( $type ) );
55 }
56 }
57
58 /**
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
65 */
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>" );
80 }
81
82 /**
83 * @return string Formatted HTML
84 */
85 private function getTypeMenu( $queryType ) {
86 global $wgLogRestrictions, $wgUser;
87
88 $out = "<select name='type'>\n";
89
90 $validTypes = LogPage::validTypes();
91 $m = array(); // Temporary array
92
93 // First pass to load the log names
94 foreach( $validTypes as $type ) {
95 $text = LogPage::logName( $type );
96 $m[$text] = $type;
97 }
98
99 // Second pass to sort by name
100 ksort($m);
101
102 // Third pass generates sorted XHTML content
103 foreach( $m as $text => $type ) {
104 $selected = ($type == $queryType);
105 // Restricted types
106 if ( isset($wgLogRestrictions[$type]) ) {
107 if ( $wgUser->isAllowed( $wgLogRestrictions[$type] ) ) {
108 $out .= Xml::option( $text, $type, $selected ) . "\n";
109 }
110 } else {
111 $out .= Xml::option( $text, $type, $selected ) . "\n";
112 }
113 }
114
115 $out .= '</select>';
116 return $out;
117 }
118
119 /**
120 * @return string Formatted HTML
121 */
122 private function getUserInput( $user ) {
123 return Xml::inputLabel( wfMsg( 'specialloguserlabel' ), 'user', 'user', 12, $user );
124 }
125
126 /**
127 * @return string Formatted HTML
128 */
129 private function getTitleInput( $title ) {
130 return Xml::inputLabel( wfMsg( 'speciallogtitlelabel' ), 'page', 'page', 20, $title );
131 }
132
133 /**
134 * @return boolean Checkbox
135 */
136 private function getTitlePattern( $pattern ) {
137 return Xml::checkLabel( wfMsg( 'log-title-wildcard' ), 'pattern', 'pattern', $pattern );
138 }
139
140
141 protected function getWrapperElement() {
142 global $wgDateGroupedLogs;
143 return $wgDateGroupedLogs ? 'div' : 'ul';
144 }
145
146 public function beginLogEventsList() {
147 return Xml::openElement( $this->getWrapperElement() ) . "\n";
148 }
149
150 public function endLogEventsList() {
151 return Xml::closeElement( $this->getWrapperElement() ) . "\n";
152 }
153
154 /**
155 * @param Row $row a single row from the result set
156 * @return string Formatted HTML list item
157 * @private
158 */
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 );
168 // User links
169 if( self::isDeleted($row,LogPage::DELETED_USER) ) {
170 $userLink = '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
171 } else {
172 $userLink = $this->skin->userLink( $row->log_user, $row->user_name ) .
173 $this->skin->userToolLinksRedContribs( $row->log_user, $row->user_name );
174 }
175 // Comment
176 if( self::isDeleted($row,LogPage::DELETED_COMMENT) ) {
177 $comment = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-comment') . '</span>';
178 } else {
179 $comment = $wgContLang->getDirMark() . $this->skin->commentBlock( $row->log_comment );
180 }
181 // Extract extra parameters
182 $paramArray = LogPage::extractParams( $row->log_params );
183 $revert = $del = '';
184 // Some user can hide log items and have review links
185 if( $wgUser->isAllowed( 'deleterevision' ) ) {
186 $del = $this->showhideLinks( $row ) . ' ';
187 }
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] );
192 if( $destTitle ) {
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' ) . ')';
199 }
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() ) ) . ')';
204 // Show unblock link
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' ) . ')';
212 // Show unmerge link
213 } else if ( $row->log_action == 'merge' ) {
214 $merge = SpecialPage::getTitleFor( 'Mergehistory' );
215 $revert = '(' . $this->skin->makeKnownLinkObj( $merge, $this->message['revertmerge'],
216 wfArrayToCGI(
217 array('target' => $paramArray[0], 'dest' => $title->getPrefixedText(), 'mergepoint' => $paramArray[1] )
218 )
219 ) . ')';
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] ) ) );
230 } else {
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 ) ) );
235 }
236 }
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] ) ) );
247 } else {
248 foreach( $Ids as $n => $id ) {
249 $revert .= $this->skin->makeKnownLinkObj( $revdel, '#'.($n+1),
250 wfArrayToCGI( array('logid' => $id ) ) );
251 }
252 }
253 $revert = "($revert)";
254 } else {
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.
259 }
260 }
261 // Event description
262 if( self::isDeleted($row,LogPage::DELETED_ACTION) ) {
263 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
264 } else {
265 $action = LogPage::actionText( $row->log_type, $row->log_action, $title, $this->skin, $paramArray, true );
266 }
267
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" );
273
274 static $lastdate = false;
275 if ( $date !== $lastdate ) {
276 $lastdate = $date;
277 return Xml::element('h4', null, $date) . "\n" . $line;
278 } else {
279 return $line;
280 }
281 } else {
282 return Xml::tags('li', null, "$del$time $userLink $action $comment $revert" );
283 }
284
285 }
286
287 /**
288 * @param Row $row
289 * @private
290 */
291 private function showhideLinks( $row ) {
292 global $wgAllowLogDeletion;
293
294 if( !$wgAllowLogDeletion )
295 return "";
296
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'];
304 } else {
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>";
309 }
310 return "<tt>(<small>$del</small>)</tt>";
311 }
312
313 /**
314 * Determine if the current user is allowed to view a particular
315 * field of this log row, if it's marked as deleted.
316 * @param Row $row
317 * @param int $field
318 * @return bool
319 */
320 public static function userCan( $row, $field ) {
321 if( ( $row->log_deleted & $field ) == $field ) {
322 global $wgUser;
323 $permission = ( $row->log_deleted & LogPage::DELETED_RESTRICTED ) == LogPage::DELETED_RESTRICTED
324 ? 'hiderevision'
325 : 'deleterevision';
326 wfDebug( "Checking for $permission due to $field match on $row->log_deleted\n" );
327 return $wgUser->isAllowed( $permission );
328 } else {
329 return true;
330 }
331 }
332
333 /**
334 * @param Row $row
335 * @param int $field one of DELETED_* bitfield constants
336 * @return bool
337 */
338 public static function isDeleted( $row, $field ) {
339 return ($row->log_deleted & $field) == $field;
340 }
341
342 /**
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
348 */
349 public static function showLogExtract( $out, $type='', $page='', $user='' ) {
350 global $wgUser;
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();
355 if( $logBody ) {
356 $out->addHTML(
357 $loglist->beginLogEventsList() .
358 $logBody .
359 $loglist->endLogEventsList()
360 );
361 } else {
362 $out->addWikiMsg( 'logempty' );
363 }
364 }
365
366 /**
367 * SQL clause to skip forbidden log types for this user
368 * @param Database $db
369 * @returns mixed (string or false)
370 */
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'";
380 }
381 }
382 if( !empty($hiddenLogs) ) {
383 return 'log_type NOT IN(' . implode(',',$hiddenLogs) . ')';
384 }
385 return false;
386 }
387 }
388
389 /**
390 * @addtogroup Pager
391 */
392 class LogPager extends ReverseChronologicalPager {
393 private $type = '', $user = '', $title = '', $pattern = '';
394 public $mLogEventsList;
395 /**
396 * constructor
397 * @param LogEventsList $loglist,
398 * @param string $type,
399 * @param string $user,
400 * @param string $page,
401 * @param string $pattern
402 * @param array $conds
403 */
404 function __construct( $loglist, $type='', $user='', $title='', $pattern='', $conds = array() ) {
405 parent::__construct();
406 $this->mConds = $conds;
407
408 $this->mLogEventsList = $loglist;
409
410 $this->limitType( $type );
411 $this->limitUser( $user );
412 $this->limitTitle( $title, $pattern );
413 }
414
415 /**
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)
419 * @private
420 */
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]) ) {
425 $type = '';
426 }
427 // Don't show private logs to unpriviledged users
428 $hideLogs = LogEventsList::getExcludeClause( $this->mDb );
429 if( $hideLogs !== false ) {
430 $this->mConds[] = $hideLogs;
431 }
432 if( empty($type) ) {
433 return false;
434 }
435 $this->type = $type;
436 $this->mConds['log_type'] = $type;
437 }
438
439 /**
440 * Set the log reader to return only entries by the given user.
441 * @param string $name (In)valid user name
442 * @private
443 */
444 function limitUser( $name ) {
445 if( $name == '' ) {
446 return false;
447 }
448 $usertitle = Title::makeTitleSafe( NS_USER, $name );
449 if( is_null($usertitle) ) {
450 return false;
451 }
452 $this->user = $usertitle->getText();
453 /* Fetch userid at first, if known, provides awesome query plan afterwards */
454 $userid = User::idFromName( $this->user );
455 if( !$userid ) {
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";
459 } else {
460 $this->mConds['log_user'] = $userid;
461 }
462 }
463
464 /**
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
468 * @private
469 */
470 function limitTitle( $page, $pattern ) {
471 global $wgMiserMode;
472
473 $title = Title::newFromText( $page );
474 if( strlen($page) == 0 || !$title instanceof Title )
475 return false;
476
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%'";
485 } else {
486 $this->mConds['log_namespace'] = $ns;
487 $this->mConds['log_title'] = $title->getDBkey();
488 }
489 }
490
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->user ) {
498 $index = array( 'USE INDEX' => array( 'logging' => 'user_time' ) );
499 } else if( $this->title || $this->pattern ) {
500 $index = array( 'USE INDEX' => array( 'logging' => 'page_time' ) );
501 } else if( $this->type ) {
502 $index = array( 'USE INDEX' => array( 'logging' => 'type_time' ) );
503 } else {
504 $index = array( 'USE INDEX' => array( 'logging' => 'times' ) );
505 }
506 return array(
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,
511 'options' => $index
512 );
513 }
514
515 function getIndexField() {
516 return 'log_timestamp';
517 }
518
519 function formatRow( $row ) {
520 return $this->mLogEventsList->logLine( $row );
521 }
522
523 public function getType() {
524 return $this->type;
525 }
526
527 public function getUser() {
528 return $this->user;
529 }
530
531 public function getPage() {
532 return $this->title;
533 }
534
535 public function getPattern() {
536 return $this->pattern;
537 }
538 }
539
540 /**
541 *
542 * @addtogroup SpecialPage
543 */
544 class LogReader {
545 var $pager;
546 /**
547 * @param WebRequest $request For internal use use a FauxRequest object to pass arbitrary parameters.
548 */
549 function __construct( $request ) {
550 global $wgUser;
551 # Get parameters
552 $type = $request->getVal( 'type' );
553 $user = $request->getText( 'user' );
554 $title = $request->getText( 'page' );
555 $pattern = $request->getBool( 'pattern' );
556
557 $loglist = new LogEventsList( $wgUser->getSkin() );
558 $this->pager = new LogPager( $loglist, $type, $user, $title, $pattern );
559 }
560
561 /**
562 * Is there at least one row?
563 * @return bool
564 */
565 public function hasRows() {
566 return isset($this->pager) ? ($this->pager->getNumRows() > 0) : false;
567 }
568 }
569
570 /**
571 *
572 * @addtogroup SpecialPage
573 */
574 class LogViewer {
575 const NO_ACTION_LINK = 1;
576 /**
577 * @var LogReader $reader
578 */
579 var $reader;
580 /**
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
584 */
585 function __construct( &$reader, $flags = 0 ) {
586 global $wgUser;
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;
592 }
593
594 /**
595 * Take over the whole output page in $wgOut with the log display.
596 */
597 public function show() {
598 global $wgOut;
599 # Set title and add header
600 $this->logEventsList->showHeader( $wgOut, $pager->getType() );
601 # Show form options
602 $this->logEventsList->showOptions( $wgOut, $this->pager->getType(), $this->pager->getUser(),
603 $this->pager->getPage(), $this->pager->getPattern() );
604 # Insert list
605 $logBody = $this->pager->getBody();
606 if( $logBody ) {
607 $wgOut->addHTML(
608 $this->pager->getNavigationBar() .
609 $this->logEventsList->beginLogEventsList() .
610 $logBody .
611 $this->logEventsList->endLogEventsList() .
612 $this->pager->getNavigationBar()
613 );
614 } else {
615 $wgOut->addWikiMsg( 'logempty' );
616 }
617 }
618
619 /**
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
624 */
625 public function showList( &$out ) {
626 $logBody = $this->pager->getBody();
627 if( $logBody ) {
628 $out->addHTML(
629 $this->logEventsList->beginLogEventsList() .
630 $logBody .
631 $this->logEventsList->endLogEventsList()
632 );
633 } else {
634 $out->addWikiMsg( 'logempty' );
635 }
636 }
637 }
638