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