Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
80.77% covered (warning)
80.77%
42 / 52
66.67% covered (warning)
66.67%
4 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
HTMLRestrictionsField
82.35% covered (warning)
82.35%
42 / 51
66.67% covered (warning)
66.67%
4 / 6
16.24
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
1
 loadDataFromRequest
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 getDefault
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 validate
76.92% covered (warning)
76.92%
10 / 13
0.00% covered (danger)
0.00%
0 / 1
7.60
 getInputHTML
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 getInputOOUI
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace MediaWiki\HTMLForm\Field;
4
5use MediaWiki\HTMLForm\HTMLFormField;
6use MediaWiki\MediaWikiServices;
7use MediaWiki\Request\WebRequest;
8use Message;
9use MWRestrictions;
10
11/**
12 * Class for updating an MWRestrictions value (which is, currently, basically just an IP address
13 * list).
14 *
15 * Will be represented as a textarea with one address per line, with intelligent defaults for
16 * label, help text and row count.
17 *
18 * The value returned will be an MWRestrictions or the input string if it was not a list of
19 * valid IP ranges.
20 *
21 */
22class HTMLRestrictionsField extends HTMLFormField {
23    protected const DEFAULT_ROWS = 5;
24
25    private HTMLTextAreaField $ipField;
26    private HTMLTagMultiselectField $pagesField;
27
28    /**
29     * @stable to call
30     * @inheritDoc
31     */
32    public function __construct( array $params ) {
33        parent::__construct( $params );
34        $this->ipField = new HTMLTextAreaField( [
35            'parent' => $params['parent'],
36            'fieldname' => $params['fieldname'] . '-ip',
37            'rows' => self::DEFAULT_ROWS,
38            'required' => $params['required'] ?? false,
39            'help-message' => 'restrictionsfield-help',
40            'label-message' => 'restrictionsfield-label',
41        ] );
42
43        // Cannot really use a TitlesMultiselect field as the pages could be
44        // on other wikis!
45        $this->pagesField = new HTMLTagMultiselectField( [
46            'parent' => $params['parent'],
47            'fieldname' => $params['fieldname'] . '-pages',
48            'label-message' => 'restrictionsfields-pages-label',
49            'help-message' => 'restrictionsfields-pages-help',
50            'allowArbitrary' => true,
51            'required' => false,
52            'max' => 25,
53        ] );
54    }
55
56    /**
57     * @param WebRequest $request
58     * @return MWRestrictions Restrictions object
59     */
60    public function loadDataFromRequest( $request ) {
61        if ( !$request->getCheck( $this->mName . '-ip' ) ) {
62            return $this->getDefault();
63        }
64
65        $ipValue = rtrim( $request->getText( $this->mName . '-ip' ), "\r\n" );
66        $ips = $ipValue === '' ? [] : explode( "\n", $ipValue );
67        $pagesValue = $request->getText( $this->mName . '-pages' );
68        $pageList = $pagesValue ? explode( "\n", $pagesValue ) : [];
69        return MWRestrictions::newFromArray( [ 'IPAddresses' => $ips, 'Pages' => $pageList ] );
70    }
71
72    /**
73     * @return MWRestrictions
74     */
75    public function getDefault() {
76        return parent::getDefault() ?? MWRestrictions::newDefault();
77    }
78
79    /**
80     * @param MWRestrictions $value The value the field was submitted with
81     * @param array $alldata The data collected from the form
82     *
83     * @return bool|string|Message True on success, or String/Message error to display, or
84     *   false to fail validation without displaying an error.
85     */
86    public function validate( $value, $alldata ) {
87        if ( $this->isHidden( $alldata ) ) {
88            return true;
89        }
90
91        if (
92            isset( $this->mParams['required'] ) && $this->mParams['required'] !== false
93            && !$value->toArray()['IPAddresses']
94        ) {
95            return $this->msg( 'htmlform-required' );
96        }
97
98        if ( !$value->validity->isGood() ) {
99            $statusFormatter = MediaWikiServices::getInstance()->getFormatterFactory()->getStatusFormatter(
100                $this->mParent->getContext()
101            );
102            return $statusFormatter->getMessage( $value->validity );
103        }
104
105        if ( isset( $this->mValidationCallback ) ) {
106            return call_user_func( $this->mValidationCallback, $value, $alldata, $this->mParent );
107        }
108
109        return true;
110    }
111
112    /**
113     * @param MWRestrictions $value
114     * @return string
115     */
116    public function getInputHTML( $value ) {
117        $ipValue = implode( "\n", $value->toArray()['IPAddresses'] );
118        $pagesValue = implode( "\n", $value->toArray()['Pages'] ?? [] );
119        return (
120            $this->ipField->getDiv( $ipValue ) .
121            $this->pagesField->getDiv( $pagesValue )
122        );
123    }
124
125    /**
126     * @param MWRestrictions $value
127     * @return string
128     * @suppress PhanParamSignatureMismatch
129     */
130    public function getInputOOUI( $value ) {
131        $ipValue = implode( "\n", $value->toArray()['IPAddresses'] );
132        $pagesValue = implode( "\n", $value->toArray()['Pages'] ?? [] );
133        return (
134            $this->ipField->getOOUI( $ipValue ) .
135            $this->pagesField->getOOUI( $pagesValue )
136        );
137    }
138}
139
140/** @deprecated class alias since 1.42 */
141class_alias( HTMLRestrictionsField::class, 'HTMLRestrictionsField' );