MediaWiki master
ApiBlockInfoHelper.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\Api;
22
31
40
59 public function getBlockDetails(
60 Block $block,
61 Language $language,
62 UserIdentity $user
63 ) {
64 $blocker = $block->getBlocker();
65
66 $vals = [];
67 $vals['blockid'] = $block->getId();
68 $vals['blockedby'] = $blocker ? $blocker->getName() : '';
69 $vals['blockedbyid'] = $blocker ? $blocker->getId() : 0;
70 $vals['blockreason'] = $block->getReasonComment()
71 ->message->inLanguage( $language )->plain();
72 $vals['blockedtimestamp'] = wfTimestamp( TS_ISO_8601, $block->getTimestamp() );
73 $expiry = ApiResult::formatExpiry( $block->getExpiry(), 'infinite' );
74 $vals['blockexpiry'] = $expiry;
75 $vals['blockpartial'] = !$block->isSitewide();
76 $vals['blocknocreate'] = $block->isCreateAccountBlocked();
77 $vals['blockanononly'] = !$block->isHardblock();
78 if ( $block instanceof AbstractBlock ) {
79 $vals['blockemail'] = $block->isEmailBlocked();
80 $vals['blockowntalk'] = !$block->isUsertalkEditAllowed();
81 }
82
83 // Formatted timestamps
84 $vals['blockedtimestampformatted'] = $language->formatExpiry(
85 $block->getTimestamp(), true, 'infinity', $user
86 );
87 if ( $expiry !== 'infinite' ) {
88 $vals['blockexpiryformatted'] = $language->formatExpiry(
89 $expiry, true, 'infinity', $user
90 );
91 $vals['blockexpiryrelative'] = $language->getHumanTimestamp(
92 new MWTimestamp( $expiry ), new MWTimestamp(), $user
93 );
94 }
95
96 if ( $block instanceof SystemBlock ) {
97 $vals['systemblocktype'] = $block->getSystemBlockType();
98 }
99
100 if ( $block instanceof CompositeBlock ) {
101 $components = [];
102 foreach ( $block->getOriginalBlocks() as $singleBlock ) {
103 $components[] = $this->getBlockDetails( $singleBlock, $language, $user );
104 }
105 $vals['blockcomponents'] = $components;
106 }
107
108 return $vals;
109 }
110
116 public function getBlockCode( Block $block ): string {
117 if ( $block instanceof DatabaseBlock && $block->getType() === Block::TYPE_AUTO ) {
118 return 'autoblocked';
119 }
120 return 'blocked';
121 }
122
123}
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
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:81
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: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.
getExpiry()
Get the block expiry time.
Interface for objects representing user identity.