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.
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.
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
21 namespace MediaWiki\Api
;
23 use MediaWiki\Block\AbstractBlock
;
24 use MediaWiki\Block\Block
;
25 use MediaWiki\Block\CompositeBlock
;
26 use MediaWiki\Block\DatabaseBlock
;
27 use MediaWiki\Block\SystemBlock
;
28 use MediaWiki\Language\Language
;
29 use MediaWiki\User\UserIdentity
;
30 use MediaWiki\Utils\MWTimestamp
;
35 trait ApiBlockInfoTrait
{
38 * Get basic info about a given block
40 * @param Language|null $language
41 * @return array Array containing several keys:
42 * - blockid - ID of the block
43 * - blockedby - username of the blocker
44 * - blockedbyid - user ID of the blocker
45 * - blockreason - reason provided for the block
46 * - blockedtimestamp - timestamp for when the block was placed/modified
47 * - blockedtimestampformatted - blockedtimestamp, formatted for the current locale
48 * - blockexpiry - expiry time of the block
49 * - blockexpiryformatted - blockexpiry formatted for the current locale, omitted if infinite
50 * - blockexpiryrelative - relative time to blockexpiry (e.g. 'in 5 days'), omitted if infinite
51 * - blockpartial - block only applies to certain pages, namespaces and/or actions
52 * - systemblocktype - system block type, if any
53 * - blockcomponents - If the block is a composite block, this will be an array of block
56 private function getBlockDetails(
60 $language ??
= $this->getLanguage();
62 $blocker = $block->getBlocker();
65 $vals['blockid'] = $block->getId();
66 $vals['blockedby'] = $blocker ?
$blocker->getName() : '';
67 $vals['blockedbyid'] = $blocker ?
$blocker->getId() : 0;
68 $vals['blockreason'] = $block->getReasonComment()
69 ->message
->inLanguage( $language )->plain();
70 $vals['blockedtimestamp'] = wfTimestamp( TS_ISO_8601
, $block->getTimestamp() );
71 $expiry = ApiResult
::formatExpiry( $block->getExpiry(), 'infinite' );
72 $vals['blockexpiry'] = $expiry;
73 $vals['blockpartial'] = !$block->isSitewide();
74 $vals['blocknocreate'] = $block->isCreateAccountBlocked();
75 $vals['blockanononly'] = !$block->isHardblock();
76 if ( $block instanceof AbstractBlock
) {
77 $vals['blockemail'] = $block->isEmailBlocked();
78 $vals['blockowntalk'] = !$block->isUsertalkEditAllowed();
81 $user = $this->getUser();
82 // Formatted timestamps
83 $vals['blockedtimestampformatted'] = $language->formatExpiry(
84 $block->getTimestamp(), true, 'infinity', $user
86 if ( $expiry !== 'infinite' ) {
87 $vals['blockexpiryformatted'] = $language->formatExpiry(
88 $expiry, true, 'infinity', $user
90 $vals['blockexpiryrelative'] = $language->getHumanTimestamp(
91 new MWTimestamp( $expiry ), new MWTimestamp(), $user
95 if ( $block instanceof SystemBlock
) {
96 $vals['systemblocktype'] = $block->getSystemBlockType();
99 if ( $block instanceof CompositeBlock
) {
101 foreach ( $block->getOriginalBlocks() as $singleBlock ) {
102 $components[] = $this->getBlockDetails( $singleBlock, $language );
104 $vals['blockcomponents'] = $components;
111 * Get the API error code, to be used in ApiMessage::create or ApiBase::dieWithError
112 * @param Block $block
115 private function getBlockCode( Block
$block ): string {
116 if ( $block instanceof DatabaseBlock
&& $block->getType() === Block
::TYPE_AUTO
) {
117 return 'autoblocked';
122 // region Methods required from ApiBase
123 /** @name Methods required from ApiBase
128 * @see IContextSource::getLanguage
131 abstract public function getLanguage();
134 * @see IContextSource::getUser
135 * @return UserIdentity
137 abstract public function getUser();
140 // endregion -- end of methods required from ApiBase
144 /** @deprecated class alias since 1.43 */
145 class_alias( ApiBlockInfoTrait
::class, 'ApiBlockInfoTrait' );