Document and flag some functions
[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 $skin = $wgUser->getSkin();
156 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
157 $time = $wgLang->timeanddate( wfTimestamp(TS_MW, $row->log_timestamp), true );
158 // Enter the existence or non-existence of this page into the link cache,
159 // for faster makeLinkObj() in LogPage::actionText()
160 $linkCache =& LinkCache::singleton();
161 $linkCache->addLinkObj( $title );
162 // User links
163 if( self::isDeleted($row,LogPage::DELETED_USER) ) {
164 $userLink = '<span class="history-deleted">' . wfMsgHtml( 'rev-deleted-user' ) . '</span>';
165 } else {
166 $userLink = $this->skin->userLink( $row->log_user, $row->user_name ) .
167 $this->skin->userToolLinksRedContribs( $row->log_user, $row->user_name );
168 }
169 // Comment
170 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 $this->message['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 $this->message['undeletelink'], 'target='. urlencode( $title->getPrefixedDBkey() ) ) . ')';
198 // Show unblock link
199 } else if( $row->log_action == 'block' && $wgUser->isAllowed( 'block' ) ) {
200 $revert = '(' . $this->skin->makeKnownLinkObj( SpecialPage::getTitleFor( 'Ipblocklist' ),
201 $this->message['unblocklink'],
202 'action=unblock&ip=' . urlencode( $row->log_title ) ) . ')';
203 // Show change protection link
204 } else if( ( $row->log_action == 'protect' || $row->log_action == 'modify' ) && $wgUser->isAllowed( 'protect' ) ) {
205 $revert = '(' . $this->skin->makeKnownLinkObj( $title, $this->message['protect_change'], 'action=unprotect' ) . ')';
206 // Show unmerge link
207 } else if ( $row->log_action == 'merge' ) {
208 $merge = SpecialPage::getTitleFor( 'Mergehistory' );
209 $revert = '(' . $this->skin->makeKnownLinkObj( $merge, $this->message['revertmerge'],
210 wfArrayToCGI(
211 array('target' => $paramArray[0], 'dest' => $title->getPrefixedText(), 'mergepoint' => $paramArray[1] )
212 )
213 ) . ')';
214 // If an edit was hidden from a page give a review link to the history
215 } else if( $row->log_action == 'revision' && $wgUser->isAllowed( 'deleterevision' ) && isset($paramArray[2]) ) {
216 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
217 // Different revision types use different URL params...
218 $subtype = isset($paramArray[2]) ? $paramArray[1] : '';
219 // Link to each hidden object ID, $paramArray[1] is the url param. List if several...
220 $Ids = explode( ',', $paramArray[2] );
221 if( count($Ids) == 1 ) {
222 $revert = $this->skin->makeKnownLinkObj( $revdel, $this->message['revdel-restore'],
223 wfArrayToCGI( array('target' => $paramArray[0], $paramArray[1] => $Ids[0] ) ) );
224 } else {
225 $revert .= $this->message['revdel-restore'].':';
226 foreach( $Ids as $n => $id ) {
227 $revert .= ' '.$this->skin->makeKnownLinkObj( $revdel, '#'.($n+1),
228 wfArrayToCGI( array('target' => $paramArray[0], $paramArray[1] => $id ) ) );
229 }
230 }
231 $revert = "($revert)";
232 // Hidden log items, give review link
233 } else if( $row->log_action == 'event' && $wgUser->isAllowed( 'deleterevision' ) && isset($paramArray[2]) ) {
234 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
235 $revert .= $this->message['revdel-restore'];
236 $Ids = explode( ',', $paramArray[0] );
237 // Link to each hidden object ID, $paramArray[1] is the url param. List if several...
238 if( count($Ids) == 1 ) {
239 $revert = $this->skin->makeKnownLinkObj( $revdel, $this->message['revdel-restore'],
240 wfArrayToCGI( array('logid' => $Ids[0] ) ) );
241 } else {
242 foreach( $Ids as $n => $id ) {
243 $revert .= $this->skin->makeKnownLinkObj( $revdel, '#'.($n+1),
244 wfArrayToCGI( array('logid' => $id ) ) );
245 }
246 }
247 $revert = "($revert)";
248 } else {
249 wfRunHooks( 'LogLine', array( $row->log_type, $row->log_action, $title, $paramArray,
250 &$comment, &$revert, $row->log_timestamp ) );
251 // wfDebug( "Invoked LogLine hook for " $row->log_type . ", " . $row->log_action . "\n" );
252 // Do nothing. The implementation is handled by the hook modifiying the passed-by-ref parameters.
253 }
254 }
255 // Event description
256 if( self::isDeleted($row,LogPage::DELETED_ACTION) ) {
257 $action = '<span class="history-deleted">' . wfMsgHtml('rev-deleted-event') . '</span>';
258 } else {
259 $action = LogPage::actionText( $row->log_type, $row->log_action, $title, $this->skin, $paramArray, true );
260 }
261
262 return "<li>$del$time $userLink $action $comment $revert</li>\n";
263 }
264
265 /**
266 * @param Row $row
267 * @private
268 */
269 private function showhideLinks( $row ) {
270 global $wgAllowLogDeletion;
271
272 if( !$wgAllowLogDeletion )
273 return "";
274
275 $revdel = SpecialPage::getTitleFor( 'Revisiondelete' );
276 // If event was hidden from sysops
277 if( !self::userCan( $row, LogPage::DELETED_RESTRICTED ) ) {
278 $del = $this->message['rev-delundel'];
279 } else if( $row->log_type == 'suppress' ) {
280 // No one should be hiding from the oversight log
281 $del = $this->message['rev-delundel'];
282 } else {
283 $del = $this->skin->makeKnownLinkObj( $revdel, $this->message['rev-delundel'], 'logid='.$row->log_id );
284 // Bolden oversighted content
285 if( self::isDeleted( $row, LogPage::DELETED_RESTRICTED ) )
286 $del = "<strong>$del</strong>";
287 }
288 return "<tt>(<small>$del</small>)</tt>";
289 }
290
291 /**
292 * Determine if the current user is allowed to view a particular
293 * field of this log row, if it's marked as deleted.
294 * @param Row $row
295 * @param int $field
296 * @return bool
297 */
298 public static function userCan( $row, $field ) {
299 if( ( $row->log_deleted & $field ) == $field ) {
300 global $wgUser;
301 $permission = ( $row->log_deleted & LogPage::DELETED_RESTRICTED ) == LogPage::DELETED_RESTRICTED
302 ? 'hiderevision'
303 : 'deleterevision';
304 wfDebug( "Checking for $permission due to $field match on $row->log_deleted\n" );
305 return $wgUser->isAllowed( $permission );
306 } else {
307 return true;
308 }
309 }
310
311 /**
312 * @param Row $row
313 * @param int $field one of DELETED_* bitfield constants
314 * @return bool
315 */
316 public static function isDeleted( $row, $field ) {
317 return ($row->log_deleted & $field) == $field;
318 }
319
320 /**
321 * Quick function to show a short log extract
322 * @param OutputPage $out
323 * @param string $type
324 * @param string $page
325 * @param string $user
326 */
327 public static function showLogExtract( $out, $type='', $page='', $user='' ) {
328 global $wgUser;
329 # Insert list of top 50 or so items
330 $loglist = new LogEventsList( $wgUser->getSkin() );
331 $pager = new LogPager( $loglist, $type, $user, $page, '' );
332 $logBody = $pager->getBody();
333 if( $logBody ) {
334 $out->addHTML(
335 $loglist->beginLogEventsList() .
336 $logBody .
337 $loglist->endLogEventsList()
338 );
339 } else {
340 $out->addWikiMsg( 'logempty' );
341 }
342 }
343
344 /**
345 * SQL clause to skip forbidden log types for this user
346 * @param Database $db
347 * @returns mixed (string or false)
348 */
349 public static function getExcludeClause( $db ) {
350 global $wgLogRestrictions, $wgUser;
351 // Reset the array, clears extra "where" clauses when $par is used
352 $hiddenLogs = array();
353 // Don't show private logs to unpriviledged users
354 foreach( $wgLogRestrictions as $logtype => $right ) {
355 if( !$wgUser->isAllowed($right) ) {
356 $safetype = $db->strencode( $logtype );
357 $hiddenLogs[] = "'$safetype'";
358 }
359 }
360 if( !empty($hiddenLogs) ) {
361 return 'log_type NOT IN(' . implode(',',$hiddenLogs) . ')';
362 }
363 return false;
364 }
365 }
366
367 /**
368 * @addtogroup Pager
369 */
370 class LogPager extends ReverseChronologicalPager {
371 private $type = '', $user = '', $title = '', $pattern = '';
372 public $mLogEventsList;
373 /**
374 * constructor
375 * @param LogEventsList $loglist,
376 * @param string $type,
377 * @param string $user,
378 * @param string $page,
379 * @param string $pattern
380 * @param array $conds
381 */
382 function __construct( $loglist, $type='', $user='', $title='', $pattern='', $conds = array() ) {
383 parent::__construct();
384 $this->mConds = $conds;
385
386 $this->mLogEventsList = $loglist;
387
388 $this->limitType( $type );
389 $this->limitUser( $user );
390 $this->limitTitle( $title, $pattern );
391 }
392
393 /**
394 * Set the log reader to return only entries of the given type.
395 * Type restrictions enforced here
396 * @param string $type A log type ('upload', 'delete', etc)
397 * @private
398 */
399 private function limitType( $type ) {
400 global $wgLogRestrictions, $wgUser;
401 // Don't even show header for private logs; don't recognize it...
402 if( isset($wgLogRestrictions[$type]) && !$wgUser->isAllowed($wgLogRestrictions[$type]) ) {
403 $type = '';
404 }
405 // Don't show private logs to unpriviledged users
406 $hideLogs = LogEventsList::getExcludeClause( $this->mDb );
407 if( $hideLogs !== false ) {
408 $this->mConds[] = $hideLogs;
409 }
410 if( empty($type) ) {
411 return false;
412 }
413 $this->type = $type;
414 $this->mConds['log_type'] = $type;
415 }
416
417 /**
418 * Set the log reader to return only entries by the given user.
419 * @param string $name (In)valid user name
420 * @private
421 */
422 function limitUser( $name ) {
423 if( $name == '' ) {
424 return false;
425 }
426 $usertitle = Title::makeTitleSafe( NS_USER, $name );
427 if( is_null($usertitle) ) {
428 return false;
429 }
430 $this->user = $usertitle->getText();
431 /* Fetch userid at first, if known, provides awesome query plan afterwards */
432 $userid = User::idFromName( $this->user );
433 if( !$userid ) {
434 /* It should be nicer to abort query at all,
435 but for now it won't pass anywhere behind the optimizer */
436 $this->mConds[] = "NULL";
437 } else {
438 $this->mConds['log_user'] = $userid;
439 }
440 }
441
442 /**
443 * Set the log reader to return only entries affecting the given page.
444 * (For the block and rights logs, this is a user page.)
445 * @param string $page Title name as text
446 * @private
447 */
448 function limitTitle( $page, $pattern ) {
449 global $wgMiserMode;
450
451 $title = Title::newFromText( $page );
452 if( strlen($page) == 0 || !$title instanceof Title )
453 return false;
454
455 $this->title = $title->getPrefixedText();
456 $this->pattern = $pattern;
457 $ns = $title->getNamespace();
458 if( $pattern && !$wgMiserMode ) {
459 # use escapeLike to avoid expensive search patterns like 't%st%'
460 $safetitle = $this->mDb->escapeLike( $title->getDBkey() );
461 $this->mConds['log_namespace'] = $ns;
462 $this->mConds[] = "log_title LIKE '$safetitle%'";
463 } else {
464 $this->mConds['log_namespace'] = $ns;
465 $this->mConds['log_title'] = $title->getDBkey();
466 }
467 }
468
469 function getQueryInfo() {
470 $this->mConds[] = 'user_id = log_user';
471 # Hack this until live
472 global $wgAllowLogDeletion;
473 $log_id = $wgAllowLogDeletion ? 'log_id' : '0 AS log_id';
474 return array(
475 'tables' => array( 'logging', 'user' ),
476 'fields' => array( 'log_type', 'log_action', 'log_user', 'log_namespace', 'log_title',
477 'log_params', 'log_comment', $log_id, 'log_deleted', 'log_timestamp', 'user_name' ),
478 'conds' => $this->mConds,
479 'options' => array()
480 );
481 }
482
483 function getIndexField() {
484 return 'log_timestamp';
485 }
486
487 function formatRow( $row ) {
488 return $this->mLogEventsList->logLine( $row );
489 }
490
491 public function getType() {
492 return $this->type;
493 }
494
495 public function getUser() {
496 return $this->user;
497 }
498
499 public function getPage() {
500 return $this->title;
501 }
502
503 public function getPattern() {
504 return $this->pattern;
505 }
506 }
507
508 /**
509 *
510 * @addtogroup SpecialPage
511 */
512 class LogReader {
513 var $pager;
514 /**
515 * @param WebRequest $request For internal use use a FauxRequest object to pass arbitrary parameters.
516 */
517 function __construct( $request ) {
518 global $wgUser;
519 # Get parameters
520 $type = $request->getVal( 'type' );
521 $user = $request->getText( 'user' );
522 $title = $request->getText( 'page' );
523 $pattern = $request->getBool( 'pattern' );
524
525 $loglist = new LogEventsList( $wgUser->getSkin() );
526 $this->pager = new LogPager( $loglist, $type, $user, $title, $pattern );
527 }
528
529 /**
530 * Is there at least one row?
531 * @return bool
532 */
533 public function hasRows() {
534 return isset($this->pager) ? ($this->pager->getNumRows() > 0) : false;
535 }
536 }
537
538 /**
539 *
540 * @addtogroup SpecialPage
541 */
542 class LogViewer {
543 const NO_ACTION_LINK = 1;
544 /**
545 * @var LogReader $reader
546 */
547 var $reader;
548 /**
549 * @param LogReader &$reader where to get our data from
550 * @param integer $flags Bitwise combination of flags:
551 * LogEventsList::NO_ACTION_LINK Don't show restore/unblock/block links
552 */
553 function __construct( &$reader, $flags = 0 ) {
554 global $wgUser;
555 $this->reader =& $reader;
556 $this->reader->pager->mLogEventsList->flags = $flags;
557 # Aliases for shorter code...
558 $this->pager =& $this->reader->pager;
559 $this->logEventsList =& $this->reader->pager->mLogEventsList;
560 }
561
562 /**
563 * Take over the whole output page in $wgOut with the log display.
564 */
565 public function show() {
566 global $wgOut;
567 # Set title and add header
568 $this->logEventsList->showHeader( $wgOut, $pager->getType() );
569 # Show form options
570 $this->logEventsList->showOptions( $wgOut, $this->pager->getType(), $this->pager->getUser(),
571 $this->pager->getPage(), $this->pager->getPattern() );
572 # Insert list
573 $logBody = $this->pager->getBody();
574 if( $logBody ) {
575 $wgOut->addHTML(
576 $this->pager->getNavigationBar() .
577 $this->logEventsList->beginLogEventsList() .
578 $logBody .
579 $this->logEventsList->endLogEventsList() .
580 $this->pager->getNavigationBar()
581 );
582 } else {
583 $wgOut->addWikiMsg( 'logempty' );
584 }
585 }
586
587 /**
588 * Output just the list of entries given by the linked LogReader,
589 * with extraneous UI elements. Use for displaying log fragments in
590 * another page (eg at Special:Undelete)
591 * @param OutputPage $out where to send output
592 */
593 public function showList( &$out ) {
594 $logBody = $this->pager->getBody();
595 if( $logBody ) {
596 $out->addHTML(
597 $this->logEventsList->beginLogEventsList() .
598 $logBody .
599 $this->logEventsList->endLogEventsList()
600 );
601 } else {
602 $out->addWikiMsg( 'logempty' );
603 }
604 }
605 }
606