3 * Copyright (C) 2004 Gabriel Wicke <gw@wikidev.net>
4 * http://www.aulinx.de/
5 * Based on PageHistory and SpecialExport
7 * License: GPL (http://www.gnu.org/copyleft/gpl.html)
9 * @author Gabriel Wicke <gw@wikidev.net>
14 require_once( 'Revision.php' );
21 var $mArticle, $mTitle, $mRequest;
22 var $mOldId, $mGen, $mCharset;
23 var $mSmaxage, $mMaxage;
24 var $mContentType, $mExpandTemplates;
26 function RawPage( &$article, $request = false ) {
27 global $wgRequest, $wgInputEncoding, $wgSquidMaxage, $wgJsMimeType;
29 $allowedCTypes = array('text/x-wiki', $wgJsMimeType, 'text/css', 'application/x-zope-edit');
30 $this->mArticle
=& $article;
31 $this->mTitle
=& $article->mTitle
;
33 if ( $request === false ) {
34 $this->mRequest
=& $wgRequest;
36 $this->mRequest
= $request;
39 $ctype = $this->mRequest
->getVal( 'ctype' );
40 $smaxage = $this->mRequest
->getIntOrNull( 'smaxage', $wgSquidMaxage );
41 $maxage = $this->mRequest
->getInt( 'maxage', $wgSquidMaxage );
42 $this->mExpandTemplates
= $this->mRequest
->getVal( 'templates' ) === 'expand';
44 $oldid = $this->mRequest
->getInt( 'oldid' );
45 switch ( $wgRequest->getText( 'direction' ) ) {
47 $oldid = $this->mTitle
->getNextRevisionId( $oldid );
50 $oldid = $this->mTitle
->getPreviousRevisionId( $oldid );
56 $this->mOldId
= $oldid;
58 # special case for 'generated' raw things: user css/js
59 $gen = $this->mRequest
->getVal( 'gen' );
63 if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
64 if($ctype == '') $ctype = 'text/css';
65 } elseif ($gen == 'js') {
67 if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
68 if($ctype == '') $ctype = $wgJsMimeType;
72 $this->mCharset
= $wgInputEncoding;
73 $this->mSmaxage
= intval( $smaxage );
74 $this->mMaxage
= $maxage;
75 if ( $ctype == '' or ! in_array( $ctype, $allowedCTypes ) ) {
76 $this->mContentType
= 'text/x-wiki';
78 $this->mContentType
= $ctype;
83 global $wgOut, $wgScript;
85 if( isset( $_SERVER['SCRIPT_URL'] ) ) {
86 # Normally we use PHP_SELF to get the URL to the script
87 # as it was called, minus the query string.
89 # Some sites use Apache rewrite rules to handle subdomains,
90 # and have PHP set up in a weird way that causes PHP_SELF
91 # to contain the rewritten URL instead of the one that the
94 # If in this mode, use SCRIPT_URL instead, which mod_rewrite
95 # provides containing the "before" URL.
96 $url = $_SERVER['SCRIPT_URL'];
98 $url = $_SERVER['PHP_SELF'];
101 $ua = @$_SERVER['HTTP_USER_AGENT'];
102 if( strcmp( $wgScript, $url ) && strpos( $ua, 'MSIE' ) !== false ) {
103 # Internet Explorer will ignore the Content-Type header if it
104 # thinks it sees a file extension it recognizes. Make sure that
105 # all raw requests are done through the script node, which will
106 # have eg '.php' and should remain safe.
108 # We used to redirect to a canonical-form URL as a general
109 # backwards-compatibility / good-citizen nice thing. However
110 # a lot of servers are set up in buggy ways, resulting in
111 # redirect loops which hang the browser until the CSS load
114 # Just return a 403 Forbidden and get it over with.
115 wfHttpError( 403, 'Forbidden',
116 'Raw pages must be accessed through the primary script entry point.' );
120 header( "Content-type: ".$this->mContentType
.'; charset='.$this->mCharset
);
121 # allow the client to cache this for 24 hours
122 header( 'Cache-Control: s-maxage='.$this->mSmaxage
.', max-age='.$this->mMaxage
);
123 echo $this->getRawText();
127 function getRawText() {
128 global $wgUser, $wgOut;
130 $sk = $wgUser->getSkin();
131 $sk->initPage($wgOut);
132 if($this->mGen
== 'css') {
133 return $sk->getUserStylesheet();
134 } else if($this->mGen
== 'js') {
135 return $sk->getUserJs();
138 return $this->getArticleText();
142 function getArticleText() {
145 if( $this->mTitle
) {
148 // If it's a MediaWiki message we can just hit the message cache
149 if ( $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
150 $text = wfMsgForContentNoTrans( $this->mTitle
->getDbkey() );
152 // Get it from the DB
153 $rev = Revision
::newFromTitle( $this->mTitle
, $this->mOldId
);
155 $lastmod = wfTimestamp( TS_RFC2822
, $rev->getTimestamp() );
156 header( "Last-modified: $lastmod" );
157 $text = $rev->isDeleted() ?
'' : $rev->getText();
162 return $this->parseArticleText( $text );
165 # Bad title or page does not exist
166 if( $this->mContentType
== 'text/x-wiki' ) {
167 # Don't return a 404 response for CSS or JavaScript;
168 # 404s aren't generally cached and it would create
169 # extra hits when user CSS/JS are on and the user doesn't
171 header( "HTTP/1.0 404 Not Found" );
176 function parseArticleText( $text ) {
180 if ( $this->mExpandTemplates
) {
183 $parser = new Parser();
184 $parser->Options( new ParserOptions() ); // We don't want this to be user-specific
185 $parser->Title( $wgTitle );
186 $parser->OutputType( OT_HTML
);
188 return $parser->replaceVariables( $text );