Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
90.00% covered (success)
90.00%
9 / 10
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialPageData
90.00% covered (success)
90.00%
9 / 10
83.33% covered (warning)
83.33%
5 / 6
7.05
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setRequestHandler
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 initDependencies
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 newDefaultRequestHandler
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 showForm
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * @license GPL-2.0-or-later
4 * @file
5 */
6
7namespace MediaWiki\Specials;
8
9use MediaWiki\Exception\HttpError;
10use MediaWiki\LinkedData\PageDataRequestHandler;
11use MediaWiki\SpecialPage\UnlistedSpecialPage;
12
13/**
14 * Special page to act as an endpoint for accessing raw page data.
15 *
16 * The web server should generally be configured to make this accessible via a canonical URL/URI,
17 * such as <http://my.domain.org/data/main/Foo>.
18 *
19 * @ingroup SpecialPage
20 */
21class SpecialPageData extends UnlistedSpecialPage {
22
23    /**
24     * @var PageDataRequestHandler|null
25     */
26    private $requestHandler = null;
27
28    public function __construct() {
29        parent::__construct( 'PageData' );
30    }
31
32    /**
33     * Sets the request handler to be used by the special page.
34     * May be used when a particular instance of PageDataRequestHandler is already
35     * known, e.g. during testing.
36     *
37     * If no request handler is set using this method, a default handler is created
38     * on demand by initDependencies().
39     */
40    public function setRequestHandler( PageDataRequestHandler $requestHandler ) {
41        $this->requestHandler = $requestHandler;
42    }
43
44    /**
45     * Initialize any un-initialized members from global context.
46     * In particular, this initializes $this->requestHandler
47     */
48    protected function initDependencies() {
49        $this->requestHandler ??= $this->newDefaultRequestHandler();
50    }
51
52    /**
53     * Creates a PageDataRequestHandler based on global defaults.
54     *
55     * @return PageDataRequestHandler
56     */
57    private function newDefaultRequestHandler() {
58        return new PageDataRequestHandler();
59    }
60
61    /**
62     * @see SpecialWikibasePage::execute
63     *
64     * @param string|null $subPage
65     *
66     * @throws HttpError
67     */
68    public function execute( $subPage ) {
69        $this->initDependencies();
70
71        // If there is no title, show an HTML form
72        // TODO: Don't do this if HTML is not acceptable according to HTTP headers.
73        if ( !$this->requestHandler->canHandleRequest( $subPage, $this->getRequest() ) ) {
74            $this->showForm();
75            return;
76        }
77
78        $this->requestHandler->handleRequest( $subPage, $this->getRequest(), $this->getOutput() );
79    }
80
81    /**
82     * Shows an informative page to the user; Called when there is no page to output.
83     */
84    public function showForm() {
85        $this->getOutput()->showErrorPage( 'pagedata-title', 'pagedata-text' );
86    }
87}
88
89// @codeCoverageIgnoreStart
90/**
91 * Retain the old class name for backwards compatibility.
92 * @deprecated since 1.41
93 */
94class_alias( SpecialPageData::class, 'SpecialPageData' );
95// @codeCoverageIgnoreEnd