2017-12-16 00:26:25 +00:00
|
|
|
<?php
|
|
|
|
|
2020-09-18 14:49:13 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
|
2018-04-29 17:52:45 +00:00
|
|
|
use MediaWiki\Linker\LinkRenderer;
|
2020-01-08 13:33:10 +00:00
|
|
|
use Wikimedia\AtEase\AtEase;
|
2018-04-29 17:52:45 +00:00
|
|
|
|
2017-12-16 00:26:25 +00:00
|
|
|
/**
|
|
|
|
* Class to build paginated filter list
|
|
|
|
*/
|
|
|
|
class AbuseFilterPager extends TablePager {
|
|
|
|
|
2018-11-08 14:34:32 +00:00
|
|
|
/**
|
|
|
|
* @var AbuseFilterViewList The associated page
|
|
|
|
*/
|
|
|
|
public $mPage;
|
|
|
|
/**
|
|
|
|
* @var array Query WHERE conditions
|
|
|
|
*/
|
|
|
|
public $mConds;
|
|
|
|
/**
|
2020-10-01 11:31:04 +00:00
|
|
|
* @var string|null The pattern being searched
|
2018-11-08 14:34:32 +00:00
|
|
|
*/
|
2019-10-30 12:36:19 +00:00
|
|
|
private $mSearchPattern;
|
|
|
|
/**
|
2020-10-01 11:31:04 +00:00
|
|
|
* @var string|null The pattern search mode (LIKE, RLIKE or IRLIKE)
|
2019-10-30 12:36:19 +00:00
|
|
|
*/
|
|
|
|
private $mSearchMode;
|
2018-04-29 17:52:45 +00:00
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
/**
|
|
|
|
* @param AbuseFilterViewList $page
|
|
|
|
* @param array $conds
|
|
|
|
* @param LinkRenderer $linkRenderer
|
2020-10-01 11:31:04 +00:00
|
|
|
* @param ?string $searchPattern Null if no pattern was specified
|
|
|
|
* @param ?string $searchMode
|
2018-04-04 21:14:25 +00:00
|
|
|
*/
|
2019-10-30 12:36:19 +00:00
|
|
|
public function __construct(
|
|
|
|
AbuseFilterViewList $page,
|
|
|
|
$conds,
|
|
|
|
LinkRenderer $linkRenderer,
|
2020-10-01 11:31:04 +00:00
|
|
|
?string $searchPattern,
|
|
|
|
?string $searchMode
|
2019-10-30 12:36:19 +00:00
|
|
|
) {
|
2020-10-01 11:31:04 +00:00
|
|
|
parent::__construct( $page->getContext(), $linkRenderer );
|
2017-12-16 00:26:25 +00:00
|
|
|
$this->mPage = $page;
|
|
|
|
$this->mConds = $conds;
|
2019-10-30 12:36:19 +00:00
|
|
|
$this->mSearchPattern = $searchPattern;
|
|
|
|
$this->mSearchMode = $searchMode;
|
2017-12-16 00:26:25 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getQueryInfo() {
|
2017-12-16 00:26:25 +00:00
|
|
|
return [
|
|
|
|
'tables' => [ 'abuse_filter' ],
|
|
|
|
'fields' => [
|
2018-05-03 18:32:03 +00:00
|
|
|
// All columns but af_comments
|
2017-12-16 00:26:25 +00:00
|
|
|
'af_id',
|
|
|
|
'af_enabled',
|
|
|
|
'af_deleted',
|
2018-02-17 14:09:36 +00:00
|
|
|
'af_pattern',
|
2017-12-16 00:26:25 +00:00
|
|
|
'af_global',
|
|
|
|
'af_public_comments',
|
|
|
|
'af_hidden',
|
|
|
|
'af_hit_count',
|
|
|
|
'af_timestamp',
|
|
|
|
'af_user_text',
|
|
|
|
'af_user',
|
|
|
|
'af_actions',
|
|
|
|
'af_group',
|
2018-05-03 18:32:03 +00:00
|
|
|
'af_throttled'
|
2017-12-16 00:26:25 +00:00
|
|
|
],
|
|
|
|
'conds' => $this->mConds,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-10-30 12:36:19 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* This is the same as the parent implementation if no search pattern was specified.
|
|
|
|
* Otherwise, it does a query with no limit and then slices the results à la ContribsPager.
|
|
|
|
*/
|
|
|
|
public function reallyDoQuery( $offset, $limit, $order ) {
|
2020-10-01 11:31:04 +00:00
|
|
|
if ( $this->mSearchMode === null ) {
|
2019-10-30 12:36:19 +00:00
|
|
|
return parent::reallyDoQuery( $offset, $limit, $order );
|
|
|
|
}
|
|
|
|
|
|
|
|
list( $tables, $fields, $conds, $fname, $options, $join_conds ) =
|
|
|
|
$this->buildQueryInfo( $offset, $limit, $order );
|
|
|
|
|
|
|
|
unset( $options['LIMIT'] );
|
|
|
|
$res = $this->mDb->select( $tables, $fields, $conds, $fname, $options, $join_conds );
|
|
|
|
|
|
|
|
$filtered = [];
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
if ( $this->matchesPattern( $row->af_pattern ) ) {
|
|
|
|
$filtered[ $row->af_id ] = $row;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort results and enforce limit like ContribsPager
|
|
|
|
if ( $order === self::QUERY_ASCENDING ) {
|
|
|
|
ksort( $filtered );
|
|
|
|
} else {
|
|
|
|
krsort( $filtered );
|
|
|
|
}
|
|
|
|
$filtered = array_slice( $filtered, 0, $limit );
|
|
|
|
$filtered = array_values( $filtered );
|
|
|
|
return new FakeResultWrapper( $filtered );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether $subject matches the given $pattern.
|
|
|
|
*
|
|
|
|
* @param string $subject
|
|
|
|
* @return bool
|
|
|
|
* @throws LogicException
|
|
|
|
*/
|
|
|
|
private function matchesPattern( $subject ) {
|
|
|
|
$pattern = $this->mSearchPattern;
|
|
|
|
switch ( $this->mSearchMode ) {
|
|
|
|
case 'RLIKE':
|
|
|
|
return (bool)preg_match( "/$pattern/u", $subject );
|
|
|
|
case 'IRLIKE':
|
|
|
|
return (bool)preg_match( "/$pattern/ui", $subject );
|
|
|
|
case 'LIKE':
|
|
|
|
return mb_stripos( $subject, $pattern ) !== false;
|
|
|
|
default:
|
|
|
|
throw new LogicException( "Unknown search type {$this->mSearchMode}" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
/**
|
2020-10-01 11:31:04 +00:00
|
|
|
* Note: this method is called by parent::__construct
|
2018-04-04 21:14:25 +00:00
|
|
|
* @see Pager::getFieldNames()
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getFieldNames() {
|
2020-09-18 14:49:13 +00:00
|
|
|
$afPermManager = AbuseFilterServices::getPermissionManager();
|
|
|
|
|
2017-12-16 00:26:25 +00:00
|
|
|
$headers = [
|
|
|
|
'af_id' => 'abusefilter-list-id',
|
|
|
|
'af_public_comments' => 'abusefilter-list-public',
|
|
|
|
'af_actions' => 'abusefilter-list-consequences',
|
|
|
|
'af_enabled' => 'abusefilter-list-status',
|
|
|
|
'af_timestamp' => 'abusefilter-list-lastmodified',
|
|
|
|
'af_hidden' => 'abusefilter-list-visibility',
|
|
|
|
];
|
|
|
|
|
2020-09-22 19:11:11 +00:00
|
|
|
$user = $this->getUser();
|
2020-09-18 14:49:13 +00:00
|
|
|
if ( $afPermManager->canSeeLogDetails( $user ) ) {
|
2017-12-16 00:26:25 +00:00
|
|
|
$headers['af_hit_count'] = 'abusefilter-list-hitcount';
|
|
|
|
}
|
|
|
|
|
2020-09-18 14:49:13 +00:00
|
|
|
if ( $afPermManager->canViewPrivateFilters( $user ) && $this->mSearchMode !== null ) {
|
2020-10-01 11:31:04 +00:00
|
|
|
// This is also excluded in the default view
|
2018-02-17 14:09:36 +00:00
|
|
|
$headers['af_pattern'] = 'abusefilter-list-pattern';
|
|
|
|
}
|
|
|
|
|
2018-03-10 21:47:33 +00:00
|
|
|
if ( count( $this->getConfig()->get( 'AbuseFilterValidGroups' ) ) > 1 ) {
|
2017-12-16 00:26:25 +00:00
|
|
|
$headers['af_group'] = 'abusefilter-list-group';
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $headers as &$msg ) {
|
|
|
|
$msg = $this->msg( $msg )->text();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $headers;
|
|
|
|
}
|
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @param string $value
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function formatValue( $name, $value ) {
|
2017-12-16 00:26:25 +00:00
|
|
|
$lang = $this->getLanguage();
|
2019-08-27 09:40:01 +00:00
|
|
|
$user = $this->getUser();
|
2020-09-22 19:11:11 +00:00
|
|
|
$linkRenderer = $this->getLinkRenderer();
|
2017-12-16 00:26:25 +00:00
|
|
|
$row = $this->mCurrentRow;
|
|
|
|
|
|
|
|
switch ( $name ) {
|
|
|
|
case 'af_id':
|
2020-09-22 19:11:11 +00:00
|
|
|
return $linkRenderer->makeLink(
|
2019-12-07 17:20:10 +00:00
|
|
|
SpecialPage::getTitleFor( 'AbuseFilter', $value ),
|
2017-12-16 00:26:25 +00:00
|
|
|
$lang->formatNum( intval( $value ) )
|
|
|
|
);
|
2018-02-17 14:09:36 +00:00
|
|
|
case 'af_pattern':
|
2019-10-30 12:36:19 +00:00
|
|
|
return $this->getHighlightedPattern( $row );
|
2017-12-16 00:26:25 +00:00
|
|
|
case 'af_public_comments':
|
2020-09-22 19:11:11 +00:00
|
|
|
return $linkRenderer->makeLink(
|
2019-12-07 17:20:10 +00:00
|
|
|
SpecialPage::getTitleFor( 'AbuseFilter', $row->af_id ),
|
2017-12-16 00:26:25 +00:00
|
|
|
$value
|
|
|
|
);
|
|
|
|
case 'af_actions':
|
|
|
|
$actions = explode( ',', $value );
|
|
|
|
$displayActions = [];
|
2020-08-25 19:33:37 +00:00
|
|
|
$context = $this->getContext();
|
2017-12-16 00:26:25 +00:00
|
|
|
foreach ( $actions as $action ) {
|
2020-08-25 19:33:37 +00:00
|
|
|
$displayActions[] = AbuseFilter::getActionDisplay( $action, $context );
|
2017-12-16 00:26:25 +00:00
|
|
|
}
|
2018-07-04 15:04:05 +00:00
|
|
|
return $lang->commaList( $displayActions );
|
2017-12-16 00:26:25 +00:00
|
|
|
case 'af_enabled':
|
|
|
|
$statuses = [];
|
|
|
|
if ( $row->af_deleted ) {
|
|
|
|
$statuses[] = $this->msg( 'abusefilter-deleted' )->parse();
|
|
|
|
} elseif ( $row->af_enabled ) {
|
|
|
|
$statuses[] = $this->msg( 'abusefilter-enabled' )->parse();
|
2018-05-03 18:32:03 +00:00
|
|
|
if ( $row->af_throttled ) {
|
|
|
|
$statuses[] = $this->msg( 'abusefilter-throttled' )->parse();
|
|
|
|
}
|
2017-12-16 00:26:25 +00:00
|
|
|
} else {
|
|
|
|
$statuses[] = $this->msg( 'abusefilter-disabled' )->parse();
|
|
|
|
}
|
|
|
|
|
2018-03-10 21:47:33 +00:00
|
|
|
if ( $row->af_global && $this->getConfig()->get( 'AbuseFilterIsCentral' ) ) {
|
2017-12-16 00:26:25 +00:00
|
|
|
$statuses[] = $this->msg( 'abusefilter-status-global' )->parse();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $lang->commaList( $statuses );
|
|
|
|
case 'af_hidden':
|
|
|
|
$msg = $value ? 'abusefilter-hidden' : 'abusefilter-unhidden';
|
|
|
|
return $this->msg( $msg )->parse();
|
|
|
|
case 'af_hit_count':
|
2019-03-28 19:59:53 +00:00
|
|
|
// Global here is used to determine whether the log entry is for an external, global
|
|
|
|
// filter, but all filters shown on Special:AbuseFilter are local.
|
|
|
|
$global = false;
|
2020-09-18 14:49:13 +00:00
|
|
|
$afPermManager = AbuseFilterServices::getPermissionManager();
|
|
|
|
if ( $afPermManager->canSeeLogDetails( $user, $row->af_id, $global, $row->af_hidden ) ) {
|
2017-12-16 00:26:25 +00:00
|
|
|
$count_display = $this->msg( 'abusefilter-hitcount' )
|
2018-07-04 15:04:05 +00:00
|
|
|
->numParams( $value )->text();
|
2020-09-22 19:11:11 +00:00
|
|
|
$link = $linkRenderer->makeKnownLink(
|
2017-12-16 00:26:25 +00:00
|
|
|
SpecialPage::getTitleFor( 'AbuseLog' ),
|
|
|
|
$count_display,
|
|
|
|
[],
|
|
|
|
[ 'wpSearchFilter' => $row->af_id ]
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$link = "";
|
|
|
|
}
|
|
|
|
return $link;
|
|
|
|
case 'af_timestamp':
|
|
|
|
$userLink =
|
|
|
|
Linker::userLink(
|
|
|
|
$row->af_user,
|
|
|
|
$row->af_user_text
|
|
|
|
) .
|
|
|
|
Linker::userToolLinks(
|
|
|
|
$row->af_user,
|
|
|
|
$row->af_user_text
|
|
|
|
);
|
2016-01-21 09:57:56 +00:00
|
|
|
|
2017-12-16 00:26:25 +00:00
|
|
|
return $this->msg( 'abusefilter-edit-lastmod-text' )
|
2016-01-21 09:57:56 +00:00
|
|
|
->rawParams(
|
|
|
|
$this->mPage->getLinkToLatestDiff(
|
|
|
|
$row->af_id,
|
|
|
|
$lang->timeanddate( $value, true )
|
|
|
|
),
|
|
|
|
$userLink,
|
|
|
|
$this->mPage->getLinkToLatestDiff(
|
|
|
|
$row->af_id,
|
|
|
|
$lang->date( $value, true )
|
|
|
|
),
|
|
|
|
$this->mPage->getLinkToLatestDiff(
|
|
|
|
$row->af_id,
|
|
|
|
$lang->time( $value, true )
|
|
|
|
)
|
2018-07-04 15:04:05 +00:00
|
|
|
)->params(
|
2016-01-21 09:57:56 +00:00
|
|
|
wfEscapeWikiText( $row->af_user_text )
|
2019-10-30 12:36:19 +00:00
|
|
|
)->parse();
|
2017-12-16 00:26:25 +00:00
|
|
|
case 'af_group':
|
|
|
|
return AbuseFilter::nameGroup( $value );
|
|
|
|
default:
|
|
|
|
throw new MWException( "Unknown row type $name!" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
/**
|
2019-10-30 12:36:19 +00:00
|
|
|
* Get the filter pattern with <b> elements surrounding the searched pattern
|
|
|
|
*
|
|
|
|
* @param stdClass $row
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function getHighlightedPattern( stdClass $row ) {
|
2020-10-01 11:31:04 +00:00
|
|
|
if ( $this->mSearchMode === null ) {
|
|
|
|
throw new LogicException( 'Cannot search without a mode.' );
|
|
|
|
}
|
2019-10-30 12:36:19 +00:00
|
|
|
$maxLen = 50;
|
|
|
|
if ( $this->mSearchMode === 'LIKE' ) {
|
|
|
|
$position = mb_stripos( $row->af_pattern, $this->mSearchPattern );
|
|
|
|
$length = mb_strlen( $this->mSearchPattern );
|
|
|
|
} else {
|
|
|
|
$regex = '/' . $this->mSearchPattern . '/u';
|
|
|
|
if ( $this->mSearchMode === 'IRLIKE' ) {
|
|
|
|
$regex .= 'i';
|
|
|
|
}
|
|
|
|
|
|
|
|
$matches = [];
|
2020-01-08 13:33:10 +00:00
|
|
|
AtEase::suppressWarnings();
|
2019-10-30 12:36:19 +00:00
|
|
|
$check = preg_match(
|
|
|
|
$regex,
|
|
|
|
$row->af_pattern,
|
|
|
|
$matches
|
|
|
|
);
|
2020-01-08 13:33:10 +00:00
|
|
|
AtEase::restoreWarnings();
|
2019-10-30 12:36:19 +00:00
|
|
|
// This may happen in case of catastrophic backtracking, or regexps matching
|
|
|
|
// the empty string.
|
|
|
|
if ( $check === false || strlen( $matches[0] ) === 0 ) {
|
|
|
|
return htmlspecialchars( mb_substr( $row->af_pattern, 0, 50 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
$length = mb_strlen( $matches[0] );
|
|
|
|
$position = mb_strpos( $row->af_pattern, $matches[0] );
|
|
|
|
}
|
|
|
|
|
|
|
|
$remaining = $maxLen - $length;
|
|
|
|
if ( $remaining <= 0 ) {
|
|
|
|
$pattern = '<b>' .
|
|
|
|
htmlspecialchars( mb_substr( $row->af_pattern, $position, $maxLen ) ) .
|
|
|
|
'</b>';
|
|
|
|
} else {
|
|
|
|
// Center the snippet on the matched string
|
|
|
|
$minoffset = max( $position - round( $remaining / 2 ), 0 );
|
|
|
|
$pattern = mb_substr( $row->af_pattern, $minoffset, $maxLen );
|
|
|
|
$pattern =
|
|
|
|
htmlspecialchars( mb_substr( $pattern, 0, $position - $minoffset ) ) .
|
|
|
|
'<b>' .
|
|
|
|
htmlspecialchars( mb_substr( $pattern, $position - $minoffset, $length ) ) .
|
|
|
|
'</b>' .
|
|
|
|
htmlspecialchars( mb_substr(
|
|
|
|
$pattern,
|
|
|
|
$position - $minoffset + $length,
|
|
|
|
$remaining - ( $position - $minoffset + $length )
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return $pattern;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-04 21:14:25 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getDefaultSort() {
|
2017-12-16 00:26:25 +00:00
|
|
|
return 'af_id';
|
|
|
|
}
|
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTableClass() {
|
2019-03-22 14:52:21 +00:00
|
|
|
return parent::getTableClass() . ' mw-abusefilter-list-scrollable';
|
2018-02-17 14:09:36 +00:00
|
|
|
}
|
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
/**
|
|
|
|
* @see TablePager::getRowClass()
|
|
|
|
* @param stdClass $row
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getRowClass( $row ) {
|
2017-12-16 00:26:25 +00:00
|
|
|
if ( $row->af_enabled ) {
|
2018-05-03 18:32:03 +00:00
|
|
|
return $row->af_throttled ? 'mw-abusefilter-list-throttled' : 'mw-abusefilter-list-enabled';
|
2017-12-16 00:26:25 +00:00
|
|
|
} elseif ( $row->af_deleted ) {
|
|
|
|
return 'mw-abusefilter-list-deleted';
|
|
|
|
} else {
|
|
|
|
return 'mw-abusefilter-list-disabled';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
/**
|
|
|
|
* @param string $name
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isFieldSortable( $name ) {
|
2020-09-18 14:49:13 +00:00
|
|
|
$afPermManager = AbuseFilterServices::getPermissionManager();
|
|
|
|
|
2017-12-16 00:26:25 +00:00
|
|
|
$sortable_fields = [
|
|
|
|
'af_id',
|
|
|
|
'af_enabled',
|
|
|
|
'af_timestamp',
|
|
|
|
'af_hidden',
|
|
|
|
'af_group',
|
|
|
|
];
|
2020-09-18 14:49:13 +00:00
|
|
|
if ( $afPermManager->canSeeLogDetails( $this->getUser() ) ) {
|
2017-12-16 00:26:25 +00:00
|
|
|
$sortable_fields[] = 'af_hit_count';
|
2019-03-03 18:07:38 +00:00
|
|
|
$sortable_fields[] = 'af_public_comments';
|
2017-12-16 00:26:25 +00:00
|
|
|
}
|
|
|
|
return in_array( $name, $sortable_fields );
|
|
|
|
}
|
2018-07-15 23:55:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @see IndexPager::getExtraSortFields
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getExtraSortFields() {
|
|
|
|
return [ 'af_enabled' => 'af_deleted' ];
|
|
|
|
}
|
2017-12-16 00:26:25 +00:00
|
|
|
}
|