| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter; |
| 4 |
|
| 5 |
use MediaWiki\Extension\AbuseFilter\Filter\AbstractFilter; |
| 6 |
use MediaWiki\Language\Language; |
| 7 |
use MediaWiki\Language\RawMessage; |
| 8 |
use MediaWiki\Message\Message; |
| 9 |
use MessageLocalizer; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class SpecsFormatter { |
| 15 |
public const SERVICE_NAME = 'AbuseFilterSpecsFormatter'; |
| 16 |
|
| 17 |
|
| 18 |
private $messageLocalizer; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public function __construct( MessageLocalizer $messageLocalizer ) { |
| 24 |
$this->messageLocalizer = $messageLocalizer; |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public function setMessageLocalizer( MessageLocalizer $messageLocalizer ): void { |
| 31 |
$this->messageLocalizer = $messageLocalizer; |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public function getActionDisplay( string $action ): string { |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
$msg = $this->messageLocalizer->msg( "abusefilter-action-$action" ); |
| 45 |
return $msg->isDisabled() ? htmlspecialchars( $action ) : $msg->escaped(); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public function getActionMessage( string $action ): Message { |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
$msg = $this->messageLocalizer->msg( "abusefilter-action-$action" ); |
| 58 |
|
| 59 |
return $msg->isDisabled() ? new RawMessage( $action ) : $msg; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public function formatAction( string $action, array $parameters, Language $lang ): string { |
| 69 |
if ( count( $parameters ) === 0 || ( $action === 'block' && count( $parameters ) !== 3 ) ) { |
| 70 |
$displayAction = $this->getActionDisplay( $action ); |
| 71 |
} elseif ( $action === 'block' ) { |
| 72 |
|
| 73 |
$messages = [ |
| 74 |
$this->messageLocalizer->msg( 'abusefilter-block-anon' )->escaped() . |
| 75 |
$this->messageLocalizer->msg( 'colon-separator' )->escaped() . |
| 76 |
$lang->translateBlockExpiry( $parameters[1] ), |
| 77 |
$this->messageLocalizer->msg( 'abusefilter-block-user' )->escaped() . |
| 78 |
$this->messageLocalizer->msg( 'colon-separator' )->escaped() . |
| 79 |
$lang->translateBlockExpiry( $parameters[2] ) |
| 80 |
]; |
| 81 |
if ( $parameters[0] === 'blocktalk' ) { |
| 82 |
$messages[] = $this->messageLocalizer->msg( 'abusefilter-block-talk' )->escaped(); |
| 83 |
} |
| 84 |
$displayAction = $lang->commaList( $messages ); |
| 85 |
} elseif ( $action === 'throttle' ) { |
| 86 |
array_shift( $parameters ); |
| 87 |
[ $actions, $time ] = explode( ',', array_shift( $parameters ) ); |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
foreach ( $parameters as &$val ) { |
| 94 |
if ( strpos( $val, ',' ) !== false ) { |
| 95 |
$subGroups = explode( ',', $val ); |
| 96 |
foreach ( $subGroups as &$group ) { |
| 97 |
$msg = $this->messageLocalizer->msg( "abusefilter-throttle-$group" ); |
| 98 |
|
| 99 |
|
| 100 |
$group = $msg->exists() ? $msg->text() : $group; |
| 101 |
} |
| 102 |
unset( $group ); |
| 103 |
$val = $lang->listToText( $subGroups ); |
| 104 |
} else { |
| 105 |
$msg = $this->messageLocalizer->msg( "abusefilter-throttle-$val" ); |
| 106 |
$val = $msg->exists() ? $msg->text() : $val; |
| 107 |
} |
| 108 |
} |
| 109 |
unset( $val ); |
| 110 |
$groups = $lang->semicolonList( $parameters ); |
| 111 |
|
| 112 |
$displayAction = $this->getActionDisplay( $action ) . |
| 113 |
$this->messageLocalizer->msg( 'colon-separator' )->escaped() . |
| 114 |
$this->messageLocalizer->msg( 'abusefilter-throttle-details' ) |
| 115 |
->params( $actions, $time, $groups )->escaped(); |
| 116 |
} else { |
| 117 |
$displayAction = $this->getActionDisplay( $action ) . |
| 118 |
$this->messageLocalizer->msg( 'colon-separator' )->escaped() . |
| 119 |
$lang->semicolonList( array_map( 'htmlspecialchars', $parameters ) ); |
| 120 |
} |
| 121 |
|
| 122 |
return $displayAction; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public function formatFlags( string $value, Language $lang ): string { |
| 131 |
$flags = array_filter( explode( ',', $value ) ); |
| 132 |
$flagsDisplay = []; |
| 133 |
foreach ( $flags as $flag ) { |
| 134 |
$flagsDisplay[] = $this->messageLocalizer->msg( "abusefilter-history-$flag" )->escaped(); |
| 135 |
} |
| 136 |
|
| 137 |
return $lang->commaList( $flagsDisplay ); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public function formatFilterFlags( AbstractFilter $filter, Language $lang ): string { |
| 146 |
$flags = array_filter( [ |
| 147 |
'enabled' => $filter->isEnabled(), |
| 148 |
'deleted' => $filter->isDeleted(), |
| 149 |
'hidden' => $filter->isHidden(), |
| 150 |
'protected' => $filter->isProtected(), |
| 151 |
'global' => $filter->isGlobal() |
| 152 |
] ); |
| 153 |
$flagsDisplay = []; |
| 154 |
foreach ( $flags as $flag => $_ ) { |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
$flagsDisplay[] = $this->messageLocalizer->msg( "abusefilter-history-$flag" )->escaped(); |
| 162 |
} |
| 163 |
|
| 164 |
return $lang->commaList( $flagsDisplay ); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public function nameGroup( string $group ): string { |
| 174 |
|
| 175 |
$msg = $this->messageLocalizer->msg( "abusefilter-group-$group" ); |
| 176 |
return $msg->isDisabled() ? $group : $msg->escaped(); |
| 177 |
} |
| 178 |
} |