Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 94
0.00% covered (danger)
0.00%
0 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
PFRegExpInput
0.00% covered (danger)
0.00%
0 / 94
0.00% covered (danger)
0.00%
0 / 9
420
0.00% covered (danger)
0.00%
0 / 1
 getName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 __construct
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 1
110
 newFromInput
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 getResourceModuleNames
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 getParameters
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
2
 getHtmlText
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getOtherPropTypesHandled
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getJsInitFunctionData
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getJsValidationFunctionData
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3/**
4 * @author Stephan Gambke
5 * @file
6 * @ingroup PF
7 */
8
9/**
10 * This class represents the RegExp input.
11 *
12 * @ingroup PF
13 */
14class PFRegExpInput extends PFFormInput {
15
16    /** @var PFFormInput */
17    protected $mBaseInput;
18
19    public static function getName(): string {
20        return 'regexp';
21    }
22
23    /**
24     * @param string $input_number The number of the input in the form.
25     * @param string $cur_value The current value of the input field.
26     * @param string $input_name The name of the input.
27     * @param bool $disabled Is this input disabled?
28     * @param array $other_args An associative array of other parameters that were present in the
29     *  input definition.
30     */
31    public function __construct( $input_number, $cur_value, $input_name, $disabled, array $other_args ) {
32        global $wgPageFormsFormPrinter;
33
34        parent::__construct( $input_number, $cur_value, $input_name, $disabled, $other_args );
35
36        // set OR character
37        if ( array_key_exists( 'or char', $this->mOtherArgs ) ) {
38            $orChar = trim( $this->mOtherArgs['or char'] );
39            unset( $this->mOtherArgs['or char'] );
40        } else {
41            $orChar = '!';
42        }
43
44        // set regexp string
45        if ( array_key_exists( 'regexp', $this->mOtherArgs ) ) {
46            $regExp = str_replace( $orChar, '|', trim( $this->mOtherArgs['regexp'] ) );
47            unset( $this->mOtherArgs['regexp'] );
48
49            // check for leading/trailing delimiter and remove it (else reset regexp)
50            if ( preg_match( "/^\/.*\/\$/", $regExp ) ) {
51                $regExp = substr( $regExp, 1, strlen( $regExp ) - 2 );
52            } else {
53                $regExp = '.*';
54            }
55        } else {
56            $regExp = '.*';
57        }
58
59        // set inverse string
60        $invertRegexp = array_key_exists( 'inverse', $this->mOtherArgs );
61        unset( $this->mOtherArgs['inverse'] );
62
63        // set failure message string
64        if ( array_key_exists( 'message', $this->mOtherArgs ) ) {
65            $errorMessage = trim( $this->mOtherArgs['message'] );
66            unset( $this->mOtherArgs['message'] );
67        } else {
68            $errorMessage = wfMessage( 'pf-regexp-wrongformat' )->text();
69        }
70
71        // sanitize error message and regexp for JS
72        $jsFunctionData = [
73            'retext' => $regExp,
74            'inverse' => $invertRegexp,
75            'message' => $errorMessage,
76        ];
77
78        // Finally set name and parameters for the validation function
79        $this->addJsValidationFunctionData( 'PF_RE_validate', $jsFunctionData );
80
81        // set base input type name
82        if ( array_key_exists( 'base type', $this->mOtherArgs ) ) {
83            $baseType = trim( $this->mOtherArgs['base type'] );
84            unset( $this->mOtherArgs['base type'] );
85
86            // If base type is unknown, set it to 'text'.
87            $allInputTypes = $wgPageFormsFormPrinter->getAllInputTypes();
88            if ( !in_array( $baseType, $allInputTypes ) ) {
89                $baseType = 'text';
90            }
91        } else {
92            $baseType = 'text';
93        }
94
95        // create other_args array for base input type if base prefix was set
96        if ( array_key_exists( 'base prefix', $this->mOtherArgs ) ) {
97            // set base prefix
98            $basePrefix = trim( $this->mOtherArgs['base prefix'] ) . ".";
99            unset( $this->mOtherArgs['base prefix'] );
100
101            // create new other_args param
102            $newOtherArgs = [];
103
104            foreach ( $this->mOtherArgs as $key => $value ) {
105                if ( strpos( $key, $basePrefix ) === 0 ) {
106                    $newOtherArgs[substr( $key, strlen( $basePrefix ) )] = $value;
107                } else {
108                    $newOtherArgs[$key] = $value;
109                }
110            }
111
112        } else {
113            $newOtherArgs = $this->mOtherArgs;
114        }
115
116        // Create base input.
117        $baseInputClass = $wgPageFormsFormPrinter->getInputType( $baseType );
118        $this->mBaseInput = new $baseInputClass(
119            $this->mInputNumber, $this->mCurrentValue, $this->mInputName, $this->mIsDisabled, $newOtherArgs
120        );
121    }
122
123    /**
124     * Makes this input a wrapper around a previously-defined input.
125     *
126     * @param PFFormInput $formInput
127     * @return PFRegExpInput
128     */
129    public static function newFromInput( $formInput ) {
130        return new PFRegExpInput(
131            $formInput->mInputNumber,
132            $formInput->mCurrentValue,
133            $formInput->mInputName,
134            $formInput->mIsDisabled,
135            $formInput->mOtherArgs
136        );
137    }
138
139    /**
140     * Returns the names of the resource modules this input type uses.
141     *
142     * Returns the names of the modules as an array or - if there is only one
143     * module - as a string.
144     *
145     * @return null|string|array
146     */
147    public function getResourceModuleNames() {
148        $modules = $this->mBaseInput->getResourceModuleNames();
149        if ( is_array( $modules ) ) {
150            return array_merge( $modules, [ 'ext.pageforms.regexp' ] );
151        } elseif ( is_string( $modules ) ) {
152            return [ $modules, 'ext.pageforms.regexp' ];
153        } else {
154            return 'ext.pageforms.regexp';
155        }
156    }
157
158    /**
159     * Returns the set of parameters for this form input.
160     * @return array[]
161     */
162    public static function getParameters() {
163        $params = parent::getParameters();
164        $params['regexp'] = [
165            'name' => 'regexp',
166            'type' => 'string',
167            'description' => wfMessage( 'pf-regexp-regexp' )->text()
168        ];
169        $params['base type'] = [
170            'name' => 'base type',
171            'type' => 'string',
172            'description' => wfMessage( 'pf-regexp-basetype' )->text()
173        ];
174        $params['base prefix'] = [
175            'name' => 'base prefix',
176            'type' => 'string',
177            'description' => wfMessage( 'pf-regexp-baseprefix' )->text()
178        ];
179        $params['or char'] = [
180            'name' => 'or char',
181            'type' => 'string',
182            'description' => wfMessage( 'pf-regexp-orchar' )->text()
183        ];
184        $params['inverse'] = [
185            'name' => 'inverse',
186            'type' => 'string',
187            'description' => wfMessage( 'pf-regexp-inverse' )->text()
188        ];
189        $params['message'] = [
190            'name' => 'message',
191            'type' => 'string',
192            'description' => wfMessage( 'pf-regexp-message' )->text()
193        ];
194
195        return $params;
196    }
197
198    /**
199     * Returns the HTML code to be included in the output page for this input.
200     *
201     * Ideally this HTML code should provide a basic functionality even if the
202     * browser is not JavaScript capable. I.e. even without JavaScript the user
203     * should be able to input values.
204     * @return string
205     */
206    public function getHtmlText(): string {
207        return $this->mBaseInput->getHtmlText();
208    }
209
210    /**
211     * Returns the set of SMW property types which this input can
212     * handle, but for which it isn't the default input.
213     *
214     * @deprecated
215     * @return string[]
216     */
217    public static function getOtherPropTypesHandled() {
218        return [ '_str', '_num', '_dat', '_geo', '_ema', '_tel', '_wpg', '_tem', '_qty' ];
219    }
220
221    /**
222     * Returns the name and parameters for the initialization JavaScript
223     * function for this input type, if any.
224     * @return array[]
225     */
226    public function getJsInitFunctionData() {
227        return array_merge( $this->mJsInitFunctionData, $this->mBaseInput->getJsInitFunctionData() );
228    }
229
230    /**
231     * Returns the name and parameters for the validation JavaScript
232     * functions for this input type, if any.
233     * @return array[]
234     */
235    public function getJsValidationFunctionData() {
236        return array_merge( $this->mJsValidationFunctionData, $this->mBaseInput->getJsValidationFunctionData() );
237    }
238}