Merge "mw.Feedback: If the message is posted remotely, link the title correctly"
[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 * @throws HttpError
44 */
45 public function canHandleRequest( $subPage, WebRequest $request ) {
46 if ( $subPage === '' || $subPage === null ) {
47 if ( $request->getText( 'target', '' ) === '' ) {
48 return false;
49 } else {
50 return true;
51 }
52 }
53
54 $parts = explode( '/', $subPage, 2 );
55 if ( $parts !== 2 ) {
56 $slot = $parts[0];
57 if ( $slot === 'main' || $slot === '' ) {
58 return true;
59 }
60 }
61
62 return false;
63 }
64
65 /**
66 * Main method for handling requests.
67 *
68 * @param string $subPage
69 * @param WebRequest $request The request parameters. Known parameters are:
70 * - title: the page title
71 * - format: the format
72 * - oldid|revision: the revision ID
73 * @param OutputPage $output
74 *
75 * @note: Instead of an output page, a WebResponse could be sufficient, but
76 * redirect logic is currently implemented in OutputPage.
77 *
78 * @throws HttpError
79 */
80 public function handleRequest( $subPage, WebRequest $request, OutputPage $output ) {
81 // No matter what: The response is always public
82 $output->getRequest()->response()->header( 'Access-Control-Allow-Origin: *' );
83
84 if ( !$this->canHandleRequest( $subPage, $request ) ) {
85 throw new HttpError( 400, wfMessage( 'pagedata-bad-title', $subPage ) );
86 }
87
88 $revision = 0;
89
90 $parts = explode( '/', $subPage, 2 );
91 if ( $subPage !== '' ) {
92 $title = $parts[1];
93 } else {
94 $title = $request->getText( 'target', '' );
95 }
96
97 $revision = $request->getInt( 'oldid', $revision );
98 $revision = $request->getInt( 'revision', $revision );
99
100 if ( $title === null || $title === '' ) {
101 //TODO: different error message?
102 throw new HttpError( 400, wfMessage( 'pagedata-bad-title', $title ) );
103 }
104
105 try {
106 $title = Title::newFromTextThrow( $title );
107 } catch ( MalformedTitleException $ex ) {
108 throw new HttpError( 400, wfMessage( 'pagedata-bad-title', $title ) );
109 }
110
111 $this->httpContentNegotiation( $request, $output, $title, $revision );
112 }
113
114 /**
115 * Applies HTTP content negotiation.
116 * If the negotiation is successful, this method will set the appropriate redirect
117 * in the OutputPage object and return. Otherwise, an HttpError is thrown.
118 *
119 * @param WebRequest $request
120 * @param OutputPage $output
121 * @param Title $title
122 * @param int $revision The desired revision
123 *
124 * @throws HttpError
125 */
126 public function httpContentNegotiation(
127 WebRequest $request,
128 OutputPage $output,
129 Title $title,
130 $revision = 0
131 ) {
132 $contentHandler = ContentHandler::getForTitle( $title );
133 $mimeTypes = $contentHandler->getSupportedFormats();
134
135 $headers = $request->getAllHeaders();
136 if ( isset( $headers['ACCEPT'] ) ) {
137 $parser = new HttpAcceptParser();
138 $accept = $parser->parseWeights( $headers['ACCEPT'] );
139 } else {
140 // anything goes
141 $accept = [
142 '*' => 0.1 // just to make extra sure
143 ];
144 // prefer the default
145 $accept[$mimeTypes[0]] = 1;
146 }
147
148 $negotiator = new HttpAcceptNegotiator( $mimeTypes );
149 $format = $negotiator->getBestSupportedKey( $accept, null );
150
151 if ( $format === null ) {
152 $format = isset( $accept['text/html'] ) ? 'text/html' : null;
153 }
154
155 if ( $format === null ) {
156 $msg = wfMessage( 'pagedata-not-acceptable', implode( ', ', $mimeTypes ) );
157 throw new HttpError( 406, $msg );
158 }
159
160 $url = $this->getDocUrl( $title, $format, $revision );
161 $output->redirect( $url, 303 );
162 }
163
164 /**
165 * Returns a url representing the given title.
166 *
167 * @param Title $title
168 * @param string|null $format The (normalized) format name, or ''
169 * @param int $revision
170 * @return string
171 */
172 private function getDocUrl( Title $title, $format = '', $revision = 0 ) {
173 $params = [];
174
175 if ( $revision > 0 ) {
176 $params['oldid'] = $revision;
177 }
178
179 if ( $format === 'text/html' ) {
180 return $title->getFullURL( $params );
181 }
182
183 $params[ 'action' ] = 'raw';
184
185 return $title->getFullURL( $params );
186 }
187
188 }