Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
97.44% |
38 / 39 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
ApiBlockInfoTrait | |
97.44% |
38 / 39 |
|
50.00% |
1 / 2 |
11 | |
0.00% |
0 / 1 |
getBlockDetails | |
100.00% |
36 / 36 |
|
100.00% |
1 / 1 |
8 | |||
getBlockCode | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
3.33 | |||
getLanguage | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
getUser | n/a |
0 / 0 |
n/a |
0 / 0 |
0 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | use MediaWiki\Block\AbstractBlock; |
22 | use MediaWiki\Block\Block; |
23 | use MediaWiki\Block\CompositeBlock; |
24 | use MediaWiki\Block\DatabaseBlock; |
25 | use MediaWiki\Block\SystemBlock; |
26 | use MediaWiki\User\User; |
27 | use MediaWiki\Utils\MWTimestamp; |
28 | |
29 | /** |
30 | * @ingroup API |
31 | */ |
32 | trait ApiBlockInfoTrait { |
33 | |
34 | /** |
35 | * Get basic info about a given block |
36 | * @param Block $block |
37 | * @param Language|null $language |
38 | * @return array Array containing several keys: |
39 | * - blockid - ID of the block |
40 | * - blockedby - username of the blocker |
41 | * - blockedbyid - user ID of the blocker |
42 | * - blockreason - reason provided for the block |
43 | * - blockedtimestamp - timestamp for when the block was placed/modified |
44 | * - blockedtimestampformatted - blockedtimestamp, formatted for the current locale |
45 | * - blockexpiry - expiry time of the block |
46 | * - blockexpiryformatted - blockexpiry formatted for the current locale, omitted if infinite |
47 | * - blockexpiryrelative - relative time to blockexpiry (e.g. 'in 5 days'), omitted if infinite |
48 | * - blockpartial - block only applies to certain pages, namespaces and/or actions |
49 | * - systemblocktype - system block type, if any |
50 | * - blockcomponents - If the block is a composite block, this will be an array of block |
51 | * info arrays |
52 | */ |
53 | private function getBlockDetails( |
54 | Block $block, |
55 | $language = null |
56 | ) { |
57 | $language ??= $this->getLanguage(); |
58 | |
59 | $blocker = $block->getBlocker(); |
60 | |
61 | $vals = []; |
62 | $vals['blockid'] = $block->getId(); |
63 | $vals['blockedby'] = $blocker ? $blocker->getName() : ''; |
64 | $vals['blockedbyid'] = $blocker ? $blocker->getId() : 0; |
65 | $vals['blockreason'] = $block->getReasonComment() |
66 | ->message->inLanguage( $language )->plain(); |
67 | $vals['blockedtimestamp'] = wfTimestamp( TS_ISO_8601, $block->getTimestamp() ); |
68 | $expiry = ApiResult::formatExpiry( $block->getExpiry(), 'infinite' ); |
69 | $vals['blockexpiry'] = $expiry; |
70 | $vals['blockpartial'] = !$block->isSitewide(); |
71 | $vals['blocknocreate'] = $block->isCreateAccountBlocked(); |
72 | $vals['blockanononly'] = !$block->isHardblock(); |
73 | if ( $block instanceof AbstractBlock ) { |
74 | $vals['blockemail'] = $block->isEmailBlocked(); |
75 | $vals['blockowntalk'] = !$block->isUsertalkEditAllowed(); |
76 | } |
77 | |
78 | $user = $this->getUser(); |
79 | // Formatted timestamps |
80 | $vals['blockedtimestampformatted'] = $language->formatExpiry( |
81 | $block->getTimestamp(), true, 'infinity', $user |
82 | ); |
83 | if ( $expiry !== 'infinite' ) { |
84 | $vals['blockexpiryformatted'] = $language->formatExpiry( |
85 | $expiry, true, 'infinity', $user |
86 | ); |
87 | $vals['blockexpiryrelative'] = $language->getHumanTimestamp( |
88 | new MWTimestamp( $expiry ), new MWTimestamp(), $user |
89 | ); |
90 | } |
91 | |
92 | if ( $block instanceof SystemBlock ) { |
93 | $vals['systemblocktype'] = $block->getSystemBlockType(); |
94 | } |
95 | |
96 | if ( $block instanceof CompositeBlock ) { |
97 | $components = []; |
98 | foreach ( $block->getOriginalBlocks() as $singleBlock ) { |
99 | $components[] = $this->getBlockDetails( $singleBlock, $language ); |
100 | } |
101 | $vals['blockcomponents'] = $components; |
102 | } |
103 | |
104 | return $vals; |
105 | } |
106 | |
107 | /** |
108 | * Get the API error code, to be used in ApiMessage::create or ApiBase::dieWithError |
109 | * @param Block $block |
110 | * @return string |
111 | */ |
112 | private function getBlockCode( Block $block ): string { |
113 | if ( $block instanceof DatabaseBlock && $block->getType() === Block::TYPE_AUTO ) { |
114 | return 'autoblocked'; |
115 | } |
116 | return 'blocked'; |
117 | } |
118 | |
119 | // region Methods required from ApiBase |
120 | /** @name Methods required from ApiBase |
121 | * @{ |
122 | */ |
123 | |
124 | /** |
125 | * @see IContextSource::getLanguage |
126 | * @return Language |
127 | */ |
128 | abstract public function getLanguage(); |
129 | |
130 | /** |
131 | * @see IContextSource::getUser |
132 | * @return User |
133 | */ |
134 | abstract public function getUser(); |
135 | |
136 | /** @} */ |
137 | // endregion -- end of methods required from ApiBase |
138 | |
139 | } |