Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
MediaWikiSourceUrlParser
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
1 / 1
 parseTitleFromSourceUrl
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3namespace FileImporter\Remote\MediaWiki;
4
5use FileImporter\Data\SourceUrl;
6
7/**
8 * @license GPL-2.0-or-later
9 * @author Thiemo Kreuz
10 */
11trait MediaWikiSourceUrlParser {
12
13    private function parseTitleFromSourceUrl( SourceUrl $sourceUrl ): ?string {
14        $parsed = $sourceUrl->getParsedUrl();
15
16        $query = $parsed['query'] ?? '';
17        parse_str( $query, $parameters );
18
19        $path = $parsed['path'] ?? '';
20        $lastSlash = strrpos( $path, '/' );
21
22        if ( array_key_exists( 'title', $parameters ) ) {
23            $title = $parameters['title'];
24        } elseif ( $lastSlash !== false ) {
25            $title = rawurldecode( substr( $path, $lastSlash + 1 ) );
26        } else {
27            return null;
28        }
29
30        return $title === '' ? null : $title;
31    }
32
33}