00001 <?php
00017 class RawPage {
00018 var $mArticle, $mTitle, $mRequest;
00019 var $mOldId, $mGen, $mCharset, $mSection;
00020 var $mSmaxage, $mMaxage;
00021 var $mContentType, $mExpandTemplates;
00022
00023 function __construct( &$article, $request = false ) {
00024 global $wgRequest, $wgInputEncoding, $wgSquidMaxage, $wgJsMimeType, $wgGroupPermissions;
00025
00026 $allowedCTypes = array('text/x-wiki', $wgJsMimeType, 'text/css', 'application/x-zope-edit');
00027 $this->mArticle =& $article;
00028 $this->mTitle =& $article->mTitle;
00029
00030 if( $request === false ) {
00031 $this->mRequest =& $wgRequest;
00032 } else {
00033 $this->mRequest = $request;
00034 }
00035
00036 $ctype = $this->mRequest->getVal( 'ctype' );
00037 $smaxage = $this->mRequest->getIntOrNull( 'smaxage' );
00038 $maxage = $this->mRequest->getInt( 'maxage', $wgSquidMaxage );
00039
00040 $this->mExpandTemplates = $this->mRequest->getVal( 'templates' ) === 'expand';
00041 $this->mUseMessageCache = $this->mRequest->getBool( 'usemsgcache' );
00042
00043 $this->mSection = $this->mRequest->getIntOrNull( 'section' );
00044
00045 $oldid = $this->mRequest->getInt( 'oldid' );
00046
00047 switch( $wgRequest->getText( 'direction' ) ) {
00048 case 'next':
00049 # output next revision, or nothing if there isn't one
00050 if( $oldid ) {
00051 $oldid = $this->mTitle->getNextRevisionId( $oldid );
00052 }
00053 $oldid = $oldid ? $oldid : -1;
00054 break;
00055 case 'prev':
00056 # output previous revision, or nothing if there isn't one
00057 if( ! $oldid ) {
00058 # get the current revision so we can get the penultimate one
00059 $this->mArticle->getTouched();
00060 $oldid = $this->mArticle->mLatest;
00061 }
00062 $prev = $this->mTitle->getPreviousRevisionId( $oldid );
00063 $oldid = $prev ? $prev : -1 ;
00064 break;
00065 case 'cur':
00066 $oldid = 0;
00067 break;
00068 }
00069 $this->mOldId = $oldid;
00070
00071 # special case for 'generated' raw things: user css/js
00072 $gen = $this->mRequest->getVal( 'gen' );
00073
00074 if( $gen == 'css' ) {
00075 $this->mGen = $gen;
00076 if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
00077 if($ctype == '') $ctype = 'text/css';
00078 } elseif( $gen == 'js' ) {
00079 $this->mGen = $gen;
00080 if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
00081 if($ctype == '') $ctype = $wgJsMimeType;
00082 } else {
00083 $this->mGen = false;
00084 }
00085 $this->mCharset = $wgInputEncoding;
00086
00087 # Force caching for CSS and JS raw content, default: 5 minutes
00088 if( is_null($smaxage) and ($ctype=='text/css' or $ctype==$wgJsMimeType) ) {
00089 global $wgForcedRawSMaxage;
00090 $this->mSmaxage = intval($wgForcedRawSMaxage);
00091 } else {
00092 $this->mSmaxage = intval( $smaxage );
00093 }
00094 $this->mMaxage = $maxage;
00095
00096 # Output may contain user-specific data;
00097 # vary generated content for open sessions and private wikis
00098 if( $this->mGen or !$wgGroupPermissions['*']['read'] ) {
00099 $this->mPrivateCache = $this->mSmaxage == 0 || session_id() != '';
00100 } else {
00101 $this->mPrivateCache = false;
00102 }
00103
00104 if( $ctype == '' or ! in_array( $ctype, $allowedCTypes ) ) {
00105 $this->mContentType = 'text/x-wiki';
00106 } else {
00107 $this->mContentType = $ctype;
00108 }
00109 }
00110
00111 function view() {
00112 global $wgOut, $wgScript, $wgRequest;
00113
00114 if( $wgRequest->isPathInfoBad() ) {
00115 # Internet Explorer will ignore the Content-Type header if it
00116 # thinks it sees a file extension it recognizes. Make sure that
00117 # all raw requests are done through the script node, which will
00118 # have eg '.php' and should remain safe.
00119 #
00120 # We used to redirect to a canonical-form URL as a general
00121 # backwards-compatibility / good-citizen nice thing. However
00122 # a lot of servers are set up in buggy ways, resulting in
00123 # redirect loops which hang the browser until the CSS load
00124 # times out.
00125 #
00126 # Just return a 403 Forbidden and get it over with.
00127 wfHttpError( 403, 'Forbidden',
00128 'Invalid file extension found in PATH_INFO or QUERY_STRING. ' .
00129 'Raw pages must be accessed through the primary script entry point.' );
00130 return;
00131 }
00132
00133 header( "Content-type: ".$this->mContentType.'; charset='.$this->mCharset );
00134 # allow the client to cache this for 24 hours
00135 $mode = $this->mPrivateCache ? 'private' : 'public';
00136 header( 'Cache-Control: '.$mode.', s-maxage='.$this->mSmaxage.', max-age='.$this->mMaxage );
00137
00138 global $wgUseFileCache;
00139 if( $wgUseFileCache and HTMLFileCache::useFileCache() ) {
00140 $cache = new HTMLFileCache( $this->mTitle, 'raw' );
00141 if( $cache->isFileCacheGood( ) ) {
00142 $cache->loadFromFileCache();
00143 $wgOut->disable();
00144 return;
00145 } else {
00146 ob_start( array(&$cache, 'saveToFileCache' ) );
00147 }
00148 }
00149
00150 $text = $this->getRawText();
00151
00152 if( !wfRunHooks( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {
00153 wfDebug( __METHOD__ . ": RawPageViewBeforeOutput hook broke raw page output.\n" );
00154 }
00155
00156 echo $text;
00157 $wgOut->disable();
00158 }
00159
00160 function getRawText() {
00161 global $wgUser, $wgOut, $wgRequest;
00162 if( $this->mGen ) {
00163 $sk = $wgUser->getSkin();
00164 if( !StubObject::isRealObject( $wgOut ) )
00165 $wgOut->_unstub( 2 );
00166 $sk->initPage( $wgOut );
00167 if( $this->mGen == 'css' ) {
00168 return $sk->generateUserStylesheet();
00169 } else if( $this->mGen == 'js' ) {
00170 return $sk->generateUserJs();
00171 }
00172 } else {
00173 return $this->getArticleText();
00174 }
00175 }
00176
00177 function getArticleText() {
00178 $found = false;
00179 $text = '';
00180 if( $this->mTitle ) {
00181
00182 if( $this->mUseMessageCache && $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
00183 $key = $this->mTitle->getDBkey();
00184 $text = wfMsgForContentNoTrans( $key );
00185 # If the message doesn't exist, return a blank
00186 if( wfEmptyMsg( $key, $text ) )
00187 $text = '';
00188 $found = true;
00189 } else {
00190
00191 $rev = Revision::newFromTitle( $this->mTitle, $this->mOldId );
00192 if( $rev ) {
00193 $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
00194 header( "Last-modified: $lastmod" );
00195
00196 if( !is_null($this->mSection ) ) {
00197 global $wgParser;
00198 $text = $wgParser->getSection ( $rev->getText(), $this->mSection );
00199 } else
00200 $text = $rev->getText();
00201 $found = true;
00202 }
00203 }
00204 }
00205
00206 # Bad title or page does not exist
00207 if( !$found && $this->mContentType == 'text/x-wiki' ) {
00208 # Don't return a 404 response for CSS or JavaScript;
00209 # 404s aren't generally cached and it would create
00210 # extra hits when user CSS/JS are on and the user doesn't
00211 # have the pages.
00212 header( "HTTP/1.0 404 Not Found" );
00213 }
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223 if( strlen( $text ) == 0 &&
00224 ($this->mContentType == 'text/css' ||
00225 $this->mContentType == 'text/javascript' ) ) {
00226 return "/* Empty */";
00227 }
00228
00229 return $this->parseArticleText( $text );
00230 }
00231
00232 function parseArticleText( $text ) {
00233 if( $text === '' )
00234 return '';
00235 else
00236 if( $this->mExpandTemplates ) {
00237 global $wgParser;
00238 return $wgParser->preprocess( $text, $this->mTitle, new ParserOptions() );
00239 } else
00240 return $text;
00241 }
00242 }