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;
00113
00114 if( isset( $_SERVER['SCRIPT_URL'] ) ) {
00115 # Normally we use PHP_SELF to get the URL to the script
00116 # as it was called, minus the query string.
00117 #
00118 # Some sites use Apache rewrite rules to handle subdomains,
00119 # and have PHP set up in a weird way that causes PHP_SELF
00120 # to contain the rewritten URL instead of the one that the
00121 # outside world sees.
00122 #
00123 # If in this mode, use SCRIPT_URL instead, which mod_rewrite
00124 # provides containing the "before" URL.
00125 $url = $_SERVER['SCRIPT_URL'];
00126 } else {
00127 $url = $_SERVER['PHP_SELF'];
00128 }
00129
00130 if( $url == '' ) {
00131 # This will make the next check fail with a confusing error
00132 # message, so we should mention it separately.
00133 wfHttpError( 500, 'Internal Server Error',
00134 "\$_SERVER['PHP_SELF'] is not set. Perhaps you're using CGI" .
00135 " and haven't set cgi.fix_pathinfo = 1 in php.ini?" );
00136 return;
00137 }
00138
00139 if( strcmp( $wgScript, $url ) ) {
00140 # Internet Explorer will ignore the Content-Type header if it
00141 # thinks it sees a file extension it recognizes. Make sure that
00142 # all raw requests are done through the script node, which will
00143 # have eg '.php' and should remain safe.
00144 #
00145 # We used to redirect to a canonical-form URL as a general
00146 # backwards-compatibility / good-citizen nice thing. However
00147 # a lot of servers are set up in buggy ways, resulting in
00148 # redirect loops which hang the browser until the CSS load
00149 # times out.
00150 #
00151 # Just return a 403 Forbidden and get it over with.
00152 wfHttpError( 403, 'Forbidden',
00153 'Raw pages must be accessed through the primary script entry point.' );
00154 return;
00155 }
00156
00157 header( "Content-type: ".$this->mContentType.'; charset='.$this->mCharset );
00158 # allow the client to cache this for 24 hours
00159 $mode = $this->mPrivateCache ? 'private' : 'public';
00160 header( 'Cache-Control: '.$mode.', s-maxage='.$this->mSmaxage.', max-age='.$this->mMaxage );
00161
00162 if( HTMLFileCache::useFileCache() ) {
00163 $cache = new HTMLFileCache( $this->mTitle, 'raw' );
00164 if( $cache->isFileCacheGood( ) ) {
00165 $cache->loadFromFileCache();
00166 $wgOut->disable();
00167 return;
00168 } else {
00169 ob_start( array(&$cache, 'saveToFileCache' ) );
00170 }
00171 }
00172
00173 $text = $this->getRawText();
00174
00175 if( !wfRunHooks( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {
00176 wfDebug( __METHOD__ . ": RawPageViewBeforeOutput hook broke raw page output.\n" );
00177 }
00178
00179 echo $text;
00180 $wgOut->disable();
00181 }
00182
00183 function getRawText() {
00184 global $wgUser, $wgOut, $wgRequest;
00185 if( $this->mGen ) {
00186 $sk = $wgUser->getSkin();
00187 if( !StubObject::isRealObject( $wgOut ) )
00188 $wgOut->_unstub( 2 );
00189 $sk->initPage( $wgOut );
00190 if( $this->mGen == 'css' ) {
00191 return $sk->generateUserStylesheet();
00192 } else if( $this->mGen == 'js' ) {
00193 return $sk->generateUserJs();
00194 }
00195 } else {
00196 return $this->getArticleText();
00197 }
00198 }
00199
00200 function getArticleText() {
00201 $found = false;
00202 $text = '';
00203 if( $this->mTitle ) {
00204
00205 if( $this->mUseMessageCache && $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
00206 $key = $this->mTitle->getDBkey();
00207 $text = wfMsgForContentNoTrans( $key );
00208 # If the message doesn't exist, return a blank
00209 if( wfEmptyMsg( $key, $text ) )
00210 $text = '';
00211 $found = true;
00212 } else {
00213
00214 $rev = Revision::newFromTitle( $this->mTitle, $this->mOldId );
00215 if( $rev ) {
00216 $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
00217 header( "Last-modified: $lastmod" );
00218
00219 if( !is_null($this->mSection ) ) {
00220 global $wgParser;
00221 $text = $wgParser->getSection ( $rev->getText(), $this->mSection );
00222 } else
00223 $text = $rev->getText();
00224 $found = true;
00225 }
00226 }
00227 }
00228
00229 # Bad title or page does not exist
00230 if( !$found && $this->mContentType == 'text/x-wiki' ) {
00231 # Don't return a 404 response for CSS or JavaScript;
00232 # 404s aren't generally cached and it would create
00233 # extra hits when user CSS/JS are on and the user doesn't
00234 # have the pages.
00235 header( "HTTP/1.0 404 Not Found" );
00236 }
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246 if( strlen( $text ) == 0 &&
00247 ($this->mContentType == 'text/css' ||
00248 $this->mContentType == 'text/javascript' ) ) {
00249 return "/* Empty */";
00250 }
00251
00252 return $this->parseArticleText( $text );
00253 }
00254
00255 function parseArticleText( $text ) {
00256 if( $text === '' )
00257 return '';
00258 else
00259 if( $this->mExpandTemplates ) {
00260 global $wgParser;
00261 return $wgParser->preprocess( $text, $this->mTitle, new ParserOptions() );
00262 } else
00263 return $text;
00264 }
00265 }