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