| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright (C) 2011-2020 Wikimedia Foundation and others. |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 |
*/ |
| 19 |
declare( strict_types = 1 ); |
| 20 |
|
| 21 |
namespace MWParsoid\Rest\Handler; |
| 22 |
|
| 23 |
use MediaWiki\Rest\Handler\Helper\ParsoidFormatHelper; |
| 24 |
use MediaWiki\Rest\Handler\ParsoidHandler as CoreParsoidHandler; |
| 25 |
use MediaWiki\Rest\HttpException; |
| 26 |
use MediaWiki\Rest\Response; |
| 27 |
use MediaWiki\Revision\SlotRecord; |
| 28 |
use Wikimedia\ParamValidator\ParamValidator; |
| 29 |
use Wikimedia\Parsoid\Config\PageConfig; |
| 30 |
|
| 31 |
/** |
| 32 |
* Handler for displaying or rendering the content of a page: |
| 33 |
* - /{domain}/v3/page/{format}/{title} |
| 34 |
* - /{domain}/v3/page/{format}/{title}/{revision} |
| 35 |
* @see https://www.mediawiki.org/wiki/Parsoid/API#GET |
| 36 |
*/ |
| 37 |
class PageHandler extends CoreParsoidHandler { |
| 38 |
|
| 39 |
// NOTE: this controls redirects by overriding methods! |
| 40 |
use EndpointRedirectTrait; |
| 41 |
|
| 42 |
/** @inheritDoc */ |
| 43 |
public function getParamSettings() { |
| 44 |
return [ |
| 45 |
'domain' => [ |
| 46 |
self::PARAM_SOURCE => 'path', |
| 47 |
ParamValidator::PARAM_TYPE => 'string', |
| 48 |
ParamValidator::PARAM_REQUIRED => true, |
| 49 |
], |
| 50 |
'format' => [ |
| 51 |
self::PARAM_SOURCE => 'path', |
| 52 |
ParamValidator::PARAM_TYPE => 'string', |
| 53 |
ParamValidator::PARAM_REQUIRED => true, |
| 54 |
], |
| 55 |
'title' => [ |
| 56 |
self::PARAM_SOURCE => 'path', |
| 57 |
ParamValidator::PARAM_TYPE => 'string', |
| 58 |
ParamValidator::PARAM_REQUIRED => true, |
| 59 |
], |
| 60 |
'revision' => [ |
| 61 |
self::PARAM_SOURCE => 'path', |
| 62 |
ParamValidator::PARAM_TYPE => 'string', |
| 63 |
ParamValidator::PARAM_REQUIRED => false, |
| 64 |
], |
| 65 |
]; |
| 66 |
} |
| 67 |
|
| 68 |
/** @inheritDoc */ |
| 69 |
public function execute(): Response { |
| 70 |
$request = $this->getRequest(); |
| 71 |
$format = $request->getPathParam( 'format' ); |
| 72 |
|
| 73 |
if ( !in_array( $format, ParsoidFormatHelper::VALID_PAGE, true ) ) { |
| 74 |
throw new HttpException( |
| 75 |
"Invalid page format: {$format}", 404 |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
$attribs = $this->getRequestAttributes(); |
| 80 |
|
| 81 |
if ( !$this->acceptable( $attribs ) ) { // mutates $attribs |
| 82 |
throw new HttpException( |
| 83 |
'Not acceptable', 406 |
| 84 |
); |
| 85 |
} |
| 86 |
|
| 87 |
$pageConfig = $this->tryToCreatePageConfig( $attribs ); |
| 88 |
|
| 89 |
if ( $format === ParsoidFormatHelper::FORMAT_WIKITEXT ) { |
| 90 |
return $this->getPageContentResponse( $pageConfig, $attribs ); |
| 91 |
} else { |
| 92 |
return $this->wt2html( $pageConfig, $attribs ); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Return the content of a page. This is the case when GET /page/ is |
| 98 |
* called with format=wikitext. |
| 99 |
* |
| 100 |
* @param PageConfig $pageConfig |
| 101 |
* @param array $attribs Request attributes from getRequestAttributes() |
| 102 |
* @return Response |
| 103 |
* @throws HttpException |
| 104 |
*/ |
| 105 |
protected function getPageContentResponse( |
| 106 |
PageConfig $pageConfig, array $attribs |
| 107 |
) { |
| 108 |
$content = $pageConfig->getRevisionContent(); |
| 109 |
if ( !$content ) { |
| 110 |
throw new HttpException( |
| 111 |
'The specified revision does not exist.', 404 |
| 112 |
); |
| 113 |
} |
| 114 |
$response = $this->getResponseFactory()->create(); |
| 115 |
$response->setStatus( 200 ); |
| 116 |
$response->setHeader( 'X-ContentModel', $content->getModel( SlotRecord::MAIN ) ); |
| 117 |
ParsoidFormatHelper::setContentType( |
| 118 |
$response, ParsoidFormatHelper::FORMAT_WIKITEXT, |
| 119 |
$attribs['envOptions']['outputContentVersion'] |
| 120 |
); |
| 121 |
$response->getBody()->write( $content->getContent( SlotRecord::MAIN ) ); |
| 122 |
return $response; |
| 123 |
} |
| 124 |
|
| 125 |
} |