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