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>
13 * A simple method to retrieve the plain source of an article,
14 * using "action=raw" in the GET request string.
17 var $mArticle, $mTitle, $mRequest;
18 var $mOldId, $mGen, $mCharset;
19 var $mSmaxage, $mMaxage;
20 var $mContentType, $mExpandTemplates;
22 function __construct( &$article, $request = false ) {
23 global $wgRequest, $wgInputEncoding, $wgSquidMaxage, $wgJsMimeType;
25 $allowedCTypes = array('text/x-wiki', $wgJsMimeType, 'text/css', 'application/x-zope-edit');
26 $this->mArticle
=& $article;
27 $this->mTitle
=& $article->mTitle
;
29 if ( $request === false ) {
30 $this->mRequest
=& $wgRequest;
32 $this->mRequest
= $request;
35 $ctype = $this->mRequest
->getVal( 'ctype' );
36 $smaxage = $this->mRequest
->getIntOrNull( 'smaxage', $wgSquidMaxage );
37 $maxage = $this->mRequest
->getInt( 'maxage', $wgSquidMaxage );
38 $this->mExpandTemplates
= $this->mRequest
->getVal( 'templates' ) === 'expand';
39 $this->mUseMessageCache
= $this->mRequest
->getBool( 'usemsgcache' );
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;
84 // Output may contain user-specific data; vary for open sessions
85 $this->mPrivateCache
= ( $this->mSmaxage
== 0 ) ||
86 ( session_id() != '' );
88 if ( $ctype == '' or ! in_array( $ctype, $allowedCTypes ) ) {
89 $this->mContentType
= 'text/x-wiki';
91 $this->mContentType
= $ctype;
96 global $wgOut, $wgScript;
98 if( isset( $_SERVER['SCRIPT_URL'] ) ) {
99 # Normally we use PHP_SELF to get the URL to the script
100 # as it was called, minus the query string.
102 # Some sites use Apache rewrite rules to handle subdomains,
103 # and have PHP set up in a weird way that causes PHP_SELF
104 # to contain the rewritten URL instead of the one that the
105 # outside world sees.
107 # If in this mode, use SCRIPT_URL instead, which mod_rewrite
108 # provides containing the "before" URL.
109 $url = $_SERVER['SCRIPT_URL'];
111 $url = $_SERVER['PHP_SELF'];
114 $ua = @$_SERVER['HTTP_USER_AGENT'];
115 if( strcmp( $wgScript, $url ) && strpos( $ua, 'MSIE' ) !== false ) {
116 # Internet Explorer will ignore the Content-Type header if it
117 # thinks it sees a file extension it recognizes. Make sure that
118 # all raw requests are done through the script node, which will
119 # have eg '.php' and should remain safe.
121 # We used to redirect to a canonical-form URL as a general
122 # backwards-compatibility / good-citizen nice thing. However
123 # a lot of servers are set up in buggy ways, resulting in
124 # redirect loops which hang the browser until the CSS load
127 # Just return a 403 Forbidden and get it over with.
128 wfHttpError( 403, 'Forbidden',
129 'Raw pages must be accessed through the primary script entry point.' );
133 header( "Content-type: ".$this->mContentType
.'; charset='.$this->mCharset
);
134 # allow the client to cache this for 24 hours
135 $mode = $this->mPrivateCache ?
'private' : 'public';
136 header( 'Cache-Control: '.$mode.', s-maxage='.$this->mSmaxage
.', max-age='.$this->mMaxage
);
137 $text = $this->getRawText();
139 if( !wfRunHooks( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {
140 wfDebug( __METHOD__
. ': RawPageViewBeforeOutput hook broke raw page output.' );
147 function getRawText() {
148 global $wgUser, $wgOut, $wgRequest;
150 $sk = $wgUser->getSkin();
151 $sk->initPage($wgOut);
152 if($this->mGen
== 'css') {
153 return $sk->getUserStylesheet();
154 } else if($this->mGen
== 'js') {
155 return $sk->getUserJs();
158 return $this->getArticleText();
162 function getArticleText() {
165 if( $this->mTitle
) {
166 // If it's a MediaWiki message we can just hit the message cache
167 if ( $this->mUseMessageCache
&& $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
168 $key = $this->mTitle
->getDBkey();
169 $text = wfMsgForContentNoTrans( $key );
170 # If the message doesn't exist, return a blank
171 if( wfEmptyMsg( $key, $text ) )
175 // Get it from the DB
176 $rev = Revision
::newFromTitle( $this->mTitle
, $this->mOldId
);
178 $lastmod = wfTimestamp( TS_RFC2822
, $rev->getTimestamp() );
179 header( "Last-modified: $lastmod" );
180 $text = $rev->getText();
186 # Bad title or page does not exist
187 if( !$found && $this->mContentType
== 'text/x-wiki' ) {
188 # Don't return a 404 response for CSS or JavaScript;
189 # 404s aren't generally cached and it would create
190 # extra hits when user CSS/JS are on and the user doesn't
192 header( "HTTP/1.0 404 Not Found" );
195 // Special-case for empty CSS/JS
197 // Internet Explorer for Mac handles empty files badly;
198 // particularly so when keep-alive is active. It can lead
199 // to long timeouts as it seems to sit there waiting for
200 // more data that never comes.
202 // Give it a comment...
203 if( strlen( $text ) == 0 &&
204 ($this->mContentType
== 'text/css' ||
205 $this->mContentType
== 'text/javascript' ) ) {
206 return "/* Empty */";
209 return $this->parseArticleText( $text );
212 function parseArticleText( $text ) {
216 if ( $this->mExpandTemplates
) {
218 return $wgParser->preprocess( $text, $this->mTitle
, new ParserOptions() );