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>
16 var $mArticle, $mTitle, $mRequest;
17 var $mOldId, $mGen, $mCharset;
18 var $mSmaxage, $mMaxage;
19 var $mContentType, $mExpandTemplates;
21 function __construct( &$article, $request = false ) {
22 global $wgRequest, $wgInputEncoding, $wgSquidMaxage, $wgJsMimeType;
24 $allowedCTypes = array('text/x-wiki', $wgJsMimeType, 'text/css', 'application/x-zope-edit');
25 $this->mArticle
=& $article;
26 $this->mTitle
=& $article->mTitle
;
28 if ( $request === false ) {
29 $this->mRequest
=& $wgRequest;
31 $this->mRequest
= $request;
34 $ctype = $this->mRequest
->getVal( 'ctype' );
35 $smaxage = $this->mRequest
->getIntOrNull( 'smaxage', $wgSquidMaxage );
36 $maxage = $this->mRequest
->getInt( 'maxage', $wgSquidMaxage );
37 $this->mExpandTemplates
= $this->mRequest
->getVal( 'templates' ) === 'expand';
38 $this->mUseMessageCache
= $this->mRequest
->getBool( 'usemsgcache' );
40 $oldid = $this->mRequest
->getInt( 'oldid' );
41 switch ( $wgRequest->getText( 'direction' ) ) {
43 # output next revision, or nothing if there isn't one
45 $oldid = $this->mTitle
->getNextRevisionId( $oldid );
47 $oldid = $oldid ?
$oldid : -1;
50 # output previous revision, or nothing if there isn't one
52 # get the current revision so we can get the penultimate one
53 $this->mArticle
->getTouched();
54 $oldid = $this->mArticle
->mLatest
;
56 $prev = $this->mTitle
->getPreviousRevisionId( $oldid );
57 $oldid = $prev ?
$prev : -1 ;
63 $this->mOldId
= $oldid;
65 # special case for 'generated' raw things: user css/js
66 $gen = $this->mRequest
->getVal( 'gen' );
70 if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
71 if($ctype == '') $ctype = 'text/css';
72 } elseif ($gen == 'js') {
74 if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
75 if($ctype == '') $ctype = $wgJsMimeType;
79 $this->mCharset
= $wgInputEncoding;
80 $this->mSmaxage
= intval( $smaxage );
81 $this->mMaxage
= $maxage;
83 // Output may contain user-specific data; vary for open sessions
84 $this->mPrivateCache
= ( $this->mSmaxage
== 0 ) ||
85 ( session_id() != '' );
87 if ( $ctype == '' or ! in_array( $ctype, $allowedCTypes ) ) {
88 $this->mContentType
= 'text/x-wiki';
90 $this->mContentType
= $ctype;
95 global $wgOut, $wgScript;
97 if( isset( $_SERVER['SCRIPT_URL'] ) ) {
98 # Normally we use PHP_SELF to get the URL to the script
99 # as it was called, minus the query string.
101 # Some sites use Apache rewrite rules to handle subdomains,
102 # and have PHP set up in a weird way that causes PHP_SELF
103 # to contain the rewritten URL instead of the one that the
104 # outside world sees.
106 # If in this mode, use SCRIPT_URL instead, which mod_rewrite
107 # provides containing the "before" URL.
108 $url = $_SERVER['SCRIPT_URL'];
110 $url = $_SERVER['PHP_SELF'];
113 $ua = @$_SERVER['HTTP_USER_AGENT'];
114 if( strcmp( $wgScript, $url ) && strpos( $ua, 'MSIE' ) !== false ) {
115 # Internet Explorer will ignore the Content-Type header if it
116 # thinks it sees a file extension it recognizes. Make sure that
117 # all raw requests are done through the script node, which will
118 # have eg '.php' and should remain safe.
120 # We used to redirect to a canonical-form URL as a general
121 # backwards-compatibility / good-citizen nice thing. However
122 # a lot of servers are set up in buggy ways, resulting in
123 # redirect loops which hang the browser until the CSS load
126 # Just return a 403 Forbidden and get it over with.
127 wfHttpError( 403, 'Forbidden',
128 'Raw pages must be accessed through the primary script entry point.' );
132 header( "Content-type: ".$this->mContentType
.'; charset='.$this->mCharset
);
133 # allow the client to cache this for 24 hours
134 $mode = $this->mPrivateCache ?
'private' : 'public';
135 header( 'Cache-Control: '.$mode.', s-maxage='.$this->mSmaxage
.', max-age='.$this->mMaxage
);
136 $text = $this->getRawText();
138 if( !wfRunHooks( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {
139 wfDebug( __METHOD__
. ': RawPageViewBeforeOutput hook broke raw page output.' );
146 function getRawText() {
147 global $wgUser, $wgOut, $wgRequest;
149 $sk = $wgUser->getSkin();
150 $sk->initPage($wgOut);
151 if($this->mGen
== 'css') {
152 return $sk->getUserStylesheet();
153 } else if($this->mGen
== 'js') {
154 return $sk->getUserJs();
157 return $this->getArticleText();
161 function getArticleText() {
164 if( $this->mTitle
) {
165 // If it's a MediaWiki message we can just hit the message cache
166 if ( $this->mUseMessageCache
&& $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
167 $key = $this->mTitle
->getDBkey();
168 $text = wfMsgForContentNoTrans( $key );
169 # If the message doesn't exist, return a blank
170 if( wfEmptyMsg( $key, $text ) )
174 // Get it from the DB
175 $rev = Revision
::newFromTitle( $this->mTitle
, $this->mOldId
);
177 $lastmod = wfTimestamp( TS_RFC2822
, $rev->getTimestamp() );
178 header( "Last-modified: $lastmod" );
179 $text = $rev->getText();
185 # Bad title or page does not exist
186 if( !$found && $this->mContentType
== 'text/x-wiki' ) {
187 # Don't return a 404 response for CSS or JavaScript;
188 # 404s aren't generally cached and it would create
189 # extra hits when user CSS/JS are on and the user doesn't
191 header( "HTTP/1.0 404 Not Found" );
194 // Special-case for empty CSS/JS
196 // Internet Explorer for Mac handles empty files badly;
197 // particularly so when keep-alive is active. It can lead
198 // to long timeouts as it seems to sit there waiting for
199 // more data that never comes.
201 // Give it a comment...
202 if( strlen( $text ) == 0 &&
203 ($this->mContentType
== 'text/css' ||
204 $this->mContentType
== 'text/javascript' ) ) {
205 return "/* Empty */";
208 return $this->parseArticleText( $text );
211 function parseArticleText( $text ) {
215 if ( $this->mExpandTemplates
) {
217 return $wgParser->preprocess( $text, $this->mTitle
, new ParserOptions() );