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