Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
76.92% covered (warning)
76.92%
30 / 39
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiBlockInfoHelper
76.92% covered (warning)
76.92%
30 / 39
0.00% covered (danger)
0.00%
0 / 2
13.77
0.00% covered (danger)
0.00%
0 / 1
 getBlockDetails
83.33% covered (warning)
83.33%
30 / 36
0.00% covered (danger)
0.00%
0 / 1
9.37
 getBlockCode
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace MediaWiki\Api;
8
9use MediaWiki\Block\AbstractBlock;
10use MediaWiki\Block\Block;
11use MediaWiki\Block\CompositeBlock;
12use MediaWiki\Block\DatabaseBlock;
13use MediaWiki\Block\SystemBlock;
14use MediaWiki\Language\Language;
15use MediaWiki\User\UserIdentity;
16use MediaWiki\Utils\MWTimestamp;
17use Wikimedia\Timestamp\TimestampFormat as TS;
18
19/**
20 * Helper class for API modules that display block information. Intended for use via
21 * composition.
22 *
23 * @ingroup API
24 * @since 1.44
25 */
26class ApiBlockInfoHelper {
27
28    /**
29     * Get basic info about a given block
30     *
31     * @return array Array containing several keys:
32     *  - blockid - ID of the block
33     *  - blockedby - username of the blocker
34     *  - blockedbyid - user ID of the blocker
35     *  - blockreason - reason provided for the block
36     *  - blockedtimestamp - timestamp for when the block was placed/modified
37     *  - blockedtimestampformatted - blockedtimestamp, formatted for the current locale
38     *  - blockexpiry - expiry time of the block
39     *  - blockexpiryformatted - blockexpiry formatted for the current locale, omitted if infinite
40     *  - blockexpiryrelative - relative time to blockexpiry (e.g. 'in 5 days'), omitted if infinite
41     *  - blockpartial - block only applies to certain pages, namespaces and/or actions
42     *  - systemblocktype - system block type, if any
43     *  - blockcomponents - If the block is a composite block, this will be an array of block
44     *    info arrays
45     */
46    public function getBlockDetails(
47        Block $block,
48        Language $language,
49        UserIdentity $user
50    ) {
51        $blocker = $block->getBlocker();
52        $expiry = ApiResult::formatExpiry( $block->getExpiry(), 'infinite' );
53
54        $vals = [
55            'blockid' => $block->getId(),
56            'blockedby' => $blocker ? $blocker->getName() : '',
57            'blockedbyid' => $blocker ? $blocker->getId() : 0,
58            'blockreason' => $block->getReasonComment()->message->inLanguage( $language )->plain(),
59            'blockedtimestamp' => wfTimestamp( TS::ISO_8601, $block->getTimestamp() ),
60            'blockexpiry' => $expiry,
61            'blockpartial' => !$block->isSitewide(),
62            'blocknocreate' => $block->isCreateAccountBlocked(),
63            'blockanononly' => !$block->isHardblock(),
64        ];
65
66        if ( $block instanceof AbstractBlock ) {
67            $vals['blockemail'] = $block->isEmailBlocked();
68            $vals['blockowntalk'] = !$block->isUsertalkEditAllowed();
69        }
70        if ( $block instanceof DatabaseBlock ) {
71            $vals['blockautoblocking'] = $block->isAutoblocking();
72        }
73
74        // Formatted timestamps
75        $vals['blockedtimestampformatted'] = $language->formatExpiry(
76            $block->getTimestamp(), true, 'infinity', $user
77        );
78        if ( $expiry !== 'infinite' ) {
79            $vals['blockexpiryformatted'] = $language->formatExpiry(
80                $expiry, true, 'infinity', $user
81            );
82            $vals['blockexpiryrelative'] = $language->getHumanTimestamp(
83                new MWTimestamp( $expiry ), new MWTimestamp(), $user
84            );
85        }
86
87        if ( $block instanceof SystemBlock ) {
88            $vals['systemblocktype'] = $block->getSystemBlockType();
89        }
90
91        if ( $block instanceof CompositeBlock ) {
92            $components = [];
93            foreach ( $block->getOriginalBlocks() as $singleBlock ) {
94                $components[] = $this->getBlockDetails( $singleBlock, $language, $user );
95            }
96            $vals['blockcomponents'] = $components;
97        }
98
99        return $vals;
100    }
101
102    /**
103     * Get the API error code, to be used in ApiMessage::create or ApiBase::dieWithError
104     * @param Block $block
105     * @return string
106     */
107    public function getBlockCode( Block $block ): string {
108        if ( $block instanceof DatabaseBlock && $block->getType() === Block::TYPE_AUTO ) {
109            return 'autoblocked';
110        }
111        return 'blocked';
112    }
113
114}