Suggest running update.php on database error
[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 ) ) $smaxage = $wgSquidMaxage;
80 if($ctype == '') $ctype = 'text/css';
81 } else {
82 $this->mGen = false;
83 }
84 $this->mCharset = $wgInputEncoding;
85
86 # Force caching for CSS and JS raw content, default: 5 minutes
87 if( is_null($smaxage) and ($ctype=='text/css' or $ctype==$wgJsMimeType) ) {
88 global $wgForcedRawSMaxage;
89 $this->mSmaxage = intval($wgForcedRawSMaxage);
90 } else {
91 $this->mSmaxage = intval( $smaxage );
92 }
93 $this->mMaxage = $maxage;
94
95 # Output may contain user-specific data;
96 # vary generated content for open sessions and private wikis
97 if( $this->mGen or !$wgGroupPermissions['*']['read'] ) {
98 $this->mPrivateCache = $this->mSmaxage == 0 || session_id() != '';
99 } else {
100 $this->mPrivateCache = false;
101 }
102
103 if( $ctype == '' or ! in_array( $ctype, $allowedCTypes ) ) {
104 $this->mContentType = 'text/x-wiki';
105 } else {
106 $this->mContentType = $ctype;
107 }
108 }
109
110 function view() {
111 global $wgOut, $wgRequest;
112
113 if( $wgRequest->isPathInfoBad() ) {
114 # Internet Explorer will ignore the Content-Type header if it
115 # thinks it sees a file extension it recognizes. Make sure that
116 # all raw requests are done through the script node, which will
117 # have eg '.php' and should remain safe.
118 #
119 # We used to redirect to a canonical-form URL as a general
120 # backwards-compatibility / good-citizen nice thing. However
121 # a lot of servers are set up in buggy ways, resulting in
122 # redirect loops which hang the browser until the CSS load
123 # times out.
124 #
125 # Just return a 403 Forbidden and get it over with.
126 wfHttpError( 403, 'Forbidden',
127 'Invalid file extension found in PATH_INFO. ' .
128 'Raw pages must be accessed through the primary script entry point.' );
129 return;
130 }
131
132 header( "Content-type: ".$this->mContentType.'; charset='.$this->mCharset );
133 # allow the client to cache this for 24 hours
134 $mode = $this->mPrivateCache ? 'private' : 'public';
135 header( 'Cache-Control: '.$mode.', s-maxage='.$this->mSmaxage.', max-age='.$this->mMaxage );
136
137 global $wgUseFileCache;
138 if( $wgUseFileCache and HTMLFileCache::useFileCache() ) {
139 $cache = new HTMLFileCache( $this->mTitle, 'raw' );
140 if( $cache->isFileCacheGood( /* Assume up to date */ ) ) {
141 $cache->loadFromFileCache();
142 $wgOut->disable();
143 return;
144 } else {
145 ob_start( array(&$cache, 'saveToFileCache' ) );
146 }
147 }
148
149 $text = $this->getRawText();
150
151 if( !wfRunHooks( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {
152 wfDebug( __METHOD__ . ": RawPageViewBeforeOutput hook broke raw page output.\n" );
153 }
154
155 echo $text;
156 $wgOut->disable();
157 }
158
159 function getRawText() {
160 global $wgUser, $wgOut;
161 if( $this->mGen ) {
162 $sk = $wgUser->getSkin();
163 if( !StubObject::isRealObject( $wgOut ) )
164 $wgOut->_unstub( 2 );
165 $sk->initPage( $wgOut );
166 if( $this->mGen == 'css' ) {
167 return $sk->generateUserStylesheet();
168 }
169 } else {
170 return $this->getArticleText();
171 }
172 }
173
174 function getArticleText() {
175 $found = false;
176 $text = '';
177 if( $this->mTitle ) {
178 // If it's a MediaWiki message we can just hit the message cache
179 if( $this->mUseMessageCache && $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
180 $key = $this->mTitle->getDBkey();
181 $text = wfMsgForContentNoTrans( $key );
182 # If the message doesn't exist, return a blank
183 if( wfEmptyMsg( $key, $text ) )
184 $text = '';
185 $found = true;
186 } else {
187 // Get it from the DB
188 $rev = Revision::newFromTitle( $this->mTitle, $this->mOldId );
189 if( $rev ) {
190 $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
191 header( "Last-modified: $lastmod" );
192
193 if( !is_null($this->mSection ) ) {
194 global $wgParser;
195 $text = $wgParser->getSection ( $rev->getText(), $this->mSection );
196 } else
197 $text = $rev->getText();
198 $found = true;
199 }
200 }
201 }
202
203 # Bad title or page does not exist
204 if( !$found && $this->mContentType == 'text/x-wiki' ) {
205 # Don't return a 404 response for CSS or JavaScript;
206 # 404s aren't generally cached and it would create
207 # extra hits when user CSS/JS are on and the user doesn't
208 # have the pages.
209 header( "HTTP/1.0 404 Not Found" );
210 }
211
212 // Special-case for empty CSS/JS
213 //
214 // Internet Explorer for Mac handles empty files badly;
215 // particularly so when keep-alive is active. It can lead
216 // to long timeouts as it seems to sit there waiting for
217 // more data that never comes.
218 //
219 // Give it a comment...
220 if( strlen( $text ) == 0 &&
221 ($this->mContentType == 'text/css' ||
222 $this->mContentType == 'text/javascript' ) ) {
223 return "/* Empty */";
224 }
225
226 return $this->parseArticleText( $text );
227 }
228
229 function parseArticleText( $text ) {
230 if( $text === '' )
231 return '';
232 else
233 if( $this->mExpandTemplates ) {
234 global $wgParser;
235 return $wgParser->preprocess( $text, $this->mTitle, new ParserOptions() );
236 } else
237 return $text;
238 }
239 }