Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
CIDRCalculator
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 4
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 toString
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getHtml
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 1
12
 __toString
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace MediaWiki\CheckUser\CheckUser\Widgets;
4
5use CollapsibleFieldsetLayout;
6use MediaWiki\Output\OutputPage;
7use OOUI\Element;
8use OOUI\FieldsetLayout;
9use OOUI\LabelWidget;
10use OOUI\MultilineTextInputWidget;
11use OOUI\PanelLayout;
12use OOUI\Widget;
13
14class CIDRCalculator {
15
16    private bool $mCollapsible;
17
18    /**
19     * Text to be shown as the legend for
20     * the calculator.
21     * Similar to HTMLForm's $mWrapperLegend.
22     *
23     * @var string|bool
24     */
25    private $mWrapperLegend;
26
27    private array $mWrapperAttributes;
28
29    private bool $mCollapsed;
30
31    private OutputPage $out;
32
33    /**
34     * @param OutputPage $out
35     * @param array $config an array with any of the following keys:
36     *   * collapsable - whether to allow the CIDR calculator wrapper fieldset
37     *      to be collapsable (boolean with default of false)
38     *   * wrapperLegend - the text to use as the title for the CIDR calculator
39     *      (default is the message checkuser-cidr-label). Use false for no legend.
40     *   * wrapperAttributes - any attributes to apply to the wrapper fieldset (an array)
41     *   * collapsed - whether to have the wrapper fieldset be collapsed by default
42     *      (boolean with default of false)
43     */
44    public function __construct( OutputPage $out, array $config = [] ) {
45        $this->out = $out;
46
47        // Just in case the modules were not loaded
48        $out->addModules( [ 'ext.checkUser', 'ext.checkUser.styles' ] );
49        $this->mCollapsible = $config['collapsable'] ?? false;
50        $this->mWrapperLegend = $config['wrapperLegend'] ?? $out->msg( 'checkuser-cidr-label' )->escaped();
51        $this->mWrapperAttributes = $config['wrapperAttributes'] ?? [];
52        $this->mCollapsed = $config['collapsed'] ?? false;
53    }
54
55    /**
56     * Get the string (HTML) representation of the calculator
57     *
58     * @return string
59     */
60    public function toString(): string {
61        return $this->getHtml();
62    }
63
64    /**
65     * Get the HTML for the calculator.
66     *
67     * @return string
68     */
69    public function getHtml(): string {
70        $items = [];
71        $items[] = new MultilineTextInputWidget( [
72            'classes' => [ 'mw-checkuser-cidr-iplist' ],
73            'rows' => 5,
74            'dir' => 'ltr',
75        ] );
76        $input = new CIDRCalculatorResultBox( [
77            'size' => 35,
78            'classes' => [ 'mw-checkuser-cidr-res' ],
79            'name' => 'mw-checkuser-cidr-res',
80        ] );
81        $items[] = new LabelWidget( [
82            'input' => $input,
83            'classes' => [ 'mw-checkuser-cidr-res-label' ],
84            'label' => $this->out->msg( 'checkuser-cidr-res' )->text(),
85        ] );
86        $items[] = $input;
87        $items[] = new LabelWidget( [
88            'classes' => [ 'mw-checkuser-cidr-tool-links' ]
89        ] );
90        $items[] = new LabelWidget( [
91            'classes' => [ 'mw-checkuser-cidr-ipnote' ]
92        ] );
93        // From OOUIForm but modified.
94        if ( is_string( $this->mWrapperLegend ) ) {
95            $attributes = [
96                    'label' => $this->mWrapperLegend,
97                    'collapsed' => $this->mCollapsed,
98                    'items' => $items,
99                ] + Element::configFromHtmlAttributes( $this->mWrapperAttributes );
100            if ( $this->mCollapsible ) {
101                $content = new CollapsibleFieldsetLayout( $attributes );
102            } else {
103                $content = new FieldsetLayout( $attributes );
104            }
105        } else {
106            $content = new Widget( [
107                'content' => $items
108            ] );
109        }
110        return ( new PanelLayout( [
111            'classes' => [ 'mw-checkuser-cidrform mw-checkuser-cidr-calculator-hidden' ],
112            'id' => 'mw-checkuser-cidrform',
113            'expanded' => false,
114            'padded' => $this->mWrapperLegend !== false,
115            'framed' => $this->mWrapperLegend !== false,
116            'content' => $content,
117        ] ) )->toString();
118    }
119
120    /**
121     * Magic method implementation.
122     *
123     * Copied from OOUI\Tag
124     *
125     * @return string
126     */
127    public function __toString() {
128        try {
129            return $this->toString();
130        } catch ( \Exception $ex ) {
131            trigger_error( (string)$ex, E_USER_ERROR );
132        }
133    }
134}