add Last-Modified header
[lhc/web/wiklou.git] / includes / RawPage.php
1 <?php
2 # Copyright (C) 2004 Gabriel Wicke <gw@wikidev.net>
3 # http://www.aulinx.de/
4 # Based on PageHistory and SpecialExport
5 #
6 # License: GPL (http://www.gnu.org/copyleft/gpl.html)
7
8 class RawPage {
9
10 function RawPage( $article ) {
11 global $wgRequest, $wgInputEncoding, $wgSquidMaxage;
12 $allowedCTypes = array('text/x-wiki', 'text/javascript', 'text/css', 'application/x-zope-edit');
13 $this->mArticle =& $article;
14 $this->mTitle =& $article->mTitle;
15
16 $ctype = $wgRequest->getText( 'ctype' );
17 $charset = $wgRequest->getText( 'charset' );
18 $smaxage = $wgRequest->getText( 'smaxage' );
19 $maxage = $wgRequest->getText( 'maxage' );
20 $this->mOldId = $wgRequest->getInt( 'oldid' );
21 # special case for 'generated' raw things: user css/js
22 $gen = $wgRequest->getText( 'gen' );
23 if($gen == 'css') {
24 $this->mGen = $gen;
25 if($smaxage == '') $smaxage = $wgSquidMaxage;
26 if($ctype == '') $ctype = 'text/css';
27 } else if ($gen == 'js') {
28 $this->mGen = $gen;
29 if($smaxage == '') $smaxage = $wgSquidMaxage;
30 if($ctype == '') $ctype = 'text/javascript';
31 } else {
32 $this->mGen = false;
33 }
34 $this->mCharset = !empty($charset) ? $charset : $wgInputEncoding;
35 $this->mSmaxage = ($smaxage != '') ? $smaxage : 0;
36 $this->mMaxage = ($maxage != '') ? $maxage : 86400;
37 if(empty($ctype) or !in_array($ctype, $allowedCTypes)) {
38 $this->mContentType = 'text/x-wiki';
39 } else {
40 $this->mContentType = $ctype;
41 }
42 }
43 function view() {
44 global $wgUser, $wgOut;
45 header( "Content-type: ".$this->mContentType.'; charset='.$this->mCharset );
46 # allow the client to cache this for 24 hours
47 header( 'Cache-Control: s-maxage='.$this->mSmaxage.', max-age='.$this->mMaxage );
48 if($this->mGen) {
49 $sk = $wgUser->getSkin();
50 $sk->initPage($wgOut);
51 if($this->mGen == 'css') {
52 echo $sk->getUserStylesheet();
53 } else if($this->mGen == 'js') {
54 echo $sk->getUserJs();
55 }
56 } else {
57 echo $this->getrawtext();
58 }
59 wfAbruptExit();
60 }
61
62 function getrawtext () {
63 global $wgInputEncoding, $wgLang, $wgIsPg;
64 if( !$this->mTitle ) return '';
65 $t = wfStrencode( $this->mTitle->getDBKey() );
66 $ns = $this->mTitle->getNamespace();
67 # special case
68 if($ns == NS_MEDIAWIKI) {
69 $rawtext = wfMsg($t);
70 if($wgInputEncoding != $this->mCharset)
71 $rawtext = $wgLang->iconv( $wgInputEncoding, $this->mCharset, $rawtext );
72 return $rawtext;
73 }
74 # else get it from the DB
75 if(!empty($this->mOldId)) {
76 $oldtable=$wgIsPg?'"old"':'old';
77 $sql = "SELECT old_text AS text,old_timestamp AS timestamp,".
78 "old_user AS user,old_flags AS flags FROM $oldtable " .
79 "WHERE old_id={$this->mOldId}";
80 } else {
81 $sql = "SELECT cur_id as id,cur_timestamp as timestamp,cur_user as user,cur_user_text as user_text," .
82 "cur_restrictions as restrictions,cur_comment as comment,cur_text as text FROM cur " .
83 "WHERE cur_namespace=$ns AND cur_title='$t'";
84 }
85 $res = wfQuery( $sql, DB_READ );
86 if( $s = wfFetchObject( $res ) ) {
87 $rawtext = Article::getRevisionText( $s, "" );
88 if($wgInputEncoding != $this->mCharset)
89 $rawtext = $wgLang->iconv( $wgInputEncoding, $this->mCharset, $rawtext );
90 header( 'Last-modified: '.gmdate( "D, j M Y H:i:s", wfTimestamp2Unix( $s->timestamp )).' GMT' );
91 return $rawtext;
92 } else {
93 return '';
94 }
95 }
96 }
97 ?>