Fixed spacing in actions/cache/filebackend/filerepo/job folder
[mediawiki.git] / includes / actions / RawAction.php
blob3fa20b70e5b5ce74b191c2aa7435b7b4fb9d689e
1 <?php
2 /**
3 * Raw page text accessor
5 * Copyright © 2004 Gabriel Wicke <wicke@wikidev.net>
6 * http://wikidev.net/
8 * Based on HistoryPage and SpecialExport
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.
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.
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
25 * @author Gabriel Wicke <wicke@wikidev.net>
26 * @file
29 /**
30 * A simple method to retrieve the plain source of an article,
31 * using "action=raw" in the GET request string.
33 * @ingroup Actions
35 class RawAction extends FormlessAction {
36 private $mGen;
38 public function getName() {
39 return 'raw';
42 public function requiresWrite() {
43 return false;
46 public function requiresUnblock() {
47 return false;
50 function onView() {
51 global $wgSquidMaxage, $wgForcedRawSMaxage, $wgJsMimeType;
53 $this->getOutput()->disable();
54 $request = $this->getRequest();
56 if ( !$request->checkUrlExtension() ) {
57 return;
60 if ( $this->getOutput()->checkLastModified( $this->page->getTouched() ) ) {
61 return; // Client cache fresh and headers sent, nothing more to do.
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' );
69 if ( $gen == 'css' || $gen == 'js' ) {
70 $this->mGen = $gen;
71 if ( $smaxage === null ) {
72 $smaxage = $wgSquidMaxage;
74 } else {
75 $this->mGen = false;
78 $contentType = $this->getContentType();
80 # Force caching for CSS and JS raw content, default: 5 minutes
81 if ( $smaxage === null ) {
82 if ( $contentType == 'text/css' || $contentType == $wgJsMimeType ) {
83 $smaxage = intval( $wgForcedRawSMaxage );
84 } else {
85 $smaxage = 0;
89 $maxage = $request->getInt( 'maxage', $wgSquidMaxage );
91 $response = $request->response();
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::groupHasPermission( '*', '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 );
101 $text = $this->getRawText();
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' );
111 if ( !wfRunHooks( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {
112 wfDebug( __METHOD__ . ": RawPageViewBeforeOutput hook broke raw page output.\n" );
115 echo $text;
119 * Get the text that should be returned, or false if the page or revision
120 * was not found.
122 * @return String|Bool
124 public function getRawText() {
125 global $wgParser;
127 # No longer used
128 if ( $this->mGen ) {
129 return '';
132 $text = false;
133 $title = $this->getTitle();
134 $request = $this->getRequest();
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 = '';
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" );
152 // Public-only due to cache headers
153 $content = $rev->getContent();
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 );
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();
180 if ( $text !== false && $text !== '' && $request->getVal( 'templates' ) === 'expand' ) {
181 $text = $wgParser->preprocess( $text, $title, ParserOptions::newFromContext( $this->getContext() ) );
184 return $text;
188 * Get the ID of the revision that should used to get the text.
190 * @return Integer
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 );
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();
208 $prev = $this->getTitle()->getPreviousRevisionID( $oldid );
209 $oldid = $prev ? $prev : -1;
210 break;
211 case 'cur':
212 $oldid = 0;
213 break;
215 return $oldid;
219 * Get the content type to use for the response
221 * @return String
223 public function getContentType() {
224 global $wgJsMimeType;
226 $ctype = $this->getRequest()->getVal( 'ctype' );
228 if ( $ctype == '' ) {
229 $gen = $this->getRequest()->getVal( 'gen' );
230 if ( $gen == 'js' ) {
231 $ctype = $wgJsMimeType;
232 } elseif ( $gen == 'css' ) {
233 $ctype = 'text/css';
237 $allowedCTypes = array( 'text/x-wiki', $wgJsMimeType, 'text/css', 'application/x-zope-edit' );
238 if ( $ctype == '' || !in_array( $ctype, $allowedCTypes ) ) {
239 $ctype = 'text/x-wiki';
242 return $ctype;
247 * Backward compatibility for extensions
249 * @deprecated in 1.19
251 class RawPage extends RawAction {
252 public $mOldId;
255 * @param Page $page
256 * @param WebRequest|bool $request The WebRequest (default: false).
258 function __construct( Page $page, $request = false ) {
259 wfDeprecated( __CLASS__, '1.19' );
260 parent::__construct( $page );
262 if ( $request !== false ) {
263 $context = new DerivativeContext( $this->getContext() );
264 $context->setRequest( $request );
265 $this->context = $context;
269 public function view() {
270 $this->onView();
273 public function getOldId() {
274 # Some extensions like to set $mOldId
275 if ( $this->mOldId !== null ) {
276 return $this->mOldId;
278 return parent::getOldId();