2009-01-23 19:23:19 +00:00
|
|
|
<?php
|
|
|
|
|
2020-11-29 10:53:29 +00:00
|
|
|
namespace MediaWiki\Extension\AbuseFilter\View;
|
|
|
|
|
2021-01-31 16:45:12 +00:00
|
|
|
use Flow\Data\Listener\RecentChangesListener;
|
2024-06-12 18:01:35 +00:00
|
|
|
use MediaWiki\Context\ContextSource;
|
|
|
|
use MediaWiki\Context\IContextSource;
|
2020-10-23 14:19:02 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager;
|
2024-03-29 12:40:23 +00:00
|
|
|
use MediaWiki\Html\Html;
|
2020-10-03 17:13:32 +00:00
|
|
|
use MediaWiki\Linker\LinkRenderer;
|
2021-03-11 10:50:44 +00:00
|
|
|
use MediaWiki\Permissions\Authority;
|
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2023-12-10 19:03:19 +00:00
|
|
|
use MediaWiki\SpecialPage\SpecialPage;
|
2023-08-19 17:49:36 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2020-11-29 10:53:29 +00:00
|
|
|
use OOUI;
|
|
|
|
use RecentChange;
|
2023-06-07 15:41:20 +00:00
|
|
|
use UnexpectedValueException;
|
2024-06-15 10:45:26 +00:00
|
|
|
use Wikimedia\Assert\Assert;
|
2024-05-22 20:01:47 +00:00
|
|
|
use Wikimedia\Rdbms\IExpression;
|
|
|
|
use Wikimedia\Rdbms\IReadableDatabase;
|
2023-06-12 15:05:15 +00:00
|
|
|
use Wikimedia\Rdbms\Platform\ISQLPlatform;
|
2016-12-04 15:13:16 +00:00
|
|
|
|
2011-11-16 05:34:24 +00:00
|
|
|
abstract class AbuseFilterView extends ContextSource {
|
2020-10-23 14:19:02 +00:00
|
|
|
|
2024-05-22 20:01:47 +00:00
|
|
|
private const MAP_ACTION_TO_LOG_TYPE = [
|
|
|
|
// action => [ rc_log_type, rc_log_action ]
|
2024-06-15 10:45:26 +00:00
|
|
|
'move' => [ 'move', [ 'move', 'move_redir' ] ],
|
|
|
|
'createaccount' => [ 'newusers', [ 'create', 'create2', 'byemail', 'autocreate' ] ],
|
2024-05-22 20:01:47 +00:00
|
|
|
'delete' => [ 'delete', 'delete' ],
|
|
|
|
'upload' => [ 'upload', [ 'upload', 'overwrite', 'revert' ] ],
|
|
|
|
];
|
|
|
|
|
2024-06-15 10:45:26 +00:00
|
|
|
protected AbuseFilterPermissionManager $afPermManager;
|
2020-10-23 14:19:02 +00:00
|
|
|
|
2018-11-08 14:34:32 +00:00
|
|
|
/**
|
|
|
|
* @var array The parameters of the current request
|
|
|
|
*/
|
2024-06-15 10:45:26 +00:00
|
|
|
protected array $mParams;
|
2013-10-15 13:22:05 +00:00
|
|
|
|
2024-06-15 10:45:26 +00:00
|
|
|
protected LinkRenderer $linkRenderer;
|
2020-10-23 14:19:02 +00:00
|
|
|
|
2024-06-15 10:45:26 +00:00
|
|
|
protected string $basePageName;
|
2016-12-04 15:13:16 +00:00
|
|
|
|
2011-11-16 05:34:24 +00:00
|
|
|
/**
|
2020-10-23 14:19:02 +00:00
|
|
|
* @param AbuseFilterPermissionManager $afPermManager
|
2020-10-03 17:13:32 +00:00
|
|
|
* @param IContextSource $context
|
|
|
|
* @param LinkRenderer $linkRenderer
|
2020-10-16 22:36:15 +00:00
|
|
|
* @param string $basePageName
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param array $params
|
2011-11-16 05:34:24 +00:00
|
|
|
*/
|
2020-10-16 22:36:15 +00:00
|
|
|
public function __construct(
|
2020-10-23 14:19:02 +00:00
|
|
|
AbuseFilterPermissionManager $afPermManager,
|
2020-10-16 22:36:15 +00:00
|
|
|
IContextSource $context,
|
|
|
|
LinkRenderer $linkRenderer,
|
|
|
|
string $basePageName,
|
|
|
|
array $params
|
|
|
|
) {
|
2009-01-23 19:23:19 +00:00
|
|
|
$this->mParams = $params;
|
2020-10-03 17:13:32 +00:00
|
|
|
$this->setContext( $context );
|
|
|
|
$this->linkRenderer = $linkRenderer;
|
2020-10-16 22:36:15 +00:00
|
|
|
$this->basePageName = $basePageName;
|
2020-10-23 14:19:02 +00:00
|
|
|
$this->afPermManager = $afPermManager;
|
2009-01-23 19:23:19 +00:00
|
|
|
}
|
|
|
|
|
2011-02-10 17:32:57 +00:00
|
|
|
/**
|
2019-12-07 17:20:10 +00:00
|
|
|
* @param string|int $subpage
|
2011-02-10 17:32:57 +00:00
|
|
|
* @return Title
|
|
|
|
*/
|
2018-04-04 21:14:25 +00:00
|
|
|
public function getTitle( $subpage = '' ) {
|
2020-10-16 22:36:15 +00:00
|
|
|
return SpecialPage::getTitleFor( $this->basePageName, $subpage );
|
2009-01-23 19:23:19 +00:00
|
|
|
}
|
2009-10-07 13:57:06 +00:00
|
|
|
|
2018-04-04 21:14:25 +00:00
|
|
|
/**
|
|
|
|
* Function to show the page
|
|
|
|
*/
|
|
|
|
abstract public function show();
|
2009-01-23 19:23:19 +00:00
|
|
|
|
2018-12-09 13:33:30 +00:00
|
|
|
/**
|
|
|
|
* Build input and button for loading a filter
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function buildFilterLoader() {
|
|
|
|
$loadText =
|
|
|
|
new OOUI\TextInputWidget(
|
|
|
|
[
|
|
|
|
'type' => 'number',
|
|
|
|
'name' => 'wpInsertFilter',
|
|
|
|
'id' => 'mw-abusefilter-load-filter'
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$loadButton =
|
|
|
|
new OOUI\ButtonWidget(
|
|
|
|
[
|
|
|
|
'label' => $this->msg( 'abusefilter-test-load' )->text(),
|
|
|
|
'id' => 'mw-abusefilter-load'
|
|
|
|
]
|
|
|
|
);
|
|
|
|
$loadGroup =
|
|
|
|
new OOUI\ActionFieldLayout(
|
|
|
|
$loadText,
|
|
|
|
$loadButton,
|
|
|
|
[
|
|
|
|
'label' => $this->msg( 'abusefilter-test-load-filter' )->text()
|
|
|
|
]
|
|
|
|
);
|
|
|
|
// CSS class for reducing default input field width
|
2024-03-29 12:40:23 +00:00
|
|
|
return Html::rawElement(
|
2023-06-12 15:08:25 +00:00
|
|
|
'div',
|
|
|
|
[ 'class' => 'mw-abusefilter-load-filter-id' ],
|
|
|
|
$loadGroup
|
|
|
|
);
|
2018-12-09 13:33:30 +00:00
|
|
|
}
|
|
|
|
|
2017-07-13 08:19:09 +00:00
|
|
|
/**
|
2024-05-22 20:01:47 +00:00
|
|
|
* @param IReadableDatabase $db
|
2020-06-02 08:13:17 +00:00
|
|
|
* @param string|false $action 'edit', 'move', 'createaccount', 'delete' or false for all
|
2024-05-22 20:01:47 +00:00
|
|
|
* @return IExpression
|
2017-07-13 08:19:09 +00:00
|
|
|
*/
|
2024-05-22 20:01:47 +00:00
|
|
|
public function buildTestConditions( IReadableDatabase $db, $action = false ) {
|
2024-06-15 10:45:26 +00:00
|
|
|
Assert::parameterType( [ 'string', 'false' ], $action, '$action' );
|
2021-01-31 16:45:12 +00:00
|
|
|
$editSources = [
|
|
|
|
RecentChange::SRC_EDIT,
|
|
|
|
RecentChange::SRC_NEW,
|
|
|
|
];
|
|
|
|
if ( in_array( 'flow', $this->getConfig()->get( 'AbuseFilterValidGroups' ), true ) ) {
|
|
|
|
// TODO Should this be separated somehow? Also, this case should be handled via a hook, not
|
|
|
|
// by special-casing Flow here.
|
|
|
|
// @phan-suppress-next-line PhanUndeclaredClassConstant Temporary solution
|
|
|
|
$editSources[] = RecentChangesListener::SRC_FLOW;
|
|
|
|
}
|
2024-05-22 20:01:47 +00:00
|
|
|
if ( $action === 'edit' ) {
|
|
|
|
return $db->expr( 'rc_source', '=', $editSources );
|
|
|
|
}
|
|
|
|
if ( $action !== false ) {
|
|
|
|
if ( !isset( self::MAP_ACTION_TO_LOG_TYPE[$action] ) ) {
|
2023-06-07 15:41:20 +00:00
|
|
|
throw new UnexpectedValueException( __METHOD__ . ' called with invalid action: ' . $action );
|
2024-05-22 20:01:47 +00:00
|
|
|
}
|
|
|
|
[ $logType, $logAction ] = self::MAP_ACTION_TO_LOG_TYPE[$action];
|
|
|
|
return $db->expr( 'rc_source', '=', RecentChange::SRC_LOG )
|
|
|
|
->and( 'rc_log_type', '=', $logType )
|
|
|
|
->and( 'rc_log_action', '=', $logAction );
|
2018-05-01 10:40:56 +00:00
|
|
|
}
|
|
|
|
|
2024-05-22 20:01:47 +00:00
|
|
|
// filter edit and log actions
|
|
|
|
$conds = [];
|
|
|
|
foreach ( self::MAP_ACTION_TO_LOG_TYPE as [ $logType, $logAction ] ) {
|
|
|
|
$conds[] = $db->expr( 'rc_log_type', '=', $logType )
|
|
|
|
->and( 'rc_log_action', '=', $logAction );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $db->expr( 'rc_source', '=', $editSources )
|
|
|
|
->orExpr(
|
|
|
|
$db->expr( 'rc_source', '=', RecentChange::SRC_LOG )
|
2024-07-11 16:36:04 +00:00
|
|
|
->andExpr( $db->orExpr( $conds ) )
|
2024-05-22 20:01:47 +00:00
|
|
|
);
|
2017-07-13 08:19:09 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 22:09:41 +00:00
|
|
|
/**
|
2021-03-11 10:50:44 +00:00
|
|
|
* @todo Core should provide a method for this (T233222)
|
2023-06-12 15:05:15 +00:00
|
|
|
* @param ISQLPlatform $db
|
2021-03-11 10:50:44 +00:00
|
|
|
* @param Authority $authority
|
2021-03-09 22:09:41 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2023-06-12 15:05:15 +00:00
|
|
|
public function buildVisibilityConditions( ISQLPlatform $db, Authority $authority ): array {
|
2021-03-11 10:50:44 +00:00
|
|
|
if ( !$authority->isAllowed( 'deletedhistory' ) ) {
|
|
|
|
$bitmask = RevisionRecord::DELETED_USER;
|
|
|
|
} elseif ( !$authority->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
|
|
|
|
$bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
|
|
|
|
} else {
|
|
|
|
$bitmask = 0;
|
|
|
|
}
|
|
|
|
return $bitmask
|
|
|
|
? [ $db->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask" ]
|
|
|
|
: [];
|
2021-03-09 22:09:41 +00:00
|
|
|
}
|
|
|
|
|
2016-01-21 09:57:56 +00:00
|
|
|
/**
|
|
|
|
* @param string|int $id
|
|
|
|
* @param string|null $text
|
|
|
|
* @return string HTML
|
|
|
|
*/
|
|
|
|
public function getLinkToLatestDiff( $id, $text = null ) {
|
|
|
|
return $this->linkRenderer->makeKnownLink(
|
|
|
|
$this->getTitle( "history/$id/diff/prev/cur" ),
|
|
|
|
$text
|
|
|
|
);
|
|
|
|
}
|
2020-10-25 14:54:56 +00:00
|
|
|
|
2009-01-29 22:44:31 +00:00
|
|
|
}
|