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
46 public function getBlockDetails(
47 Block $block,
48 Language $language,
49 UserIdentity $user
50 ) {
51 $blocker = $block->getBlocker();
52 $expiry = ApiResult::formatExpiry( $block->getExpiry(), 'infinite' );
53
54 $vals = [
55 'blockid' => $block->getId(),
56 'blockedby' => $blocker ? $blocker->getName() : '',
57 'blockedbyid' => $blocker ? $blocker->getId() : 0,
58 'blockreason' => $block->getReasonComment()->message->inLanguage( $language )->plain(),
59 'blockedtimestamp' => wfTimestamp( TS::ISO_8601, $block->getTimestamp() ),
60 'blockexpiry' => $expiry,
61 'blockpartial' => !$block->isSitewide(),
62 'blocknocreate' => $block->isCreateAccountBlocked(),
63 'blockanononly' => !$block->isHardblock(),
64 ];
65
66 if ( $block instanceof AbstractBlock ) {
67 $vals['blockemail'] = $block->isEmailBlocked();
68 $vals['blockowntalk'] = !$block->isUsertalkEditAllowed();
69 }
70 if ( $block instanceof DatabaseBlock ) {
71 $vals['blockautoblocking'] = $block->isAutoblocking();
72 }
73
74 // Formatted timestamps
75 $vals['blockedtimestampformatted'] = $language->formatExpiry(
76 $block->getTimestamp(), true, 'infinity', $user
77 );
78 if ( $expiry !== 'infinite' ) {
79 $vals['blockexpiryformatted'] = $language->formatExpiry(
80 $expiry, true, 'infinity', $user
81 );
82 $vals['blockexpiryrelative'] = $language->getHumanTimestamp(
83 new MWTimestamp( $expiry ), new MWTimestamp(), $user
84 );
85 }
86
87 if ( $block instanceof SystemBlock ) {
88 $vals['systemblocktype'] = $block->getSystemBlockType();
89 }
90
91 if ( $block instanceof CompositeBlock ) {
92 $components = [];
93 foreach ( $block->getOriginalBlocks() as $singleBlock ) {
94 $components[] = $this->getBlockDetails( $singleBlock, $language, $user );
95 }
96 $vals['blockcomponents'] = $components;
97 }
98
99 return $vals;
100 }
101
107 public function getBlockCode( Block $block ): string {
108 if ( $block instanceof DatabaseBlock && $block->getType() === Block::TYPE_AUTO ) {
109 return 'autoblocked';
110 }
111 return 'blocked';
112 }
113
114}
wfTimestamp( $outputtype=TS::UNIX, $ts=0)
Get a timestamp string in one of various formats.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:69
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:31
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.