Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
75.00% |
9 / 12 |
|
66.67% |
4 / 6 |
CRAP | |
0.00% |
0 / 1 |
| SpecialPageData | |
81.82% |
9 / 11 |
|
66.67% |
4 / 6 |
8.38 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setRequestHandler | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| initDependencies | |
50.00% |
1 / 2 |
|
0.00% |
0 / 1 |
2.50 | |||
| newDefaultRequestHandler | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| showForm | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\Specials; |
| 8 | |
| 9 | use MediaWiki\Exception\HttpError; |
| 10 | use MediaWiki\LinkedData\PageDataRequestHandler; |
| 11 | use 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 | */ |
| 21 | class 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 | if ( $this->requestHandler === null ) { |
| 50 | $this->requestHandler = $this->newDefaultRequestHandler(); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Creates a PageDataRequestHandler based on global defaults. |
| 56 | * |
| 57 | * @return PageDataRequestHandler |
| 58 | */ |
| 59 | private function newDefaultRequestHandler() { |
| 60 | return new PageDataRequestHandler(); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * @see SpecialWikibasePage::execute |
| 65 | * |
| 66 | * @param string|null $subPage |
| 67 | * |
| 68 | * @throws HttpError |
| 69 | */ |
| 70 | public function execute( $subPage ) { |
| 71 | $this->initDependencies(); |
| 72 | |
| 73 | // If there is no title, show an HTML form |
| 74 | // TODO: Don't do this if HTML is not acceptable according to HTTP headers. |
| 75 | if ( !$this->requestHandler->canHandleRequest( $subPage, $this->getRequest() ) ) { |
| 76 | $this->showForm(); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | $this->requestHandler->handleRequest( $subPage, $this->getRequest(), $this->getOutput() ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Shows an informative page to the user; Called when there is no page to output. |
| 85 | */ |
| 86 | public function showForm() { |
| 87 | $this->getOutput()->showErrorPage( 'pagedata-title', 'pagedata-text' ); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Retain the old class name for backwards compatibility. |
| 93 | * @deprecated since 1.41 |
| 94 | */ |
| 95 | class_alias( SpecialPageData::class, 'SpecialPageData' ); |