Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
SourceUrl
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
5 / 5
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 getUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getParsedUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getHost
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace FileImporter\Data;
4
5use FileImporter\Exceptions\InvalidArgumentException;
6use MediaWiki\MediaWikiServices;
7
8/**
9 * Class representing a (possibly unnormalized) URL passed into the extension for importing. Can be
10 * any URL (e.g. a Flickr image), not necessarily pointing to a MediaWiki.
11 *
12 * Basic normalization that is true for any URL can be done here (e.g. Unicode normalization).
13 * Normalizations for specific sources (as detected by dedicated SourceUrlCheckers) need to be done
14 * in dedicated SourceUrlNormalizers.
15 *
16 * @license GPL-2.0-or-later
17 * @author Addshore
18 */
19class SourceUrl {
20
21    private const ERROR_SOURCE_URL_UNPARSEABLE = 'sourceUrlUnparseable';
22
23    private string $url;
24    /** @var string[] */
25    private $parsed;
26
27    /**
28     * @param string $url For example, https://en.wikipedia.org/wiki/File:Foo.JPG
29     *
30     * @throws InvalidArgumentException When $url is not parsable
31     */
32    public function __construct( string $url ) {
33        $this->url = trim( $url );
34        $this->parsed = MediaWikiServices::getInstance()->getUrlUtils()->parse( $this->url ) ?? [];
35        if ( !$this->parsed ) {
36            throw new InvalidArgumentException( '$url is not parsable',
37                self::ERROR_SOURCE_URL_UNPARSEABLE );
38        }
39    }
40
41    /**
42     * @return string The raw URL
43     */
44    public function getUrl(): string {
45        return $this->url;
46    }
47
48    /**
49     * @return string[] Parsed URL array provided by wfParseUrl
50     */
51    public function getParsedUrl(): array {
52        return $this->parsed;
53    }
54
55    /**
56     * @return string The host, for example "en.wikipedia.org"
57     */
58    public function getHost(): string {
59        return strtolower( $this->parsed['host'] );
60    }
61
62    public function __toString(): string {
63        return $this->url;
64    }
65
66}