Kill useless div that bumps everything down
[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->addHtml( 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 public function beginLogEventsList() {
141 return "<ul>\n";
142 }
143
144 public function endLogEventsList() {
145 return "</ul>\n";
146 }
147
148 /**
149 * @param Row $row a single row from the result set
150 * @return string Formatted HTML list item
151 * @private
152 */
153 public function logLine( $row ) {
154 global $wgLang, $wgUser, $wgContLang;
155
156 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
157 $time = $wgLang->timeanddate( wfTimestamp(TS_MW, $row->log_timestamp), true );
158 // User links
159 if( self::isDeleted($row,LogPage::DELETED_USER) ) {
160 $userLink = '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
161 } else {
162 $userLink = $this->skin->userLink( $row->log_user, $row->user_name ) .
163 $this->skin->userToolLinks( $row->log_user, $row->user_name );
164 }
165 // Comment
166 if( self::isDeleted($row,LogPage::DELETED_COMMENT) ) {
167 $comment = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-comment') . '</span>';
168 } else {
169 $comment = $wgContLang->getDirMark() . $this->skin->commentBlock( $row->log_comment );
170 }
171 // Extract extra parameters
172 $paramArray = LogPage::extractParams( $row->log_params );
173 $revert = $del = '';
174 // Some user can hide log items and have review links
175 if( $wgUser->isAllowed( 'deleterevision' ) ) {
176 $del = $this->showhideLinks( $row ) . ' ';
177 }
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] );
182 if( $destTitle ) {
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' ) . ')';
189 }
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() ) ) . ')';
194 // Show unblock link
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' ) . ')';
202 // Show unmerge link
203 } else if ( $row->log_action == 'merge' ) {
204 $merge = SpecialPage::getTitleFor( 'Mergehistory' );
205 $revert = '(' . $this->skin->makeKnownLinkObj( $merge, $this->message['revertmerge'],
206 wfArrayToCGI(
207 array('target' => $paramArray[0], 'dest' => $title->getPrefixedText(), 'mergepoint' => $paramArray[1] )
208 )
209 ) . ')';
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] ) ) );
220 } else {
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 ) ) );
225 }
226 }
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] ) ) );
237 } else {
238 foreach( $Ids as $n => $id ) {
239 $revert .= $this->skin->makeKnownLinkObj( $revdel, '#'.($n+1),
240 wfArrayToCGI( array('logid' => $id ) ) );
241 }
242 }
243 $revert = "($revert)";
244 } else {
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.
249 }
250 }
251 // Event description
252 if( self::isDeleted($row,LogPage::DELETED_ACTION) ) {
253 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
254 } else {
255 $action = LogPage::actionText( $row->log_type, $row->log_action, $title, $this->skin, $paramArray, true );
256 }
257
258 return "<li>$del$time $userLink $action $comment $revert</li>\n";
259 }
260
261 /**
262 * @param Row $row
263 * @private
264 */
265 private function showhideLinks( $row ) {
266 global $wgAllowLogDeletion;
267
268 if( !$wgAllowLogDeletion )
269 return "";
270
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'];
278 } else {
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>";
283 }
284 return "<tt>(<small>$del</small>)</tt>";
285 }
286
287 /**
288 * Determine if the current user is allowed to view a particular
289 * field of this log row, if it's marked as deleted.
290 * @param Row $row
291 * @param int $field
292 * @return bool
293 */
294 public static function userCan( $row, $field ) {
295 if( ( $row->log_deleted & $field ) == $field ) {
296 global $wgUser;
297 $permission = ( $row->log_deleted & LogPage::DELETED_RESTRICTED ) == LogPage::DELETED_RESTRICTED
298 ? 'hiderevision'
299 : 'deleterevision';
300 wfDebug( "Checking for $permission due to $field match on $row->log_deleted\n" );
301 return $wgUser->isAllowed( $permission );
302 } else {
303 return true;
304 }
305 }
306
307 /**
308 * @param Row $row
309 * @param int $field one of DELETED_* bitfield constants
310 * @return bool
311 */
312 public static function isDeleted( $row, $field ) {
313 return ($row->log_deleted & $field) == $field;
314 }
315
316 /**
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
322 */
323 public static function showLogExtract( $out, $type='', $page='', $user='' ) {
324 global $wgUser;
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();
329 if( $logBody ) {
330 $out->addHTML(
331 $loglist->beginLogEventsList() .
332 $logBody .
333 $loglist->endLogEventsList()
334 );
335 } else {
336 $out->addWikiMsg( 'logempty' );
337 }
338 }
339
340 /**
341 * SQL clause to skip forbidden log types for this user
342 * @param Database $db
343 * @returns mixed (string or false)
344 */
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;
354 }
355 }
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) . ')';
360 }
361 return false;
362 }
363 }
364
365 /**
366 * @addtogroup Pager
367 */
368 class LogPager extends ReverseChronologicalPager {
369 private $type = '', $user = '', $title = '', $pattern = '';
370 public $mLogEventsList;
371 /**
372 * constructor
373 * @param LogEventsList $loglist,
374 * @param string $type,
375 * @param string $user,
376 * @param string $page,
377 * @param string $pattern
378 * @param array $conds
379 */
380 function __construct( $loglist, $type='', $user='', $title='', $pattern='', $conds = array() ) {
381 parent::__construct();
382 $this->mConds = $conds;
383
384 $this->mLogEventsList = $loglist;
385
386 $this->limitType( $type );
387 $this->limitUser( $user );
388 $this->limitTitle( $title, $pattern );
389 }
390
391 function getDefaultQuery() {
392 $query = parent::getDefaultQuery();
393 $query['type'] = $this->type;
394 return $query;
395 }
396
397 /**
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)
401 * @private
402 */
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]) ) {
407 $type = '';
408 }
409 // Don't show private logs to unpriviledged users
410 $hideLogs = LogEventsList::getExcludeClause( $this->mDb );
411 if( $hideLogs !== false ) {
412 $this->mConds[] = $hideLogs;
413 }
414 if( empty($type) ) {
415 return false;
416 }
417 $this->type = $type;
418 $this->mConds['log_type'] = $type;
419 }
420
421 /**
422 * Set the log reader to return only entries by the given user.
423 * @param string $name (In)valid user name
424 * @private
425 */
426 function limitUser( $name ) {
427 if( $name == '' ) {
428 return false;
429 }
430 $usertitle = Title::makeTitleSafe( NS_USER, $name );
431 if( is_null($usertitle) ) {
432 return false;
433 }
434 $this->user = $usertitle->getText();
435 /* Fetch userid at first, if known, provides awesome query plan afterwards */
436 $userid = User::idFromName( $this->user );
437 if( !$userid ) {
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";
441 } else {
442 $this->mConds['log_user'] = $userid;
443 }
444 }
445
446 /**
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
450 * @private
451 */
452 function limitTitle( $page, $pattern ) {
453 global $wgMiserMode;
454
455 $title = Title::newFromText( $page );
456 if( strlen($page) == 0 || !$title instanceof Title )
457 return false;
458
459 $this->title = $title->getPrefixedText();
460 $ns = $title->getNamespace();
461 if( $pattern && !$wgMiserMode ) {
462 # use escapeLike to avoid expensive search patterns like 't%st%'
463 $safetitle = $this->mDb->escapeLike( $title->getDBkey() );
464 $this->mConds['log_namespace'] = $ns;
465 $this->mConds[] = "log_title LIKE '$safetitle%'";
466 $this->pattern = $pattern;
467 } else {
468 $this->mConds['log_namespace'] = $ns;
469 $this->mConds['log_title'] = $title->getDBkey();
470 }
471 }
472
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' ) );
483 } else {
484 $index = array( 'USE INDEX' => array( 'logging' => 'times' ) );
485 }
486 return array(
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,
491 'options' => $index
492 );
493 }
494
495 function getIndexField() {
496 return 'log_timestamp';
497 }
498
499 function getStartBody() {
500 wfProfileIn( __METHOD__ );
501 # Do a link batch query
502 $lb = new LinkBatch;
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 ) );
507 }
508 $lb->execute();
509 $this->mResult->seek( 0 );
510 wfProfileOut( __METHOD__ );
511 return '';
512 }
513
514 function formatRow( $row ) {
515 return $this->mLogEventsList->logLine( $row );
516 }
517
518 public function getType() {
519 return $this->type;
520 }
521
522 public function getUser() {
523 return $this->user;
524 }
525
526 public function getPage() {
527 return $this->title;
528 }
529
530 public function getPattern() {
531 return $this->pattern;
532 }
533 }
534
535 /**
536 *
537 * @addtogroup SpecialPage
538 */
539 class LogReader {
540 var $pager;
541 /**
542 * @param WebRequest $request For internal use use a FauxRequest object to pass arbitrary parameters.
543 */
544 function __construct( $request ) {
545 global $wgUser;
546 # Get parameters
547 $type = $request->getVal( 'type' );
548 $user = $request->getText( 'user' );
549 $title = $request->getText( 'page' );
550 $pattern = $request->getBool( 'pattern' );
551
552 $loglist = new LogEventsList( $wgUser->getSkin() );
553 $this->pager = new LogPager( $loglist, $type, $user, $title, $pattern );
554 }
555
556 /**
557 * Is there at least one row?
558 * @return bool
559 */
560 public function hasRows() {
561 return isset($this->pager) ? ($this->pager->getNumRows() > 0) : false;
562 }
563 }
564
565 /**
566 *
567 * @addtogroup SpecialPage
568 */
569 class LogViewer {
570 const NO_ACTION_LINK = 1;
571 /**
572 * @var LogReader $reader
573 */
574 var $reader;
575 /**
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
579 */
580 function __construct( &$reader, $flags = 0 ) {
581 global $wgUser;
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;
587 }
588
589 /**
590 * Take over the whole output page in $wgOut with the log display.
591 */
592 public function show() {
593 global $wgOut;
594 # Set title and add header
595 $this->logEventsList->showHeader( $wgOut, $pager->getType() );
596 # Show form options
597 $this->logEventsList->showOptions( $wgOut, $this->pager->getType(), $this->pager->getUser(),
598 $this->pager->getPage(), $this->pager->getPattern() );
599 # Insert list
600 $logBody = $this->pager->getBody();
601 if( $logBody ) {
602 $wgOut->addHTML(
603 $this->pager->getNavigationBar() .
604 $this->logEventsList->beginLogEventsList() .
605 $logBody .
606 $this->logEventsList->endLogEventsList() .
607 $this->pager->getNavigationBar()
608 );
609 } else {
610 $wgOut->addWikiMsg( 'logempty' );
611 }
612 }
613
614 /**
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
619 */
620 public function showList( &$out ) {
621 $logBody = $this->pager->getBody();
622 if( $logBody ) {
623 $out->addHTML(
624 $this->logEventsList->beginLogEventsList() .
625 $logBody .
626 $this->logEventsList->endLogEventsList()
627 );
628 } else {
629 $out->addWikiMsg( 'logempty' );
630 }
631 }
632 }
633