Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
14 / 14 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
BlockInfoRetriever | |
100.00% |
14 / 14 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getName | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
retrieveFromIP | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\IPInfo\InfoRetriever; |
4 | |
5 | use MediaWiki\Block\Block; |
6 | use MediaWiki\Block\BlockManager; |
7 | use MediaWiki\IPInfo\Info\BlockInfo; |
8 | |
9 | class BlockInfoRetriever implements InfoRetriever { |
10 | private BlockManager $blockManager; |
11 | |
12 | public function __construct( BlockManager $blockManager ) { |
13 | $this->blockManager = $blockManager; |
14 | } |
15 | |
16 | /** @inheritDoc */ |
17 | public function getName(): string { |
18 | return 'ipinfo-source-block'; |
19 | } |
20 | |
21 | /** @inheritDoc */ |
22 | public function retrieveFromIP( string $ip ): BlockInfo { |
23 | // Active block(s) |
24 | $activeBlock = $this->blockManager->getIPBlock( $ip, true ); |
25 | |
26 | if ( $activeBlock ) { |
27 | // SECURITY: do not include autoblocks in the number of blocks shown to the user, T310763 |
28 | $allBlocks = $activeBlock->toArray(); |
29 | $nonAutoBlocks = array_filter( |
30 | $allBlocks, |
31 | static function ( $block ) { |
32 | return $block->getType() !== Block::TYPE_AUTO; |
33 | } |
34 | ); |
35 | $numActiveBlocks = count( $nonAutoBlocks ); |
36 | } else { |
37 | $numActiveBlocks = 0; |
38 | } |
39 | |
40 | // Past block(s) |
41 | // |
42 | // TODO |
43 | // |
44 | // Notes: |
45 | // |
46 | // * The ipblocks table only stores details of active or recently expired blocks. Expired |
47 | // blocks can be purged from the database at any time by running the |
48 | // maintenance/purgeExpiredBlocks.php script in MediaWiki Core |
49 | // |
50 | // * All blocks, reblocks, and unblocks have rows in the logging table. However, the |
51 | // table does not support querying by IP address range like the ipblocks table does. |
52 | |
53 | return new BlockInfo( $numActiveBlocks ); |
54 | } |
55 | } |