2009-01-23 19:23:19 +00:00
|
|
|
<?php
|
|
|
|
|
2020-01-15 16:08:53 +00:00
|
|
|
use MediaWiki\Extension\AbuseFilter\AbuseFilterServices;
|
2017-07-13 08:19:09 +00:00
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
2016-12-04 15:13:16 +00:00
|
|
|
|
2011-11-16 05:34:24 +00:00
|
|
|
abstract class AbuseFilterView extends ContextSource {
|
2018-11-08 14:34:32 +00:00
|
|
|
/**
|
2020-10-03 13:05:20 +00:00
|
|
|
* @var string|null The ID of the current filter
|
2018-11-08 14:34:32 +00:00
|
|
|
*/
|
|
|
|
public $mFilter;
|
|
|
|
/**
|
|
|
|
* @var bool Whether the form was submitted
|
|
|
|
*/
|
|
|
|
public $mSubmit;
|
|
|
|
/**
|
|
|
|
* @var SpecialAbuseFilter The related special page object
|
|
|
|
*/
|
|
|
|
public $mPage;
|
|
|
|
/**
|
|
|
|
* @var array The parameters of the current request
|
|
|
|
*/
|
|
|
|
public $mParams;
|
2013-10-15 13:22:05 +00:00
|
|
|
|
2016-12-04 15:13:16 +00:00
|
|
|
/**
|
|
|
|
* @var \MediaWiki\Linker\LinkRenderer
|
|
|
|
*/
|
|
|
|
protected $linkRenderer;
|
|
|
|
|
2011-11-16 05:34:24 +00:00
|
|
|
/**
|
2017-10-06 18:52:31 +00:00
|
|
|
* @param SpecialAbuseFilter $page
|
|
|
|
* @param array $params
|
2011-11-16 05:34:24 +00:00
|
|
|
*/
|
2018-10-17 05:15:21 +00:00
|
|
|
public function __construct( SpecialAbuseFilter $page, $params ) {
|
2009-01-23 19:23:19 +00:00
|
|
|
$this->mPage = $page;
|
|
|
|
$this->mParams = $params;
|
2011-11-16 05:34:24 +00:00
|
|
|
$this->setContext( $this->mPage->getContext() );
|
2017-08-20 12:27:02 +00:00
|
|
|
$this->linkRenderer = $page->getLinkRenderer();
|
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 = '' ) {
|
2013-12-27 06:36:34 +00:00
|
|
|
return $this->mPage->getPageTitle( $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-04-08 15:57:23 +00:00
|
|
|
/**
|
|
|
|
* @param string $rules
|
|
|
|
* @param bool $addResultDiv
|
|
|
|
* @param bool $externalForm
|
2018-05-04 19:56:45 +00:00
|
|
|
* @param bool $needsModifyRights
|
2018-08-20 14:48:54 +00:00
|
|
|
* @param-taint $rules none
|
2018-04-08 15:57:23 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2019-08-17 16:53:13 +00:00
|
|
|
protected function buildEditBox(
|
2018-04-08 15:57:23 +00:00
|
|
|
$rules,
|
|
|
|
$addResultDiv = true,
|
2018-05-04 19:56:45 +00:00
|
|
|
$externalForm = false,
|
|
|
|
$needsModifyRights = true
|
2018-04-08 15:57:23 +00:00
|
|
|
) {
|
|
|
|
$this->getOutput()->enableOOUI();
|
2019-08-27 09:40:01 +00:00
|
|
|
$user = $this->getUser();
|
2018-04-08 15:57:23 +00:00
|
|
|
|
|
|
|
// Rules are in English
|
2018-10-21 11:02:13 +00:00
|
|
|
$editorAttribs = [ 'dir' => 'ltr' ];
|
2018-04-08 15:57:23 +00:00
|
|
|
|
|
|
|
$noTestAttrib = [];
|
2018-05-04 19:56:45 +00:00
|
|
|
$isUserAllowed = $needsModifyRights ?
|
2019-08-27 10:08:17 +00:00
|
|
|
AbuseFilter::canEdit( $user ) :
|
2019-08-27 09:40:01 +00:00
|
|
|
AbuseFilter::canViewPrivate( $user );
|
2018-05-04 19:56:45 +00:00
|
|
|
if ( !$isUserAllowed ) {
|
2018-04-08 15:57:23 +00:00
|
|
|
$noTestAttrib['disabled'] = 'disabled';
|
|
|
|
$addResultDiv = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$rules = rtrim( $rules ) . "\n";
|
2018-08-20 14:48:54 +00:00
|
|
|
$switchEditor = null;
|
2018-04-08 15:57:23 +00:00
|
|
|
|
2018-10-21 11:02:13 +00:00
|
|
|
$rulesContainer = '';
|
2018-04-08 15:57:23 +00:00
|
|
|
if ( ExtensionRegistry::getInstance()->isLoaded( 'CodeEditor' ) ) {
|
2018-10-21 11:02:13 +00:00
|
|
|
$aceAttribs = [
|
|
|
|
'name' => 'wpAceFilterEditor',
|
|
|
|
'id' => 'wpAceFilterEditor',
|
|
|
|
'class' => 'mw-abusefilter-editor'
|
|
|
|
];
|
|
|
|
$attribs = array_merge( $editorAttribs, $aceAttribs );
|
2018-04-08 15:57:23 +00:00
|
|
|
|
|
|
|
$switchEditor =
|
|
|
|
new OOUI\ButtonWidget(
|
|
|
|
[
|
|
|
|
'label' => $this->msg( 'abusefilter-edit-switch-editor' )->text(),
|
|
|
|
'id' => 'mw-abusefilter-switcheditor'
|
|
|
|
] + $noTestAttrib
|
|
|
|
);
|
|
|
|
|
2018-10-21 11:02:13 +00:00
|
|
|
$rulesContainer .= Xml::element( 'div', $attribs, $rules );
|
2018-04-08 15:57:23 +00:00
|
|
|
|
|
|
|
// Add Ace configuration variable
|
2019-08-27 10:08:17 +00:00
|
|
|
$editorConfig = AbuseFilter::getAceConfig( $isUserAllowed );
|
2018-04-08 15:57:23 +00:00
|
|
|
$this->getOutput()->addJsConfigVars( 'aceConfig', $editorConfig );
|
|
|
|
}
|
|
|
|
|
2018-10-21 11:02:13 +00:00
|
|
|
// Build a dummy textarea to be used: for submitting form if CodeEditor isn't installed,
|
|
|
|
// and in case JS is disabled (with or without CodeEditor)
|
2019-08-27 10:08:17 +00:00
|
|
|
if ( !$isUserAllowed ) {
|
2018-10-21 11:02:13 +00:00
|
|
|
$editorAttribs['readonly'] = 'readonly';
|
|
|
|
}
|
|
|
|
if ( $externalForm ) {
|
|
|
|
$editorAttribs['form'] = 'wpFilterForm';
|
|
|
|
}
|
2019-08-17 16:53:13 +00:00
|
|
|
$rulesContainer .= Xml::textarea( 'wpFilterRules', $rules, 40, 15, $editorAttribs );
|
2018-10-21 11:02:13 +00:00
|
|
|
|
2019-08-27 10:08:17 +00:00
|
|
|
if ( $isUserAllowed ) {
|
2020-01-15 16:08:53 +00:00
|
|
|
$keywordsManager = AbuseFilterServices::getKeywordsManager();
|
2018-04-08 15:57:23 +00:00
|
|
|
// Generate builder drop-down
|
2020-01-15 16:08:53 +00:00
|
|
|
$rawDropDown = $keywordsManager->getBuilderValues();
|
2018-07-11 22:33:09 +00:00
|
|
|
|
|
|
|
// The array needs to be rearranged to be understood by OOUI. It comes with the format
|
|
|
|
// [ group-msg-key => [ text-to-add => text-msg-key ] ] and we need it as
|
|
|
|
// [ group-msg => [ text-msg => text-to-add ] ]
|
|
|
|
// Also, the 'other' element must be the first one.
|
2018-08-20 14:48:54 +00:00
|
|
|
$dropDownOptions = [ $this->msg( 'abusefilter-edit-builder-select' )->text() => 'other' ];
|
2018-07-11 22:33:09 +00:00
|
|
|
foreach ( $rawDropDown as $group => $values ) {
|
2018-04-08 15:57:23 +00:00
|
|
|
// Give grep a chance to find the usages:
|
|
|
|
// abusefilter-edit-builder-group-op-arithmetic, abusefilter-edit-builder-group-op-comparison,
|
|
|
|
// abusefilter-edit-builder-group-op-bool, abusefilter-edit-builder-group-misc,
|
|
|
|
// abusefilter-edit-builder-group-funcs, abusefilter-edit-builder-group-vars
|
2018-07-11 22:33:09 +00:00
|
|
|
$localisedGroup = $this->msg( "abusefilter-edit-builder-group-$group" )->text();
|
2018-08-20 14:48:54 +00:00
|
|
|
$dropDownOptions[ $localisedGroup ] = array_flip( $values );
|
2018-07-11 22:33:09 +00:00
|
|
|
$newKeys = array_map(
|
|
|
|
function ( $key ) use ( $group ) {
|
|
|
|
return $this->msg( "abusefilter-edit-builder-$group-$key" )->text();
|
|
|
|
},
|
2018-08-20 14:48:54 +00:00
|
|
|
array_keys( $dropDownOptions[ $localisedGroup ] )
|
2018-07-11 22:33:09 +00:00
|
|
|
);
|
2018-08-20 14:48:54 +00:00
|
|
|
$dropDownOptions[ $localisedGroup ] = array_combine(
|
|
|
|
$newKeys, $dropDownOptions[ $localisedGroup ] );
|
2018-04-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 14:48:54 +00:00
|
|
|
$dropDownList = Xml::listDropDownOptionsOoui( $dropDownOptions );
|
2018-04-08 15:57:23 +00:00
|
|
|
$dropDown = new OOUI\DropdownInputWidget( [
|
|
|
|
'name' => 'wpFilterBuilder',
|
|
|
|
'inputId' => 'wpFilterBuilder',
|
2018-08-20 14:48:54 +00:00
|
|
|
'options' => $dropDownList
|
2018-04-08 15:57:23 +00:00
|
|
|
] );
|
|
|
|
|
2018-08-20 14:48:54 +00:00
|
|
|
$formElements = [ new OOUI\FieldLayout( $dropDown ) ];
|
2018-04-08 15:57:23 +00:00
|
|
|
|
|
|
|
// Button for syntax check
|
|
|
|
$syntaxCheck =
|
|
|
|
new OOUI\ButtonWidget(
|
|
|
|
[
|
|
|
|
'label' => $this->msg( 'abusefilter-edit-check' )->text(),
|
|
|
|
'id' => 'mw-abusefilter-syntaxcheck'
|
|
|
|
] + $noTestAttrib
|
|
|
|
);
|
|
|
|
|
|
|
|
// Button for switching editor (if Ace is used)
|
2018-08-20 14:48:54 +00:00
|
|
|
if ( $switchEditor !== null ) {
|
|
|
|
$formElements[] = new OOUI\FieldLayout(
|
2018-04-08 15:57:23 +00:00
|
|
|
new OOUI\Widget( [
|
|
|
|
'content' => new OOUI\HorizontalLayout( [
|
|
|
|
'items' => [ $switchEditor, $syntaxCheck ]
|
|
|
|
] )
|
2018-08-20 14:48:54 +00:00
|
|
|
] )
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$formElements[] = new OOUI\FieldLayout( $syntaxCheck );
|
2018-04-08 15:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$fieldSet = new OOUI\FieldsetLayout( [
|
|
|
|
'items' => $formElements,
|
2018-03-23 09:13:41 +00:00
|
|
|
'classes' => [ 'mw-abusefilter-edit-buttons', 'mw-abusefilter-javascript-tools' ]
|
2018-04-08 15:57:23 +00:00
|
|
|
] );
|
|
|
|
|
|
|
|
$rulesContainer .= $fieldSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $addResultDiv ) {
|
|
|
|
$rulesContainer .= Xml::element( 'div',
|
|
|
|
[ 'id' => 'mw-abusefilter-syntaxresult', 'style' => 'display: none;' ],
|
|
|
|
' ' );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add script
|
|
|
|
$this->getOutput()->addModules( 'ext.abuseFilter.edit' );
|
|
|
|
|
|
|
|
return $rulesContainer;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
$loadDiv =
|
|
|
|
Xml::tags(
|
|
|
|
'div',
|
|
|
|
[ 'class' => 'mw-abusefilter-load-filter-id' ],
|
|
|
|
$loadGroup
|
|
|
|
);
|
|
|
|
return $loadDiv;
|
|
|
|
}
|
|
|
|
|
2017-07-13 08:19:09 +00:00
|
|
|
/**
|
|
|
|
* @param IDatabase $db
|
2020-06-02 08:13:17 +00:00
|
|
|
* @param string|false $action 'edit', 'move', 'createaccount', 'delete' or false for all
|
2017-07-13 08:19:09 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2018-05-01 10:40:56 +00:00
|
|
|
public function buildTestConditions( IDatabase $db, $action = false ) {
|
2017-07-13 08:19:09 +00:00
|
|
|
// If one of these is true, we're abusefilter compatible.
|
2018-05-01 10:40:56 +00:00
|
|
|
switch ( $action ) {
|
|
|
|
case 'edit':
|
|
|
|
return $db->makeList( [
|
|
|
|
// Actually, this is only one condition, but this way we get it as string
|
|
|
|
'rc_source' => [
|
|
|
|
RecentChange::SRC_EDIT,
|
|
|
|
RecentChange::SRC_NEW,
|
|
|
|
]
|
|
|
|
], LIST_AND );
|
|
|
|
case 'move':
|
|
|
|
return $db->makeList( [
|
|
|
|
'rc_source' => RecentChange::SRC_LOG,
|
|
|
|
'rc_log_type' => 'move',
|
|
|
|
'rc_log_action' => 'move'
|
|
|
|
], LIST_AND );
|
|
|
|
case 'createaccount':
|
|
|
|
return $db->makeList( [
|
|
|
|
'rc_source' => RecentChange::SRC_LOG,
|
|
|
|
'rc_log_type' => 'newusers',
|
2018-09-13 13:27:34 +00:00
|
|
|
'rc_log_action' => [ 'create', 'autocreate' ]
|
2018-05-01 10:40:56 +00:00
|
|
|
], LIST_AND );
|
|
|
|
case 'delete':
|
|
|
|
return $db->makeList( [
|
|
|
|
'rc_source' => RecentChange::SRC_LOG,
|
|
|
|
'rc_log_type' => 'delete',
|
|
|
|
'rc_log_action' => 'delete'
|
|
|
|
], LIST_AND );
|
2018-07-11 23:54:48 +00:00
|
|
|
case 'upload':
|
|
|
|
return $db->makeList( [
|
|
|
|
'rc_source' => RecentChange::SRC_LOG,
|
|
|
|
'rc_log_type' => 'upload',
|
|
|
|
'rc_log_action' => [ 'upload', 'overwrite', 'revert' ]
|
|
|
|
], LIST_AND );
|
2019-01-24 20:48:50 +00:00
|
|
|
case false:
|
|
|
|
// Done later
|
|
|
|
break;
|
2018-10-03 15:55:06 +00:00
|
|
|
default:
|
2020-06-02 08:13:17 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeSuspiciousStringExpression False does not reach here
|
2018-10-03 15:55:06 +00:00
|
|
|
throw new MWException( __METHOD__ . ' called with invalid action: ' . $action );
|
2018-05-01 10:40:56 +00:00
|
|
|
}
|
|
|
|
|
2017-07-13 08:19:09 +00:00
|
|
|
return $db->makeList( [
|
|
|
|
'rc_source' => [
|
|
|
|
RecentChange::SRC_EDIT,
|
|
|
|
RecentChange::SRC_NEW,
|
|
|
|
],
|
|
|
|
$db->makeList( [
|
|
|
|
'rc_source' => RecentChange::SRC_LOG,
|
|
|
|
$db->makeList( [
|
|
|
|
$db->makeList( [
|
|
|
|
'rc_log_type' => 'move',
|
|
|
|
'rc_log_action' => 'move'
|
|
|
|
], LIST_AND ),
|
|
|
|
$db->makeList( [
|
|
|
|
'rc_log_type' => 'newusers',
|
2018-09-13 13:27:34 +00:00
|
|
|
'rc_log_action' => [ 'create', 'autocreate' ]
|
2017-07-13 08:19:09 +00:00
|
|
|
], LIST_AND ),
|
2017-08-20 11:48:20 +00:00
|
|
|
$db->makeList( [
|
|
|
|
'rc_log_type' => 'delete',
|
|
|
|
'rc_log_action' => 'delete'
|
|
|
|
], LIST_AND ),
|
2018-07-11 23:54:48 +00:00
|
|
|
$db->makeList( [
|
|
|
|
'rc_log_type' => 'upload',
|
|
|
|
'rc_log_action' => [ 'upload', 'overwrite', 'revert' ]
|
|
|
|
], LIST_AND ),
|
2017-07-13 08:19:09 +00:00
|
|
|
], LIST_OR ),
|
|
|
|
], LIST_AND ),
|
|
|
|
], LIST_OR );
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
);
|
|
|
|
}
|
2009-01-29 22:44:31 +00:00
|
|
|
}
|