Merge "Set $wgNoFollowLinks to false iff "Authorized editors only" selected"
[lhc/web/wiklou.git] / includes / logging / LogEventsList.php
1 <?php
2 /**
3 * Contain classes to list log entries
4 *
5 * Copyright © 2004 Brion Vibber <brion@pobox.com>, 2008 Aaron Schulz
6 * http://www.mediawiki.org/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 */
25
26 class LogEventsList extends ContextSource {
27 const NO_ACTION_LINK = 1;
28 const NO_EXTRA_USER_LINKS = 2;
29 const USE_REVDEL_CHECKBOXES = 4;
30
31 public $flags;
32
33 /**
34 * @var Array
35 */
36 protected $mDefaultQuery;
37
38 /**
39 * Constructor.
40 * The first two parameters used to be $skin and $out, but now only a context
41 * is needed, that's why there's a second unused parameter.
42 *
43 * @param $context IContextSource Context to use; formerly it was Skin object.
44 * @param $unused void Unused; used to be an OutputPage object.
45 * @param int $flags flags; can be a combinaison of self::NO_ACTION_LINK,
46 * self::NO_EXTRA_USER_LINKS or self::USE_REVDEL_CHECKBOXES.
47 */
48 public function __construct( $context, $unused = null, $flags = 0 ) {
49 if ( $context instanceof IContextSource ) {
50 $this->setContext( $context );
51 } else {
52 // Old parameters, $context should be a Skin object
53 $this->setContext( $context->getContext() );
54 }
55
56 $this->flags = $flags;
57 }
58
59 /**
60 * Deprecated alias for getTitle(); do not use.
61 *
62 * @deprecated in 1.20; use getTitle() instead.
63 * @return Title object
64 */
65 public function getDisplayTitle() {
66 return $this->getTitle();
67 }
68
69 /**
70 * Set page title and show header for this log type
71 * @param $type Array
72 * @deprecated in 1.19
73 */
74 public function showHeader( $type ) {
75 wfDeprecated( __METHOD__, '1.19' );
76 // If only one log type is used, then show a special message...
77 $headerType = count( $type ) == 1 ? $type[0] : '';
78 $out = $this->getOutput();
79 if ( LogPage::isLogType( $headerType ) ) {
80 $page = new LogPage( $headerType );
81 $out->setPageTitle( $page->getName()->text() );
82 $out->addHTML( $page->getDescription()->parseAsBlock() );
83 } else {
84 $out->addHTML( $this->msg( 'alllogstext' )->parse() );
85 }
86 }
87
88 /**
89 * Show options for the log list
90 *
91 * @param string $types or Array
92 * @param $user String
93 * @param $page String
94 * @param $pattern String
95 * @param $year Integer: year
96 * @param $month Integer: month
97 * @param $filter: array
98 * @param $tagFilter: array?
99 */
100 public function showOptions( $types = array(), $user = '', $page = '', $pattern = '', $year = '',
101 $month = '', $filter = null, $tagFilter = '' ) {
102 global $wgScript, $wgMiserMode;
103
104 $title = SpecialPage::getTitleFor( 'Log' );
105
106 // For B/C, we take strings, but make sure they are converted...
107 $types = ( $types === '' ) ? array() : (array)$types;
108
109 $tagSelector = ChangeTags::buildTagFilterSelector( $tagFilter );
110
111 $html = Html::hidden( 'title', $title->getPrefixedDBkey() );
112
113 // Basic selectors
114 $html .= $this->getTypeMenu( $types ) . "\n";
115 $html .= $this->getUserInput( $user ) . "\n";
116 $html .= $this->getTitleInput( $page ) . "\n";
117 $html .= $this->getExtraInputs( $types ) . "\n";
118
119 // Title pattern, if allowed
120 if ( !$wgMiserMode ) {
121 $html .= $this->getTitlePattern( $pattern ) . "\n";
122 }
123
124 // date menu
125 $html .= Xml::tags( 'p', null, Xml::dateMenu( $year, $month ) );
126
127 // Tag filter
128 if ( $tagSelector ) {
129 $html .= Xml::tags( 'p', null, implode( '&#160;', $tagSelector ) );
130 }
131
132 // Filter links
133 if ( $filter ) {
134 $html .= Xml::tags( 'p', null, $this->getFilterLinks( $filter ) );
135 }
136
137 // Submit button
138 $html .= Xml::submitButton( $this->msg( 'allpagessubmit' )->text() );
139
140 // Fieldset
141 $html = Xml::fieldset( $this->msg( 'log' )->text(), $html );
142
143 // Form wrapping
144 $html = Xml::tags( 'form', array( 'action' => $wgScript, 'method' => 'get' ), $html );
145
146 $this->getOutput()->addHTML( $html );
147 }
148
149 /**
150 * @param $filter Array
151 * @return String: Formatted HTML
152 */
153 private function getFilterLinks( $filter ) {
154 // show/hide links
155 $messages = array( $this->msg( 'show' )->escaped(), $this->msg( 'hide' )->escaped() );
156 // Option value -> message mapping
157 $links = array();
158 $hiddens = ''; // keep track for "go" button
159 foreach ( $filter as $type => $val ) {
160 // Should the below assignment be outside the foreach?
161 // Then it would have to be copied. Not certain what is more expensive.
162 $query = $this->getDefaultQuery();
163 $queryKey = "hide_{$type}_log";
164
165 $hideVal = 1 - intval( $val );
166 $query[$queryKey] = $hideVal;
167
168 $link = Linker::linkKnown(
169 $this->getTitle(),
170 $messages[$hideVal],
171 array(),
172 $query
173 );
174
175 // Message: log-show-hide-patrol
176 $links[$type] = $this->msg( "log-show-hide-{$type}" )->rawParams( $link )->escaped();
177 $hiddens .= Html::hidden( "hide_{$type}_log", $val ) . "\n";
178 }
179
180 // Build links
181 return '<small>' . $this->getLanguage()->pipeList( $links ) . '</small>' . $hiddens;
182 }
183
184 private function getDefaultQuery() {
185 if ( !isset( $this->mDefaultQuery ) ) {
186 $this->mDefaultQuery = $this->getRequest()->getQueryValues();
187 unset( $this->mDefaultQuery['title'] );
188 unset( $this->mDefaultQuery['dir'] );
189 unset( $this->mDefaultQuery['offset'] );
190 unset( $this->mDefaultQuery['limit'] );
191 unset( $this->mDefaultQuery['order'] );
192 unset( $this->mDefaultQuery['month'] );
193 unset( $this->mDefaultQuery['year'] );
194 }
195
196 return $this->mDefaultQuery;
197 }
198
199 /**
200 * @param $queryTypes Array
201 * @return String: Formatted HTML
202 */
203 private function getTypeMenu( $queryTypes ) {
204 $queryType = count( $queryTypes ) == 1 ? $queryTypes[0] : '';
205 $selector = $this->getTypeSelector();
206 $selector->setDefault( $queryType );
207
208 return $selector->getHtml();
209 }
210
211 /**
212 * Returns log page selector.
213 * @return XmlSelect
214 * @since 1.19
215 */
216 public function getTypeSelector() {
217 $typesByName = array(); // Temporary array
218 // First pass to load the log names
219 foreach ( LogPage::validTypes() as $type ) {
220 $page = new LogPage( $type );
221 $restriction = $page->getRestriction();
222 if ( $this->getUser()->isAllowed( $restriction ) ) {
223 $typesByName[$type] = $page->getName()->text();
224 }
225 }
226
227 // Second pass to sort by name
228 asort( $typesByName );
229
230 // Always put "All public logs" on top
231 $public = $typesByName[''];
232 unset( $typesByName[''] );
233 $typesByName = array( '' => $public ) + $typesByName;
234
235 $select = new XmlSelect( 'type' );
236 foreach ( $typesByName as $type => $name ) {
237 $select->addOption( $name, $type );
238 }
239
240 return $select;
241 }
242
243 /**
244 * @param $user String
245 * @return String: Formatted HTML
246 */
247 private function getUserInput( $user ) {
248 $label = Xml::inputLabel(
249 $this->msg( 'specialloguserlabel' )->text(),
250 'user',
251 'mw-log-user',
252 15,
253 $user
254 );
255
256 return '<span style="white-space: nowrap">' . $label . '</span>';
257 }
258
259 /**
260 * @param $title String
261 * @return String: Formatted HTML
262 */
263 private function getTitleInput( $title ) {
264 $label = Xml::inputLabel(
265 $this->msg( 'speciallogtitlelabel' )->text(),
266 'page',
267 'mw-log-page',
268 20,
269 $title
270 );
271
272 return '<span style="white-space: nowrap">' . $label . '</span>';
273 }
274
275 /**
276 * @param $pattern
277 * @return string Checkbox
278 */
279 private function getTitlePattern( $pattern ) {
280 return '<span style="white-space: nowrap">' .
281 Xml::checkLabel( $this->msg( 'log-title-wildcard' )->text(), 'pattern', 'pattern', $pattern ) .
282 '</span>';
283 }
284
285 /**
286 * @param $types
287 * @return string
288 */
289 private function getExtraInputs( $types ) {
290 $offender = $this->getRequest()->getVal( 'offender' );
291 $user = User::newFromName( $offender, false );
292 if ( !$user || ( $user->getId() == 0 && !IP::isIPAddress( $offender ) ) ) {
293 $offender = ''; // Blank field if invalid
294 }
295 if ( count( $types ) == 1 && $types[0] == 'suppress' ) {
296 return Xml::inputLabel( $this->msg( 'revdelete-offender' )->text(), 'offender',
297 'mw-log-offender', 20, $offender );
298 }
299
300 return '';
301 }
302
303 /**
304 * @return string
305 */
306 public function beginLogEventsList() {
307 return "<ul>\n";
308 }
309
310 /**
311 * @return string
312 */
313 public function endLogEventsList() {
314 return "</ul>\n";
315 }
316
317 /**
318 * @param $row Row: a single row from the result set
319 * @return String: Formatted HTML list item
320 */
321 public function logLine( $row ) {
322 $entry = DatabaseLogEntry::newFromRow( $row );
323 $formatter = LogFormatter::newFromEntry( $entry );
324 $formatter->setContext( $this->getContext() );
325 $formatter->setShowUserToolLinks( !( $this->flags & self::NO_EXTRA_USER_LINKS ) );
326
327 $time = htmlspecialchars( $this->getLanguage()->userTimeAndDate(
328 $entry->getTimestamp(), $this->getUser() ) );
329
330 $action = $formatter->getActionText();
331
332 if ( $this->flags & self::NO_ACTION_LINK ) {
333 $revert = '';
334 } else {
335 $revert = $formatter->getActionLinks();
336 if ( $revert != '' ) {
337 $revert = '<span class="mw-logevent-actionlink">' . $revert . '</span>';
338 }
339 }
340
341 $comment = $formatter->getComment();
342
343 // Some user can hide log items and have review links
344 $del = $this->getShowHideLinks( $row );
345
346 // Any tags...
347 list( $tagDisplay, $newClasses ) = ChangeTags::formatSummaryRow( $row->ts_tags, 'logevent' );
348 $classes = array_merge(
349 array( 'mw-logline-' . $entry->getType() ),
350 $newClasses
351 );
352
353 return Html::rawElement( 'li', array( 'class' => $classes ),
354 "$del $time $action $comment $revert $tagDisplay" ) . "\n";
355 }
356
357 /**
358 * @param $row Row
359 * @return string
360 */
361 private function getShowHideLinks( $row ) {
362 // We don't want to see the links and
363 // no one can hide items from the suppress log.
364 if ( ( $this->flags == self::NO_ACTION_LINK )
365 || $row->log_type == 'suppress'
366 ) {
367 return '';
368 }
369 $del = '';
370 $user = $this->getUser();
371 // Don't show useless checkbox to people who cannot hide log entries
372 if ( $user->isAllowed( 'deletedhistory' ) ) {
373 $canHide = $user->isAllowed( 'deletelogentry' );
374 if ( $row->log_deleted || $canHide ) {
375 // Show checkboxes instead of links.
376 if ( $canHide && $this->flags & self::USE_REVDEL_CHECKBOXES ) {
377 // If event was hidden from sysops
378 if ( !self::userCan( $row, LogPage::DELETED_RESTRICTED, $user ) ) {
379 $del = Xml::check( 'deleterevisions', false, array( 'disabled' => 'disabled' ) );
380 } else {
381 $del = Xml::check(
382 'showhiderevisions',
383 false,
384 array( 'name' => 'ids[' . $row->log_id . ']' )
385 );
386 }
387 } else {
388 // If event was hidden from sysops
389 if ( !self::userCan( $row, LogPage::DELETED_RESTRICTED, $user ) ) {
390 $del = Linker::revDeleteLinkDisabled( $canHide );
391 } else {
392 $query = array(
393 'target' => SpecialPage::getTitleFor( 'Log', $row->log_type )->getPrefixedDBkey(),
394 'type' => 'logging',
395 'ids' => $row->log_id,
396 );
397 $del = Linker::revDeleteLink(
398 $query,
399 self::isDeleted( $row, LogPage::DELETED_RESTRICTED ),
400 $canHide
401 );
402 }
403 }
404 }
405 }
406
407 return $del;
408 }
409
410 /**
411 * @param $row Row
412 * @param $type Mixed: string/array
413 * @param $action Mixed: string/array
414 * @param $right string
415 * @return Boolean
416 */
417 public static function typeAction( $row, $type, $action, $right = '' ) {
418 $match = is_array( $type ) ?
419 in_array( $row->log_type, $type ) : $row->log_type == $type;
420 if ( $match ) {
421 $match = is_array( $action ) ?
422 in_array( $row->log_action, $action ) : $row->log_action == $action;
423 if ( $match && $right ) {
424 global $wgUser;
425 $match = $wgUser->isAllowed( $right );
426 }
427 }
428
429 return $match;
430 }
431
432 /**
433 * Determine if the current user is allowed to view a particular
434 * field of this log row, if it's marked as deleted.
435 *
436 * @param $row Row
437 * @param $field Integer
438 * @param $user User object to check, or null to use $wgUser
439 * @return Boolean
440 */
441 public static function userCan( $row, $field, User $user = null ) {
442 return self::userCanBitfield( $row->log_deleted, $field, $user );
443 }
444
445 /**
446 * Determine if the current user is allowed to view a particular
447 * field of this log row, if it's marked as deleted.
448 *
449 * @param $bitfield Integer (current field)
450 * @param $field Integer
451 * @param $user User object to check, or null to use $wgUser
452 * @return Boolean
453 */
454 public static function userCanBitfield( $bitfield, $field, User $user = null ) {
455 if ( $bitfield & $field ) {
456 if ( $bitfield & LogPage::DELETED_RESTRICTED ) {
457 $permission = 'suppressrevision';
458 } else {
459 $permission = 'deletedhistory';
460 }
461 wfDebug( "Checking for $permission due to $field match on $bitfield\n" );
462 if ( $user === null ) {
463 global $wgUser;
464 $user = $wgUser;
465 }
466
467 return $user->isAllowed( $permission );
468 } else {
469 return true;
470 }
471 }
472
473 /**
474 * @param $row Row
475 * @param $field Integer: one of DELETED_* bitfield constants
476 * @return Boolean
477 */
478 public static function isDeleted( $row, $field ) {
479 return ( $row->log_deleted & $field ) == $field;
480 }
481
482 /**
483 * Show log extract. Either with text and a box (set $msgKey) or without (don't set $msgKey)
484 *
485 * @param $out OutputPage|String-by-reference
486 * @param string|array $types Log types to show
487 * @param string|Title $page The page title to show log entries for
488 * @param string $user The user who made the log entries
489 * @param array $param Associative Array with the following additional options:
490 * - lim Integer Limit of items to show, default is 50
491 * - conds Array Extra conditions for the query (e.g. "log_action != 'revision'")
492 * - showIfEmpty boolean Set to false if you don't want any output in case the loglist is empty
493 * if set to true (default), "No matching items in log" is displayed if loglist is empty
494 * - msgKey Array If you want a nice box with a message, set this to the key of the message.
495 * First element is the message key, additional optional elements are parameters for the key
496 * that are processed with wfMessage
497 * - offset Set to overwrite offset parameter in WebRequest
498 * set to '' to unset offset
499 * - wrap String Wrap the message in html (usually something like "<div ...>$1</div>").
500 * - flags Integer display flags (NO_ACTION_LINK,NO_EXTRA_USER_LINKS)
501 * - useRequestParams boolean Set true to use Pager-related parameters in the WebRequest
502 * @return Integer Number of total log items (not limited by $lim)
503 */
504 public static function showLogExtract(
505 &$out, $types = array(), $page = '', $user = '', $param = array()
506 ) {
507 $defaultParameters = array(
508 'lim' => 25,
509 'conds' => array(),
510 'showIfEmpty' => true,
511 'msgKey' => array( '' ),
512 'wrap' => "$1",
513 'flags' => 0,
514 'useRequestParams' => false,
515 );
516 # The + operator appends elements of remaining keys from the right
517 # handed array to the left handed, whereas duplicated keys are NOT overwritten.
518 $param += $defaultParameters;
519 # Convert $param array to individual variables
520 $lim = $param['lim'];
521 $conds = $param['conds'];
522 $showIfEmpty = $param['showIfEmpty'];
523 $msgKey = $param['msgKey'];
524 $wrap = $param['wrap'];
525 $flags = $param['flags'];
526 $useRequestParams = $param['useRequestParams'];
527 if ( !is_array( $msgKey ) ) {
528 $msgKey = array( $msgKey );
529 }
530
531 if ( $out instanceof OutputPage ) {
532 $context = $out->getContext();
533 } else {
534 $context = RequestContext::getMain();
535 }
536
537 # Insert list of top 50 (or top $lim) items
538 $loglist = new LogEventsList( $context, null, $flags );
539 $pager = new LogPager( $loglist, $types, $user, $page, '', $conds );
540 if ( !$useRequestParams ) {
541 # Reset vars that may have been taken from the request
542 $pager->mLimit = 50;
543 $pager->mDefaultLimit = 50;
544 $pager->mOffset = "";
545 $pager->mIsBackwards = false;
546 }
547 if ( isset( $param['offset'] ) ) { # Tell pager to ignore WebRequest offset
548 $pager->setOffset( $param['offset'] );
549 }
550 if ( $lim > 0 ) {
551 $pager->mLimit = $lim;
552 }
553 $logBody = $pager->getBody();
554 $s = '';
555 if ( $logBody ) {
556 if ( $msgKey[0] ) {
557 $s = '<div class="mw-warning-with-logexcerpt">';
558
559 if ( count( $msgKey ) == 1 ) {
560 $s .= $context->msg( $msgKey[0] )->parseAsBlock();
561 } else { // Process additional arguments
562 $args = $msgKey;
563 array_shift( $args );
564 $s .= $context->msg( $msgKey[0], $args )->parseAsBlock();
565 }
566 }
567 $s .= $loglist->beginLogEventsList() .
568 $logBody .
569 $loglist->endLogEventsList();
570 } elseif ( $showIfEmpty ) {
571 $s = Html::rawElement( 'div', array( 'class' => 'mw-warning-logempty' ),
572 $context->msg( 'logempty' )->parse() );
573 }
574 if ( $pager->getNumRows() > $pager->mLimit ) { # Show "Full log" link
575 $urlParam = array();
576 if ( $page instanceof Title ) {
577 $urlParam['page'] = $page->getPrefixedDBkey();
578 } elseif ( $page != '' ) {
579 $urlParam['page'] = $page;
580 }
581 if ( $user != '' ) {
582 $urlParam['user'] = $user;
583 }
584 if ( !is_array( $types ) ) { # Make it an array, if it isn't
585 $types = array( $types );
586 }
587 # If there is exactly one log type, we can link to Special:Log?type=foo
588 if ( count( $types ) == 1 ) {
589 $urlParam['type'] = $types[0];
590 }
591 $s .= Linker::link(
592 SpecialPage::getTitleFor( 'Log' ),
593 $context->msg( 'log-fulllog' )->escaped(),
594 array(),
595 $urlParam
596 );
597 }
598 if ( $logBody && $msgKey[0] ) {
599 $s .= '</div>';
600 }
601
602 if ( $wrap != '' ) { // Wrap message in html
603 $s = str_replace( '$1', $s, $wrap );
604 }
605
606 /* hook can return false, if we don't want the message to be emitted (Wikia BugId:7093) */
607 if ( wfRunHooks( 'LogEventsListShowLogExtract', array( &$s, $types, $page, $user, $param ) ) ) {
608 // $out can be either an OutputPage object or a String-by-reference
609 if ( $out instanceof OutputPage ) {
610 $out->addHTML( $s );
611 } else {
612 $out = $s;
613 }
614 }
615
616 return $pager->getNumRows();
617 }
618
619 /**
620 * SQL clause to skip forbidden log types for this user
621 *
622 * @param $db DatabaseBase
623 * @param $audience string, public/user
624 * @param $user User object to check, or null to use $wgUser
625 * @return Mixed: string or false
626 */
627 public static function getExcludeClause( $db, $audience = 'public', User $user = null ) {
628 global $wgLogRestrictions;
629
630 if ( $audience != 'public' && $user === null ) {
631 global $wgUser;
632 $user = $wgUser;
633 }
634
635 // Reset the array, clears extra "where" clauses when $par is used
636 $hiddenLogs = array();
637
638 // Don't show private logs to unprivileged users
639 foreach ( $wgLogRestrictions as $logType => $right ) {
640 if ( $audience == 'public' || !$user->isAllowed( $right ) ) {
641 $hiddenLogs[] = $logType;
642 }
643 }
644 if ( count( $hiddenLogs ) == 1 ) {
645 return 'log_type != ' . $db->addQuotes( $hiddenLogs[0] );
646 } elseif ( $hiddenLogs ) {
647 return 'log_type NOT IN (' . $db->makeList( $hiddenLogs ) . ')';
648 }
649
650 return false;
651 }
652 }