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