2009-01-23 19:23:19 +00:00
|
|
|
<?php
|
|
|
|
|
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-04-29 17:52:45 +00:00
|
|
|
public $mFilter, $mHistoryID, $mSubmit, $mPage, $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-04-04 21:14:25 +00:00
|
|
|
public function __construct( $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
|
|
|
/**
|
|
|
|
* @param string $subpage
|
|
|
|
* @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
|
|
|
|
2011-08-24 22:11:52 +00:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-07-09 13:35:06 +00:00
|
|
|
public function canEdit() {
|
2017-04-07 19:23:11 +00:00
|
|
|
return (
|
|
|
|
!$this->getUser()->isBlocked() &&
|
|
|
|
$this->getUser()->isAllowed( 'abusefilter-modify' )
|
|
|
|
);
|
2009-01-23 19:23:19 +00:00
|
|
|
}
|
2009-07-03 14:17:05 +00:00
|
|
|
|
2011-08-24 22:11:52 +00:00
|
|
|
/**
|
|
|
|
* @return bool
|
2012-09-12 15:31:24 +00:00
|
|
|
*/
|
2013-07-09 13:35:06 +00:00
|
|
|
public function canEditGlobal() {
|
|
|
|
return $this->getUser()->isAllowed( 'abusefilter-modify-global' );
|
|
|
|
}
|
2012-09-12 15:31:24 +00:00
|
|
|
|
2013-07-09 13:35:06 +00:00
|
|
|
/**
|
|
|
|
* Whether the user can edit the given filter.
|
|
|
|
*
|
|
|
|
* @param object $row Filter row
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function canEditFilter( $row ) {
|
|
|
|
return (
|
|
|
|
$this->canEdit() &&
|
|
|
|
!( isset( $row->af_global ) && $row->af_global == 1 && !$this->canEditGlobal() )
|
|
|
|
);
|
2012-09-12 15:31:24 +00:00
|
|
|
}
|
|
|
|
|
2018-04-08 15:57:23 +00:00
|
|
|
/**
|
|
|
|
* @param string $rules
|
|
|
|
* @param string $textName
|
|
|
|
* @param bool $addResultDiv
|
|
|
|
* @param bool $externalForm
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function buildEditBox(
|
|
|
|
$rules,
|
|
|
|
$textName = 'wpFilterRules',
|
|
|
|
$addResultDiv = true,
|
|
|
|
$externalForm = false
|
|
|
|
) {
|
|
|
|
$this->getOutput()->enableOOUI();
|
|
|
|
|
|
|
|
// Rules are in English
|
|
|
|
$editorAttrib = [ 'dir' => 'ltr' ];
|
|
|
|
|
|
|
|
$noTestAttrib = [];
|
|
|
|
if ( !$this->getUser()->isAllowed( 'abusefilter-modify' ) ) {
|
|
|
|
$noTestAttrib['disabled'] = 'disabled';
|
|
|
|
$addResultDiv = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$rules = rtrim( $rules ) . "\n";
|
2018-04-30 13:55:10 +00:00
|
|
|
$canEdit = $this->canEdit();
|
2018-04-08 15:57:23 +00:00
|
|
|
|
|
|
|
if ( ExtensionRegistry::getInstance()->isLoaded( 'CodeEditor' ) ) {
|
|
|
|
$editorAttrib['name'] = 'wpAceFilterEditor';
|
|
|
|
$editorAttrib['id'] = 'wpAceFilterEditor';
|
|
|
|
$editorAttrib['class'] = 'mw-abusefilter-editor';
|
|
|
|
|
|
|
|
$switchEditor =
|
|
|
|
new OOUI\ButtonWidget(
|
|
|
|
[
|
|
|
|
'label' => $this->msg( 'abusefilter-edit-switch-editor' )->text(),
|
|
|
|
'id' => 'mw-abusefilter-switcheditor'
|
|
|
|
] + $noTestAttrib
|
|
|
|
);
|
|
|
|
|
|
|
|
$rulesContainer = Xml::element( 'div', $editorAttrib, $rules );
|
|
|
|
|
|
|
|
// Dummy textarea for submitting form and to use in case JS is disabled
|
2018-05-02 17:31:35 +00:00
|
|
|
$textareaAttribs = [];
|
2018-04-08 15:57:23 +00:00
|
|
|
if ( $externalForm ) {
|
|
|
|
$textareaAttribs['form'] = 'wpFilterForm';
|
|
|
|
}
|
|
|
|
$rulesContainer .= Xml::textarea( $textName, $rules, 40, 15, $textareaAttribs );
|
|
|
|
|
|
|
|
$editorConfig = AbuseFilter::getAceConfig( $canEdit );
|
|
|
|
|
|
|
|
// Add Ace configuration variable
|
|
|
|
$this->getOutput()->addJsConfigVars( 'aceConfig', $editorConfig );
|
|
|
|
} else {
|
|
|
|
if ( !$canEdit ) {
|
|
|
|
$editorAttrib['readonly'] = 'readonly';
|
|
|
|
}
|
|
|
|
if ( $externalForm ) {
|
|
|
|
$editorAttrib['form'] = 'wpFilterForm';
|
|
|
|
}
|
|
|
|
$rulesContainer = Xml::textarea( $textName, $rules, 40, 15, $editorAttrib );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $canEdit ) {
|
|
|
|
// Generate builder drop-down
|
|
|
|
$dropDown = AbuseFilter::getBuilderValues();
|
|
|
|
|
|
|
|
// The array needs to be rearranged to be understood by OOUI
|
|
|
|
foreach ( $dropDown as $group => $values ) {
|
|
|
|
// 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
|
|
|
|
$localisedLabel = $this->msg( "abusefilter-edit-builder-group-$group" )->text();
|
|
|
|
$dropDown[ $localisedLabel ] = $dropDown[ $group ];
|
|
|
|
unset( $dropDown[ $group ] );
|
|
|
|
$dropDown[ $localisedLabel ] = array_flip( $dropDown[ $localisedLabel ] );
|
|
|
|
foreach ( $values as $content => $name ) {
|
|
|
|
$localisedInnerLabel = $this->msg( "abusefilter-edit-builder-$group-$name" )->text();
|
|
|
|
$dropDown[ $localisedLabel ][ $localisedInnerLabel ] = $dropDown[ $localisedLabel ][ $name ];
|
|
|
|
unset( $dropDown[ $localisedLabel ][ $name ] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$dropDown = [ $this->msg( 'abusefilter-edit-builder-select' )->text() => 'other' ] + $dropDown;
|
|
|
|
$dropDown = Xml::listDropDownOptionsOoui( $dropDown );
|
|
|
|
$dropDown = new OOUI\DropdownInputWidget( [
|
|
|
|
'name' => 'wpFilterBuilder',
|
|
|
|
'inputId' => 'wpFilterBuilder',
|
|
|
|
'options' => $dropDown
|
|
|
|
] );
|
|
|
|
|
|
|
|
$dropDown = new OOUI\FieldLayout( $dropDown );
|
|
|
|
$formElements = [ $dropDown ];
|
|
|
|
|
|
|
|
// Button for syntax check
|
|
|
|
$syntaxCheck =
|
|
|
|
new OOUI\ButtonWidget(
|
|
|
|
[
|
|
|
|
'label' => $this->msg( 'abusefilter-edit-check' )->text(),
|
|
|
|
'id' => 'mw-abusefilter-syntaxcheck'
|
|
|
|
] + $noTestAttrib
|
|
|
|
);
|
|
|
|
$group = $syntaxCheck;
|
|
|
|
|
|
|
|
// Button for switching editor (if Ace is used)
|
|
|
|
if ( isset( $switchEditor ) ) {
|
|
|
|
$group =
|
|
|
|
new OOUI\Widget( [
|
|
|
|
'content' => new OOUI\HorizontalLayout( [
|
|
|
|
'items' => [ $switchEditor, $syntaxCheck ]
|
|
|
|
] )
|
|
|
|
] );
|
|
|
|
}
|
|
|
|
$group = new OOUI\FieldLayout( $group );
|
|
|
|
$formElements[] = $group;
|
|
|
|
|
|
|
|
$fieldSet = new OOUI\FieldsetLayout( [
|
|
|
|
'items' => $formElements,
|
|
|
|
'classes' => [ 'mw-abusefilter-edit-buttons' ]
|
|
|
|
] );
|
|
|
|
|
|
|
|
$rulesContainer .= $fieldSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $addResultDiv ) {
|
|
|
|
$rulesContainer .= Xml::element( 'div',
|
|
|
|
[ 'id' => 'mw-abusefilter-syntaxresult', 'style' => 'display: none;' ],
|
|
|
|
' ' );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add script
|
|
|
|
$this->getOutput()->addModules( 'ext.abuseFilter.edit' );
|
|
|
|
AbuseFilter::$editboxName = $textName;
|
|
|
|
|
|
|
|
return $rulesContainer;
|
|
|
|
}
|
|
|
|
|
2017-07-13 08:19:09 +00:00
|
|
|
/**
|
|
|
|
* @param IDatabase $db
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function buildTestConditions( IDatabase $db ) {
|
|
|
|
// If one of these is true, we're abusefilter compatible.
|
|
|
|
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',
|
|
|
|
'rc_log_action' => 'create'
|
|
|
|
], LIST_AND ),
|
2017-08-20 11:48:20 +00:00
|
|
|
$db->makeList( [
|
|
|
|
'rc_log_type' => 'delete',
|
|
|
|
'rc_log_action' => 'delete'
|
|
|
|
], LIST_AND ),
|
|
|
|
// @todo: add upload
|
2017-07-13 08:19:09 +00:00
|
|
|
], LIST_OR ),
|
|
|
|
], LIST_AND ),
|
|
|
|
], LIST_OR );
|
|
|
|
}
|
|
|
|
|
2011-08-24 22:11:52 +00:00
|
|
|
/**
|
2012-01-03 17:29:10 +00:00
|
|
|
* @static
|
2011-08-24 22:11:52 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2018-04-04 21:14:25 +00:00
|
|
|
public static function canViewPrivate() {
|
2009-07-03 14:17:05 +00:00
|
|
|
global $wgUser;
|
|
|
|
static $canView = null;
|
|
|
|
|
|
|
|
if ( is_null( $canView ) ) {
|
2013-07-09 13:35:06 +00:00
|
|
|
$canView = $wgUser->isAllowedAny( 'abusefilter-modify', 'abusefilter-view-private' );
|
2009-07-03 14:17:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $canView;
|
|
|
|
}
|
2017-07-13 08:19:09 +00:00
|
|
|
|
2009-01-29 22:44:31 +00:00
|
|
|
}
|