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