| 1 | <?php |
| 2 | /** |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 16 | * http://www.gnu.org/copyleft/gpl.html |
| 17 | * |
| 18 | * @file |
| 19 | */ |
| 20 | |
| 21 | namespace MediaWiki\Specials; |
| 22 | |
| 23 | use HttpError; |
| 24 | use MediaWiki\LinkedData\PageDataRequestHandler; |
| 25 | use MediaWiki\SpecialPage\UnlistedSpecialPage; |
| 26 | |
| 27 | /** |
| 28 | * Special page to act as an endpoint for accessing raw page data. |
| 29 | * |
| 30 | * The web server should generally be configured to make this accessible via a canonical URL/URI, |
| 31 | * such as <http://my.domain.org/data/main/Foo>. |
| 32 | * |
| 33 | * @ingroup SpecialPage |
| 34 | */ |
| 35 | class SpecialPageData extends UnlistedSpecialPage { |
| 36 | |
| 37 | /** |
| 38 | * @var PageDataRequestHandler|null |
| 39 | */ |
| 40 | private $requestHandler = null; |
| 41 | |
| 42 | public function __construct() { |
| 43 | parent::__construct( 'PageData' ); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Sets the request handler to be used by the special page. |
| 48 | * May be used when a particular instance of PageDataRequestHandler is already |
| 49 | * known, e.g. during testing. |
| 50 | * |
| 51 | * If no request handler is set using this method, a default handler is created |
| 52 | * on demand by initDependencies(). |
| 53 | * |
| 54 | * @param PageDataRequestHandler $requestHandler |
| 55 | */ |
| 56 | public function setRequestHandler( PageDataRequestHandler $requestHandler ) { |
| 57 | $this->requestHandler = $requestHandler; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Initialize any un-initialized members from global context. |
| 62 | * In particular, this initializes $this->requestHandler |
| 63 | */ |
| 64 | protected function initDependencies() { |
| 65 | if ( $this->requestHandler === null ) { |
| 66 | $this->requestHandler = $this->newDefaultRequestHandler(); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Creates a PageDataRequestHandler based on global defaults. |
| 72 | * |
| 73 | * @return PageDataRequestHandler |
| 74 | */ |
| 75 | private function newDefaultRequestHandler() { |
| 76 | return new PageDataRequestHandler(); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @see SpecialWikibasePage::execute |
| 81 | * |
| 82 | * @param string|null $subPage |
| 83 | * |
| 84 | * @throws HttpError |
| 85 | */ |
| 86 | public function execute( $subPage ) { |
| 87 | $this->initDependencies(); |
| 88 | |
| 89 | // If there is no title, show an HTML form |
| 90 | // TODO: Don't do this if HTML is not acceptable according to HTTP headers. |
| 91 | if ( !$this->requestHandler->canHandleRequest( $subPage, $this->getRequest() ) ) { |
| 92 | $this->showForm(); |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | $this->requestHandler->handleRequest( $subPage, $this->getRequest(), $this->getOutput() ); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Shows an informative page to the user; Called when there is no page to output. |
| 101 | */ |
| 102 | public function showForm() { |
| 103 | $this->getOutput()->showErrorPage( 'pagedata-title', 'pagedata-text' ); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Retain the old class name for backwards compatibility. |
| 109 | * @deprecated since 1.41 |
| 110 | */ |
| 111 | class_alias( SpecialPageData::class, 'SpecialPageData' ); |