Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
20 / 20 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| Validator | |
100.00% |
20 / 20 |
|
100.00% |
3 / 3 |
8 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| checkUrl | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |||
| checkTitle | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
| 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. |
| 7 | * |
| 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. |
| 12 | * |
| 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 | * |
| 17 | * @file |
| 18 | */ |
| 19 | |
| 20 | namespace MediaWiki\Extension\RealMe; |
| 21 | |
| 22 | use MediaWiki\Title\Title; |
| 23 | use MediaWiki\Utils\UrlUtils; |
| 24 | use MessageLocalizer; |
| 25 | |
| 26 | class Validator { |
| 27 | |
| 28 | public function __construct( |
| 29 | private readonly MessageLocalizer $localizer, |
| 30 | private readonly UrlUtils $urlUtils, |
| 31 | ) { |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Validate the specified URL, returning errors if necessary |
| 36 | */ |
| 37 | public function checkUrl( string $url ): array { |
| 38 | $errors = []; |
| 39 | $parsed = $this->urlUtils->parse( $url ); |
| 40 | if ( !$parsed ) { |
| 41 | $errors[] = $this->localizer->msg( 'realme-preference-error-invalid' ) |
| 42 | ->plaintextParams( $url ); |
| 43 | return $errors; |
| 44 | } |
| 45 | |
| 46 | if ( $parsed['scheme'] !== 'http' && $parsed['scheme'] !== 'https' ) { |
| 47 | $errors[] = $this->localizer->msg( 'realme-preference-error-not-http' ) |
| 48 | ->plaintextParams( $url, $parsed['scheme'] . $parsed['delimiter'] ); |
| 49 | } |
| 50 | |
| 51 | return $errors; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Validate the specified title, returning errors if necessary |
| 56 | */ |
| 57 | public function checkTitle( string $title ): array { |
| 58 | $errors = []; |
| 59 | $titleObj = Title::newFromText( $title ); |
| 60 | if ( $titleObj ) { |
| 61 | if ( $titleObj->getPrefixedText() !== $title ) { |
| 62 | $errors[] = $this->localizer->msg( 'realme-config-error-canonical' ) |
| 63 | ->plaintextParams( $titleObj->getPrefixedText(), $title ); |
| 64 | } |
| 65 | } else { |
| 66 | $errors[] = $this->localizer->msg( 'realme-config-error-invalidtitle' ) |
| 67 | ->plaintextParams( $title ); |
| 68 | } |
| 69 | |
| 70 | return $errors; |
| 71 | } |
| 72 | } |