MediaWiki master
AutoblockExemptionList.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Block;
4
5use Generator;
8use Psr\Log\LoggerInterface;
9use Wikimedia\IPUtils;
12
19 public const CONSTRUCTOR_OPTIONS = [
21 ];
22
23 private ServiceOptions $options;
24 private LoggerInterface $logger;
26 private ITextFormatter $textFormatter;
27
28 public function __construct(
29 ServiceOptions $options,
30 LoggerInterface $logger,
31 ITextFormatter $textFormatter
32 ) {
33 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
34 $this->options = $options;
35 $this->logger = $logger;
36 $this->textFormatter = $textFormatter;
37 }
38
40 private function getOnWikiExemptionList() {
41 $list = $this->textFormatter->format(
42 MessageValue::new( 'block-autoblock-exemptionlist' )
43 );
44 $lines = explode( "\n", $list );
45
46 foreach ( $lines as $line ) {
47 // List items only
48 if ( !str_starts_with( $line, '*' ) ) {
49 continue;
50 }
51
52 $wlEntry = substr( $line, 1 );
53 $wlEntry = trim( $wlEntry );
54 yield $wlEntry;
55 }
56 }
57
59 private function getExemptionList() {
60 // @phan-suppress-next-line PhanTypeInvalidYieldFrom
61 yield from $this->options->get( MainConfigNames::AutoblockExemptions );
62 yield from $this->getOnWikiExemptionList();
63 }
64
71 public function isExempt( $ip ) {
72 $this->logger->debug( "Checking the autoblock exemption list.." );
73 foreach ( $this->getExemptionList() as $wlEntry ) {
74 $this->logger->debug( "Checking $ip against $wlEntry..." );
75
76 // Is the IP in this range?
77 if ( IPUtils::isInRange( $ip, $wlEntry ) ) {
78 $this->logger->debug( " IP $ip matches $wlEntry, not autoblocking" );
79 return true;
80 } else {
81 $this->logger->debug( " No match" );
82 }
83 }
84
85 return false;
86 }
87}
Provides access to the wiki's autoblock exemption list.
isExempt( $ip)
Checks whether a given IP is on the autoblock exemption list.
__construct(ServiceOptions $options, LoggerInterface $logger, ITextFormatter $textFormatter)
A class for passing options to services.
assertRequiredOptions(array $expectedKeys)
Assert that the list of options provided in this instance exactly match $expectedKeys,...
A class containing constants representing the names of configuration variables.
const AutoblockExemptions
Name constant for the AutoblockExemptions setting, for use with Config::get()
Value object representing a message for i18n.
format(MessageSpecifier $message)
Convert a MessageSpecifier to text.