Debian Codegraph @

includes/api/ApiBlockInfoTrait.php

raw symbols SCIP
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 namespace MediaWiki\Api;
22
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\User;
30 use MediaWiki\Utils\MWTimestamp;
31
32 /**
33 * @ingroup API
34 */
35 trait ApiBlockInfoTrait {
36
37 /**
38 * Get basic info about a given block
39 * @param Block $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
54 * info arrays
55 */
56 private function getBlockDetails(
57 Block $block,
58 $language = null
59 ) {
60 $language ??= $this->getLanguage();
61
62 $blocker = $block->getBlocker();
63
64 $vals = [];
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();
79 }
80
81 $user = $this->getUser();
82 // Formatted timestamps
83 $vals['blockedtimestampformatted'] = $language->formatExpiry(
84 $block->getTimestamp(), true, 'infinity', $user
85 );
86 if ( $expiry !== 'infinite' ) {
87 $vals['blockexpiryformatted'] = $language->formatExpiry(
88 $expiry, true, 'infinity', $user
89 );
90 $vals['blockexpiryrelative'] = $language->getHumanTimestamp(
91 new MWTimestamp( $expiry ), new MWTimestamp(), $user
92 );
93 }
94
95 if ( $block instanceof SystemBlock ) {
96 $vals['systemblocktype'] = $block->getSystemBlockType();
97 }
98
99 if ( $block instanceof CompositeBlock ) {
100 $components = [];
101 foreach ( $block->getOriginalBlocks() as $singleBlock ) {
102 $components[] = $this->getBlockDetails( $singleBlock, $language );
103 }
104 $vals['blockcomponents'] = $components;
105 }
106
107 return $vals;
108 }
109
110 /**
111 * Get the API error code, to be used in ApiMessage::create or ApiBase::dieWithError
112 * @param Block $block
113 * @return string
114 */
115 private function getBlockCode( Block $block ): string {
116 if ( $block instanceof DatabaseBlock && $block->getType() === Block::TYPE_AUTO ) {
117 return 'autoblocked';
118 }
119 return 'blocked';
120 }
121
122 // region Methods required from ApiBase
123 /** @name Methods required from ApiBase
124 * @{
125 */
126
127 /**
128 * @see IContextSource::getLanguage
129 * @return Language
130 */
131 abstract public function getLanguage();
132
133 /**
134 * @see IContextSource::getUser
135 * @return User
136 */
137 abstract public function getUser();
138
139 /** @} */
140 // endregion -- end of methods required from ApiBase
141
142 }
143
144 /** @deprecated class alias since 1.43 */
145 class_alias( ApiBlockInfoTrait::class, 'ApiBlockInfoTrait' );