Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
45.95% covered (danger)
45.95%
17 / 37
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
RemoteWikiPageProvider
45.95% covered (danger)
45.95%
17 / 37
25.00% covered (danger)
25.00%
1 / 4
11.69
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 loadPageRevisionProperties
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
6
 loadData
83.33% covered (warning)
83.33%
15 / 18
0.00% covered (danger)
0.00%
0 / 1
2.02
 getCachedSegmentsKeyComponents
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Wikispeech\Segment;
4
5/**
6 * @file
7 * @ingroup Extensions
8 * @license GPL-2.0-or-later
9 */
10
11use FormatJson;
12use MediaWiki\Http\HttpRequestFactory;
13use Title;
14
15/**
16 * @since 0.1.10
17 */
18class RemoteWikiPageProvider extends AbstractPageProvider {
19
20    /** @var string */
21    private $consumerUrl;
22
23    /** @var HttpRequestFactory */
24    private $requestFactory;
25
26    /**
27     * @since 0.1.10
28     * @param string $consumerUrl
29     * @param HttpRequestFactory $requestFactory
30     */
31    public function __construct(
32        string $consumerUrl,
33        HttpRequestFactory $requestFactory
34    ) {
35        $this->consumerUrl = $consumerUrl;
36        $this->requestFactory = $requestFactory;
37    }
38
39    /**
40     * @since 0.1.10
41     * @param int $revisionId
42     * @return PageRevisionProperties
43     * @throws RemoteWikiPageProviderException If unable to get response from remote wiki.
44     */
45    public function loadPageRevisionProperties( int $revisionId ): PageRevisionProperties {
46        $request = wfAppendQuery(
47            $this->consumerUrl . '/api.php',
48            [
49                'action' => 'parse',
50                'format' => 'json',
51                'oldid' => $revisionId,
52            ]
53        );
54        $responseString = $this->requestFactory->get( $request );
55        if ( $responseString === null ) {
56            throw new RemoteWikiPageProviderException( 'Failed getting response from remote wiki' );
57        }
58        $response = FormatJson::parse( $responseString )->getValue();
59        return new PageRevisionProperties(
60            Title::newFromTextThrow( $response->parse->title ),
61            $response->parse->pageid
62        );
63    }
64
65    /**
66     * @since 0.1.10
67     * @param Title $title
68     * @throws RemoteWikiPageProviderException If unable to fetch remote wiki page
69     */
70    public function loadData( Title $title ): void {
71        $request = wfAppendQuery(
72            $this->consumerUrl . '/api.php',
73            [
74                'action' => 'parse',
75                'format' => 'json',
76                'page' => $title,
77                'prop' => 'text|revid|displaytitle'
78            ]
79        );
80        $responseString = $this->requestFactory->get( $request );
81        if ( $responseString === null ) {
82            throw new RemoteWikiPageProviderException(
83                "Failed to get page with title '$title' from consumer on URL $this->consumerUrl."
84            );
85        }
86        $response = FormatJson::parse( $responseString )->getValue();
87        $this->setDisplayTitle( $response->parse->displaytitle );
88        $this->setPageContent( $response->parse->text->{'*'} );
89        $this->setRevisionId( $response->parse->revid );
90    }
91
92    /**
93     * @since 0.1.10
94     * @return string
95     */
96    public function getCachedSegmentsKeyComponents(): string {
97        return $this->consumerUrl;
98    }
99
100}