RawPage.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /**
  3. * Copyright (C) 2004 Gabriel Wicke <wicke@wikidev.net>
  4. * http://wikidev.net/
  5. * Based on PageHistory and SpecialExport
  6. *
  7. * License: GPL (http://www.gnu.org/copyleft/gpl.html)
  8. *
  9. * @author Gabriel Wicke <wicke@wikidev.net>
  10. * @file
  11. */
  12. /**
  13. * A simple method to retrieve the plain source of an article,
  14. * using "action=raw" in the GET request string.
  15. */
  16. class RawPage {
  17. var $mArticle, $mTitle, $mRequest;
  18. var $mOldId, $mGen, $mCharset, $mSection;
  19. var $mSmaxage, $mMaxage;
  20. var $mContentType, $mExpandTemplates;
  21. function __construct( &$article, $request = false ) {
  22. global $wgRequest, $wgInputEncoding, $wgSquidMaxage, $wgJsMimeType, $wgGroupPermissions;
  23. $allowedCTypes = array('text/x-wiki', $wgJsMimeType, 'text/css', 'application/x-zope-edit');
  24. $this->mArticle =& $article;
  25. $this->mTitle =& $article->mTitle;
  26. if( $request === false ) {
  27. $this->mRequest =& $wgRequest;
  28. } else {
  29. $this->mRequest = $request;
  30. }
  31. $ctype = $this->mRequest->getVal( 'ctype' );
  32. $smaxage = $this->mRequest->getIntOrNull( 'smaxage' );
  33. $maxage = $this->mRequest->getInt( 'maxage', $wgSquidMaxage );
  34. $this->mExpandTemplates = $this->mRequest->getVal( 'templates' ) === 'expand';
  35. $this->mUseMessageCache = $this->mRequest->getBool( 'usemsgcache' );
  36. $this->mSection = $this->mRequest->getIntOrNull( 'section' );
  37. $oldid = $this->mRequest->getInt( 'oldid' );
  38. switch( $wgRequest->getText( 'direction' ) ) {
  39. case 'next':
  40. # output next revision, or nothing if there isn't one
  41. if( $oldid ) {
  42. $oldid = $this->mTitle->getNextRevisionId( $oldid );
  43. }
  44. $oldid = $oldid ? $oldid : -1;
  45. break;
  46. case 'prev':
  47. # output previous revision, or nothing if there isn't one
  48. if( ! $oldid ) {
  49. # get the current revision so we can get the penultimate one
  50. $this->mArticle->getTouched();
  51. $oldid = $this->mArticle->mLatest;
  52. }
  53. $prev = $this->mTitle->getPreviousRevisionId( $oldid );
  54. $oldid = $prev ? $prev : -1 ;
  55. break;
  56. case 'cur':
  57. $oldid = 0;
  58. break;
  59. }
  60. $this->mOldId = $oldid;
  61. # special case for 'generated' raw things: user css/js
  62. $gen = $this->mRequest->getVal( 'gen' );
  63. if( $gen == 'css' ) {
  64. $this->mGen = $gen;
  65. if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
  66. if($ctype == '') $ctype = 'text/css';
  67. } elseif( $gen == 'js' ) {
  68. $this->mGen = $gen;
  69. if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
  70. if($ctype == '') $ctype = $wgJsMimeType;
  71. } else {
  72. $this->mGen = false;
  73. }
  74. $this->mCharset = $wgInputEncoding;
  75. # Force caching for CSS and JS raw content, default: 5 minutes
  76. if( is_null($smaxage) and ($ctype=='text/css' or $ctype==$wgJsMimeType) ) {
  77. global $wgForcedRawSMaxage;
  78. $this->mSmaxage = intval($wgForcedRawSMaxage);
  79. } else {
  80. $this->mSmaxage = intval( $smaxage );
  81. }
  82. $this->mMaxage = $maxage;
  83. # Output may contain user-specific data;
  84. # vary generated content for open sessions and private wikis
  85. if( $this->mGen or !$wgGroupPermissions['*']['read'] ) {
  86. $this->mPrivateCache = $this->mSmaxage == 0 || session_id() != '';
  87. } else {
  88. $this->mPrivateCache = false;
  89. }
  90. if( $ctype == '' or ! in_array( $ctype, $allowedCTypes ) ) {
  91. $this->mContentType = 'text/x-wiki';
  92. } else {
  93. $this->mContentType = $ctype;
  94. }
  95. }
  96. function view() {
  97. global $wgOut, $wgScript;
  98. if( isset( $_SERVER['SCRIPT_URL'] ) ) {
  99. # Normally we use PHP_SELF to get the URL to the script
  100. # as it was called, minus the query string.
  101. #
  102. # Some sites use Apache rewrite rules to handle subdomains,
  103. # and have PHP set up in a weird way that causes PHP_SELF
  104. # to contain the rewritten URL instead of the one that the
  105. # outside world sees.
  106. #
  107. # If in this mode, use SCRIPT_URL instead, which mod_rewrite
  108. # provides containing the "before" URL.
  109. $url = $_SERVER['SCRIPT_URL'];
  110. } else {
  111. $url = $_SERVER['PHP_SELF'];
  112. }
  113. if( $url == '' ) {
  114. # This will make the next check fail with a confusing error
  115. # message, so we should mention it separately.
  116. wfHttpError( 500, 'Internal Server Error',
  117. "\$_SERVER['PHP_SELF'] is not set. Perhaps you're using CGI" .
  118. " and haven't set cgi.fix_pathinfo = 1 in php.ini?" );
  119. return;
  120. }
  121. if( strcmp( $wgScript, $url ) ) {
  122. # Internet Explorer will ignore the Content-Type header if it
  123. # thinks it sees a file extension it recognizes. Make sure that
  124. # all raw requests are done through the script node, which will
  125. # have eg '.php' and should remain safe.
  126. #
  127. # We used to redirect to a canonical-form URL as a general
  128. # backwards-compatibility / good-citizen nice thing. However
  129. # a lot of servers are set up in buggy ways, resulting in
  130. # redirect loops which hang the browser until the CSS load
  131. # times out.
  132. #
  133. # Just return a 403 Forbidden and get it over with.
  134. wfHttpError( 403, 'Forbidden',
  135. 'Raw pages must be accessed through the primary script entry point.' );
  136. return;
  137. }
  138. header( "Content-type: ".$this->mContentType.'; charset='.$this->mCharset );
  139. # allow the client to cache this for 24 hours
  140. $mode = $this->mPrivateCache ? 'private' : 'public';
  141. header( 'Cache-Control: '.$mode.', s-maxage='.$this->mSmaxage.', max-age='.$this->mMaxage );
  142. if( HTMLFileCache::useFileCache() ) {
  143. $cache = new HTMLFileCache( $this->mTitle, 'raw' );
  144. if( $cache->isFileCacheGood( /* Assume up to date */ ) ) {
  145. $cache->loadFromFileCache();
  146. $wgOut->disable();
  147. return;
  148. } else {
  149. ob_start( array(&$cache, 'saveToFileCache' ) );
  150. }
  151. }
  152. $text = $this->getRawText();
  153. if( !wfRunHooks( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {
  154. wfDebug( __METHOD__ . ": RawPageViewBeforeOutput hook broke raw page output.\n" );
  155. }
  156. echo $text;
  157. $wgOut->disable();
  158. }
  159. function getRawText() {
  160. global $wgUser, $wgOut, $wgRequest;
  161. if( $this->mGen ) {
  162. $sk = $wgUser->getSkin();
  163. if( !StubObject::isRealObject( $wgOut ) )
  164. $wgOut->_unstub( 2 );
  165. $sk->initPage( $wgOut );
  166. if( $this->mGen == 'css' ) {
  167. return $sk->generateUserStylesheet();
  168. } else if( $this->mGen == 'js' ) {
  169. return $sk->generateUserJs();
  170. }
  171. } else {
  172. return $this->getArticleText();
  173. }
  174. }
  175. function getArticleText() {
  176. $found = false;
  177. $text = '';
  178. if( $this->mTitle ) {
  179. // If it's a MediaWiki message we can just hit the message cache
  180. if( $this->mUseMessageCache && $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
  181. $key = $this->mTitle->getDBkey();
  182. $text = wfMsgForContentNoTrans( $key );
  183. # If the message doesn't exist, return a blank
  184. if( wfEmptyMsg( $key, $text ) )
  185. $text = '';
  186. $found = true;
  187. } else {
  188. // Get it from the DB
  189. $rev = Revision::newFromTitle( $this->mTitle, $this->mOldId );
  190. if( $rev ) {
  191. $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
  192. header( "Last-modified: $lastmod" );
  193. if( !is_null($this->mSection ) ) {
  194. global $wgParser;
  195. $text = $wgParser->getSection ( $rev->getText(), $this->mSection );
  196. } else
  197. $text = $rev->getText();
  198. $found = true;
  199. }
  200. }
  201. }
  202. # Bad title or page does not exist
  203. if( !$found && $this->mContentType == 'text/x-wiki' ) {
  204. # Don't return a 404 response for CSS or JavaScript;
  205. # 404s aren't generally cached and it would create
  206. # extra hits when user CSS/JS are on and the user doesn't
  207. # have the pages.
  208. header( "HTTP/1.0 404 Not Found" );
  209. }
  210. // Special-case for empty CSS/JS
  211. //
  212. // Internet Explorer for Mac handles empty files badly;
  213. // particularly so when keep-alive is active. It can lead
  214. // to long timeouts as it seems to sit there waiting for
  215. // more data that never comes.
  216. //
  217. // Give it a comment...
  218. if( strlen( $text ) == 0 &&
  219. ($this->mContentType == 'text/css' ||
  220. $this->mContentType == 'text/javascript' ) ) {
  221. return "/* Empty */";
  222. }
  223. return $this->parseArticleText( $text );
  224. }
  225. function parseArticleText( $text ) {
  226. if( $text === '' )
  227. return '';
  228. else
  229. if( $this->mExpandTemplates ) {
  230. global $wgParser;
  231. return $wgParser->preprocess( $text, $this->mTitle, new ParserOptions() );
  232. } else
  233. return $text;
  234. }
  235. }