Localisation updates from http://translatewiki.net.
[mediawiki.git] / includes / actions / RawAction.php
blobdd14311974744e316ea1cf44c821a2d4686b7614
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;
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 == 'text/javascript' ) {
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 $ctype = $this->getRequest()->getVal( 'ctype' );
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';
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';
240 return $ctype;
245 * Backward compatibility for extensions
247 * @deprecated in 1.19
249 class RawPage extends RawAction {
250 public $mOldId;
253 * @param Page $page
254 * @param WebRequest|bool $request The WebRequest (default: false).
256 function __construct( Page $page, $request = false ) {
257 wfDeprecated( __CLASS__, '1.19' );
258 parent::__construct( $page );
260 if ( $request !== false ) {
261 $context = new DerivativeContext( $this->getContext() );
262 $context->setRequest( $request );
263 $this->context = $context;
267 public function view() {
268 $this->onView();
271 public function getOldId() {
272 # Some extensions like to set $mOldId
273 if ( $this->mOldId !== null ) {
274 return $this->mOldId;
276 return parent::getOldId();