Slight UI cleanup
[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 private $skin;
23 public $flags;
24
25 function __construct( &$skin, $flags = 0 ) {
26 $this->skin =& $skin;
27 $this->flags = $flags;
28 $this->preCacheMessages();
29 }
30
31 /**
32 * As we use the same small set of messages in various methods and that
33 * they are called often, we call them once and save them in $this->message
34 */
35 private function preCacheMessages() {
36 // Precache various messages
37 if( !isset( $this->message ) ) {
38 foreach( explode(' ', 'viewpagelogs revhistory filehist rev-delundel' ) as $msg ) {
39 $this->message[$msg] = wfMsgExt( $msg, array( 'escape') );
40 }
41 }
42 }
43
44 /**
45 * Set page title and show header for this log type
46 * @param OutputPage $out where to send output
47 * @param strin $type
48 */
49 public function showHeader( $out, $type ) {
50 if( LogPage::isLogType( $type ) ) {
51 $out->setPageTitle( LogPage::logName( $type ) );
52 $out->addWikiText( LogPage::logHeader( $type ) );
53 }
54 }
55
56 /**
57 * Show options for the log list
58 * @param OutputPage $out where to send output
59 * @param string $type,
60 * @param string $user,
61 * @param string $page,
62 * @param string $pattern
63 */
64 public function showOptions( $out, $type, $user, $page, $pattern ) {
65 global $wgScript, $wgMiserMode;
66 $action = htmlspecialchars( $wgScript );
67 $title = SpecialPage::getTitleFor( 'Log' );
68 $special = htmlspecialchars( $title->getPrefixedDBkey() );
69 $out->addHTML( "<form action=\"$action\" method=\"get\"><fieldset>" .
70 Xml::element( 'legend', array(), wfMsg( 'log' ) ) .
71 Xml::hidden( 'title', $special ) . "\n" .
72 $this->getTypeMenu( $type ) . "\n" .
73 $this->getUserInput( $user ) . "\n" .
74 $this->getTitleInput( $page ) . "\n" .
75 ( !$wgMiserMode ? ($this->getTitlePattern( $pattern )."\n") : "" ) .
76 Xml::submitButton( wfMsg( 'allpagessubmit' ) ) . "\n" .
77 "</fieldset></form>" );
78 }
79
80 /**
81 * @return string Formatted HTML
82 */
83 private function getTypeMenu( $queryType ) {
84 global $wgLogRestrictions, $wgUser;
85
86 $out = "<select name='type'>\n";
87
88 $validTypes = LogPage::validTypes();
89 $m = array(); // Temporary array
90
91 // First pass to load the log names
92 foreach( $validTypes as $type ) {
93 $text = LogPage::logName( $type );
94 $m[$text] = $type;
95 }
96
97 // Second pass to sort by name
98 ksort($m);
99
100 // Third pass generates sorted XHTML content
101 foreach( $m as $text => $type ) {
102 $selected = ($type == $queryType);
103 // Restricted types
104 if ( isset($wgLogRestrictions[$type]) ) {
105 if ( $wgUser->isAllowed( $wgLogRestrictions[$type] ) ) {
106 $out .= Xml::option( $text, $type, $selected ) . "\n";
107 }
108 } else {
109 $out .= Xml::option( $text, $type, $selected ) . "\n";
110 }
111 }
112
113 $out .= '</select>';
114 return $out;
115 }
116
117 /**
118 * @return string Formatted HTML
119 */
120 private function getUserInput( $user ) {
121 return Xml::inputLabel( wfMsg( 'specialloguserlabel' ), 'user', 'user', 12, $user );
122 }
123
124 /**
125 * @return string Formatted HTML
126 */
127 private function getTitleInput( $title ) {
128 return Xml::inputLabel( wfMsg( 'speciallogtitlelabel' ), 'page', 'page', 20, $title );
129 }
130
131 /**
132 * @return boolean Checkbox
133 */
134 private function getTitlePattern( $pattern ) {
135 return Xml::checkLabel( wfMsg( 'log-title-wildcard' ), 'pattern', 'pattern', $pattern );
136 }
137
138 public function beginLogEventsList() {
139 return "<ul>\n";
140 }
141
142 public function endLogEventsList() {
143 return "</ul>\n";
144 }
145
146 /**
147 * @param Row $row a single row from the result set
148 * @return string Formatted HTML list item
149 * @private
150 */
151 public function logLine( $row ) {
152 global $wgLang, $wgUser, $wgContLang;
153 $skin = $wgUser->getSkin();
154 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
155 $time = $wgLang->timeanddate( wfTimestamp(TS_MW, $row->log_timestamp), true );
156 // Enter the existence or non-existence of this page into the link cache,
157 // for faster makeLinkObj() in LogPage::actionText()
158 $linkCache =& LinkCache::singleton();
159 $linkCache->addLinkObj( $title );
160 // User links
161 if( self::isDeleted($row,LogPage::DELETED_USER) ) {
162 $userLink = '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
163 } else {
164 $userLink = $this->skin->userLink( $row->log_user, $row->user_name ) .
165 $this->skin->userToolLinksRedContribs( $row->log_user, $row->user_name );
166 }
167 // Comment
168 if( $row->log_action == 'create2' ) {
169 $comment = ''; // Suppress from old account creations, useless and can contain incorrect links
170 } else if( self::isDeleted($row,LogPage::DELETED_COMMENT) ) {
171 $comment = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-comment') . '</span>';
172 } else {
173 $comment = $wgContLang->getDirMark() . $this->skin->commentBlock( $row->log_comment );
174 }
175 // Extract extra parameters
176 $paramArray = LogPage::extractParams( $row->log_params );
177 $revert = $del = '';
178 // Some user can hide log items and have review links
179 if( $wgUser->isAllowed( 'deleterevision' ) ) {
180 $del = $this->showhideLinks( $row ) . ' ';
181 }
182 // Add review links and such...
183 if( !($this->flags & self::NO_ACTION_LINK) && !($row->log_deleted & LogPage::DELETED_ACTION) ) {
184 if( $row->log_type == 'move' && isset( $paramArray[0] ) && $wgUser->isAllowed( 'move' ) ) {
185 $destTitle = Title::newFromText( $paramArray[0] );
186 if( $destTitle ) {
187 $revert = '(' . $this->skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Movepage' ),
188 wfMsg( 'revertmove' ),
189 'wpOldTitle=' . urlencode( $destTitle->getPrefixedDBkey() ) .
190 '&wpNewTitle=' . urlencode( $title->getPrefixedDBkey() ) .
191 '&wpReason=' . urlencode( wfMsgForContent( 'revertmove' ) ) .
192 '&wpMovetalk=0' ) . ')';
193 }
194 // Show undelete link
195 } else if( $row->log_action == 'delete' && $wgUser->isAllowed( 'delete' ) ) {
196 $revert = '(' . $this->skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Undelete' ),
197 wfMsg( 'undeletelink' ) ,
198 'target='. urlencode( $title->getPrefixedDBkey() ) ) . ')';
199 // Show unblock link
200 } else if( $row->log_action == 'block' && $wgUser->isAllowed( 'block' ) ) {
201 $revert = '(' . $this->skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Ipblocklist' ),
202 wfMsg( 'unblocklink' ),
203 'action=unblock&ip=' . urlencode( $row->log_title ) ) . ')';
204 // Show change protection link
205 } else if( ( $row->log_action == 'protect' || $row->log_action == 'modify' ) && $wgUser->isAllowed( 'protect' ) ) {
206 $revert = '(' . $this->skin->makeKnownLinkObj( $title, wfMsg( 'protect_change' ), 'action=unprotect' ) . ')';
207 // Show unmerge link
208 } else if ( $row->log_action == 'merge' ) {
209 $merge = SpecialPage::getTitleFor( 'Mergehistory' );
210 $revert = '(' . $this->skin->makeKnownLinkObj( $merge, wfMsg('revertmerge'),
211 wfArrayToCGI(
212 array('target' => $paramArray[0], 'dest' => $title->getPrefixedText(), 'mergepoint' => $paramArray[1] )
213 )
214 ) . ')';
215 // If an edit was hidden from a page give a review link to the history
216 } else if( $row->log_action == 'revision' && $wgUser->isAllowed( 'deleterevision' ) ) {
217 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
218 // Different revision types use different URL params...
219 $subtype = isset($paramArray[2]) ? $paramArray[1] : '';
220 // Link to each hidden object ID, $paramArray[1] is the url param. List if several...
221 $Ids = explode( ',', $paramArray[2] );
222 if( count($Ids) == 1 ) {
223 $revert = $this->skin->makeKnownLinkObj( $revdel, wfMsgHtml('revdel-restore'),
224 wfArrayToCGI( array('target' => $paramArray[0], $paramArray[1] => $Ids[0] ) ) );
225 } else {
226 $revert .= wfMsgHtml('revdel-restore').':';
227 foreach( $Ids as $n => $id ) {
228 $revert .= ' '.$this->skin->makeKnownLinkObj( $revdel, '#'.($n+1),
229 wfArrayToCGI( array('target' => $paramArray[0], $paramArray[1] => $id ) ) );
230 }
231 }
232 $revert = "($revert)";
233 // Hidden log items, give review link
234 } else if( $row->log_action == 'event' && $wgUser->isAllowed( 'deleterevision' ) ) {
235 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
236 $revert .= wfMsgHtml('revdel-restore');
237 $Ids = explode( ',', $paramArray[0] );
238 // Link to each hidden object ID, $paramArray[1] is the url param. List if several...
239 if( count($Ids) == 1 ) {
240 $revert = $this->skin->makeKnownLinkObj( $revdel, wfMsgHtml('revdel-restore'),
241 wfArrayToCGI( array('logid' => $Ids[0] ) ) );
242 } else {
243 foreach( $Ids as $n => $id ) {
244 $revert .= $this->skin->makeKnownLinkObj( $revdel, '#'.($n+1),
245 wfArrayToCGI( array('logid' => $id ) ) );
246 }
247 }
248 $revert = "($revert)";
249 } else {
250 wfRunHooks( 'LogLine', array( $row->log_type, $row->log_action, $title, $paramArray,
251 &$comment, &$revert, $row->log_timestamp ) );
252 // wfDebug( "Invoked LogLine hook for " $row->log_type . ", " . $row->log_action . "\n" );
253 // Do nothing. The implementation is handled by the hook modifiying the passed-by-ref parameters.
254 }
255 }
256 // Event description
257 if( $row->log_deleted & LogPage::DELETED_ACTION ) {
258 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
259 } else {
260 $action = LogPage::actionText( $row->log_type, $row->log_action, $title, $this->skin, $paramArray, true );
261 }
262
263 return "<li>$del$time $userLink $action $comment $revert</li>\n";
264 }
265
266 /**
267 * @param Row $row
268 * @private
269 */
270 private function showhideLinks( $row ) {
271 global $wgAllowLogDeletion;
272
273 if( !$wgAllowLogDeletion )
274 return "";
275
276 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
277 // If event was hidden from sysops
278 if( !self::userCan( $row, LogPage::DELETED_RESTRICTED ) ) {
279 $del = $this->message['rev-delundel'];
280 } else if( $row->log_type == 'suppress' ) {
281 // No one should be hiding from the oversight log
282 $del = $this->message['rev-delundel'];
283 } else {
284 $del = $this->skin->makeKnownLinkObj( $revdel, $this->message['rev-delundel'], 'logid='.$row->log_id );
285 // Bolden oversighted content
286 if( self::isDeleted( $row, LogPage::DELETED_RESTRICTED ) )
287 $del = "<strong>$del</strong>";
288 }
289 return "<tt>(<small>$del</small>)</tt>";
290 }
291
292 /**
293 * SQL clause to skip forbidden log types for this user
294 * @param Database $db
295 * @returns mixed (string or false)
296 */
297 public static function getExcludeClause( $db ) {
298 global $wgLogRestrictions, $wgUser;
299 // Reset the array, clears extra "where" clauses when $par is used
300 $hiddenLogs = array();
301 // Don't show private logs to unpriviledged users
302 foreach( $wgLogRestrictions as $logtype => $right ) {
303 if( !$wgUser->isAllowed($right) ) {
304 $safetype = $db->strencode( $logtype );
305 $hiddenLogs[] = "'$safetype'";
306 }
307 }
308 if( !empty($hiddenLogs) ) {
309 return 'log_type NOT IN(' . implode(',',$hiddenLogs) . ')';
310 }
311 return false;
312 }
313
314 /**
315 * Determine if the current user is allowed to view a particular
316 * field of this log row, if it's marked as deleted.
317 * @param Row $row
318 * @param int $field
319 * @return bool
320 */
321 public static function userCan( $row, $field ) {
322 if( ( $row->log_deleted & $field ) == $field ) {
323 global $wgUser;
324 $permission = ( $row->log_deleted & LogPage::DELETED_RESTRICTED ) == LogPage::DELETED_RESTRICTED
325 ? 'hiderevision'
326 : 'deleterevision';
327 wfDebug( "Checking for $permission due to $field match on $row->log_deleted\n" );
328 return $wgUser->isAllowed( $permission );
329 } else {
330 return true;
331 }
332 }
333
334 /**
335 * @param Row $row
336 * @param int $field one of DELETED_* bitfield constants
337 * @return bool
338 */
339 public static function isDeleted( $row, $field ) {
340 return ($row->log_deleted & $field) == $field;
341 }
342 }
343
344 /**
345 * @addtogroup Pager
346 */
347 class LogPager extends ReverseChronologicalPager {
348 private $type = '', $user = '', $title = '', $pattern = '';
349 public $mLogEventsList;
350 /**
351 * constructor
352 * @param LogEventsList $loglist,
353 * @param string $type,
354 * @param string $user,
355 * @param string $page,
356 * @param string $pattern
357 * @param array $conds
358 */
359 function __construct( $loglist, $type, $user, $title, $pattern, $conds = array() ) {
360 parent::__construct();
361 $this->mConds = $conds;
362
363 $this->mLogEventsList = $loglist;
364
365 $this->limitType( $type );
366 $this->limitUser( $user );
367 $this->limitTitle( $title, $pattern );
368 }
369
370 /**
371 * Set the log reader to return only entries of the given type.
372 * Type restrictions enforced here
373 * @param string $type A log type ('upload', 'delete', etc)
374 * @private
375 */
376 private function limitType( $type ) {
377 global $wgLogRestrictions, $wgUser;
378 // Don't even show header for private logs; don't recognize it...
379 if( isset($wgLogRestrictions[$type]) && !$wgUser->isAllowed($wgLogRestrictions[$type]) ) {
380 $type = '';
381 }
382 // Don't show private logs to unpriviledged users
383 $hideLogs = LogEventsList::getExcludeClause( $this->mDb );
384 if( $hideLogs !== false ) {
385 $this->mConds[] = $hideLogs;
386 }
387 if( empty($type) ) {
388 return false;
389 }
390 $this->type = $type;
391 $this->mConds['log_type'] = $type;
392 }
393
394 /**
395 * Set the log reader to return only entries by the given user.
396 * @param string $name (In)valid user name
397 * @private
398 */
399 function limitUser( $name ) {
400 if( $name == '' ) {
401 return false;
402 }
403 $usertitle = Title::makeTitleSafe( NS_USER, $name );
404 if( is_null($usertitle) ) {
405 return false;
406 }
407 $this->user = $usertitle->getText();
408 /* Fetch userid at first, if known, provides awesome query plan afterwards */
409 $userid = User::idFromName( $this->user );
410 if( !$userid ) {
411 /* It should be nicer to abort query at all,
412 but for now it won't pass anywhere behind the optimizer */
413 $this->mConds[] = "NULL";
414 } else {
415 $this->mConds['log_user'] = $userid;
416 }
417 }
418
419 /**
420 * Set the log reader to return only entries affecting the given page.
421 * (For the block and rights logs, this is a user page.)
422 * @param string $page Title name as text
423 * @private
424 */
425 function limitTitle( $page, $pattern ) {
426 global $wgMiserMode;
427
428 $title = Title::newFromText( $page );
429 if( strlen($page) == 0 || !$title instanceof Title )
430 return false;
431
432 $this->title = $title->getPrefixedText();
433 $this->pattern = $pattern;
434 $ns = $title->getNamespace();
435 if( $pattern && !$wgMiserMode ) {
436 # use escapeLike to avoid expensive search patterns like 't%st%'
437 $safetitle = $this->mDb->escapeLike( $title->getDBkey() );
438 $this->mConds['log_namespace'] = $ns;
439 $this->mConds[] = "log_title LIKE '$safetitle%'";
440 } else {
441 $this->mConds['log_namespace'] = $ns;
442 $this->mConds['log_title'] = $title->getDBkey();
443 }
444 }
445
446 function getQueryInfo() {
447 $this->mConds[] = 'user_id = log_user';
448 # Hack this until live
449 global $wgAllowLogDeletion;
450 $log_id = $wgAllowLogDeletion ? 'log_id' : '0 AS log_id';
451 return array(
452 'tables' => array( 'logging', 'user' ),
453 'fields' => array( 'log_type', 'log_action', 'log_user', 'log_namespace', 'log_title',
454 'log_params', 'log_comment', $log_id, 'log_deleted', 'log_timestamp', 'user_name' ),
455 'conds' => $this->mConds,
456 'options' => array()
457 );
458 }
459
460 function getIndexField() {
461 return 'log_timestamp';
462 }
463
464 function formatRow( $row ) {
465 return $this->mLogEventsList->logLine( $row );
466 }
467
468 public function getType() {
469 return $this->type;
470 }
471
472 public function getUser() {
473 return $this->user;
474 }
475
476 public function getPage() {
477 return $this->title;
478 }
479
480 public function getPattern() {
481 return $this->pattern;
482 }
483 }
484
485 /**
486 *
487 * @addtogroup SpecialPage
488 */
489 class LogReader {
490 var $db, $joinClauses, $whereClauses;
491 var $type = '', $user = '', $title = null, $pattern = false;
492 /**
493 * @param WebRequest $request For internal use use a FauxRequest object to pass arbitrary parameters.
494 */
495 function __construct( $request ) {
496 global $wgUser;
497 # Get parameters
498 $type = $request->getVal( 'type' );
499 $user = $request->getText( 'user' );
500 $title = $request->getText( 'page' );
501 $pattern = $request->getBool( 'pattern' );
502
503 $loglist = new LogEventsList( $wgUser->getSkin() );
504 $this->pager = new LogPager( $loglist, $type, $user, $title, $pattern );
505 }
506
507 /**
508 * Is there at least one row?
509 * @return bool
510 */
511 public function hasRows() {
512 return isset($this->pager) ? ($this->pager->getNumRows() > 0) : false;
513 }
514 }
515
516 /**
517 *
518 * @addtogroup SpecialPage
519 */
520 class LogViewer {
521 const NO_ACTION_LINK = 1;
522 /**
523 * @var LogReader $reader
524 */
525 var $reader;
526 var $numResults = 0;
527 var $flags = 0;
528
529 /**
530 * @param LogReader &$reader where to get our data from
531 * @param integer $flags Bitwise combination of flags:
532 * LogEventsList::NO_ACTION_LINK Don't show restore/unblock/block links
533 */
534 function __construct( &$reader, $flags = 0 ) {
535 global $wgUser;
536 $this->skin = $wgUser->getSkin();
537 $this->reader =& $reader;
538 $this->reader->pager->mLogEventsList->flags = $flags;
539 # Aliases for shorter code...
540 $this->pager =& $this->reader->pager;
541 $this->logEventsList =& $this->reader->pager->mLogEventsList;
542 }
543
544 /**
545 * Take over the whole output page in $wgOut with the log display.
546 */
547 public function show() {
548 global $wgOut;
549 # Set title and add header
550 $this->logEventsList->showHeader( $wgOut, $pager->getType() );
551 # Show form options
552 $this->logEventsList->showOptions( $wgOut, $this->pager->getType(), $this->pager->getUser(),
553 $this->pager->getPage(), $this->pager->getPattern() );
554 # Insert list
555 $logBody = $this->pager->getBody();
556 if( $logBody ) {
557 $wgOut->addHTML(
558 $this->pager->getNavigationBar() .
559 $this->logEventsList->beginLogEventsList() .
560 $logBody .
561 $this->logEventsList->endLogEventsList() .
562 $this->pager->getNavigationBar()
563 );
564 } else {
565 $wgOut->addWikiMsg( 'logempty' );
566 }
567 }
568
569 /**
570 * Output just the list of entries given by the linked LogReader,
571 * with extraneous UI elements. Use for displaying log fragments in
572 * another page (eg at Special:Undelete)
573 * @param OutputPage $out where to send output
574 */
575 public function showList( &$out ) {
576 $logBody = $this->pager->getBody();
577 if( $logBody ) {
578 $out->addHTML(
579 $this->logEventsList->beginLogEventsList() .
580 $logBody .
581 $this->logEventsList->endLogEventsList()
582 );
583 } else {
584 $out->addWikiMsg( 'logempty' );
585 }
586 }
587 }
588