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