Merge "Special:BlockList: Update remove/change block links"
[mediawiki.git] / includes / auth / ButtonAuthenticationRequest.php
blobe295f034bc45ec8a63355b1c1e0a9fdd9c0a7925
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
19 * @ingroup Auth
22 namespace MediaWiki\Auth;
24 use MediaWiki\Language\RawMessage;
25 use MediaWiki\Message\Message;
27 /**
28 * This is an authentication request that just implements a simple button. Can either be
29 * subclassed, or used directly. When used directly, with $required = false in the constructor,
30 * the ButtonAuthenticationRequest will only be included in the set of requests when the button
31 * was clicked; with $required = true, it will always be included and $button->{$name} can be
32 * used to check whether it has been clicked.
34 * @stable to extend
35 * @ingroup Auth
36 * @since 1.27
38 #[\AllowDynamicProperties]
39 class ButtonAuthenticationRequest extends AuthenticationRequest {
40 /** @var string */
41 protected $name;
43 protected Message $label;
44 protected Message $help;
46 /**
47 * @stable to call
48 * @param string $name Button name (e.g. HTML "name" attribute)
49 * @param Message $label Button label
50 * @param Message $help Button help
51 * @param bool $required The button is required for authentication to proceed.
53 public function __construct( $name, Message $label, Message $help, $required = false ) {
54 $this->name = $name;
55 $this->label = $label;
56 $this->help = $help;
57 $this->required = $required ? self::REQUIRED : self::OPTIONAL;
60 /**
61 * @inheritDoc
62 * @stable to override
64 public function getUniqueId() {
65 return parent::getUniqueId() . ':' . $this->name;
68 /**
69 * @inheritDoc
70 * @stable to override
72 public function getFieldInfo() {
73 return [
74 $this->name => [
75 'type' => 'button',
76 'label' => $this->label,
77 'help' => $this->help,
82 /**
83 * Fetch a ButtonAuthenticationRequest or subclass by name
84 * @param AuthenticationRequest[] $reqs Requests to search
85 * @param string $name Name to look for
86 * @return ButtonAuthenticationRequest|null Returns null if there is not
87 * exactly one matching request.
89 public static function getRequestByName( array $reqs, $name ) {
90 $requests = array_filter( $reqs, static function ( $req ) use ( $name ) {
91 return $req instanceof ButtonAuthenticationRequest && $req->name === $name;
92 } );
93 // @phan-suppress-next-line PhanTypeMismatchReturnSuperType False positive
94 return count( $requests ) === 1 ? reset( $requests ) : null;
97 /**
98 * @codeCoverageIgnore
99 * @stable to override
100 * @param array $data
101 * @return AuthenticationRequest|static
103 public static function __set_state( $data ) {
104 if ( !isset( $data['label'] ) ) {
105 $data['label'] = new RawMessage( '$1', $data['name'] );
106 } elseif ( is_string( $data['label'] ) ) {
107 $data['label'] = new Message( $data['label'] );
108 } elseif ( is_array( $data['label'] ) && $data['label'] ) {
109 // @phan-suppress-next-line PhanParamTooFewUnpack Should infer non-emptiness from above
110 $data['label'] = Message::newFromKey( ...$data['label'] );
112 if ( !isset( $data['help'] ) ) {
113 $data['help'] = new RawMessage( '$1', $data['name'] );
114 } elseif ( is_string( $data['help'] ) ) {
115 $data['help'] = new Message( $data['help'] );
116 } elseif ( is_array( $data['help'] ) && $data['help'] ) {
117 // @phan-suppress-next-line PhanParamTooFewUnpack Should infer non-emptiness from above
118 $data['help'] = Message::newFromKey( ...$data['help'] );
120 $ret = new static( $data['name'], $data['label'], $data['help'] );
121 foreach ( $data as $k => $v ) {
122 $ret->$k = $v;
124 return $ret;