Merge "libxml_disable_entity_loader() just in case..."
[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 * @ingroup Actions
34 */
35 class RawAction extends FormlessAction {
36 private $mGen;
37
38 public function getName() {
39 return 'raw';
40 }
41
42 public function requiresWrite() {
43 return false;
44 }
45
46 public function requiresUnblock() {
47 return false;
48 }
49
50 function onView() {
51 global $wgSquidMaxage, $wgForcedRawSMaxage;
52
53 $this->getOutput()->disable();
54 $request = $this->getRequest();
55
56 if ( !$request->checkUrlExtension() ) {
57 return;
58 }
59
60 if ( $this->getOutput()->checkLastModified( $this->page->getTouched() ) ) {
61 return; // Client cache fresh and headers sent, nothing more to do.
62 }
63
64 # special case for 'generated' raw things: user css/js
65 # This is deprecated and will only return empty content
66 $gen = $request->getVal( 'gen' );
67 $smaxage = $request->getIntOrNull( 'smaxage' );
68
69 if ( $gen == 'css' || $gen == 'js' ) {
70 $this->mGen = $gen;
71 if ( $smaxage === null ) {
72 $smaxage = $wgSquidMaxage;
73 }
74 } else {
75 $this->mGen = false;
76 }
77
78 $contentType = $this->getContentType();
79
80 # Force caching for CSS and JS raw content, default: 5 minutes
81 if ( $smaxage === null ) {
82 if ( $contentType == 'text/css' || $contentType == 'text/javascript' ) {
83 $smaxage = intval( $wgForcedRawSMaxage );
84 } else {
85 $smaxage = 0;
86 }
87 }
88
89 $maxage = $request->getInt( 'maxage', $wgSquidMaxage );
90
91 $response = $request->response();
92
93 $response->header( 'Content-type: ' . $contentType . '; charset=UTF-8' );
94 # Output may contain user-specific data;
95 # vary generated content for open sessions on private wikis
96 $privateCache = !User::isEveryoneAllowed( 'read' ) && ( $smaxage == 0 || session_id() != '' );
97 # allow the client to cache this for 24 hours
98 $mode = $privateCache ? 'private' : 'public';
99 $response->header( 'Cache-Control: ' . $mode . ', s-maxage=' . $smaxage . ', max-age=' . $maxage );
100
101 $text = $this->getRawText();
102
103 if ( $text === false && $contentType == 'text/x-wiki' ) {
104 # Don't return a 404 response for CSS or JavaScript;
105 # 404s aren't generally cached and it would create
106 # extra hits when user CSS/JS are on and the user doesn't
107 # have the pages.
108 $response->header( 'HTTP/1.x 404 Not Found' );
109 }
110
111 if ( !wfRunHooks( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {
112 wfDebug( __METHOD__ . ": RawPageViewBeforeOutput hook broke raw page output.\n" );
113 }
114
115 echo $text;
116 }
117
118 /**
119 * Get the text that should be returned, or false if the page or revision
120 * was not found.
121 *
122 * @return String|Bool
123 */
124 public function getRawText() {
125 global $wgParser;
126
127 # No longer used
128 if ( $this->mGen ) {
129 return '';
130 }
131
132 $text = false;
133 $title = $this->getTitle();
134 $request = $this->getRequest();
135
136 // If it's a MediaWiki message we can just hit the message cache
137 if ( $request->getBool( 'usemsgcache' ) && $title->getNamespace() == NS_MEDIAWIKI ) {
138 // The first "true" is to use the database, the second is to use the content langue
139 // and the last one is to specify the message key already contains the language in it ("/de", etc.)
140 $text = MessageCache::singleton()->get( $title->getDBkey(), true, true, true );
141 // If the message doesn't exist, return a blank
142 if ( $text === false ) {
143 $text = '';
144 }
145 } else {
146 // Get it from the DB
147 $rev = Revision::newFromTitle( $title, $this->getOldId() );
148 if ( $rev ) {
149 $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
150 $request->response()->header( "Last-modified: $lastmod" );
151
152 // Public-only due to cache headers
153 $content = $rev->getContent();
154
155 if ( $content === null ) {
156 // revision not found (or suppressed)
157 $text = false;
158 } elseif ( !$content instanceof TextContent ) {
159 // non-text content
160 wfHttpError( 415, "Unsupported Media Type", "The requested page uses the content model `"
161 . $content->getModel() . "` which is not supported via this interface." );
162 die();
163 } else {
164 // want a section?
165 $section = $request->getIntOrNull( 'section' );
166 if ( $section !== null ) {
167 $content = $content->getSection( $section );
168 }
169
170 if ( $content === null || $content === false ) {
171 // section not found (or section not supported, e.g. for JS and CSS)
172 $text = false;
173 } else {
174 $text = $content->getNativeData();
175 }
176 }
177 }
178 }
179
180 if ( $text !== false && $text !== '' && $request->getVal( 'templates' ) === 'expand' ) {
181 $text = $wgParser->preprocess( $text, $title, ParserOptions::newFromContext( $this->getContext() ) );
182 }
183
184 return $text;
185 }
186
187 /**
188 * Get the ID of the revision that should used to get the text.
189 *
190 * @return Integer
191 */
192 public function getOldId() {
193 $oldid = $this->getRequest()->getInt( 'oldid' );
194 switch ( $this->getRequest()->getText( 'direction' ) ) {
195 case 'next':
196 # output next revision, or nothing if there isn't one
197 if ( $oldid ) {
198 $oldid = $this->getTitle()->getNextRevisionID( $oldid );
199 }
200 $oldid = $oldid ? $oldid : -1;
201 break;
202 case 'prev':
203 # output previous revision, or nothing if there isn't one
204 if ( !$oldid ) {
205 # get the current revision so we can get the penultimate one
206 $oldid = $this->page->getLatest();
207 }
208 $prev = $this->getTitle()->getPreviousRevisionID( $oldid );
209 $oldid = $prev ? $prev : -1;
210 break;
211 case 'cur':
212 $oldid = 0;
213 break;
214 }
215 return $oldid;
216 }
217
218 /**
219 * Get the content type to use for the response
220 *
221 * @return String
222 */
223 public function getContentType() {
224 $ctype = $this->getRequest()->getVal( 'ctype' );
225
226 if ( $ctype == '' ) {
227 $gen = $this->getRequest()->getVal( 'gen' );
228 if ( $gen == 'js' ) {
229 $ctype = 'text/javascript';
230 } elseif ( $gen == 'css' ) {
231 $ctype = 'text/css';
232 }
233 }
234
235 $allowedCTypes = array( 'text/x-wiki', 'text/javascript', '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 /**
253 * @param Page $page
254 * @param WebRequest|bool $request The WebRequest (default: false).
255 */
256 function __construct( Page $page, $request = false ) {
257 wfDeprecated( __CLASS__, '1.19' );
258 parent::__construct( $page );
259
260 if ( $request !== false ) {
261 $context = new DerivativeContext( $this->getContext() );
262 $context->setRequest( $request );
263 $this->context = $context;
264 }
265 }
266
267 public function view() {
268 $this->onView();
269 }
270
271 public function getOldId() {
272 # Some extensions like to set $mOldId
273 if ( $this->mOldId !== null ) {
274 return $this->mOldId;
275 }
276 return parent::getOldId();
277 }
278 }