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