| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter\View; |
| 4 |
|
| 5 |
use Flow\Data\Listener\RecentChangesListener; |
| 6 |
use MediaWiki\Context\ContextSource; |
| 7 |
use MediaWiki\Context\IContextSource; |
| 8 |
use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager; |
| 9 |
use MediaWiki\Html\Html; |
| 10 |
use MediaWiki\Linker\LinkRenderer; |
| 11 |
use MediaWiki\Permissions\Authority; |
| 12 |
use MediaWiki\Revision\RevisionRecord; |
| 13 |
use MediaWiki\SpecialPage\SpecialPage; |
| 14 |
use MediaWiki\Title\Title; |
| 15 |
use OOUI; |
| 16 |
use RecentChange; |
| 17 |
use UnexpectedValueException; |
| 18 |
use Wikimedia\Assert\Assert; |
| 19 |
use Wikimedia\Rdbms\IExpression; |
| 20 |
use Wikimedia\Rdbms\IReadableDatabase; |
| 21 |
use Wikimedia\Rdbms\Platform\ISQLPlatform; |
| 22 |
|
| 23 |
abstract class AbuseFilterView extends ContextSource { |
| 24 |
|
| 25 |
private const MAP_ACTION_TO_LOG_TYPE = [ |
| 26 |
|
| 27 |
'move' => [ 'move', [ 'move', 'move_redir' ] ], |
| 28 |
'createaccount' => [ 'newusers', [ 'create', 'create2', 'byemail', 'autocreate' ] ], |
| 29 |
'delete' => [ 'delete', 'delete' ], |
| 30 |
'upload' => [ 'upload', [ 'upload', 'overwrite', 'revert' ] ], |
| 31 |
]; |
| 32 |
|
| 33 |
protected AbuseFilterPermissionManager $afPermManager; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
protected array $mParams; |
| 39 |
|
| 40 |
protected LinkRenderer $linkRenderer; |
| 41 |
|
| 42 |
protected string $basePageName; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public function __construct( |
| 52 |
AbuseFilterPermissionManager $afPermManager, |
| 53 |
IContextSource $context, |
| 54 |
LinkRenderer $linkRenderer, |
| 55 |
string $basePageName, |
| 56 |
array $params |
| 57 |
) { |
| 58 |
$this->mParams = $params; |
| 59 |
$this->setContext( $context ); |
| 60 |
$this->linkRenderer = $linkRenderer; |
| 61 |
$this->basePageName = $basePageName; |
| 62 |
$this->afPermManager = $afPermManager; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public function getTitle( $subpage = '' ) { |
| 70 |
return SpecialPage::getTitleFor( $this->basePageName, $subpage ); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
abstract public function show(); |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function buildFilterLoader() { |
| 84 |
$loadText = |
| 85 |
new OOUI\TextInputWidget( |
| 86 |
[ |
| 87 |
'type' => 'number', |
| 88 |
'name' => 'wpInsertFilter', |
| 89 |
'id' => 'mw-abusefilter-load-filter' |
| 90 |
] |
| 91 |
); |
| 92 |
$loadButton = |
| 93 |
new OOUI\ButtonWidget( |
| 94 |
[ |
| 95 |
'label' => $this->msg( 'abusefilter-test-load' )->text(), |
| 96 |
'id' => 'mw-abusefilter-load' |
| 97 |
] |
| 98 |
); |
| 99 |
$loadGroup = |
| 100 |
new OOUI\ActionFieldLayout( |
| 101 |
$loadText, |
| 102 |
$loadButton, |
| 103 |
[ |
| 104 |
'label' => $this->msg( 'abusefilter-test-load-filter' )->text() |
| 105 |
] |
| 106 |
); |
| 107 |
|
| 108 |
return Html::rawElement( |
| 109 |
'div', |
| 110 |
[ 'class' => 'mw-abusefilter-load-filter-id' ], |
| 111 |
$loadGroup |
| 112 |
); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public function buildTestConditions( IReadableDatabase $db, $action = false ) { |
| 121 |
Assert::parameterType( [ 'string', 'false' ], $action, '$action' ); |
| 122 |
$editSources = [ |
| 123 |
RecentChange::SRC_EDIT, |
| 124 |
RecentChange::SRC_NEW, |
| 125 |
]; |
| 126 |
if ( in_array( 'flow', $this->getConfig()->get( 'AbuseFilterValidGroups' ), true ) ) { |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
$editSources[] = RecentChangesListener::SRC_FLOW; |
| 131 |
} |
| 132 |
if ( $action === 'edit' ) { |
| 133 |
return $db->expr( 'rc_source', '=', $editSources ); |
| 134 |
} |
| 135 |
if ( $action !== false ) { |
| 136 |
if ( !isset( self::MAP_ACTION_TO_LOG_TYPE[$action] ) ) { |
| 137 |
throw new UnexpectedValueException( __METHOD__ . ' called with invalid action: ' . $action ); |
| 138 |
} |
| 139 |
[ $logType, $logAction ] = self::MAP_ACTION_TO_LOG_TYPE[$action]; |
| 140 |
return $db->expr( 'rc_source', '=', RecentChange::SRC_LOG ) |
| 141 |
->and( 'rc_log_type', '=', $logType ) |
| 142 |
->and( 'rc_log_action', '=', $logAction ); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
$conds = []; |
| 147 |
foreach ( self::MAP_ACTION_TO_LOG_TYPE as [ $logType, $logAction ] ) { |
| 148 |
$conds[] = $db->expr( 'rc_log_type', '=', $logType ) |
| 149 |
->and( 'rc_log_action', '=', $logAction ); |
| 150 |
} |
| 151 |
|
| 152 |
return $db->expr( 'rc_source', '=', $editSources ) |
| 153 |
->orExpr( |
| 154 |
$db->expr( 'rc_source', '=', RecentChange::SRC_LOG ) |
| 155 |
->andExpr( $db->orExpr( $conds ) ) |
| 156 |
); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public function buildVisibilityConditions( ISQLPlatform $db, Authority $authority ): array { |
| 166 |
if ( !$authority->isAllowed( 'deletedhistory' ) ) { |
| 167 |
$bitmask = RevisionRecord::DELETED_USER; |
| 168 |
} elseif ( !$authority->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) { |
| 169 |
$bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED; |
| 170 |
} else { |
| 171 |
$bitmask = 0; |
| 172 |
} |
| 173 |
return $bitmask |
| 174 |
? [ $db->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask" ] |
| 175 |
: []; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public function getLinkToLatestDiff( $id, $text = null ) { |
| 184 |
return $this->linkRenderer->makeKnownLink( |
| 185 |
$this->getTitle( "history/$id/diff/prev/cur" ), |
| 186 |
$text |
| 187 |
); |
| 188 |
} |
| 189 |
|
| 190 |
} |