Merge "Reduced some master queries by adding flags to Revision functions."
[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 $wgGroupPermissions, $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 = !$wgGroupPermissions['*']['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 $text = $rev->getText();
152 $section = $request->getIntOrNull( 'section' );
153 if ( $section !== null ) {
154 $text = $wgParser->getSection( $text, $section );
155 }
156 }
157 }
158
159 if ( $text !== false && $text !== '' && $request->getVal( 'templates' ) === 'expand' ) {
160 $text = $wgParser->preprocess( $text, $title, ParserOptions::newFromContext( $this->getContext() ) );
161 }
162
163 return $text;
164 }
165
166 /**
167 * Get the ID of the revision that should used to get the text.
168 *
169 * @return Integer
170 */
171 public function getOldId() {
172 $oldid = $this->getRequest()->getInt( 'oldid' );
173 switch ( $this->getRequest()->getText( 'direction' ) ) {
174 case 'next':
175 # output next revision, or nothing if there isn't one
176 if( $oldid ) {
177 $oldid = $this->getTitle()->getNextRevisionId( $oldid );
178 }
179 $oldid = $oldid ? $oldid : -1;
180 break;
181 case 'prev':
182 # output previous revision, or nothing if there isn't one
183 if( !$oldid ) {
184 # get the current revision so we can get the penultimate one
185 $oldid = $this->page->getLatest();
186 }
187 $prev = $this->getTitle()->getPreviousRevisionId( $oldid );
188 $oldid = $prev ? $prev : -1 ;
189 break;
190 case 'cur':
191 $oldid = 0;
192 break;
193 }
194 return $oldid;
195 }
196
197 /**
198 * Get the content type to use for the response
199 *
200 * @return String
201 */
202 public function getContentType() {
203 global $wgJsMimeType;
204
205 $ctype = $this->getRequest()->getVal( 'ctype' );
206
207 if ( $ctype == '' ) {
208 $gen = $this->getRequest()->getVal( 'gen' );
209 if ( $gen == 'js' ) {
210 $ctype = $wgJsMimeType;
211 } elseif ( $gen == 'css' ) {
212 $ctype = 'text/css';
213 }
214 }
215
216 $allowedCTypes = array( 'text/x-wiki', $wgJsMimeType, 'text/css', 'application/x-zope-edit' );
217 if ( $ctype == '' || !in_array( $ctype, $allowedCTypes ) ) {
218 $ctype = 'text/x-wiki';
219 }
220
221 return $ctype;
222 }
223 }
224
225 /**
226 * Backward compatibility for extensions
227 *
228 * @deprecated in 1.19
229 */
230 class RawPage extends RawAction {
231 public $mOldId;
232
233 function __construct( Page $page, $request = false ) {
234 wfDeprecated( __CLASS__, '1.19' );
235 parent::__construct( $page );
236
237 if ( $request !== false ) {
238 $context = new DerivativeContext( $this->getContext() );
239 $context->setRequest( $request );
240 $this->context = $context;
241 }
242 }
243
244 public function view() {
245 $this->onView();
246 }
247
248 public function getOldId() {
249 # Some extensions like to set $mOldId
250 if ( $this->mOldId !== null ) {
251 return $this->mOldId;
252 }
253 return parent::getOldId();
254 }
255 }