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>
14 * A simple method to retrieve the plain source of an article,
15 * using "action=raw" in the GET request string.
18 var $mArticle, $mTitle, $mRequest;
19 var $mOldId, $mGen, $mCharset, $mSection;
20 var $mSmaxage, $mMaxage;
21 var $mContentType, $mExpandTemplates;
23 function __construct( &$article, $request = false ) {
24 global $wgRequest, $wgInputEncoding, $wgSquidMaxage, $wgJsMimeType, $wgGroupPermissions;
26 $allowedCTypes = array('text/x-wiki', $wgJsMimeType, 'text/css', 'application/x-zope-edit');
27 $this->mArticle
=& $article;
28 $this->mTitle
=& $article->mTitle
;
30 if( $request === false ) {
31 $this->mRequest
=& $wgRequest;
33 $this->mRequest
= $request;
36 $ctype = $this->mRequest
->getVal( 'ctype' );
37 $smaxage = $this->mRequest
->getIntOrNull( 'smaxage' );
38 $maxage = $this->mRequest
->getInt( 'maxage', $wgSquidMaxage );
40 $this->mExpandTemplates
= $this->mRequest
->getVal( 'templates' ) === 'expand';
41 $this->mUseMessageCache
= $this->mRequest
->getBool( 'usemsgcache' );
43 $this->mSection
= $this->mRequest
->getIntOrNull( 'section' );
45 $oldid = $this->mRequest
->getInt( 'oldid' );
47 switch( $wgRequest->getText( 'direction' ) ) {
49 # output next revision, or nothing if there isn't one
51 $oldid = $this->mTitle
->getNextRevisionId( $oldid );
53 $oldid = $oldid ?
$oldid : -1;
56 # output previous revision, or nothing if there isn't one
58 # get the current revision so we can get the penultimate one
59 $this->mArticle
->getTouched();
60 $oldid = $this->mArticle
->mLatest
;
62 $prev = $this->mTitle
->getPreviousRevisionId( $oldid );
63 $oldid = $prev ?
$prev : -1 ;
69 $this->mOldId
= $oldid;
71 # special case for 'generated' raw things: user css/js
72 $gen = $this->mRequest
->getVal( 'gen' );
76 if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
77 if($ctype == '') $ctype = 'text/css';
78 } elseif( $gen == 'js' ) {
80 if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
81 if($ctype == '') $ctype = $wgJsMimeType;
85 $this->mCharset
= $wgInputEncoding;
87 # Force caching for CSS and JS raw content, default: 5 minutes
88 if( is_null($smaxage) and ($ctype=='text/css' or $ctype==$wgJsMimeType) ) {
89 global $wgForcedRawSMaxage;
90 $this->mSmaxage
= intval($wgForcedRawSMaxage);
92 $this->mSmaxage
= intval( $smaxage );
94 $this->mMaxage
= $maxage;
96 # Output may contain user-specific data;
97 # vary generated content for open sessions and private wikis
98 if( $this->mGen
or !$wgGroupPermissions['*']['read'] ) {
99 $this->mPrivateCache
= $this->mSmaxage
== 0 ||
session_id() != '';
101 $this->mPrivateCache
= false;
104 if( $ctype == '' or ! in_array( $ctype, $allowedCTypes ) ) {
105 $this->mContentType
= 'text/x-wiki';
107 $this->mContentType
= $ctype;
112 global $wgOut, $wgScript;
114 $url = wfGetScriptUrl();
116 # This will make the next check fail with a confusing error
117 # message, so we should mention it separately.
118 wfHttpError( 500, 'Internal Server Error',
119 "\$_SERVER['URL'] is not set. Perhaps you're using CGI" .
120 " and haven't set cgi.fix_pathinfo = 1 in php.ini?" );
124 if( strcmp( $wgScript, $url ) ) {
125 # Internet Explorer will ignore the Content-Type header if it
126 # thinks it sees a file extension it recognizes. Make sure that
127 # all raw requests are done through the script node, which will
128 # have eg '.php' and should remain safe.
130 # We used to redirect to a canonical-form URL as a general
131 # backwards-compatibility / good-citizen nice thing. However
132 # a lot of servers are set up in buggy ways, resulting in
133 # redirect loops which hang the browser until the CSS load
136 # Just return a 403 Forbidden and get it over with.
137 wfHttpError( 403, 'Forbidden',
138 'Raw pages must be accessed through the primary script entry point.' );
142 header( "Content-type: ".$this->mContentType
.'; charset='.$this->mCharset
);
143 # allow the client to cache this for 24 hours
144 $mode = $this->mPrivateCache ?
'private' : 'public';
145 header( 'Cache-Control: '.$mode.', s-maxage='.$this->mSmaxage
.', max-age='.$this->mMaxage
);
147 if( HTMLFileCache
::useFileCache() ) {
148 $cache = new HTMLFileCache( $this->mTitle
, 'raw' );
149 if( $cache->isFileCacheGood( /* Assume up to date */ ) ) {
150 $cache->loadFromFileCache();
154 ob_start( array(&$cache, 'saveToFileCache' ) );
158 $text = $this->getRawText();
160 if( !wfRunHooks( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {
161 wfDebug( __METHOD__
. ": RawPageViewBeforeOutput hook broke raw page output.\n" );
168 function getRawText() {
169 global $wgUser, $wgOut, $wgRequest;
171 $sk = $wgUser->getSkin();
172 if( !StubObject
::isRealObject( $wgOut ) )
173 $wgOut->_unstub( 2 );
174 $sk->initPage( $wgOut );
175 if( $this->mGen
== 'css' ) {
176 return $sk->generateUserStylesheet();
177 } else if( $this->mGen
== 'js' ) {
178 return $sk->generateUserJs();
181 return $this->getArticleText();
185 function getArticleText() {
188 if( $this->mTitle
) {
189 // If it's a MediaWiki message we can just hit the message cache
190 if( $this->mUseMessageCache
&& $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
191 $key = $this->mTitle
->getDBkey();
192 $text = wfMsgForContentNoTrans( $key );
193 # If the message doesn't exist, return a blank
194 if( wfEmptyMsg( $key, $text ) )
198 // Get it from the DB
199 $rev = Revision
::newFromTitle( $this->mTitle
, $this->mOldId
);
201 $lastmod = wfTimestamp( TS_RFC2822
, $rev->getTimestamp() );
202 header( "Last-modified: $lastmod" );
204 if( !is_null($this->mSection
) ) {
206 $text = $wgParser->getSection ( $rev->getText(), $this->mSection
);
208 $text = $rev->getText();
214 # Bad title or page does not exist
215 if( !$found && $this->mContentType
== 'text/x-wiki' ) {
216 # Don't return a 404 response for CSS or JavaScript;
217 # 404s aren't generally cached and it would create
218 # extra hits when user CSS/JS are on and the user doesn't
220 header( "HTTP/1.0 404 Not Found" );
223 // Special-case for empty CSS/JS
225 // Internet Explorer for Mac handles empty files badly;
226 // particularly so when keep-alive is active. It can lead
227 // to long timeouts as it seems to sit there waiting for
228 // more data that never comes.
230 // Give it a comment...
231 if( strlen( $text ) == 0 &&
232 ($this->mContentType
== 'text/css' ||
233 $this->mContentType
== 'text/javascript' ) ) {
234 return "/* Empty */";
237 return $this->parseArticleText( $text );
240 function parseArticleText( $text ) {
244 if( $this->mExpandTemplates
) {
246 return $wgParser->preprocess( $text, $this->mTitle
, new ParserOptions() );