3 * Raw page text accessor
5 * Copyright © 2004 Gabriel Wicke <wicke@wikidev.net>
8 * Based on HistoryPage and SpecialExport
10 * License: GPL (http://www.gnu.org/copyleft/gpl.html)
12 * @author Gabriel Wicke <wicke@wikidev.net>
17 * A simple method to retrieve the plain source of an article,
18 * using "action=raw" in the GET request string.
21 var $mArticle, $mTitle, $mRequest;
22 var $mOldId, $mGen, $mCharset, $mSection;
23 var $mSmaxage, $mMaxage;
24 var $mContentType, $mExpandTemplates;
26 function __construct( Article
$article, $request = false ) {
27 global $wgRequest, $wgSquidMaxage, $wgJsMimeType, $wgGroupPermissions;
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' );
41 $maxage = $this->mRequest
->getInt( 'maxage', $wgSquidMaxage );
43 $this->mExpandTemplates
= $this->mRequest
->getVal( 'templates' ) === 'expand';
44 $this->mUseMessageCache
= $this->mRequest
->getBool( 'usemsgcache' );
46 $this->mSection
= $this->mRequest
->getIntOrNull( 'section' );
48 $oldid = $this->mRequest
->getInt( 'oldid' );
50 switch( $wgRequest->getText( 'direction' ) ) {
52 # output next revision, or nothing if there isn't one
54 $oldid = $this->mTitle
->getNextRevisionId( $oldid );
56 $oldid = $oldid ?
$oldid : -1;
59 # output previous revision, or nothing if there isn't one
61 # get the current revision so we can get the penultimate one
62 $this->mArticle
->getTouched();
63 $oldid = $this->mArticle
->mLatest
;
65 $prev = $this->mTitle
->getPreviousRevisionId( $oldid );
66 $oldid = $prev ?
$prev : -1 ;
72 $this->mOldId
= $oldid;
74 # special case for 'generated' raw things: user css/js
75 $gen = $this->mRequest
->getVal( 'gen' );
79 if( is_null( $smaxage ) ) {
80 $smaxage = $wgSquidMaxage;
85 } elseif( $gen == 'js' ) {
87 if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
88 if($ctype == '') $ctype = $wgJsMimeType;
92 $this->mCharset
= 'UTF-8';
94 # Force caching for CSS and JS raw content, default: 5 minutes
95 if( is_null( $smaxage ) && ( $ctype == 'text/css' ||
$ctype == $wgJsMimeType ) ) {
96 global $wgForcedRawSMaxage;
97 $this->mSmaxage
= intval( $wgForcedRawSMaxage );
99 $this->mSmaxage
= intval( $smaxage );
101 $this->mMaxage
= $maxage;
103 # Output may contain user-specific data;
104 # vary generated content for open sessions and private wikis
105 if( $this->mGen ||
!$wgGroupPermissions['*']['read'] ) {
106 $this->mPrivateCache
= $this->mSmaxage
== 0 ||
session_id() != '';
108 $this->mPrivateCache
= false;
111 if( $ctype == '' ||
!in_array( $ctype, $allowedCTypes ) ) {
112 $this->mContentType
= 'text/x-wiki';
114 $this->mContentType
= $ctype;
119 global $wgOut, $wgRequest;
121 if( !$wgRequest->checkUrlExtension() ) {
126 header( 'Content-type: ' . $this->mContentType
. '; charset=' . $this->mCharset
);
127 # allow the client to cache this for 24 hours
128 $mode = $this->mPrivateCache ?
'private' : 'public';
129 header( 'Cache-Control: ' . $mode . ', s-maxage=' . $this->mSmaxage
. ', max-age=' . $this->mMaxage
);
131 global $wgUseFileCache;
132 if( $wgUseFileCache && HTMLFileCache
::useFileCache() ) {
133 $cache = new HTMLFileCache( $this->mTitle
, 'raw' );
134 if( $cache->isFileCacheGood( /* Assume up to date */ ) ) {
135 $cache->loadFromFileCache();
139 ob_start( array( &$cache, 'saveToFileCache' ) );
143 $text = $this->getRawText();
145 if( !wfRunHooks( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {
146 wfDebug( __METHOD__
. ": RawPageViewBeforeOutput hook broke raw page output.\n" );
153 function getRawText() {
154 global $wgUser, $wgOut;
156 $sk = $wgUser->getSkin();
157 if( !StubObject
::isRealObject( $wgOut ) ) {
158 $wgOut->_unstub( 2 );
160 $sk->initPage( $wgOut );
161 if( $this->mGen
== 'css' ) {
162 return $sk->generateUserStylesheet();
163 } else if( $this->mGen
== 'js' ) {
164 return $sk->generateUserJs();
167 return $this->getArticleText();
171 function getArticleText() {
174 if( $this->mTitle
) {
175 // If it's a MediaWiki message we can just hit the message cache
176 if( $this->mUseMessageCache
&& $this->mTitle
->getNamespace() == NS_MEDIAWIKI
) {
177 $key = $this->mTitle
->getDBkey();
178 $msg = wfMessage( $key )->inContentLanguage();
179 # If the message doesn't exist, return a blank
180 $text = !$msg->exists() ?
'' : $msg->plain();
183 // Get it from the DB
184 $rev = Revision
::newFromTitle( $this->mTitle
, $this->mOldId
);
186 $lastmod = wfTimestamp( TS_RFC2822
, $rev->getTimestamp() );
187 header( "Last-modified: $lastmod" );
189 if( !is_null( $this->mSection
) ) {
191 $text = $wgParser->getSection( $rev->getText(), $this->mSection
);
193 $text = $rev->getText();
200 # Bad title or page does not exist
201 if( !$found && $this->mContentType
== 'text/x-wiki' ) {
202 # Don't return a 404 response for CSS or JavaScript;
203 # 404s aren't generally cached and it would create
204 # extra hits when user CSS/JS are on and the user doesn't
206 header( 'HTTP/1.0 404 Not Found' );
209 return $this->parseArticleText( $text );
216 function parseArticleText( $text ) {
220 if( $this->mExpandTemplates
) {
222 return $wgParser->preprocess( $text, $this->mTitle
, new ParserOptions() );