* Documentation
[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 var $mArticle, $mTitle, $mRequest;
22 var $mOldId, $mGen, $mCharset;
23 var $mSmaxage, $mMaxage;
24 var $mContentType, $mExpandTemplates;
25
26 function RawPage( &$article, $request = false ) {
27 global $wgRequest, $wgInputEncoding, $wgSquidMaxage, $wgJsMimeType;
28
29 $allowedCTypes = array('text/x-wiki', $wgJsMimeType, 'text/css', 'application/x-zope-edit');
30 $this->mArticle =& $article;
31 $this->mTitle =& $article->mTitle;
32
33 if ( $request === false ) {
34 $this->mRequest =& $wgRequest;
35 } else {
36 $this->mRequest = $request;
37 }
38
39 $ctype = $this->mRequest->getText( 'ctype' );
40 $smaxage = $this->mRequest->getInt( 'smaxage', $wgSquidMaxage );
41 $maxage = $this->mRequest->getInt( 'maxage', $wgSquidMaxage );
42 $this->mExpandTemplates = $this->mRequest->getText( 'templates' ) === 'expand';
43 $this->mOldId = $this->mRequest->getInt( 'oldid' );
44 # special case for 'generated' raw things: user css/js
45 $gen = $this->mRequest->getText( 'gen' );
46
47 if($gen == 'css') {
48 $this->mGen = $gen;
49 if($smaxage == '') $smaxage = $wgSquidMaxage;
50 if($ctype == '') $ctype = 'text/css';
51 } else if ($gen == 'js') {
52 $this->mGen = $gen;
53 if($smaxage == '') $smaxage = $wgSquidMaxage;
54 if($ctype == '') $ctype = $wgJsMimeType;
55 } else {
56 $this->mGen = false;
57 }
58 $this->mCharset = $wgInputEncoding;
59 $this->mSmaxage = $smaxage;
60 $this->mMaxage = $maxage;
61 if ( $ctype == '' or ! in_array( $ctype, $allowedCTypes ) ) {
62 $this->mContentType = 'text/x-wiki';
63 } else {
64 $this->mContentType = $ctype;
65 }
66 }
67
68 function view() {
69 global $wgOut, $wgScript;
70
71 if( isset( $_SERVER['SCRIPT_URL'] ) ) {
72 # Normally we use PHP_SELF to get the URL to the script
73 # as it was called, minus the query string.
74 #
75 # Some sites use Apache rewrite rules to handle subdomains,
76 # and have PHP set up in a weird way that causes PHP_SELF
77 # to contain the rewritten URL instead of the one that the
78 # outside world sees.
79 #
80 # If in this mode, use SCRIPT_URL instead, which mod_rewrite
81 # provides containing the "before" URL.
82 $url = $_SERVER['SCRIPT_URL'];
83 } else {
84 $url = $_SERVER['PHP_SELF'];
85 }
86 if( strcmp( $wgScript, $url ) ) {
87 # Internet Explorer will ignore the Content-Type header if it
88 # thinks it sees a file extension it recognizes. Make sure that
89 # all raw requests are done through the script node, which will
90 # have eg '.php' and should remain safe.
91 #
92 # We used to redirect to a canonical-form URL as a general
93 # backwards-compatibility / good-citizen nice thing. However
94 # a lot of servers are set up in buggy ways, resulting in
95 # redirect loops which hang the browser until the CSS load
96 # times out.
97 #
98 # Just return a 403 Forbidden and get it over with.
99 wfHttpError( 403, 'Forbidden',
100 'Raw pages must be accessed through the primary script entry point.' );
101 return;
102 }
103
104 header( "Content-type: ".$this->mContentType.'; charset='.$this->mCharset );
105 # allow the client to cache this for 24 hours
106 header( 'Cache-Control: s-maxage='.$this->mSmaxage.', max-age='.$this->mMaxage );
107 echo $this->getRawText();
108 $wgOut->disable();
109 }
110
111 function getRawText() {
112 global $wgUser, $wgOut;
113 if($this->mGen) {
114 $sk = $wgUser->getSkin();
115 $sk->initPage($wgOut);
116 if($this->mGen == 'css') {
117 return $sk->getUserStylesheet();
118 } else if($this->mGen == 'js') {
119 return $sk->getUserJs();
120 }
121 } else {
122 return $this->getArticleText();
123 }
124 }
125
126 function getArticleText() {
127 global $wgParser;
128
129 if( $this->mTitle ) {
130 $text = '';
131
132 // If it's a MediaWiki message we can just hit the message cache
133 if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI )
134 $text = wfMsgForContentNoTrans( $this->mTitle->getDbkey() );
135 else {
136 // Get it from the DB
137 $rev = Revision::newFromTitle( $this->mTitle, $this->mOldId );
138 if ( $rev ) {
139 $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
140 header( "Last-modified: $lastmod" );
141 $text = $rev->isDeleted() ? '' : $rev->getText();
142 } else
143 $text = '';
144 }
145
146 return $this->parseArticleText( $text );
147 }
148
149 # Bad title or page does not exist
150 if( $this->mContentType == 'text/x-wiki' ) {
151 # Don't return a 404 response for CSS or JavaScript;
152 # 404s aren't generally cached and it would create
153 # extra hits when user CSS/JS are on and the user doesn't
154 # have the pages.
155 header( "HTTP/1.0 404 Not Found" );
156 }
157 return '';
158 }
159
160 function parseArticleText( $text ) {
161 if ( $text === '' )
162 return '';
163 else
164 if ( $this->mExpandTemplates ) {
165 global $wgTitle;
166
167 $parser = new Parser();
168 $parser->Options( new ParserOptions() ); // We don't want this to be user-specific
169 $parser->Title( $wgTitle );
170 $parser->OutputType( OT_HTML );
171
172 return $parser->replaceVariables( $text );
173 } else
174 return $text;
175 }
176 }
177 ?>