remove unused message
[lhc/web/wiklou.git] / includes / RawPage.php
1 <?php
2 /**
3 * Copyright (C) 2004 Gabriel Wicke <wicke@wikidev.net>
4 * http://wikidev.net/
5 * Based on PageHistory and SpecialExport
6 *
7 * License: GPL (http://www.gnu.org/copyleft/gpl.html)
8 *
9 * @author Gabriel Wicke <wicke@wikidev.net>
10 * @package MediaWiki
11 */
12
13 /**
14 * @todo document
15 * @package MediaWiki
16 */
17 class RawPage {
18 var $mArticle, $mTitle, $mRequest;
19 var $mOldId, $mGen, $mCharset;
20 var $mSmaxage, $mMaxage;
21 var $mContentType, $mExpandTemplates;
22
23 function RawPage( &$article, $request = false ) {
24 global $wgRequest, $wgInputEncoding, $wgSquidMaxage, $wgJsMimeType;
25
26 $allowedCTypes = array('text/x-wiki', $wgJsMimeType, 'text/css', 'application/x-zope-edit');
27 $this->mArticle =& $article;
28 $this->mTitle =& $article->mTitle;
29
30 if ( $request === false ) {
31 $this->mRequest =& $wgRequest;
32 } else {
33 $this->mRequest = $request;
34 }
35
36 $ctype = $this->mRequest->getVal( 'ctype' );
37 $smaxage = $this->mRequest->getIntOrNull( 'smaxage', $wgSquidMaxage );
38 $maxage = $this->mRequest->getInt( 'maxage', $wgSquidMaxage );
39 $this->mExpandTemplates = $this->mRequest->getVal( 'templates' ) === 'expand';
40
41 $oldid = $this->mRequest->getInt( 'oldid' );
42 switch ( $wgRequest->getText( 'direction' ) ) {
43 case 'next':
44 # output next revision, or nothing if there isn't one
45 if ( $oldid ) {
46 $oldid = $this->mTitle->getNextRevisionId( $oldid );
47 }
48 $oldid = $oldid ? $oldid : -1;
49 break;
50 case 'prev':
51 # output previous revision, or nothing if there isn't one
52 if ( ! $oldid ) {
53 # get the current revision so we can get the penultimate one
54 $this->mArticle->getTouched();
55 $oldid = $this->mArticle->mLatest;
56 }
57 $prev = $this->mTitle->getPreviousRevisionId( $oldid );
58 $oldid = $prev ? $prev : -1 ;
59 break;
60 case 'cur':
61 $oldid = 0;
62 break;
63 }
64 $this->mOldId = $oldid;
65
66 # special case for 'generated' raw things: user css/js
67 $gen = $this->mRequest->getVal( 'gen' );
68
69 if($gen == 'css') {
70 $this->mGen = $gen;
71 if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
72 if($ctype == '') $ctype = 'text/css';
73 } elseif ($gen == 'js') {
74 $this->mGen = $gen;
75 if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
76 if($ctype == '') $ctype = $wgJsMimeType;
77 } else {
78 $this->mGen = false;
79 }
80 $this->mCharset = $wgInputEncoding;
81 $this->mSmaxage = intval( $smaxage );
82 $this->mMaxage = $maxage;
83 if ( $ctype == '' or ! in_array( $ctype, $allowedCTypes ) ) {
84 $this->mContentType = 'text/x-wiki';
85 } else {
86 $this->mContentType = $ctype;
87 }
88 }
89
90 function view() {
91 global $wgOut, $wgScript;
92
93 if( isset( $_SERVER['SCRIPT_URL'] ) ) {
94 # Normally we use PHP_SELF to get the URL to the script
95 # as it was called, minus the query string.
96 #
97 # Some sites use Apache rewrite rules to handle subdomains,
98 # and have PHP set up in a weird way that causes PHP_SELF
99 # to contain the rewritten URL instead of the one that the
100 # outside world sees.
101 #
102 # If in this mode, use SCRIPT_URL instead, which mod_rewrite
103 # provides containing the "before" URL.
104 $url = $_SERVER['SCRIPT_URL'];
105 } else {
106 $url = $_SERVER['PHP_SELF'];
107 }
108
109 $ua = @$_SERVER['HTTP_USER_AGENT'];
110 if( strcmp( $wgScript, $url ) && strpos( $ua, 'MSIE' ) !== false ) {
111 # Internet Explorer will ignore the Content-Type header if it
112 # thinks it sees a file extension it recognizes. Make sure that
113 # all raw requests are done through the script node, which will
114 # have eg '.php' and should remain safe.
115 #
116 # We used to redirect to a canonical-form URL as a general
117 # backwards-compatibility / good-citizen nice thing. However
118 # a lot of servers are set up in buggy ways, resulting in
119 # redirect loops which hang the browser until the CSS load
120 # times out.
121 #
122 # Just return a 403 Forbidden and get it over with.
123 wfHttpError( 403, 'Forbidden',
124 'Raw pages must be accessed through the primary script entry point.' );
125 return;
126 }
127
128 header( "Content-type: ".$this->mContentType.'; charset='.$this->mCharset );
129 # allow the client to cache this for 24 hours
130 header( 'Cache-Control: s-maxage='.$this->mSmaxage.', max-age='.$this->mMaxage );
131 echo $this->getRawText();
132 $wgOut->disable();
133 }
134
135 function getRawText() {
136 global $wgUser, $wgOut;
137 if($this->mGen) {
138 $sk = $wgUser->getSkin();
139 $sk->initPage($wgOut);
140 if($this->mGen == 'css') {
141 return $sk->getUserStylesheet();
142 } else if($this->mGen == 'js') {
143 return $sk->getUserJs();
144 }
145 } else {
146 return $this->getArticleText();
147 }
148 }
149
150 function getArticleText() {
151 $found = false;
152 $text = '';
153 if( $this->mTitle ) {
154 // If it's a MediaWiki message we can just hit the message cache
155 if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI && !$this->mOldId ) {
156 $key = $this->mTitle->getDBkey();
157 $text = wfMsgForContentNoTrans( $key );
158 # If the message doesn't exist, return a blank
159 if( $text == '&lt;' . $key . '&gt;' )
160 $text = '';
161 $found = true;
162 } else {
163 // Get it from the DB
164 $rev = Revision::newFromTitle( $this->mTitle, $this->mOldId );
165 if ( $rev ) {
166 $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
167 header( "Last-modified: $lastmod" );
168 $text = $rev->getText();
169 $found = true;
170 }
171 }
172 }
173
174 # Bad title or page does not exist
175 if( !$found && $this->mContentType == 'text/x-wiki' ) {
176 # Don't return a 404 response for CSS or JavaScript;
177 # 404s aren't generally cached and it would create
178 # extra hits when user CSS/JS are on and the user doesn't
179 # have the pages.
180 header( "HTTP/1.0 404 Not Found" );
181 }
182
183 return $this->parseArticleText( $text );
184 }
185
186 function parseArticleText( $text ) {
187 if ( $text === '' )
188 return '';
189 else
190 if ( $this->mExpandTemplates ) {
191 global $wgTitle;
192
193 $parser = new Parser();
194 $parser->Options( new ParserOptions() ); // We don't want this to be user-specific
195 $parser->Title( $wgTitle );
196 $parser->OutputType( OT_HTML );
197
198 return $parser->replaceVariables( $text );
199 } else
200 return $text;
201 }
202 }
203 ?>