Fixed E_NOTICE when showing old items of delete/revision log, with those items, ...
[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' ) && !empty( $paramArray ) ) {
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' ) ) {
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 */
326 public static function showLogExtract( $out, $type='', $page='', $user='' ) {
327 global $wgUser;
328 # Insert list of top 50 or so items
329 $loglist = new LogEventsList( $wgUser->getSkin() );
330 $pager = new LogPager( $loglist, $type, $user, $page, '' );
331 $logBody = $pager->getBody();
332 if( $logBody ) {
333 $out->addHTML(
334 $loglist->beginLogEventsList() .
335 $logBody .
336 $loglist->endLogEventsList()
337 );
338 } else {
339 $out->addWikiMsg( 'logempty' );
340 }
341 }
342
343 /**
344 * SQL clause to skip forbidden log types for this user
345 * @param Database $db
346 * @returns mixed (string or false)
347 */
348 public static function getExcludeClause( $db ) {
349 global $wgLogRestrictions, $wgUser;
350 // Reset the array, clears extra "where" clauses when $par is used
351 $hiddenLogs = array();
352 // Don't show private logs to unpriviledged users
353 foreach( $wgLogRestrictions as $logtype => $right ) {
354 if( !$wgUser->isAllowed($right) ) {
355 $safetype = $db->strencode( $logtype );
356 $hiddenLogs[] = "'$safetype'";
357 }
358 }
359 if( !empty($hiddenLogs) ) {
360 return 'log_type NOT IN(' . implode(',',$hiddenLogs) . ')';
361 }
362 return false;
363 }
364 }
365
366 /**
367 * @addtogroup Pager
368 */
369 class LogPager extends ReverseChronologicalPager {
370 private $type = '', $user = '', $title = '', $pattern = '';
371 public $mLogEventsList;
372 /**
373 * constructor
374 * @param LogEventsList $loglist,
375 * @param string $type,
376 * @param string $user,
377 * @param string $page,
378 * @param string $pattern
379 * @param array $conds
380 */
381 function __construct( $loglist, $type='', $user='', $title='', $pattern='', $conds = array() ) {
382 parent::__construct();
383 $this->mConds = $conds;
384
385 $this->mLogEventsList = $loglist;
386
387 $this->limitType( $type );
388 $this->limitUser( $user );
389 $this->limitTitle( $title, $pattern );
390 }
391
392 /**
393 * Set the log reader to return only entries of the given type.
394 * Type restrictions enforced here
395 * @param string $type A log type ('upload', 'delete', etc)
396 * @private
397 */
398 private function limitType( $type ) {
399 global $wgLogRestrictions, $wgUser;
400 // Don't even show header for private logs; don't recognize it...
401 if( isset($wgLogRestrictions[$type]) && !$wgUser->isAllowed($wgLogRestrictions[$type]) ) {
402 $type = '';
403 }
404 // Don't show private logs to unpriviledged users
405 $hideLogs = LogEventsList::getExcludeClause( $this->mDb );
406 if( $hideLogs !== false ) {
407 $this->mConds[] = $hideLogs;
408 }
409 if( empty($type) ) {
410 return false;
411 }
412 $this->type = $type;
413 $this->mConds['log_type'] = $type;
414 }
415
416 /**
417 * Set the log reader to return only entries by the given user.
418 * @param string $name (In)valid user name
419 * @private
420 */
421 function limitUser( $name ) {
422 if( $name == '' ) {
423 return false;
424 }
425 $usertitle = Title::makeTitleSafe( NS_USER, $name );
426 if( is_null($usertitle) ) {
427 return false;
428 }
429 $this->user = $usertitle->getText();
430 /* Fetch userid at first, if known, provides awesome query plan afterwards */
431 $userid = User::idFromName( $this->user );
432 if( !$userid ) {
433 /* It should be nicer to abort query at all,
434 but for now it won't pass anywhere behind the optimizer */
435 $this->mConds[] = "NULL";
436 } else {
437 $this->mConds['log_user'] = $userid;
438 }
439 }
440
441 /**
442 * Set the log reader to return only entries affecting the given page.
443 * (For the block and rights logs, this is a user page.)
444 * @param string $page Title name as text
445 * @private
446 */
447 function limitTitle( $page, $pattern ) {
448 global $wgMiserMode;
449
450 $title = Title::newFromText( $page );
451 if( strlen($page) == 0 || !$title instanceof Title )
452 return false;
453
454 $this->title = $title->getPrefixedText();
455 $this->pattern = $pattern;
456 $ns = $title->getNamespace();
457 if( $pattern && !$wgMiserMode ) {
458 # use escapeLike to avoid expensive search patterns like 't%st%'
459 $safetitle = $this->mDb->escapeLike( $title->getDBkey() );
460 $this->mConds['log_namespace'] = $ns;
461 $this->mConds[] = "log_title LIKE '$safetitle%'";
462 } else {
463 $this->mConds['log_namespace'] = $ns;
464 $this->mConds['log_title'] = $title->getDBkey();
465 }
466 }
467
468 function getQueryInfo() {
469 $this->mConds[] = 'user_id = log_user';
470 # Hack this until live
471 global $wgAllowLogDeletion;
472 $log_id = $wgAllowLogDeletion ? 'log_id' : '0 AS log_id';
473 return array(
474 'tables' => array( 'logging', 'user' ),
475 'fields' => array( 'log_type', 'log_action', 'log_user', 'log_namespace', 'log_title',
476 'log_params', 'log_comment', $log_id, 'log_deleted', 'log_timestamp', 'user_name' ),
477 'conds' => $this->mConds,
478 'options' => array()
479 );
480 }
481
482 function getIndexField() {
483 return 'log_timestamp';
484 }
485
486 function formatRow( $row ) {
487 return $this->mLogEventsList->logLine( $row );
488 }
489
490 public function getType() {
491 return $this->type;
492 }
493
494 public function getUser() {
495 return $this->user;
496 }
497
498 public function getPage() {
499 return $this->title;
500 }
501
502 public function getPattern() {
503 return $this->pattern;
504 }
505 }
506
507 /**
508 *
509 * @addtogroup SpecialPage
510 */
511 class LogReader {
512 var $db, $joinClauses, $whereClauses;
513 var $type = '', $user = '', $title = null, $pattern = false;
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 var $numResults = 0;
549 var $flags = 0;
550
551 /**
552 * @param LogReader &$reader where to get our data from
553 * @param integer $flags Bitwise combination of flags:
554 * LogEventsList::NO_ACTION_LINK Don't show restore/unblock/block links
555 */
556 function __construct( &$reader, $flags = 0 ) {
557 global $wgUser;
558 $this->skin = $wgUser->getSkin();
559 $this->reader =& $reader;
560 $this->reader->pager->mLogEventsList->flags = $flags;
561 # Aliases for shorter code...
562 $this->pager =& $this->reader->pager;
563 $this->logEventsList =& $this->reader->pager->mLogEventsList;
564 }
565
566 /**
567 * Take over the whole output page in $wgOut with the log display.
568 */
569 public function show() {
570 global $wgOut;
571 # Set title and add header
572 $this->logEventsList->showHeader( $wgOut, $pager->getType() );
573 # Show form options
574 $this->logEventsList->showOptions( $wgOut, $this->pager->getType(), $this->pager->getUser(),
575 $this->pager->getPage(), $this->pager->getPattern() );
576 # Insert list
577 $logBody = $this->pager->getBody();
578 if( $logBody ) {
579 $wgOut->addHTML(
580 $this->pager->getNavigationBar() .
581 $this->logEventsList->beginLogEventsList() .
582 $logBody .
583 $this->logEventsList->endLogEventsList() .
584 $this->pager->getNavigationBar()
585 );
586 } else {
587 $wgOut->addWikiMsg( 'logempty' );
588 }
589 }
590
591 /**
592 * Output just the list of entries given by the linked LogReader,
593 * with extraneous UI elements. Use for displaying log fragments in
594 * another page (eg at Special:Undelete)
595 * @param OutputPage $out where to send output
596 */
597 public function showList( &$out ) {
598 $logBody = $this->pager->getBody();
599 if( $logBody ) {
600 $out->addHTML(
601 $this->logEventsList->beginLogEventsList() .
602 $logBody .
603 $this->logEventsList->endLogEventsList()
604 );
605 } else {
606 $out->addWikiMsg( 'logempty' );
607 }
608 }
609 }
610