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, $mSection;
19 var $mSmaxage, $mMaxage;
20 var $mContentType, $mExpandTemplates;
22 function __construct( &$article, $request = false ) {
23 global $wgRequest, $wgInputEncoding, $wgSquidMaxage, $wgJsMimeType, $wgForcedRawSMaxage, $wgGroupPermissions;
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 );
39 $this->mExpandTemplates
= $this->mRequest
->getVal( 'templates' ) === 'expand';
40 $this->mUseMessageCache
= $this->mRequest
->getBool( 'usemsgcache' );
42 $this->mSection
= $this->mRequest
->getIntOrNull( 'section' );
44 $oldid = $this->mRequest
->getInt( 'oldid' );
46 switch ( $wgRequest->getText( 'direction' ) ) {
48 # output next revision, or nothing if there isn't one
50 $oldid = $this->mTitle
->getNextRevisionId( $oldid );
52 $oldid = $oldid ?
$oldid : -1;
55 # output previous revision, or nothing if there isn't one
57 # get the current revision so we can get the penultimate one
58 $this->mArticle
->getTouched();
59 $oldid = $this->mArticle
->mLatest
;
61 $prev = $this->mTitle
->getPreviousRevisionId( $oldid );
62 $oldid = $prev ?
$prev : -1 ;
68 $this->mOldId
= $oldid;
70 # special case for 'generated' raw things: user css/js
71 $gen = $this->mRequest
->getVal( 'gen' );
75 if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
76 if($ctype == '') $ctype = 'text/css';
77 } elseif ($gen == 'js') {
79 if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
80 if($ctype == '') $ctype = $wgJsMimeType;
84 $this->mCharset
= $wgInputEncoding;
86 # Force caching for CSS and JS raw content, default: 5 minutes
87 if (is_null($smaxage) and ($ctype=='text/css' or $ctype==$wgJsMimeType)) {
88 $this->mSmaxage
= intval($wgForcedRawSMaxage);
90 $this->mSmaxage
= intval( $smaxage );
92 $this->mMaxage
= $maxage;
94 # Output may contain user-specific data;
95 # vary generated content for open sessions and private wikis
96 if ($this->mGen
or !$wgGroupPermissions['*']['read']) {
97 $this->mPrivateCache
= ( $this->mSmaxage
== 0 ) ||
98 ( session_id() != '' );
100 $this->mPrivateCache
= false;
103 if ( $ctype == '' or ! in_array( $ctype, $allowedCTypes ) ) {
104 $this->mContentType
= 'text/x-wiki';
106 $this->mContentType
= $ctype;
111 global $wgOut, $wgScript;
113 if( isset( $_SERVER['SCRIPT_URL'] ) ) {
114 # Normally we use PHP_SELF to get the URL to the script
115 # as it was called, minus the query string.
117 # Some sites use Apache rewrite rules to handle subdomains,
118 # and have PHP set up in a weird way that causes PHP_SELF
119 # to contain the rewritten URL instead of the one that the
120 # outside world sees.
122 # If in this mode, use SCRIPT_URL instead, which mod_rewrite
123 # provides containing the "before" URL.
124 $url = $_SERVER['SCRIPT_URL'];
126 $url = $_SERVER['PHP_SELF'];
129 if( strcmp( $wgScript, $url ) ) {
130 # Internet Explorer will ignore the Content-Type header if it
131 # thinks it sees a file extension it recognizes. Make sure that
132 # all raw requests are done through the script node, which will
133 # have eg '.php' and should remain safe.
135 # We used to redirect to a canonical-form URL as a general
136 # backwards-compatibility / good-citizen nice thing. However
137 # a lot of servers are set up in buggy ways, resulting in
138 # redirect loops which hang the browser until the CSS load
141 # Just return a 403 Forbidden and get it over with.
142 wfHttpError( 403, 'Forbidden',
143 'Raw pages must be accessed through the primary script entry point.' );
147 header( "Content-type: ".$this->mContentType
.'; charset='.$this->mCharset
);
148 # allow the client to cache this for 24 hours
149 $mode = $this->mPrivateCache ?
'private' : 'public';
150 header( 'Cache-Control: '.$mode.', s-maxage='.$this->mSmaxage
.', max-age='.$this->mMaxage
);
151 $text = $this->getRawText();
153 if( !wfRunHooks( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {
154 wfDebug( __METHOD__
. ': RawPageViewBeforeOutput hook broke raw page output.' );
161 function getRawText() {
162 global $wgUser, $wgOut, $wgRequest;
164 $sk = $wgUser->getSkin();
165 $sk->initPage($wgOut);
166 if($this->mGen
== 'css') {
167 return $sk->getUserStylesheet();
168 } else if($this->mGen
== 'js') {
169 return $sk->getUserJs();
172 return $this->getArticleText();
176 function getArticleText() {
179 if( $this->mTitle
) {
180 // If it's a MediaWiki message we can just hit the message cache
181 if ( $this->mUseMessageCache
&& $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
182 $key = $this->mTitle
->getDBkey();
183 $text = wfMsgForContentNoTrans( $key );
184 # If the message doesn't exist, return a blank
185 if( wfEmptyMsg( $key, $text ) )
189 // Get it from the DB
190 $rev = Revision
::newFromTitle( $this->mTitle
, $this->mOldId
);
192 $lastmod = wfTimestamp( TS_RFC2822
, $rev->getTimestamp() );
193 header( "Last-modified: $lastmod" );
195 if ( !is_null($this->mSection
) ) {
197 $text = $wgParser->getSection ( $rev->getText(), $this->mSection
);
199 $text = $rev->getText();
205 # Bad title or page does not exist
206 if( !$found && $this->mContentType
== 'text/x-wiki' ) {
207 # Don't return a 404 response for CSS or JavaScript;
208 # 404s aren't generally cached and it would create
209 # extra hits when user CSS/JS are on and the user doesn't
211 header( "HTTP/1.0 404 Not Found" );
214 // Special-case for empty CSS/JS
216 // Internet Explorer for Mac handles empty files badly;
217 // particularly so when keep-alive is active. It can lead
218 // to long timeouts as it seems to sit there waiting for
219 // more data that never comes.
221 // Give it a comment...
222 if( strlen( $text ) == 0 &&
223 ($this->mContentType
== 'text/css' ||
224 $this->mContentType
== 'text/javascript' ) ) {
225 return "/* Empty */";
228 return $this->parseArticleText( $text );
231 function parseArticleText( $text ) {
235 if ( $this->mExpandTemplates
) {
237 return $wgParser->preprocess( $text, $this->mTitle
, new ParserOptions() );