3 * Copyright (C) 2004 Gabriel Wicke <wicke@wikidev.net>
5 * Based on PageHistory and SpecialExport
7 * License: GPL (http://www.gnu.org/copyleft/gpl.html)
9 * @author Gabriel Wicke <wicke@wikidev.net>
18 var $mArticle, $mTitle, $mRequest;
19 var $mOldId, $mGen, $mCharset;
20 var $mSmaxage, $mMaxage;
21 var $mContentType, $mExpandTemplates;
23 function RawPage( &$article, $request = false ) {
24 global $wgRequest, $wgInputEncoding, $wgSquidMaxage, $wgJsMimeType;
26 $allowedCTypes = array('text/x-wiki', $wgJsMimeType, 'text/css', 'application/x-zope-edit');
27 $this->mArticle
=& $article;
28 $this->mTitle
=& $article->mTitle
;
30 if ( $request === false ) {
31 $this->mRequest
=& $wgRequest;
33 $this->mRequest
= $request;
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';
41 $oldid = $this->mRequest
->getInt( 'oldid' );
42 switch ( $wgRequest->getText( 'direction' ) ) {
44 # output next revision, or nothing if there isn't one
46 $oldid = $this->mTitle
->getNextRevisionId( $oldid );
48 $oldid = $oldid ?
$oldid : -1;
51 # output previous revision, or nothing if there isn't one
53 # get the current revision so we can get the penultimate one
54 $this->mArticle
->getTouched();
55 $oldid = $this->mArticle
->mLatest
;
57 $prev = $this->mTitle
->getPreviousRevisionId( $oldid );
58 $oldid = $prev ?
$prev : -1 ;
64 $this->mOldId
= $oldid;
66 # special case for 'generated' raw things: user css/js
67 $gen = $this->mRequest
->getVal( 'gen' );
71 if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
72 if($ctype == '') $ctype = 'text/css';
73 } elseif ($gen == 'js') {
75 if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
76 if($ctype == '') $ctype = $wgJsMimeType;
80 $this->mCharset
= $wgInputEncoding;
81 $this->mSmaxage
= intval( $smaxage );
82 $this->mMaxage
= $maxage;
83 if ( $ctype == '' or ! in_array( $ctype, $allowedCTypes ) ) {
84 $this->mContentType
= 'text/x-wiki';
86 $this->mContentType
= $ctype;
91 global $wgOut, $wgScript;
93 if( isset( $_SERVER['SCRIPT_URL'] ) ) {
94 # Normally we use PHP_SELF to get the URL to the script
95 # as it was called, minus the query string.
97 # Some sites use Apache rewrite rules to handle subdomains,
98 # and have PHP set up in a weird way that causes PHP_SELF
99 # to contain the rewritten URL instead of the one that the
100 # outside world sees.
102 # If in this mode, use SCRIPT_URL instead, which mod_rewrite
103 # provides containing the "before" URL.
104 $url = $_SERVER['SCRIPT_URL'];
106 $url = $_SERVER['PHP_SELF'];
109 $ua = @$_SERVER['HTTP_USER_AGENT'];
110 if( strcmp( $wgScript, $url ) && strpos( $ua, 'MSIE' ) !== false ) {
111 # Internet Explorer will ignore the Content-Type header if it
112 # thinks it sees a file extension it recognizes. Make sure that
113 # all raw requests are done through the script node, which will
114 # have eg '.php' and should remain safe.
116 # We used to redirect to a canonical-form URL as a general
117 # backwards-compatibility / good-citizen nice thing. However
118 # a lot of servers are set up in buggy ways, resulting in
119 # redirect loops which hang the browser until the CSS load
122 # Just return a 403 Forbidden and get it over with.
123 wfHttpError( 403, 'Forbidden',
124 'Raw pages must be accessed through the primary script entry point.' );
128 header( "Content-type: ".$this->mContentType
.'; charset='.$this->mCharset
);
129 # allow the client to cache this for 24 hours
130 header( 'Cache-Control: s-maxage='.$this->mSmaxage
.', max-age='.$this->mMaxage
);
131 echo $this->getRawText();
135 function getRawText() {
136 global $wgUser, $wgOut;
138 $sk = $wgUser->getSkin();
139 $sk->initPage($wgOut);
140 if($this->mGen
== 'css') {
141 return $sk->getUserStylesheet();
142 } else if($this->mGen
== 'js') {
143 return $sk->getUserJs();
146 return $this->getArticleText();
150 function getArticleText() {
153 if( $this->mTitle
) {
154 // If it's a MediaWiki message we can just hit the message cache
155 if ( $this->mTitle
->getNamespace() == NS_MEDIAWIKI
&& !$this->mOldId
) {
156 $key = $this->mTitle
->getDBkey();
157 $text = wfMsgForContentNoTrans( $key );
158 # If the message doesn't exist, return a blank
159 if( $text == '<' . $key . '>' )
163 // Get it from the DB
164 $rev = Revision
::newFromTitle( $this->mTitle
, $this->mOldId
);
166 $lastmod = wfTimestamp( TS_RFC2822
, $rev->getTimestamp() );
167 header( "Last-modified: $lastmod" );
168 $text = $rev->getText();
174 # Bad title or page does not exist
175 if( !$found && $this->mContentType
== 'text/x-wiki' ) {
176 # Don't return a 404 response for CSS or JavaScript;
177 # 404s aren't generally cached and it would create
178 # extra hits when user CSS/JS are on and the user doesn't
180 header( "HTTP/1.0 404 Not Found" );
183 return $this->parseArticleText( $text );
186 function parseArticleText( $text ) {
190 if ( $this->mExpandTemplates
) {
193 $parser = new Parser();
194 $parser->Options( new ParserOptions() ); // We don't want this to be user-specific
195 $parser->Title( $wgTitle );
196 $parser->OutputType( OT_HTML
);
198 return $parser->replaceVariables( $text );