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