67d31172f734707eb8511527d44eaa22d2dc0588
[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 * License: GPL (http://www.gnu.org/copyleft/gpl.html)
11 *
12 * @author Gabriel Wicke <wicke@wikidev.net>
13 * @file
14 */
15
16 /**
17 * A simple method to retrieve the plain source of an article,
18 * using "action=raw" in the GET request string.
19 */
20 class RawAction extends FormlessAction {
21 private $mGen;
22
23 public function getName() {
24 return 'raw';
25 }
26
27 public function getRestriction() {
28 return null;
29 }
30
31 public function requiresWrite() {
32 return false;
33 }
34
35 public function requiresUnblock() {
36 return false;
37 }
38
39 function onView() {
40 global $wgGroupPermissions, $wgSquidMaxage, $wgForcedRawSMaxage, $wgJsMimeType;
41
42 $this->getOutput()->disable();
43 $request = $this->getRequest();
44
45 if ( !$request->checkUrlExtension() ) {
46 return;
47 }
48
49 # special case for 'generated' raw things: user css/js
50 # This is deprecated and will only return empty content
51 $gen = $request->getVal( 'gen' );
52 $smaxage = $request->getIntOrNull( 'smaxage' );
53
54 if ( $gen == 'css' || $gen == 'js' ) {
55 $this->mGen = $gen;
56 if ( $smaxage === null ) {
57 $smaxage = $wgSquidMaxage;
58 }
59 } else {
60 $this->mGen = false;
61 }
62
63 $contentType = $this->getContentType();
64
65 # Force caching for CSS and JS raw content, default: 5 minutes
66 if ( $smaxage === null ) {
67 if ( $contentType == 'text/css' || $contentType == $wgJsMimeType ) {
68 $smaxage = intval( $wgForcedRawSMaxage );
69 } else {
70 $smaxage = 0;
71 }
72 }
73
74 $maxage = $request->getInt( 'maxage', $wgSquidMaxage );
75
76 $response = $request->response();
77
78 $response->header( 'Content-type: ' . $contentType . '; charset=UTF-8' );
79 # Output may contain user-specific data;
80 # vary generated content for open sessions on private wikis
81 $privateCache = !$wgGroupPermissions['*']['read']
82 && ( $smaxage == 0 || session_id() != '' );
83 # allow the client to cache this for 24 hours
84 $mode = $privateCache ? 'private' : 'public';
85 $response->header( 'Cache-Control: ' . $mode . ', s-maxage=' . $smaxage . ', max-age=' . $maxage );
86
87 $text = $this->getRawText();
88
89 if ( $text === false && $contentType == 'text/x-wiki' ) {
90 # Don't return a 404 response for CSS or JavaScript;
91 # 404s aren't generally cached and it would create
92 # extra hits when user CSS/JS are on and the user doesn't
93 # have the pages.
94 $response->header( 'HTTP/1.x 404 Not Found' );
95 }
96
97 if ( !wfRunHooks( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {
98 wfDebug( __METHOD__ . ": RawPageViewBeforeOutput hook broke raw page output.\n" );
99 }
100
101 echo $text;
102 }
103
104 /**
105 * Get the text that should be returned, or false if the page or revision
106 * was not found.
107 *
108 * @return String|Bool
109 */
110 public function getRawText() {
111 global $wgParser;
112
113 # No longer used
114 if( $this->mGen ) {
115 return '';
116 }
117
118 $text = false;
119 $title = $this->getTitle();
120 $request = $this->getRequest();
121
122 // If it's a MediaWiki message we can just hit the message cache
123 if ( $request->getBool( 'usemsgcache' ) && $title->getNamespace() == NS_MEDIAWIKI ) {
124 $msg = wfMessage( $title->getDBkey() )->inContentLanguage();
125 # If the message doesn't exist, return a blank
126 $text = !$msg->exists() ? '' : $msg->plain();
127 } else {
128 // Get it from the DB
129 $rev = Revision::newFromTitle( $title, $this->getOldId() );
130 if ( $rev ) {
131 $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
132 $request->response()->header( "Last-modified: $lastmod" );
133
134 $text = $rev->getText( Revision::FOR_THIS_USER, $this->getUser() );
135 $section = $request->getIntOrNull( 'section' );
136 if ( $section !== null ) {
137 $text = $wgParser->getSection( $text, $section );
138 }
139 }
140 }
141
142 if ( $text !== false && $text !== '' && $request->getVal( 'templates' ) === 'expand' ) {
143 $text = $wgParser->preprocess( $text, $title, ParserOptions::newFromContext( $this->getContext() ) );
144 }
145
146 return $text;
147 }
148
149 /**
150 * Get the ID of the revision that should used to get the text.
151 *
152 * @return Integer
153 */
154 public function getOldId() {
155 $oldid = $this->getRequest()->getInt( 'oldid' );
156 switch ( $this->getRequest()->getText( 'direction' ) ) {
157 case 'next':
158 # output next revision, or nothing if there isn't one
159 if( $oldid ) {
160 $oldid = $this->getTitle()->getNextRevisionId( $oldid );
161 }
162 $oldid = $oldid ? $oldid : -1;
163 break;
164 case 'prev':
165 # output previous revision, or nothing if there isn't one
166 if( !$oldid ) {
167 # get the current revision so we can get the penultimate one
168 $oldid = $this->getTitle()->getLatestRevID();
169 }
170 $prev = $this->getTitle()->getPreviousRevisionId( $oldid );
171 $oldid = $prev ? $prev : -1 ;
172 break;
173 case 'cur':
174 $oldid = 0;
175 break;
176 }
177 return $oldid;
178 }
179
180 /**
181 * Get the content type to use for the response
182 *
183 * @return String
184 */
185 public function getContentType() {
186 global $wgJsMimeType;
187
188 $ctype = $this->getRequest()->getVal( 'ctype' );
189
190 if ( $ctype == '' ) {
191 $gen = $this->getRequest()->getVal( 'gen' );
192 if ( $gen == 'js' ) {
193 $ctype = $wgJsMimeType;
194 } elseif ( $gen == 'css' ) {
195 $ctype = 'text/css';
196 }
197 }
198
199 $allowedCTypes = array( 'text/x-wiki', $wgJsMimeType, 'text/css', 'application/x-zope-edit' );
200 if ( $ctype == '' || !in_array( $ctype, $allowedCTypes ) ) {
201 $ctype = 'text/x-wiki';
202 }
203
204 return $ctype;
205 }
206 }
207
208 /**
209 * Backward compatibility for extensions
210 *
211 * @deprecated in 1.19
212 */
213 class RawPage extends RawAction {
214 public $mOldId;
215
216 function __construct( Page $page, $request = false ) {
217 parent::__construct( $page );
218
219 if ( $request !== false ) {
220 $context = new DerivativeContext( $this->getContext() );
221 $context->setRequest( $request );
222 $this->context = $context;
223 }
224 }
225
226 public function view() {
227 $this->onView();
228 }
229
230 public function getOldId() {
231 # Some extensions like to set $mOldId
232 if ( $this->mOldId !== null ) {
233 return $this->mOldId;
234 }
235 return parent::getOldId();
236 }
237 }