Fix mediawiki.ui.checkbox loading in mobile
[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 $canViewSuppressedOnly = $user->isAllowed( 'viewsuppressed' ) &&
359 !$user->isAllowed( 'suppressrevision' );
360 $entryIsSuppressed = self::isDeleted( $row, LogPage::DELETED_RESTRICTED );
361 $canViewThisSuppressedEntry = $canViewSuppressedOnly && $entryIsSuppressed;
362 if ( $row->log_deleted || $canHide ) {
363 // Show checkboxes instead of links.
364 if ( $canHide && $this->flags & self::USE_REVDEL_CHECKBOXES && !$canViewThisSuppressedEntry ) {
365 // If event was hidden from sysops
366 if ( !self::userCan( $row, LogPage::DELETED_RESTRICTED, $user ) ) {
367 $del = Xml::check( 'deleterevisions', false, array( 'disabled' => 'disabled' ) );
368 } else {
369 $del = Xml::check(
370 'showhiderevisions',
371 false,
372 array( 'name' => 'ids[' . $row->log_id . ']' )
373 );
374 }
375 } else {
376 // If event was hidden from sysops
377 if ( !self::userCan( $row, LogPage::DELETED_RESTRICTED, $user ) ) {
378 $del = Linker::revDeleteLinkDisabled( $canHide );
379 } else {
380 $query = array(
381 'target' => SpecialPage::getTitleFor( 'Log', $row->log_type )->getPrefixedDBkey(),
382 'type' => 'logging',
383 'ids' => $row->log_id,
384 );
385 $del = Linker::revDeleteLink(
386 $query,
387 $entryIsSuppressed,
388 $canHide && !$canViewThisSuppressedEntry
389 );
390 }
391 }
392 }
393 }
394
395 return $del;
396 }
397
398 /**
399 * @param stdClass $row Row
400 * @param string|array $type
401 * @param string|array $action
402 * @param string $right
403 * @return bool
404 */
405 public static function typeAction( $row, $type, $action, $right = '' ) {
406 $match = is_array( $type ) ?
407 in_array( $row->log_type, $type ) : $row->log_type == $type;
408 if ( $match ) {
409 $match = is_array( $action ) ?
410 in_array( $row->log_action, $action ) : $row->log_action == $action;
411 if ( $match && $right ) {
412 global $wgUser;
413 $match = $wgUser->isAllowed( $right );
414 }
415 }
416
417 return $match;
418 }
419
420 /**
421 * Determine if the current user is allowed to view a particular
422 * field of this log row, if it's marked as deleted.
423 *
424 * @param stdClass $row Row
425 * @param int $field
426 * @param User $user User to check, or null to use $wgUser
427 * @return bool
428 */
429 public static function userCan( $row, $field, User $user = null ) {
430 return self::userCanBitfield( $row->log_deleted, $field, $user );
431 }
432
433 /**
434 * Determine if the current user is allowed to view a particular
435 * field of this log row, if it's marked as deleted.
436 *
437 * @param int $bitfield Current field
438 * @param int $field
439 * @param User $user User to check, or null to use $wgUser
440 * @return bool
441 */
442 public static function userCanBitfield( $bitfield, $field, User $user = null ) {
443 if ( $bitfield & $field ) {
444 if ( $user === null ) {
445 global $wgUser;
446 $user = $wgUser;
447 }
448 if ( $bitfield & LogPage::DELETED_RESTRICTED ) {
449 $permissions = array( 'suppressrevision', 'viewsuppressed' );
450 } else {
451 $permissions = array( 'deletedhistory' );
452 }
453 $permissionlist = implode( ', ', $permissions );
454 wfDebug( "Checking for $permissionlist due to $field match on $bitfield\n" );
455 return call_user_func_array( array( $user, 'isAllowedAny' ), $permissions );
456 }
457 return true;
458 }
459
460 /**
461 * @param stdClass $row Row
462 * @param int $field One of DELETED_* bitfield constants
463 * @return bool
464 */
465 public static function isDeleted( $row, $field ) {
466 return ( $row->log_deleted & $field ) == $field;
467 }
468
469 /**
470 * Show log extract. Either with text and a box (set $msgKey) or without (don't set $msgKey)
471 *
472 * @param OutputPage|string $out By-reference
473 * @param string|array $types Log types to show
474 * @param string|Title $page The page title to show log entries for
475 * @param string $user The user who made the log entries
476 * @param array $param Associative Array with the following additional options:
477 * - lim Integer Limit of items to show, default is 50
478 * - conds Array Extra conditions for the query (e.g. "log_action != 'revision'")
479 * - showIfEmpty boolean Set to false if you don't want any output in case the loglist is empty
480 * if set to true (default), "No matching items in log" is displayed if loglist is empty
481 * - msgKey Array If you want a nice box with a message, set this to the key of the message.
482 * First element is the message key, additional optional elements are parameters for the key
483 * that are processed with wfMessage
484 * - offset Set to overwrite offset parameter in WebRequest
485 * set to '' to unset offset
486 * - wrap String Wrap the message in html (usually something like "<div ...>$1</div>").
487 * - flags Integer display flags (NO_ACTION_LINK,NO_EXTRA_USER_LINKS)
488 * - useRequestParams boolean Set true to use Pager-related parameters in the WebRequest
489 * - useMaster boolean Use master DB
490 * @return int Number of total log items (not limited by $lim)
491 */
492 public static function showLogExtract(
493 &$out, $types = array(), $page = '', $user = '', $param = array()
494 ) {
495 $defaultParameters = array(
496 'lim' => 25,
497 'conds' => array(),
498 'showIfEmpty' => true,
499 'msgKey' => array( '' ),
500 'wrap' => "$1",
501 'flags' => 0,
502 'useRequestParams' => false,
503 'useMaster' => false,
504 );
505 # The + operator appends elements of remaining keys from the right
506 # handed array to the left handed, whereas duplicated keys are NOT overwritten.
507 $param += $defaultParameters;
508 # Convert $param array to individual variables
509 $lim = $param['lim'];
510 $conds = $param['conds'];
511 $showIfEmpty = $param['showIfEmpty'];
512 $msgKey = $param['msgKey'];
513 $wrap = $param['wrap'];
514 $flags = $param['flags'];
515 $useRequestParams = $param['useRequestParams'];
516 if ( !is_array( $msgKey ) ) {
517 $msgKey = array( $msgKey );
518 }
519
520 if ( $out instanceof OutputPage ) {
521 $context = $out->getContext();
522 } else {
523 $context = RequestContext::getMain();
524 }
525
526 # Insert list of top 50 (or top $lim) items
527 $loglist = new LogEventsList( $context, null, $flags );
528 $pager = new LogPager( $loglist, $types, $user, $page, '', $conds );
529 if ( !$useRequestParams ) {
530 # Reset vars that may have been taken from the request
531 $pager->mLimit = 50;
532 $pager->mDefaultLimit = 50;
533 $pager->mOffset = "";
534 $pager->mIsBackwards = false;
535 }
536
537 if ( $param['useMaster'] ) {
538 $pager->mDb = wfGetDB( DB_MASTER );
539 }
540 if ( isset( $param['offset'] ) ) { # Tell pager to ignore WebRequest offset
541 $pager->setOffset( $param['offset'] );
542 }
543
544 if ( $lim > 0 ) {
545 $pager->mLimit = $lim;
546 }
547
548 $logBody = $pager->getBody();
549 $s = '';
550
551 if ( $logBody ) {
552 if ( $msgKey[0] ) {
553 $dir = $context->getLanguage()->getDir();
554 $lang = $context->getLanguage()->getCode();
555
556 $s = Xml::openElement( 'div', array(
557 'class' => "mw-warning-with-logexcerpt mw-content-$dir",
558 'dir' => $dir,
559 'lang' => $lang,
560 ) );
561
562 if ( count( $msgKey ) == 1 ) {
563 $s .= $context->msg( $msgKey[0] )->parseAsBlock();
564 } else { // Process additional arguments
565 $args = $msgKey;
566 array_shift( $args );
567 $s .= $context->msg( $msgKey[0], $args )->parseAsBlock();
568 }
569 }
570 $s .= $loglist->beginLogEventsList() .
571 $logBody .
572 $loglist->endLogEventsList();
573 } elseif ( $showIfEmpty ) {
574 $s = Html::rawElement( 'div', array( 'class' => 'mw-warning-logempty' ),
575 $context->msg( 'logempty' )->parse() );
576 }
577
578 if ( $pager->getNumRows() > $pager->mLimit ) { # Show "Full log" link
579 $urlParam = array();
580 if ( $page instanceof Title ) {
581 $urlParam['page'] = $page->getPrefixedDBkey();
582 } elseif ( $page != '' ) {
583 $urlParam['page'] = $page;
584 }
585
586 if ( $user != '' ) {
587 $urlParam['user'] = $user;
588 }
589
590 if ( !is_array( $types ) ) { # Make it an array, if it isn't
591 $types = array( $types );
592 }
593
594 # If there is exactly one log type, we can link to Special:Log?type=foo
595 if ( count( $types ) == 1 ) {
596 $urlParam['type'] = $types[0];
597 }
598
599 $s .= Linker::link(
600 SpecialPage::getTitleFor( 'Log' ),
601 $context->msg( 'log-fulllog' )->escaped(),
602 array(),
603 $urlParam
604 );
605 }
606
607 if ( $logBody && $msgKey[0] ) {
608 $s .= '</div>';
609 }
610
611 if ( $wrap != '' ) { // Wrap message in html
612 $s = str_replace( '$1', $s, $wrap );
613 }
614
615 /* hook can return false, if we don't want the message to be emitted (Wikia BugId:7093) */
616 if ( wfRunHooks( 'LogEventsListShowLogExtract', array( &$s, $types, $page, $user, $param ) ) ) {
617 // $out can be either an OutputPage object or a String-by-reference
618 if ( $out instanceof OutputPage ) {
619 $out->addHTML( $s );
620 } else {
621 $out = $s;
622 }
623 }
624
625 return $pager->getNumRows();
626 }
627
628 /**
629 * SQL clause to skip forbidden log types for this user
630 *
631 * @param DatabaseBase $db
632 * @param string $audience Public/user
633 * @param User $user User to check, or null to use $wgUser
634 * @return string|bool String on success, false on failure.
635 */
636 public static function getExcludeClause( $db, $audience = 'public', User $user = null ) {
637 global $wgLogRestrictions;
638
639 if ( $audience != 'public' && $user === null ) {
640 global $wgUser;
641 $user = $wgUser;
642 }
643
644 // Reset the array, clears extra "where" clauses when $par is used
645 $hiddenLogs = array();
646
647 // Don't show private logs to unprivileged users
648 foreach ( $wgLogRestrictions as $logType => $right ) {
649 if ( $audience == 'public' || !$user->isAllowed( $right ) ) {
650 $hiddenLogs[] = $logType;
651 }
652 }
653 if ( count( $hiddenLogs ) == 1 ) {
654 return 'log_type != ' . $db->addQuotes( $hiddenLogs[0] );
655 } elseif ( $hiddenLogs ) {
656 return 'log_type NOT IN (' . $db->makeList( $hiddenLogs ) . ')';
657 }
658
659 return false;
660 }
661 }