[Bug 41128] Handle null content in action=raw.
[lhc/web/wiklou.git] / includes / actions / RawAction.php
1 <?php
2 /**
3 * Raw page text accessor
4 *
5 * Copyright © 2004 Gabriel Wicke <wicke@wikidev.net>
6 * http://wikidev.net/
7 *
8 * Based on HistoryPage and SpecialExport
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 *
25 * @author Gabriel Wicke <wicke@wikidev.net>
26 * @file
27 */
28
29 /**
30 * A simple method to retrieve the plain source of an article,
31 * using "action=raw" in the GET request string.
32 */
33 class RawAction extends FormlessAction {
34 private $mGen;
35
36 public function getName() {
37 return 'raw';
38 }
39
40 public function requiresWrite() {
41 return false;
42 }
43
44 public function requiresUnblock() {
45 return false;
46 }
47
48 function onView() {
49 global $wgSquidMaxage, $wgForcedRawSMaxage, $wgJsMimeType;
50
51 $this->getOutput()->disable();
52 $request = $this->getRequest();
53
54 if ( !$request->checkUrlExtension() ) {
55 return;
56 }
57
58 if ( $this->getOutput()->checkLastModified( $this->page->getTouched() ) ) {
59 return; // Client cache fresh and headers sent, nothing more to do.
60 }
61
62 # special case for 'generated' raw things: user css/js
63 # This is deprecated and will only return empty content
64 $gen = $request->getVal( 'gen' );
65 $smaxage = $request->getIntOrNull( 'smaxage' );
66
67 if ( $gen == 'css' || $gen == 'js' ) {
68 $this->mGen = $gen;
69 if ( $smaxage === null ) {
70 $smaxage = $wgSquidMaxage;
71 }
72 } else {
73 $this->mGen = false;
74 }
75
76 $contentType = $this->getContentType();
77
78 # Force caching for CSS and JS raw content, default: 5 minutes
79 if ( $smaxage === null ) {
80 if ( $contentType == 'text/css' || $contentType == $wgJsMimeType ) {
81 $smaxage = intval( $wgForcedRawSMaxage );
82 } else {
83 $smaxage = 0;
84 }
85 }
86
87 $maxage = $request->getInt( 'maxage', $wgSquidMaxage );
88
89 $response = $request->response();
90
91 $response->header( 'Content-type: ' . $contentType . '; charset=UTF-8' );
92 # Output may contain user-specific data;
93 # vary generated content for open sessions on private wikis
94 $privateCache = !User::groupHasPermission( '*', 'read' ) && ( $smaxage == 0 || session_id() != '' );
95 # allow the client to cache this for 24 hours
96 $mode = $privateCache ? 'private' : 'public';
97 $response->header( 'Cache-Control: ' . $mode . ', s-maxage=' . $smaxage . ', max-age=' . $maxage );
98
99 $text = $this->getRawText();
100
101 if ( $text === false && $contentType == 'text/x-wiki' ) {
102 # Don't return a 404 response for CSS or JavaScript;
103 # 404s aren't generally cached and it would create
104 # extra hits when user CSS/JS are on and the user doesn't
105 # have the pages.
106 $response->header( 'HTTP/1.x 404 Not Found' );
107 }
108
109 if ( !wfRunHooks( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {
110 wfDebug( __METHOD__ . ": RawPageViewBeforeOutput hook broke raw page output.\n" );
111 }
112
113 echo $text;
114 }
115
116 /**
117 * Get the text that should be returned, or false if the page or revision
118 * was not found.
119 *
120 * @return String|Bool
121 */
122 public function getRawText() {
123 global $wgParser;
124
125 # No longer used
126 if( $this->mGen ) {
127 return '';
128 }
129
130 $text = false;
131 $title = $this->getTitle();
132 $request = $this->getRequest();
133
134 // If it's a MediaWiki message we can just hit the message cache
135 if ( $request->getBool( 'usemsgcache' ) && $title->getNamespace() == NS_MEDIAWIKI ) {
136 // The first "true" is to use the database, the second is to use the content langue
137 // and the last one is to specify the message key already contains the language in it ("/de", etc.)
138 $text = MessageCache::singleton()->get( $title->getDBkey(), true, true, true );
139 // If the message doesn't exist, return a blank
140 if ( $text === false ) {
141 $text = '';
142 }
143 } else {
144 // Get it from the DB
145 $rev = Revision::newFromTitle( $title, $this->getOldId() );
146 if ( $rev ) {
147 $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
148 $request->response()->header( "Last-modified: $lastmod" );
149
150 // Public-only due to cache headers
151 $content = $rev->getContent();
152
153 if ( $content === null ) {
154 // revision not found (or suppressed)
155 $text = false;
156 } elseif ( !$content instanceof TextContent ) {
157 // non-text content
158 wfHttpError( 415, "Unsupported Media Type", "The requested page uses the content model `"
159 . $content->getModel() . "` which is not supported via this interface." );
160 die();
161 } else {
162 // want a section?
163 $section = $request->getIntOrNull( 'section' );
164 if ( $section !== null ) {
165 $content = $content->getSection( $section );
166 }
167
168 if ( $content === null || $content === false ) {
169 // section not found (or section not supported, e.g. for JS and CSS)
170 $text = false;
171 } else {
172 $text = $content->getNativeData();
173 }
174 }
175 }
176 }
177
178 if ( $text !== false && $text !== '' && $request->getVal( 'templates' ) === 'expand' ) {
179 $text = $wgParser->preprocess( $text, $title, ParserOptions::newFromContext( $this->getContext() ) );
180 }
181
182 return $text;
183 }
184
185 /**
186 * Get the ID of the revision that should used to get the text.
187 *
188 * @return Integer
189 */
190 public function getOldId() {
191 $oldid = $this->getRequest()->getInt( 'oldid' );
192 switch ( $this->getRequest()->getText( 'direction' ) ) {
193 case 'next':
194 # output next revision, or nothing if there isn't one
195 if( $oldid ) {
196 $oldid = $this->getTitle()->getNextRevisionId( $oldid );
197 }
198 $oldid = $oldid ? $oldid : -1;
199 break;
200 case 'prev':
201 # output previous revision, or nothing if there isn't one
202 if( !$oldid ) {
203 # get the current revision so we can get the penultimate one
204 $oldid = $this->page->getLatest();
205 }
206 $prev = $this->getTitle()->getPreviousRevisionId( $oldid );
207 $oldid = $prev ? $prev : -1 ;
208 break;
209 case 'cur':
210 $oldid = 0;
211 break;
212 }
213 return $oldid;
214 }
215
216 /**
217 * Get the content type to use for the response
218 *
219 * @return String
220 */
221 public function getContentType() {
222 global $wgJsMimeType;
223
224 $ctype = $this->getRequest()->getVal( 'ctype' );
225
226 if ( $ctype == '' ) {
227 $gen = $this->getRequest()->getVal( 'gen' );
228 if ( $gen == 'js' ) {
229 $ctype = $wgJsMimeType;
230 } elseif ( $gen == 'css' ) {
231 $ctype = 'text/css';
232 }
233 }
234
235 $allowedCTypes = array( 'text/x-wiki', $wgJsMimeType, 'text/css', 'application/x-zope-edit' );
236 if ( $ctype == '' || !in_array( $ctype, $allowedCTypes ) ) {
237 $ctype = 'text/x-wiki';
238 }
239
240 return $ctype;
241 }
242 }
243
244 /**
245 * Backward compatibility for extensions
246 *
247 * @deprecated in 1.19
248 */
249 class RawPage extends RawAction {
250 public $mOldId;
251
252 function __construct( Page $page, $request = false ) {
253 wfDeprecated( __CLASS__, '1.19' );
254 parent::__construct( $page );
255
256 if ( $request !== false ) {
257 $context = new DerivativeContext( $this->getContext() );
258 $context->setRequest( $request );
259 $this->context = $context;
260 }
261 }
262
263 public function view() {
264 $this->onView();
265 }
266
267 public function getOldId() {
268 # Some extensions like to set $mOldId
269 if ( $this->mOldId !== null ) {
270 return $this->mOldId;
271 }
272 return parent::getOldId();
273 }
274 }