Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
71.43% |
15 / 21 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
WsExport | |
71.43% |
15 / 21 |
|
33.33% |
1 / 3 |
8.14 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
4 | |||
getBaseUrl | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getExportUrl | |
54.55% |
6 / 11 |
|
0.00% |
0 / 1 |
2.38 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\Wikisource; |
4 | |
5 | use MediaWiki\Language\Language; |
6 | use MediaWiki\Title\Title; |
7 | |
8 | class WsExport { |
9 | |
10 | /** @var string Language code (Wikisource subdomain). */ |
11 | private $lang; |
12 | |
13 | /** @var string Base URL for the WS Export tool. */ |
14 | private $wsExportUrl; |
15 | |
16 | /** |
17 | * @param Language $contentLanguage |
18 | * @param string $wsExportUrl |
19 | * @param string $serverName |
20 | */ |
21 | public function __construct( Language $contentLanguage, string $wsExportUrl, string $serverName ) { |
22 | // Get the language code. |
23 | // This logic is duplicated for the frontend in modules/ext.wikisource.ChooserButton.js |
24 | $this->lang = $contentLanguage->getCode(); |
25 | $wikisourcePos = strpos( $serverName, '.wikisource.org' ); |
26 | if ( $serverName === 'wikisource.org' ) { |
27 | $this->lang = 'mul'; |
28 | } elseif ( strpos( $serverName, 'en.wikisource.beta' ) !== false ) { |
29 | $this->lang = 'beta'; |
30 | } elseif ( $wikisourcePos !== false ) { |
31 | $this->lang = substr( $serverName, 0, $wikisourcePos ); |
32 | } |
33 | $this->wsExportUrl = $wsExportUrl; |
34 | } |
35 | |
36 | /** |
37 | * @return string |
38 | */ |
39 | public function getBaseUrl(): string { |
40 | return $this->wsExportUrl; |
41 | } |
42 | |
43 | /** |
44 | * Get a URL for the WS Export tool. |
45 | * @param Title $title |
46 | * @param string|null $format |
47 | * @return string |
48 | */ |
49 | public function getExportUrl( Title $title, ?string $format = null ): string { |
50 | if ( $format ) { |
51 | $params = [ |
52 | 'format' => $format, |
53 | 'lang' => $this->lang, |
54 | 'page' => $title->getPrefixedDBkey() |
55 | ]; |
56 | } else { |
57 | $params = [ |
58 | 'lang' => $this->lang, |
59 | 'title' => $title->getPrefixedDBkey() |
60 | ]; |
61 | } |
62 | return $this->wsExportUrl . '/?' . wfArrayToCgi( $params ); |
63 | } |
64 | } |