MediaWiki master
ApiBlockInfoHelper.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Api;
8
17use Wikimedia\Timestamp\TimestampFormat as TS;
18
27
47 public function getBlockDetails(
48 Block $block,
49 Language $language,
50 UserIdentity $user
51 ) {
52 $blocker = $block->getBlocker();
53 $expiry = ApiResult::formatExpiry( $block->getExpiry(), 'infinite' );
54
55 $vals = [
56 'blockid' => $block->getId(),
57 'blockedby' => $blocker ? $blocker->getName() : '',
58 'blockedbyid' => $blocker ? $blocker->getId() : 0,
59 'blockreason' => $block->getReasonComment()->message->inLanguage( $language )->plain(),
60 'blockedtimestamp' => wfTimestamp( TS::ISO_8601, $block->getTimestamp() ),
61 'blockexpiry' => $expiry,
62 'blockpartial' => !$block->isSitewide(),
63 'blocknocreate' => $block->isCreateAccountBlocked(),
64 'blockanononly' => !$block->isHardblock(),
65 ];
66
67 if ( $block instanceof AbstractBlock ) {
68 $vals['blockemail'] = $block->isEmailBlocked();
69 $vals['blockowntalk'] = !$block->isUsertalkEditAllowed();
70 if ( $block->getHideBlock() ) {
71 $vals['blockhidden'] = true;
72 }
73 }
74 if ( $block instanceof DatabaseBlock ) {
75 $vals['blockautoblocking'] = $block->isAutoblocking();
76 }
77
78 // Formatted timestamps
79 $vals['blockedtimestampformatted'] = $language->formatExpiry(
80 $block->getTimestamp(), true, 'infinity', $user
81 );
82 if ( $expiry !== 'infinite' ) {
83 $vals['blockexpiryformatted'] = $language->formatExpiry(
84 $expiry, true, 'infinity', $user
85 );
86 $vals['blockexpiryrelative'] = $language->getHumanTimestamp(
87 new MWTimestamp( $expiry ), new MWTimestamp(), $user
88 );
89 }
90
91 if ( $block instanceof SystemBlock ) {
92 $vals['systemblocktype'] = $block->getSystemBlockType();
93 }
94
95 if ( $block instanceof CompositeBlock ) {
96 $components = [];
97 foreach ( $block->getOriginalBlocks() as $singleBlock ) {
98 $components[] = $this->getBlockDetails( $singleBlock, $language, $user );
99 }
100 $vals['blockcomponents'] = $components;
101 }
102
103 return $vals;
104 }
105
111 public function getBlockCode( Block $block ): string {
112 if ( $block instanceof DatabaseBlock && $block->getType() === Block::TYPE_AUTO ) {
113 return 'autoblocked';
114 }
115 return 'blocked';
116 }
117
118}
wfTimestamp( $outputtype=TS::UNIX, $ts=0)
Get a timestamp string in one of various formats.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:71
Helper class for API modules that display block information.
getBlockCode(Block $block)
Get the API error code, to be used in ApiMessage::create or ApiBase::dieWithError.
getBlockDetails(Block $block, Language $language, UserIdentity $user)
Get basic info about a given block.
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:65
formatExpiry( $expiry, $format=true, $infinity='infinity', $user=null)
Decode an expiry (block, protection, etc.) which has come from the DB.
getHumanTimestamp(MWTimestamp $time, ?MWTimestamp $relativeTo=null, ?UserIdentity $user=null)
Get the timestamp in a human-friendly relative format, e.g., "3 days ago".
Library for creating and parsing MW-style timestamps.
Represents a block that may prevent users from performing specific operations.
Definition Block.php:32
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.
isEmailBlocked()
Get the flag indicating whether this block blocks the target from sending emails.
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.
getExpiry()
Get the block expiry time.
Interface for objects representing user identity.