* Keep generated stylesheets cache-separated for each user
[lhc/web/wiklou.git] / includes / RawPage.php
1 <?php
2 /**
3 * Copyright (C) 2004 Gabriel Wicke <gw@wikidev.net>
4 * http://www.aulinx.de/
5 * Based on PageHistory and SpecialExport
6 *
7 * License: GPL (http://www.gnu.org/copyleft/gpl.html)
8 *
9 * @author Gabriel Wicke <gw@wikidev.net>
10 * @package MediaWiki
11 */
12
13 require_once( 'Revision.php' );
14
15 /**
16 * @todo document
17 * @package MediaWiki
18 */
19 class RawPage {
20
21 function RawPage( $article ) {
22 global $wgRequest, $wgInputEncoding, $wgSquidMaxage;
23 $allowedCTypes = array('text/x-wiki', 'text/javascript', 'text/css', 'application/x-zope-edit');
24 $this->mArticle =& $article;
25 $this->mTitle =& $article->mTitle;
26
27 $ctype = $wgRequest->getText( 'ctype' );
28 $smaxage = $wgRequest->getInt( 'smaxage', $wgSquidMaxage );
29 $maxage = $wgRequest->getInt( 'maxage', $wgSquidMaxage );
30 $this->mOldId = $wgRequest->getInt( 'oldid' );
31 # special case for 'generated' raw things: user css/js
32 $gen = $wgRequest->getText( 'gen' );
33 if($gen == 'css') {
34 $this->mGen = $gen;
35 if($smaxage == '') $smaxage = $wgSquidMaxage;
36 if($ctype == '') $ctype = 'text/css';
37 } else if ($gen == 'js') {
38 $this->mGen = $gen;
39 if($smaxage == '') $smaxage = $wgSquidMaxage;
40 if($ctype == '') $ctype = 'text/javascript';
41 } else {
42 $this->mGen = false;
43 }
44 $this->mCharset = $wgInputEncoding;
45 $this->mSmaxage = $smaxage;
46 $this->mMaxage = $maxage;
47 if(empty($ctype) or !in_array($ctype, $allowedCTypes)) {
48 $this->mContentType = 'text/x-wiki';
49 } else {
50 $this->mContentType = $ctype;
51 }
52 }
53
54 function view() {
55 global $wgUser, $wgOut, $wgScript;
56
57 if( strcmp( $wgScript, $_SERVER['PHP_SELF'] ) ) {
58 # Internet Explorer will ignore the Content-Type header if it
59 # thinks it sees a file extension it recognizes. Make sure that
60 # all raw requests are done through the script node, which will
61 # have eg '.php' and should remain safe.
62
63 $destUrl = $this->mTitle->getFullUrl(
64 'action=raw' .
65 '&ctype=' . urlencode( $this->mContentType ) .
66 '&smaxage=' . urlencode( $this->mSmaxage ) .
67 '&maxage=' . urlencode( $this->mMaxage ) .
68 '&gen=' . urlencode( $this->mGen ) .
69 '&oldid=' . urlencode( $this->mOldId ) );
70 header( 'Location: ' . $destUrl );
71 $wgOut->disable();
72 return;
73 }
74
75 header( "Content-type: ".$this->mContentType.'; charset='.$this->mCharset );
76 # allow the client to cache this for 24 hours
77 header( 'Cache-Control: s-maxage='.$this->mSmaxage.', max-age='.$this->mMaxage );
78 # Make sure each logged-in user gets his/her own stylesheet
79 header( 'Vary: Cookie' );
80 if($this->mGen) {
81 $sk = $wgUser->getSkin();
82 $sk->initPage($wgOut);
83 if($this->mGen == 'css') {
84 echo $sk->getUserStylesheet();
85 } else if($this->mGen == 'js') {
86 echo $sk->getUserJs();
87 }
88 } else {
89 echo $this->getrawtext();
90 }
91 $wgOut->disable();
92 }
93
94 function getrawtext () {
95 global $wgInputEncoding, $wgContLang;
96 $fname = 'RawPage::getrawtext';
97
98 if( !$this->mTitle ) return '';
99
100 # Special case for MediaWiki: messages; we can hit the message cache.
101 if( $this->mTitle->getNamespace() == NS_MEDIAWIKI) {
102 $rawtext = wfMsg( $this->mTitle->getDbkey() );
103 return $rawtext;
104 }
105
106 # else get it from the DB
107 $rev = Revision::newFromTitle( $this->mTitle, $this->mOldId );
108 if( $rev ) {
109 $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
110 header( 'Last-modified: ' . $lastmod );
111 return $rev->getText();
112 } else {
113 return '';
114 }
115 }
116 }
117 ?>