coding style tweaks
[lhc/web/wiklou.git] / includes / RawPage.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 RawPage {
21 var $mArticle, $mTitle, $mRequest;
22 var $mOldId, $mGen, $mCharset, $mSection;
23 var $mSmaxage, $mMaxage;
24 var $mContentType, $mExpandTemplates;
25
26 function __construct( Article $article, $request = false ) {
27 global $wgRequest, $wgInputEncoding, $wgSquidMaxage, $wgJsMimeType, $wgGroupPermissions;
28
29 $allowedCTypes = array( 'text/x-wiki', $wgJsMimeType, 'text/css', 'application/x-zope-edit' );
30 $this->mArticle = $article;
31 $this->mTitle = $article->mTitle;
32
33 if( $request === false ) {
34 $this->mRequest = $wgRequest;
35 } else {
36 $this->mRequest = $request;
37 }
38
39 $ctype = $this->mRequest->getVal( 'ctype' );
40 $smaxage = $this->mRequest->getIntOrNull( 'smaxage' );
41 $maxage = $this->mRequest->getInt( 'maxage', $wgSquidMaxage );
42
43 $this->mExpandTemplates = $this->mRequest->getVal( 'templates' ) === 'expand';
44 $this->mUseMessageCache = $this->mRequest->getBool( 'usemsgcache' );
45
46 $this->mSection = $this->mRequest->getIntOrNull( 'section' );
47
48 $oldid = $this->mRequest->getInt( 'oldid' );
49
50 switch( $wgRequest->getText( 'direction' ) ) {
51 case 'next':
52 # output next revision, or nothing if there isn't one
53 if( $oldid ) {
54 $oldid = $this->mTitle->getNextRevisionId( $oldid );
55 }
56 $oldid = $oldid ? $oldid : -1;
57 break;
58 case 'prev':
59 # output previous revision, or nothing if there isn't one
60 if( !$oldid ) {
61 # get the current revision so we can get the penultimate one
62 $this->mArticle->getTouched();
63 $oldid = $this->mArticle->mLatest;
64 }
65 $prev = $this->mTitle->getPreviousRevisionId( $oldid );
66 $oldid = $prev ? $prev : -1 ;
67 break;
68 case 'cur':
69 $oldid = 0;
70 break;
71 }
72 $this->mOldId = $oldid;
73
74 # special case for 'generated' raw things: user css/js
75 $gen = $this->mRequest->getVal( 'gen' );
76
77 if( $gen == 'css' ) {
78 $this->mGen = $gen;
79 if( is_null( $smaxage ) ) {
80 $smaxage = $wgSquidMaxage;
81 }
82 if( $ctype == '' ) {
83 $ctype = 'text/css';
84 }
85 } else {
86 $this->mGen = false;
87 }
88 $this->mCharset = $wgInputEncoding;
89
90 # Force caching for CSS and JS raw content, default: 5 minutes
91 if( is_null( $smaxage ) && ( $ctype == 'text/css' || $ctype == $wgJsMimeType ) ) {
92 global $wgForcedRawSMaxage;
93 $this->mSmaxage = intval( $wgForcedRawSMaxage );
94 } else {
95 $this->mSmaxage = intval( $smaxage );
96 }
97 $this->mMaxage = $maxage;
98
99 # Output may contain user-specific data;
100 # vary generated content for open sessions and private wikis
101 if( $this->mGen || !$wgGroupPermissions['*']['read'] ) {
102 $this->mPrivateCache = $this->mSmaxage == 0 || session_id() != '';
103 } else {
104 $this->mPrivateCache = false;
105 }
106
107 if( $ctype == '' || !in_array( $ctype, $allowedCTypes ) ) {
108 $this->mContentType = 'text/x-wiki';
109 } else {
110 $this->mContentType = $ctype;
111 }
112 }
113
114 function view() {
115 global $wgOut, $wgRequest;
116
117 if( $wgRequest->isPathInfoBad() ) {
118 # Internet Explorer will ignore the Content-Type header if it
119 # thinks it sees a file extension it recognizes. Make sure that
120 # all raw requests are done through the script node, which will
121 # have eg '.php' and should remain safe.
122 #
123 # We used to redirect to a canonical-form URL as a general
124 # backwards-compatibility / good-citizen nice thing. However
125 # a lot of servers are set up in buggy ways, resulting in
126 # redirect loops which hang the browser until the CSS load
127 # times out.
128 #
129 # Just return a 403 Forbidden and get it over with.
130 wfHttpError( 403, 'Forbidden',
131 'Invalid file extension found in PATH_INFO. ' .
132 'Raw pages must be accessed through the primary script entry point.' );
133 return;
134 }
135
136 header( 'Content-type: ' . $this->mContentType . '; charset=' . $this->mCharset );
137 # allow the client to cache this for 24 hours
138 $mode = $this->mPrivateCache ? 'private' : 'public';
139 header( 'Cache-Control: ' . $mode . ', s-maxage=' . $this->mSmaxage . ', max-age=' . $this->mMaxage );
140
141 global $wgUseFileCache;
142 if( $wgUseFileCache && HTMLFileCache::useFileCache() ) {
143 $cache = new HTMLFileCache( $this->mTitle, 'raw' );
144 if( $cache->isFileCacheGood( /* Assume up to date */ ) ) {
145 $cache->loadFromFileCache();
146 $wgOut->disable();
147 return;
148 } else {
149 ob_start( array( &$cache, 'saveToFileCache' ) );
150 }
151 }
152
153 $text = $this->getRawText();
154
155 if( !wfRunHooks( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {
156 wfDebug( __METHOD__ . ": RawPageViewBeforeOutput hook broke raw page output.\n" );
157 }
158
159 echo $text;
160 $wgOut->disable();
161 }
162
163 function getRawText() {
164 global $wgUser, $wgOut;
165 if( $this->mGen ) {
166 $sk = $wgUser->getSkin();
167 if( !StubObject::isRealObject( $wgOut ) ) {
168 $wgOut->_unstub( 2 );
169 }
170 $sk->initPage( $wgOut );
171 if( $this->mGen == 'css' ) {
172 return $sk->generateUserStylesheet();
173 }
174 } else {
175 return $this->getArticleText();
176 }
177 }
178
179 function getArticleText() {
180 $found = false;
181 $text = '';
182 if( $this->mTitle ) {
183 // If it's a MediaWiki message we can just hit the message cache
184 if( $this->mUseMessageCache && $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
185 $key = $this->mTitle->getDBkey();
186 $text = wfMsgForContentNoTrans( $key );
187 # If the message doesn't exist, return a blank
188 if( wfEmptyMsg( $key, $text ) ) {
189 $text = '';
190 }
191 $found = true;
192 } else {
193 // Get it from the DB
194 $rev = Revision::newFromTitle( $this->mTitle, $this->mOldId );
195 if( $rev ) {
196 $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
197 header( "Last-modified: $lastmod" );
198
199 if( !is_null( $this->mSection ) ) {
200 global $wgParser;
201 $text = $wgParser->getSection( $rev->getText(), $this->mSection );
202 } else {
203 $text = $rev->getText();
204 }
205 $found = true;
206 }
207 }
208 }
209
210 # Bad title or page does not exist
211 if( !$found && $this->mContentType == 'text/x-wiki' ) {
212 # Don't return a 404 response for CSS or JavaScript;
213 # 404s aren't generally cached and it would create
214 # extra hits when user CSS/JS are on and the user doesn't
215 # have the pages.
216 header( 'HTTP/1.0 404 Not Found' );
217 }
218
219 // Special-case for empty CSS/JS
220 //
221 // Internet Explorer for Mac handles empty files badly;
222 // particularly so when keep-alive is active. It can lead
223 // to long timeouts as it seems to sit there waiting for
224 // more data that never comes.
225 //
226 // Give it a comment...
227 if( strlen( $text ) == 0 &&
228 ( $this->mContentType == 'text/css' ||
229 $this->mContentType == 'text/javascript' ) ) {
230 return '/* Empty */';
231 }
232
233 return $this->parseArticleText( $text );
234 }
235
236 function parseArticleText( $text ) {
237 if( $text === '' ) {
238 return '';
239 } else {
240 if( $this->mExpandTemplates ) {
241 global $wgParser;
242 return $wgParser->preprocess( $text, $this->mTitle, new ParserOptions() );
243 } else {
244 return $text;
245 }
246 }
247 }
248 }