MediaWiki master
UserRequirementsConditionFormatterTrait.php
Go to the documentation of this file.
1<?php
6
7namespace MediaWiki\Specials\Helpers;
8
9use MediaWiki\Config\Config;
10use MediaWiki\Context\IContextSource;
11use MediaWiki\HookContainer\HookRunner;
12use MediaWiki\Html\Html;
13use MediaWiki\Language\Language;
14use MediaWiki\Language\MessageLocalizer;
15use MediaWiki\MainConfigNames;
16use MediaWiki\Message\Message;
17use MediaWiki\User\UserRequirementsConditionChecker;
18
29
34 protected function formatCondition( string|int|array $condition ): string {
35 if ( is_array( $condition ) && count( $condition ) > 0 ) {
36 $condName = array_shift( $condition );
37 if ( in_array( $condName, UserRequirementsConditionChecker::VALID_OPS ) ) {
38 $listItems = '';
39 foreach ( $condition as $subcond ) {
40 $listItems .= Html::rawElement( 'li', [], $this->formatCondition( $subcond ) );
41 }
42 $htmlList = Html::rawElement( 'ul', [], $listItems );
43 $condName = match ( $condName ) {
44 '&' => 'listgrouprights-restrictedgroups-op-and',
45 '|' => 'listgrouprights-restrictedgroups-op-or',
46 '^' => 'listgrouprights-restrictedgroups-op-xor',
47 // Even though '!' is usually understood as 'NOT', in fact it's 'NAND' as it can accept
48 // multiple arguments
49 '!' => 'listgrouprights-restrictedgroups-op-nand',
50 };
51 return $this->msg( $condName )
52 ->rawParams( $htmlList )
53 ->parse();
54 } else {
55 return $this->formatAtomicCondition( $condName, $condition );
56 }
57 } elseif ( is_array( $condition ) ) {
58 return '';
59 } else {
60 return $this->formatAtomicCondition( $condition, [] );
61 }
62 }
63
68 protected function formatAtomicCondition( string|int $condName, array $args ): string {
69 $msgKey = match ( $condName ) {
70 APCOND_EDITCOUNT => 'listgrouprights-restrictedgroups-cond-editcount',
71 APCOND_AGE => 'listgrouprights-restrictedgroups-cond-age',
72 APCOND_EMAILCONFIRMED => 'listgrouprights-restrictedgroups-cond-emailconfirmed',
73 APCOND_INGROUPS => 'listgrouprights-restrictedgroups-cond-ingroups',
74 APCOND_ISIP => 'listgrouprights-restrictedgroups-cond-isip',
75 APCOND_IPINRANGE => 'listgrouprights-restrictedgroups-cond-ipinrange',
76 APCOND_AGE_FROM_EDIT => 'listgrouprights-restrictedgroups-cond-age-from-edit',
77 APCOND_BLOCKED => 'listgrouprights-restrictedgroups-cond-blocked',
78 APCOND_ISBOT => 'listgrouprights-restrictedgroups-cond-isbot',
79 default => null,
80 };
81
82 if ( $msgKey === null ) {
83 $messageSpec = null;
84 $context = $this->getContext();
85 $this->getHookRunner()->onUserRequirementsConditionDisplay( $condName, $args, $context, $messageSpec );
86 if ( $messageSpec !== null ) {
87 return $this->msg( $messageSpec )->parse();
88 } else {
89 $msgKey = 'listgrouprights-restrictedgroups-cond-' . $condName;
90 }
91 }
92 $msg = $this->msg( $msgKey );
93
94 if ( $condName === APCOND_AGE || $condName === APCOND_AGE_FROM_EDIT ) {
95 $minAge = $args[0] ?? $this->getConfig()->get( MainConfigNames::AutoConfirmAge );
96 $msg->durationParams( $minAge );
97 } elseif ( $condName === APCOND_INGROUPS ) {
98 $groupNames = [];
99 foreach ( $args as $group ) {
100 $groupNames[] = $this->getLanguage()->getGroupName( $group );
101 }
102 $msg->numParams( count( $args ) )->params( Message::listParam( $groupNames ) );
103 } elseif ( $condName === APCOND_EDITCOUNT ) {
104 $minEdits = $args[0] ?? $this->getConfig()->get( MainConfigNames::AutoConfirmCount );
105 $msg->numParams( $minEdits );
106 } else {
107 $msg->params( ...$args );
108 }
109
110 return $msg->parse();
111 }
112
113 // Abstract methods defined in SpecialPage and required by this trait
114
121 abstract protected function msg( $key, ...$params );
122
127 abstract protected function getConfig();
128
133 abstract protected function getLanguage();
134
139 abstract protected function getContext();
140
145 abstract protected function getHookRunner();
146}
const APCOND_BLOCKED
Definition Defines.php:197
const APCOND_AGE
Definition Defines.php:191
const APCOND_EMAILCONFIRMED
Definition Defines.php:192
const APCOND_IPINRANGE
Definition Defines.php:195
const APCOND_INGROUPS
Definition Defines.php:193
const APCOND_ISBOT
Definition Defines.php:198
const APCOND_AGE_FROM_EDIT
Definition Defines.php:196
const APCOND_ISIP
Definition Defines.php:194
const APCOND_EDITCOUNT
Definition Defines.php:190
const AutoConfirmCount
Name constant for the AutoConfirmCount setting, for use with Config\get().
const AutoConfirmAge
Name constant for the AutoConfirmAge setting, for use with Config\get().
static listParam(array $list, $type=ListType::AND)
Definition Message.php:1355
trait UserRequirementsConditionFormatterTrait
Trait providing methods to format user group requirement conditions as HTML.