Revert r32842, r32942 for now.
[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 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 /**
392 * Set the log reader to return only entries of the given type.
393 * Type restrictions enforced here
394 * @param string $type A log type ('upload', 'delete', etc)
395 * @private
396 */
397 private function limitType( $type ) {
398 global $wgLogRestrictions, $wgUser;
399 // Don't even show header for private logs; don't recognize it...
400 if( isset($wgLogRestrictions[$type]) && !$wgUser->isAllowed($wgLogRestrictions[$type]) ) {
401 $type = '';
402 }
403 // Don't show private logs to unpriviledged users
404 $hideLogs = LogEventsList::getExcludeClause( $this->mDb );
405 if( $hideLogs !== false ) {
406 $this->mConds[] = $hideLogs;
407 }
408 if( empty($type) ) {
409 return false;
410 }
411 $this->type = $type;
412 $this->mConds['log_type'] = $type;
413 }
414
415 /**
416 * Set the log reader to return only entries by the given user.
417 * @param string $name (In)valid user name
418 * @private
419 */
420 function limitUser( $name ) {
421 if( $name == '' ) {
422 return false;
423 }
424 $usertitle = Title::makeTitleSafe( NS_USER, $name );
425 if( is_null($usertitle) ) {
426 return false;
427 }
428 $this->user = $usertitle->getText();
429 /* Fetch userid at first, if known, provides awesome query plan afterwards */
430 $userid = User::idFromName( $this->user );
431 if( !$userid ) {
432 /* It should be nicer to abort query at all,
433 but for now it won't pass anywhere behind the optimizer */
434 $this->mConds[] = "NULL";
435 } else {
436 $this->mConds['log_user'] = $userid;
437 }
438 }
439
440 /**
441 * Set the log reader to return only entries affecting the given page.
442 * (For the block and rights logs, this is a user page.)
443 * @param string $page Title name as text
444 * @private
445 */
446 function limitTitle( $page, $pattern ) {
447 global $wgMiserMode;
448
449 $title = Title::newFromText( $page );
450 if( strlen($page) == 0 || !$title instanceof Title )
451 return false;
452
453 $this->title = $title->getPrefixedText();
454 $this->pattern = $pattern;
455 $ns = $title->getNamespace();
456 if( $pattern && !$wgMiserMode ) {
457 # use escapeLike to avoid expensive search patterns like 't%st%'
458 $safetitle = $this->mDb->escapeLike( $title->getDBkey() );
459 $this->mConds['log_namespace'] = $ns;
460 $this->mConds[] = "log_title LIKE '$safetitle%'";
461 } else {
462 $this->mConds['log_namespace'] = $ns;
463 $this->mConds['log_title'] = $title->getDBkey();
464 }
465 }
466
467 function getQueryInfo() {
468 $this->mConds[] = 'user_id = log_user';
469 # Hack this until live
470 global $wgAllowLogDeletion;
471 $log_id = $wgAllowLogDeletion ? 'log_id' : '0 AS log_id';
472 # Don't use the wrong logging index
473 if( $this->title || $this->pattern || $this->user ) {
474 $index = array( 'USE INDEX' => array( 'logging' => array('page_time','user_time') ) );
475 } else if( $this->type ) {
476 $index = array( 'USE INDEX' => array( 'logging' => 'type_time' ) );
477 } else {
478 $index = array( 'USE INDEX' => array( 'logging' => 'times' ) );
479 }
480 return array(
481 'tables' => array( 'logging', 'user' ),
482 'fields' => array( 'log_type', 'log_action', 'log_user', 'log_namespace', 'log_title',
483 'log_params', 'log_comment', $log_id, 'log_deleted', 'log_timestamp', 'user_name' ),
484 'conds' => $this->mConds,
485 'options' => $index
486 );
487 }
488
489 function getIndexField() {
490 return 'log_timestamp';
491 }
492
493 function getStartBody() {
494 wfProfileIn( __METHOD__ );
495 # Do a link batch query
496 $lb = new LinkBatch;
497 while( $row = $this->mResult->fetchObject() ) {
498 $lb->add( $row->log_namespace, $row->log_title );
499 $lb->addObj( Title::makeTitleSafe( NS_USER, $row->user_name ) );
500 $lb->addObj( Title::makeTitleSafe( NS_USER_TALK, $row->user_name ) );
501 }
502 $lb->execute();
503 $this->mResult->seek( 0 );
504 wfProfileOut( __METHOD__ );
505 return '';
506 }
507
508 function formatRow( $row ) {
509 return $this->mLogEventsList->logLine( $row );
510 }
511
512 public function getType() {
513 return $this->type;
514 }
515
516 public function getUser() {
517 return $this->user;
518 }
519
520 public function getPage() {
521 return $this->title;
522 }
523
524 public function getPattern() {
525 return $this->pattern;
526 }
527 }
528
529 /**
530 *
531 * @addtogroup SpecialPage
532 */
533 class LogReader {
534 var $pager;
535 /**
536 * @param WebRequest $request For internal use use a FauxRequest object to pass arbitrary parameters.
537 */
538 function __construct( $request ) {
539 global $wgUser;
540 # Get parameters
541 $type = $request->getVal( 'type' );
542 $user = $request->getText( 'user' );
543 $title = $request->getText( 'page' );
544 $pattern = $request->getBool( 'pattern' );
545
546 $loglist = new LogEventsList( $wgUser->getSkin() );
547 $this->pager = new LogPager( $loglist, $type, $user, $title, $pattern );
548 }
549
550 /**
551 * Is there at least one row?
552 * @return bool
553 */
554 public function hasRows() {
555 return isset($this->pager) ? ($this->pager->getNumRows() > 0) : false;
556 }
557 }
558
559 /**
560 *
561 * @addtogroup SpecialPage
562 */
563 class LogViewer {
564 const NO_ACTION_LINK = 1;
565 /**
566 * @var LogReader $reader
567 */
568 var $reader;
569 /**
570 * @param LogReader &$reader where to get our data from
571 * @param integer $flags Bitwise combination of flags:
572 * LogEventsList::NO_ACTION_LINK Don't show restore/unblock/block links
573 */
574 function __construct( &$reader, $flags = 0 ) {
575 global $wgUser;
576 $this->reader =& $reader;
577 $this->reader->pager->mLogEventsList->flags = $flags;
578 # Aliases for shorter code...
579 $this->pager =& $this->reader->pager;
580 $this->logEventsList =& $this->reader->pager->mLogEventsList;
581 }
582
583 /**
584 * Take over the whole output page in $wgOut with the log display.
585 */
586 public function show() {
587 global $wgOut;
588 # Set title and add header
589 $this->logEventsList->showHeader( $wgOut, $pager->getType() );
590 # Show form options
591 $this->logEventsList->showOptions( $wgOut, $this->pager->getType(), $this->pager->getUser(),
592 $this->pager->getPage(), $this->pager->getPattern() );
593 # Insert list
594 $logBody = $this->pager->getBody();
595 if( $logBody ) {
596 $wgOut->addHTML(
597 $this->pager->getNavigationBar() .
598 $this->logEventsList->beginLogEventsList() .
599 $logBody .
600 $this->logEventsList->endLogEventsList() .
601 $this->pager->getNavigationBar()
602 );
603 } else {
604 $wgOut->addWikiMsg( 'logempty' );
605 }
606 }
607
608 /**
609 * Output just the list of entries given by the linked LogReader,
610 * with extraneous UI elements. Use for displaying log fragments in
611 * another page (eg at Special:Undelete)
612 * @param OutputPage $out where to send output
613 */
614 public function showList( &$out ) {
615 $logBody = $this->pager->getBody();
616 if( $logBody ) {
617 $out->addHTML(
618 $this->logEventsList->beginLogEventsList() .
619 $logBody .
620 $this->logEventsList->endLogEventsList()
621 );
622 } else {
623 $out->addWikiMsg( 'logempty' );
624 }
625 }
626 }
627