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