Start splitting back-end functions from the Article user-interface class.
[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 if($this->mGen) {
79 $sk = $wgUser->getSkin();
80 $sk->initPage($wgOut);
81 if($this->mGen == 'css') {
82 echo $sk->getUserStylesheet();
83 } else if($this->mGen == 'js') {
84 echo $sk->getUserJs();
85 }
86 } else {
87 echo $this->getrawtext();
88 }
89 $wgOut->disable();
90 }
91
92 function getrawtext () {
93 global $wgInputEncoding, $wgContLang;
94 $fname = 'RawPage::getrawtext';
95
96 if( !$this->mTitle ) return '';
97
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 } else {
111 return '';
112 }
113 }
114 }
115 ?>