Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 65
0.00% covered (danger)
0.00%
0 / 18
CRAP
0.00% covered (danger)
0.00%
0 / 1
GlobalBlocking
0.00% covered (danger)
0.00%
0 / 65
0.00% covered (danger)
0.00%
0 / 18
552
0.00% covered (danger)
0.00%
0 / 1
 getUserBlock
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getUserBlockErrors
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getTargetType
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 getGlobalBlockingBlock
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 getRangeCondition
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getPrimaryGlobalBlockingDatabase
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getReplicaGlobalBlockingDatabase
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getGlobalBlockingDatabase
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 getGlobalBlockId
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 purgeExpired
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getLocalWhitelistInfo
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getLocalWhitelistInfoByIP
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 maybeLinkUserpage
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 insertBlock
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 block
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 unblock
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 buildSubtitleLinks
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 selectFields
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\GlobalBlocking;
4
5use Exception;
6use MediaWiki\Extension\GlobalBlocking\Services\GlobalBlockLookup;
7use MediaWiki\MediaWikiServices;
8use MediaWiki\SpecialPage\SpecialPage;
9use MediaWiki\User\User;
10use Message;
11use StatusValue;
12use stdClass;
13use Wikimedia\IPUtils;
14use Wikimedia\Rdbms\DBUnexpectedError;
15use Wikimedia\Rdbms\IExpression;
16
17/**
18 * Static utility class of the GlobalBlocking extension.
19 *
20 * @license GPL-2.0-or-later
21 */
22class GlobalBlocking {
23    private const TYPE_IP = 2;
24    private const TYPE_RANGE = 3;
25
26    /**
27     * @param User $user
28     * @param string|null $ip
29     * @return GlobalBlock|null
30     * @deprecated Since 1.42. Use GlobalBlockLookup::getUserBlock.
31     */
32    public static function getUserBlock( $user, $ip ) {
33        return GlobalBlockingServices::wrap( MediaWikiServices::getInstance() )
34            ->getGlobalBlockLookup()
35            ->getUserBlock( $user, $ip );
36    }
37
38    /**
39     * @param User $user
40     * @param string $ip
41     * @return Message[] empty or message objects
42     * @deprecated Since 1.42. Use GlobalBlockLookup::getUserBlockErrors.
43     */
44    public static function getUserBlockErrors( $user, $ip ) {
45        return GlobalBlockingServices::wrap( MediaWikiServices::getInstance() )
46            ->getGlobalBlockLookup()
47            ->getUserBlockErrors( $user, $ip );
48    }
49
50    /**
51     * @deprecated Since 1.42 without replacement.
52     */
53    public static function getTargetType( $target ) {
54        wfDeprecated( __METHOD__, '1.42' );
55        if ( IPUtils::isValid( $target ) ) {
56            return self::TYPE_IP;
57        } elseif ( IPUtils::isValidRange( $target ) ) {
58            return self::TYPE_RANGE;
59        }
60    }
61
62    /**
63     * Get a block
64     * @param string|null $ip The IP address to be checked
65     * @param bool $anon Include anon-only blocks
66     * @return stdClass|false The block, or false if none is found
67     * @deprecated Since 1.42. Use GlobalBlockLookup::getGlobalBlockingBlock.
68     */
69    public static function getGlobalBlockingBlock( $ip, $anon ) {
70        $flags = GlobalBlockLookup::SKIP_LOCAL_DISABLE_CHECK;
71        if ( !$anon ) {
72            $flags |= GlobalBlockLookup::SKIP_SOFT_IP_BLOCKS;
73        }
74        // Don't attempt to pass a central ID as this deprecated method does not support it,
75        // so pass 0 (which is the value to indicate no user-based blocks should be checked).
76        $result = GlobalBlockingServices::wrap( MediaWikiServices::getInstance() )
77            ->getGlobalBlockLookup()
78            ->getGlobalBlockingBlock( $ip, 0, $flags );
79        if ( $result === null ) {
80            return false;
81        }
82        return $result;
83    }
84
85    /**
86     * Get a database range condition for an IP address
87     * @param string $ip The IP address or range
88     * @return IExpression[] a SQL condition
89     * @deprecated Since 1.42. Use GlobalBlockLookup::getRangeCondition.
90     */
91    public static function getRangeCondition( $ip ) {
92        $conds = GlobalBlockingServices::wrap( MediaWikiServices::getInstance() )
93            ->getGlobalBlockLookup()
94            ->getRangeCondition( $ip );
95        return [ $conds ];
96    }
97
98    /**
99     * @deprecated Since 1.42. Use GlobalBlockingConnectionProvider::getPrimaryGlobalBlockingDatabase.
100     * @return \Wikimedia\Rdbms\IDatabase
101     */
102    public static function getPrimaryGlobalBlockingDatabase() {
103        return GlobalBlockingServices::wrap( MediaWikiServices::getInstance() )
104            ->getGlobalBlockingConnectionProvider()
105            ->getPrimaryGlobalBlockingDatabase();
106    }
107
108    /**
109     * @deprecated Since 1.42. Use GlobalBlockingConnectionProvider::getReplicaGlobalBlockingDatabase.
110     * @return \Wikimedia\Rdbms\IReadableDatabase
111     */
112    public static function getReplicaGlobalBlockingDatabase() {
113        return GlobalBlockingServices::wrap( MediaWikiServices::getInstance() )
114            ->getGlobalBlockingConnectionProvider()
115            ->getReplicaGlobalBlockingDatabase();
116    }
117
118    /**
119     * @param int $dbtype either DB_REPLICA or DB_PRIMARY
120     * @deprecated Since 1.42. Use GlobalBlockingConnectionProvider to get a database connection.
121     * @return \Wikimedia\Rdbms\IDatabase|\Wikimedia\Rdbms\IReadableDatabase
122     */
123    public static function getGlobalBlockingDatabase( $dbtype ) {
124        wfDeprecated( __METHOD__, '1.42' );
125        if ( $dbtype == DB_PRIMARY ) {
126            return self::getPrimaryGlobalBlockingDatabase();
127        } else {
128            return self::getReplicaGlobalBlockingDatabase();
129        }
130    }
131
132    /**
133     * @param string $ip
134     * @param int $dbtype either DB_REPLICA or DB_PRIMARY
135     * @return int
136     * @deprecated Since 1.42. Use GlobalBlockLookup::getGlobalBlockId.
137     */
138    public static function getGlobalBlockId( $ip, $dbtype = DB_REPLICA ) {
139        return GlobalBlockingServices::wrap( MediaWikiServices::getInstance() )
140            ->getGlobalBlockLookup()
141            ->getGlobalBlockId( $ip, $dbtype );
142    }
143
144    /**
145     * Purge stale block rows.
146     *
147     * This is expensive. It involves opening a connection to a new primary database,
148     * and doing a write query. We should only do it when a connection to the primary database
149     * is already open (currently, when a global block is made).
150     *
151     * @throws DBUnexpectedError
152     * @deprecated Since 1.42. Use GlobalBlockingBlockPurger::purgeExpiredBlocks.
153     */
154    public static function purgeExpired() {
155        wfDeprecated( __METHOD__, '1.42' );
156        GlobalBlockingServices::wrap( MediaWikiServices::getInstance() )
157            ->getGlobalBlockingBlockPurger()
158            ->purgeExpiredBlocks();
159    }
160
161    /**
162     * @param null|int $id
163     * @param null|string $address
164     * @return array|false
165     * @phan-return array{user:int,reason:string}|false
166     * @throws Exception
167     * @deprecated Since 1.42. Use GlobalBlockLocalStatusLookup::getLocalWhitelistInfo.
168     */
169    public static function getLocalWhitelistInfo( $id = null, $address = null ) {
170        return GlobalBlockingServices::wrap( MediaWikiServices::getInstance() )
171            ->getGlobalBlockLocalStatusLookup()
172            ->getLocalWhitelistInfo( $id, $address );
173    }
174
175    /**
176     * @param string $block_ip
177     * @return array|false
178     * @phan-return array{user:int,reason:string}|false
179     * @deprecated Since 1.42. Use GlobalBlockLocalStatusLookup::getLocalWhitelistInfo.
180     */
181    public static function getLocalWhitelistInfoByIP( $block_ip ) {
182        wfDeprecated( __METHOD__, '1.42' );
183        return GlobalBlockingServices::wrap( MediaWikiServices::getInstance() )
184            ->getGlobalBlockLocalStatusLookup()
185            ->getLocalWhitelistInfoByIP( $block_ip );
186    }
187
188    /**
189     * @param string $wiki_id
190     * @param string $user
191     * @return string
192     * @deprecated Since 1.42. Use GlobalBlockingLinkBuilder::maybeLinkUserpage.
193     */
194    public static function maybeLinkUserpage( $wiki_id, $user ) {
195        return GlobalBlockingServices::wrap( MediaWikiServices::getInstance() )
196            ->getGlobalBlockingLinkBuilder()
197            ->maybeLinkUserpage( $wiki_id, $user );
198    }
199
200    /**
201     * @param string $address
202     * @param string $reason
203     * @param string|false $expiry
204     * @param User $blocker
205     * @param array $options
206     * @return StatusValue
207     * @deprecated Since 1.42. Use GlobalBlockManager::block which will also create a log entry.
208     */
209    public static function insertBlock( $address, $reason, $expiry, $blocker, $options = [] ) {
210        wfDeprecated( __METHOD__, '1.42' );
211        return GlobalBlockingServices::wrap( MediaWikiServices::getInstance() )
212            ->getGlobalBlockManager()
213            ->insertBlock( $address, $reason, $expiry, $blocker, $options );
214    }
215
216    /**
217     * @param string $address
218     * @param string $reason
219     * @param string $expiry
220     * @param User $blocker
221     * @param array $options
222     * @return StatusValue An empty or fatal status
223     * @deprecated Since 1.42. Use GlobalBlockManager::block.
224     */
225    public static function block( $address, $reason, $expiry, $blocker, $options = [] ): StatusValue {
226        return GlobalBlockingServices::wrap( MediaWikiServices::getInstance() )
227            ->getGlobalBlockManager()
228            ->block( $address, $reason, $expiry, $blocker, $options );
229    }
230
231    /**
232     * @param string $address
233     * @param string $reason
234     * @param User $performer
235     * @return StatusValue An empty or fatal status
236     * @deprecated Since 1.42. Use GlobalBlockManager::unblock.
237     */
238    public static function unblock( string $address, string $reason, User $performer ): StatusValue {
239        return GlobalBlockingServices::wrap( MediaWikiServices::getInstance() )
240            ->getGlobalBlockManager()
241            ->unblock( $address, $reason, $performer );
242    }
243
244    /**
245     * Build links to other global blocking special pages, shown in the subtitle
246     * @param SpecialPage $sp SpecialPage instance for context
247     * @return string links to special pages
248     * @deprecated Since 1.42. Use GlobalBlockingLinkBuilder::buildSubtitleLinks.
249     */
250    public static function buildSubtitleLinks( SpecialPage $sp ) {
251        return GlobalBlockingServices::wrap( MediaWikiServices::getInstance() )
252            ->getGlobalBlockingLinkBuilder()
253            ->buildSubtitleLinks( $sp );
254    }
255
256    /**
257     * @deprecated Since 1.42. Use GlobalBlockLookup::selectFields instead.
258     */
259    public static function selectFields() {
260        return GlobalBlockLookup::selectFields();
261    }
262}