* @subpackage Parser
[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 /** */
14 require_once( 'Revision.php' );
15
16 /**
17 * @todo document
18 * @package MediaWiki
19 */
20 class RawPage {
21
22 function RawPage( $article ) {
23 global $wgRequest, $wgInputEncoding, $wgSquidMaxage;
24 $allowedCTypes = array('text/x-wiki', 'text/javascript', 'text/css', 'application/x-zope-edit');
25 $this->mArticle =& $article;
26 $this->mTitle =& $article->mTitle;
27
28 $ctype = $wgRequest->getText( 'ctype' );
29 $smaxage = $wgRequest->getInt( 'smaxage', $wgSquidMaxage );
30 $maxage = $wgRequest->getInt( 'maxage', $wgSquidMaxage );
31 $this->mOldId = $wgRequest->getInt( 'oldid' );
32 # special case for 'generated' raw things: user css/js
33 $gen = $wgRequest->getText( 'gen' );
34 if($gen == 'css') {
35 $this->mGen = $gen;
36 if($smaxage == '') $smaxage = $wgSquidMaxage;
37 if($ctype == '') $ctype = 'text/css';
38 } else if ($gen == 'js') {
39 $this->mGen = $gen;
40 if($smaxage == '') $smaxage = $wgSquidMaxage;
41 if($ctype == '') $ctype = 'text/javascript';
42 } else {
43 $this->mGen = false;
44 }
45 $this->mCharset = $wgInputEncoding;
46 $this->mSmaxage = $smaxage;
47 $this->mMaxage = $maxage;
48 if(empty($ctype) or !in_array($ctype, $allowedCTypes)) {
49 $this->mContentType = 'text/x-wiki';
50 } else {
51 $this->mContentType = $ctype;
52 }
53 }
54
55 function view() {
56 global $wgUser, $wgOut, $wgScript;
57
58 if( strcmp( $wgScript, $_SERVER['PHP_SELF'] ) ) {
59 # Internet Explorer will ignore the Content-Type header if it
60 # thinks it sees a file extension it recognizes. Make sure that
61 # all raw requests are done through the script node, which will
62 # have eg '.php' and should remain safe.
63
64 $destUrl = $this->mTitle->getFullUrl(
65 'action=raw' .
66 '&ctype=' . urlencode( $this->mContentType ) .
67 '&smaxage=' . urlencode( $this->mSmaxage ) .
68 '&maxage=' . urlencode( $this->mMaxage ) .
69 '&gen=' . urlencode( $this->mGen ) .
70 '&oldid=' . urlencode( $this->mOldId ) );
71 header( 'Location: ' . $destUrl );
72 $wgOut->disable();
73 return;
74 }
75
76 header( "Content-type: ".$this->mContentType.'; charset='.$this->mCharset );
77 # allow the client to cache this for 24 hours
78 header( 'Cache-Control: s-maxage='.$this->mSmaxage.', max-age='.$this->mMaxage );
79 if($this->mGen) {
80 $sk = $wgUser->getSkin();
81 $sk->initPage($wgOut);
82 if($this->mGen == 'css') {
83 echo $sk->getUserStylesheet();
84 } else if($this->mGen == 'js') {
85 echo $sk->getUserJs();
86 }
87 } else {
88 echo $this->getrawtext();
89 }
90 $wgOut->disable();
91 }
92
93 function getrawtext () {
94 global $wgInputEncoding, $wgContLang;
95 $fname = 'RawPage::getrawtext';
96
97 if( $this->mTitle ) {
98 # Special case for MediaWiki: messages; we can hit the message cache.
99 if( $this->mTitle->getNamespace() == NS_MEDIAWIKI) {
100 $rawtext = wfMsg( $this->mTitle->getDbkey() );
101 return $rawtext;
102 }
103
104 # else get it from the DB
105 $rev = Revision::newFromTitle( $this->mTitle, $this->mOldId );
106 if( $rev ) {
107 $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
108 header( 'Last-modified: ' . $lastmod );
109 return $rev->getText();
110 }
111 }
112
113 # Bad title or page does not exist
114 if( $this->mContentType == 'text/x-wiki' ) {
115 # Don't return a 404 response for CSS or JavaScript;
116 # 404s aren't generally cached and it would create
117 # extra hits when user CSS/JS are on and the user doesn't
118 # have the pages.
119 header( "HTTP/1.0 404 Not Found" );
120 }
121 return '';
122 }
123 }
124 ?>