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