linkeddata: Remove unnecessary arguments and shrink if...else
[lhc/web/wiklou.git] / includes / linkeddata / PageDataRequestHandler.php
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 use Wikimedia\Http\HttpAcceptParser;
22 use Wikimedia\Http\HttpAcceptNegotiator;
23
24 /**
25 * Request handler implementing a data interface for mediawiki pages.
26 *
27 * @author Daniel Kinzler
28 * @author Amir Sarabadanai
29 */
30 class PageDataRequestHandler {
31
32 /**
33 * Checks whether the request is complete, i.e. whether it contains all information needed
34 * to reply with page data.
35 *
36 * This does not check whether the request is valid and will actually produce a successful
37 * response.
38 *
39 * @param string|null $subPage
40 * @param WebRequest $request
41 *
42 * @return bool
43 */
44 public function canHandleRequest( $subPage, WebRequest $request ) {
45 if ( $subPage === '' || $subPage === null ) {
46 return $request->getText( 'target' ) !== '';
47 }
48
49 $parts = explode( '/', $subPage, 2 );
50 if ( $parts !== 2 ) {
51 $slot = $parts[0];
52 if ( $slot === 'main' || $slot === '' ) {
53 return true;
54 }
55 }
56
57 return false;
58 }
59
60 /**
61 * Main method for handling requests.
62 *
63 * @param string $subPage
64 * @param WebRequest $request The request parameters. Known parameters are:
65 * - title: the page title
66 * - format: the format
67 * - oldid|revision: the revision ID
68 * @param OutputPage $output
69 *
70 * @note Instead of an output page, a WebResponse could be sufficient, but
71 * redirect logic is currently implemented in OutputPage.
72 *
73 * @throws HttpError
74 */
75 public function handleRequest( $subPage, WebRequest $request, OutputPage $output ) {
76 // No matter what: The response is always public
77 $output->getRequest()->response()->header( 'Access-Control-Allow-Origin: *' );
78
79 if ( !$this->canHandleRequest( $subPage, $request ) ) {
80 throw new HttpError( 400, wfMessage( 'pagedata-bad-title', $subPage ) );
81 }
82
83 $revision = 0;
84
85 $parts = explode( '/', $subPage, 2 );
86 if ( $subPage !== '' ) {
87 $title = $parts[1];
88 } else {
89 $title = $request->getText( 'target' );
90 }
91
92 $revision = $request->getInt( 'oldid', $revision );
93 $revision = $request->getInt( 'revision', $revision );
94
95 if ( $title === null || $title === '' ) {
96 //TODO: different error message?
97 throw new HttpError( 400, wfMessage( 'pagedata-bad-title', $title ) );
98 }
99
100 try {
101 $title = Title::newFromTextThrow( $title );
102 } catch ( MalformedTitleException $ex ) {
103 throw new HttpError( 400, wfMessage( 'pagedata-bad-title', $title ) );
104 }
105
106 $this->httpContentNegotiation( $request, $output, $title, $revision );
107 }
108
109 /**
110 * Applies HTTP content negotiation.
111 * If the negotiation is successful, this method will set the appropriate redirect
112 * in the OutputPage object and return. Otherwise, an HttpError is thrown.
113 *
114 * @param WebRequest $request
115 * @param OutputPage $output
116 * @param Title $title
117 * @param int $revision The desired revision
118 *
119 * @throws HttpError
120 */
121 public function httpContentNegotiation(
122 WebRequest $request,
123 OutputPage $output,
124 Title $title,
125 $revision = 0
126 ) {
127 $contentHandler = ContentHandler::getForTitle( $title );
128 $mimeTypes = $contentHandler->getSupportedFormats();
129
130 $acceptHeader = $request->getHeader( 'Accept' );
131 if ( $acceptHeader !== false ) {
132 $parser = new HttpAcceptParser();
133 $accept = $parser->parseWeights( $acceptHeader );
134 } else {
135 // anything goes
136 $accept = [
137 '*' => 0.1 // just to make extra sure
138 ];
139 // prefer the default
140 $accept[$mimeTypes[0]] = 1;
141 }
142
143 $negotiator = new HttpAcceptNegotiator( $mimeTypes );
144 $format = $negotiator->getBestSupportedKey( $accept );
145
146 if ( $format === null ) {
147 $format = isset( $accept['text/html'] ) ? 'text/html' : null;
148 }
149
150 if ( $format === null ) {
151 $msg = wfMessage( 'pagedata-not-acceptable', implode( ', ', $mimeTypes ) );
152 throw new HttpError( 406, $msg );
153 }
154
155 $url = $this->getDocUrl( $title, $format, $revision );
156 $output->redirect( $url, 303 );
157 }
158
159 /**
160 * Returns a url representing the given title.
161 *
162 * @param Title $title
163 * @param string|null $format The (normalized) format name, or ''
164 * @param int $revision
165 * @return string
166 */
167 private function getDocUrl( Title $title, $format = '', $revision = 0 ) {
168 $params = [];
169
170 if ( $revision > 0 ) {
171 $params['oldid'] = $revision;
172 }
173
174 if ( $format === 'text/html' ) {
175 return $title->getFullURL( $params );
176 }
177
178 $params[ 'action' ] = 'raw';
179
180 return $title->getFullURL( $params );
181 }
182
183 }