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