MediaWiki master
ApiBlockInfoTrait.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Api;
22
31
36
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
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
131 abstract public function getLanguage();
132
137 abstract public function getUser();
138
140 // endregion -- end of methods required from ApiBase
141
142}
143
145class_alias( ApiBlockInfoTrait::class, 'ApiBlockInfoTrait' );
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
static formatExpiry( $expiry, $infinity='infinity')
Format an expiry timestamp for API output.
A DatabaseBlock (unlike a SystemBlock) is stored in the database, may give rise to autoblocks and may...
System blocks are temporary blocks that are created on enforcement (e.g.
Base class for language-specific code.
Definition Language.php:81
Library for creating and parsing MW-style timestamps.
Represents a block that may prevent users from performing specific operations.
Definition Block.php:45
isSitewide()
Get whether the block is a sitewide block.
isHardblock()
Get whether the block is a hard block (affects logged-in users on a given IP/range).
getBlocker()
Get the user who applied this block.
getTimestamp()
Get the timestamp indicating when the block was created.
getId( $wikiId=self::LOCAL)
Get the block ID.
getReasonComment()
Get the reason for creating the block.
isCreateAccountBlocked()
Get the flag indicating whether this block blocks the target from creating an account.
getType()
Get the type of target for this particular block.
getExpiry()
Get the block expiry time.
Interface for objects representing user identity.