mediawiki-extensions-AbuseF.../includes/EditBox/AceEditBoxBuiler.php
libraryupgrader 5377ebe819 build: Updating dependencies
composer:
* mediawiki/mediawiki-codesniffer: 36.0.0 → 37.0.0

npm:
* postcss: 7.0.35 → 7.0.36
  * https://npmjs.com/advisories/1693 (CVE-2021-23368)

Change-Id: I2b382f3bb236fb44eb24c6a257b13b8fd886541c
2021-07-21 18:51:18 +00:00

111 lines
3.4 KiB
PHP

<?php
namespace MediaWiki\Extension\AbuseFilter\EditBox;
use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager;
use MediaWiki\Extension\AbuseFilter\KeywordsManager;
use MediaWiki\Extension\AbuseFilter\Parser\AbuseFilterCachingParser;
use MediaWiki\Extension\AbuseFilter\Parser\AbuseFilterTokenizer;
use MessageLocalizer;
use OOUI\ButtonWidget;
use OOUI\HorizontalLayout;
use OOUI\Widget;
use OutputPage;
use User;
use Xml;
/**
* Class responsible for building filter edit boxes with both the Ace and the plain version
*/
class AceEditBoxBuiler extends EditBoxBuilder {
/** @var PlainEditBoxBuiler */
private $plainBuilder;
/**
* @inheritDoc
* @param PlainEditBoxBuiler $plainBuilder
*/
public function __construct(
AbuseFilterPermissionManager $afPermManager,
KeywordsManager $keywordsManager,
MessageLocalizer $messageLocalizer,
User $user,
OutputPage $output,
PlainEditBoxBuiler $plainBuilder
) {
parent::__construct( $afPermManager, $keywordsManager, $messageLocalizer, $user, $output );
$this->plainBuilder = $plainBuilder;
}
/**
* @inheritDoc
*/
protected function getEditBox( string $rules, bool $isUserAllowed, bool $externalForm ): string {
$rules = rtrim( $rules ) . "\n";
$attribs = [
// Rules are in English
'dir' => 'ltr',
'name' => 'wpAceFilterEditor',
'id' => 'wpAceFilterEditor',
'class' => 'mw-abusefilter-editor'
];
$rulesContainer = Xml::element( 'div', $attribs, $rules );
$editorConfig = $this->getAceConfig( $isUserAllowed );
$this->output->addJsConfigVars( 'aceConfig', $editorConfig );
return $rulesContainer . $this->plainBuilder->getEditBox( $rules, $isUserAllowed, $externalForm );
}
/**
* @inheritDoc
*/
protected function getEditorControls(): Widget {
$base = parent::getEditorControls();
$switchEditor = new ButtonWidget(
[
'label' => $this->localizer->msg( 'abusefilter-edit-switch-editor' )->text(),
'id' => 'mw-abusefilter-switcheditor'
]
);
return new Widget( [
'content' => new HorizontalLayout( [
'items' => [ $switchEditor, $base ]
] )
] );
}
/**
* Extract values for syntax highlight
*
* @param bool $canEdit
* @return array
*/
private function getAceConfig( bool $canEdit ): array {
$values = $this->keywordsManager->getBuilderValues();
$deprecatedVars = $this->keywordsManager->getDeprecatedVariables();
$builderVariables = implode( '|', array_keys( $values['vars'] ) );
$builderFunctions = implode( '|', array_keys( AbuseFilterCachingParser::FUNCTIONS ) );
// AbuseFilterTokenizer::KEYWORDS also includes constants (true, false and null),
// but Ace redefines these constants afterwards so this will not be an issue
$builderKeywords = implode( '|', AbuseFilterTokenizer::KEYWORDS );
// Extract operators from tokenizer like we do in AbuseFilterParserTest
$operators = implode( '|', array_map( static function ( $op ) {
return preg_quote( $op, '/' );
}, AbuseFilterTokenizer::OPERATORS ) );
$deprecatedVariables = implode( '|', array_keys( $deprecatedVars ) );
$disabledVariables = implode( '|', array_keys( $this->keywordsManager->getDisabledVariables() ) );
return [
'variables' => $builderVariables,
'functions' => $builderFunctions,
'keywords' => $builderKeywords,
'operators' => $operators,
'deprecated' => $deprecatedVariables,
'disabled' => $disabledVariables,
'aceReadOnly' => !$canEdit
];
}
}