f5e74e70e51b5836430f256e3fed5ba3f8b5cfbb
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';
40 $this->mUseMessageCache
= $this->mRequest
->getBool( 'usemsgcache' );
42 $oldid = $this->mRequest
->getInt( 'oldid' );
43 switch ( $wgRequest->getText( 'direction' ) ) {
45 # output next revision, or nothing if there isn't one
47 $oldid = $this->mTitle
->getNextRevisionId( $oldid );
49 $oldid = $oldid ?
$oldid : -1;
52 # output previous revision, or nothing if there isn't one
54 # get the current revision so we can get the penultimate one
55 $this->mArticle
->getTouched();
56 $oldid = $this->mArticle
->mLatest
;
58 $prev = $this->mTitle
->getPreviousRevisionId( $oldid );
59 $oldid = $prev ?
$prev : -1 ;
65 $this->mOldId
= $oldid;
67 # special case for 'generated' raw things: user css/js
68 $gen = $this->mRequest
->getVal( 'gen' );
72 if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
73 if($ctype == '') $ctype = 'text/css';
74 } elseif ($gen == 'js') {
76 if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
77 if($ctype == '') $ctype = $wgJsMimeType;
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';
87 $this->mContentType
= $ctype;
92 global $wgOut, $wgScript;
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.
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.
103 # If in this mode, use SCRIPT_URL instead, which mod_rewrite
104 # provides containing the "before" URL.
105 $url = $_SERVER['SCRIPT_URL'];
107 $url = $_SERVER['PHP_SELF'];
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.
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
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.' );
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();
136 function getRawText() {
137 global $wgUser, $wgOut;
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();
147 return $this->getArticleText();
151 function getArticleText() {
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( $text == '<' . $key . '>' )
164 // Get it from the DB
165 $rev = Revision
::newFromTitle( $this->mTitle
, $this->mOldId
);
167 $lastmod = wfTimestamp( TS_RFC2822
, $rev->getTimestamp() );
168 header( "Last-modified: $lastmod" );
169 $text = $rev->getText();
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
181 header( "HTTP/1.0 404 Not Found" );
184 return $this->parseArticleText( $text );
187 function parseArticleText( $text ) {
191 if ( $this->mExpandTemplates
) {
193 return $wgParser->preprocess( $text, $this->mTitle
, new ParserOptions() );