f86c16f3f3482079f13b2bc2251dd4fa87503e31
[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 __construct( &$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 $text = $this->getRawText();
141
142 if( !wfRunHooks( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {
143 wfDebug( __METHOD__ . ': RawPageViewBeforeOutput hook broke raw page output.' );
144 }
145
146 echo $text;
147 $wgOut->disable();
148 }
149
150 function getRawText() {
151 global $wgUser, $wgOut, $wgRequest;
152 if($this->mGen) {
153 $sk = $wgUser->getSkin();
154 $sk->initPage($wgOut);
155 if($this->mGen == 'css') {
156 return $sk->getUserStylesheet();
157 } else if($this->mGen == 'js') {
158 return $sk->getUserJs();
159 }
160 } else {
161 return $this->getArticleText();
162 }
163 }
164
165 function getArticleText() {
166 $found = false;
167 $text = '';
168 if( $this->mTitle ) {
169 // If it's a MediaWiki message we can just hit the message cache
170 if ( $this->mUseMessageCache && $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
171 $key = $this->mTitle->getDBkey();
172 $text = wfMsgForContentNoTrans( $key );
173 # If the message doesn't exist, return a blank
174 if( wfEmptyMsg( $key, $text ) )
175 $text = '';
176 $found = true;
177 } else {
178 // Get it from the DB
179 $rev = Revision::newFromTitle( $this->mTitle, $this->mOldId );
180 if ( $rev ) {
181 $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
182 header( "Last-modified: $lastmod" );
183 $text = $rev->getText();
184 $found = true;
185 }
186 }
187 }
188
189 # Bad title or page does not exist
190 if( !$found && $this->mContentType == 'text/x-wiki' ) {
191 # Don't return a 404 response for CSS or JavaScript;
192 # 404s aren't generally cached and it would create
193 # extra hits when user CSS/JS are on and the user doesn't
194 # have the pages.
195 header( "HTTP/1.0 404 Not Found" );
196 }
197
198 return $this->parseArticleText( $text );
199 }
200
201 function parseArticleText( $text ) {
202 if ( $text === '' )
203 return '';
204 else
205 if ( $this->mExpandTemplates ) {
206 global $wgParser;
207 return $wgParser->preprocess( $text, $this->mTitle, new ParserOptions() );
208 } else
209 return $text;
210 }
211 }
212 ?>