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;
27 $allowedCTypes = array('text/x-wiki', $wgJsMimeType, 'text/css', 'application/x-zope-edit');
28 $this->mArticle
=& $article;
29 $this->mTitle
=& $article->mTitle
;
31 if ( $request === false ) {
32 $this->mRequest
=& $wgRequest;
34 $this->mRequest
= $request;
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' );
43 $oldid = $this->mRequest
->getInt( 'oldid' );
44 switch ( $wgRequest->getText( 'direction' ) ) {
46 # output next revision, or nothing if there isn't one
48 $oldid = $this->mTitle
->getNextRevisionId( $oldid );
50 $oldid = $oldid ?
$oldid : -1;
53 # output previous revision, or nothing if there isn't one
55 # get the current revision so we can get the penultimate one
56 $this->mArticle
->getTouched();
57 $oldid = $this->mArticle
->mLatest
;
59 $prev = $this->mTitle
->getPreviousRevisionId( $oldid );
60 $oldid = $prev ?
$prev : -1 ;
66 $this->mOldId
= $oldid;
68 # special case for 'generated' raw things: user css/js
69 $gen = $this->mRequest
->getVal( 'gen' );
73 if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
74 if($ctype == '') $ctype = 'text/css';
75 } elseif ($gen == 'js') {
77 if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
78 if($ctype == '') $ctype = $wgJsMimeType;
82 $this->mCharset
= $wgInputEncoding;
83 $this->mSmaxage
= intval( $smaxage );
84 $this->mMaxage
= $maxage;
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() );
91 if ( $ctype == '' or ! in_array( $ctype, $allowedCTypes ) ) {
92 $this->mContentType
= 'text/x-wiki';
94 $this->mContentType
= $ctype;
99 global $wgOut, $wgScript;
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.
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.
110 # If in this mode, use SCRIPT_URL instead, which mod_rewrite
111 # provides containing the "before" URL.
112 $url = $_SERVER['SCRIPT_URL'];
114 $url = $_SERVER['PHP_SELF'];
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.
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
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.' );
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();
144 function getRawText() {
145 global $wgUser, $wgOut, $wgRequest;
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();
155 return $this->getArticleText();
159 function getArticleText() {
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 ) )
172 // Get it from the DB
173 $rev = Revision
::newFromTitle( $this->mTitle
, $this->mOldId
);
175 $lastmod = wfTimestamp( TS_RFC2822
, $rev->getTimestamp() );
176 header( "Last-modified: $lastmod" );
177 $text = $rev->getText();
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
189 header( "HTTP/1.0 404 Not Found" );
192 return $this->parseArticleText( $text );
195 function parseArticleText( $text ) {
199 if ( $this->mExpandTemplates
) {
201 return $wgParser->preprocess( $text, $this->mTitle
, new ParserOptions() );