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
71 // Formatted timestamps
72 $vals['blockedtimestampformatted'] = $language->formatExpiry(
73 $block->getTimestamp(), true, 'infinity', $user
74 );
75 if ( $expiry !== 'infinite' ) {
76 $vals['blockexpiryformatted'] = $language->formatExpiry(
77 $expiry, true, 'infinity', $user
78 );
79 $vals['blockexpiryrelative'] = $language->getHumanTimestamp(
80 new MWTimestamp( $expiry ), new MWTimestamp(), $user
81 );
82 }
83
84 if ( $block instanceof SystemBlock ) {
85 $vals['systemblocktype'] = $block->getSystemBlockType();
86 }
87
88 if ( $block instanceof CompositeBlock ) {
89 $components = [];
90 foreach ( $block->getOriginalBlocks() as $singleBlock ) {
91 $components[] = $this->getBlockDetails( $singleBlock, $language, $user );
92 }
93 $vals['blockcomponents'] = $components;
94 }
95
96 return $vals;
97 }
98
104 public function getBlockCode( Block $block ): string {
105 if ( $block instanceof DatabaseBlock && $block->getType() === Block::TYPE_AUTO ) {
106 return 'autoblocked';
107 }
108 return 'blocked';
109 }
110
111}
wfTimestamp( $outputtype=TS::UNIX, $ts=0)
Get a timestamp string in one of various formats.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:68
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:69
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.