API bug 10046: incorrect action produces invalid response format
[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 */
11
12 /**
13 * A simple method to retrieve the plain source of an article,
14 * using "action=raw" in the GET request string.
15 */
16 class RawPage {
17 var $mArticle, $mTitle, $mRequest;
18 var $mOldId, $mGen, $mCharset;
19 var $mSmaxage, $mMaxage;
20 var $mContentType, $mExpandTemplates;
21
22 function __construct( &$article, $request = false ) {
23 global $wgRequest, $wgInputEncoding, $wgSquidMaxage, $wgJsMimeType;
24
25 $allowedCTypes = array('text/x-wiki', $wgJsMimeType, 'text/css', 'application/x-zope-edit');
26 $this->mArticle =& $article;
27 $this->mTitle =& $article->mTitle;
28
29 if ( $request === false ) {
30 $this->mRequest =& $wgRequest;
31 } else {
32 $this->mRequest = $request;
33 }
34
35 $ctype = $this->mRequest->getVal( 'ctype' );
36 $smaxage = $this->mRequest->getIntOrNull( 'smaxage', $wgSquidMaxage );
37 $maxage = $this->mRequest->getInt( 'maxage', $wgSquidMaxage );
38 $this->mExpandTemplates = $this->mRequest->getVal( 'templates' ) === 'expand';
39 $this->mUseMessageCache = $this->mRequest->getBool( 'usemsgcache' );
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
84 // Output may contain user-specific data; vary for open sessions
85 $this->mPrivateCache = ( $this->mSmaxage == 0 ) ||
86 ( session_id() != '' );
87
88 if ( $ctype == '' or ! in_array( $ctype, $allowedCTypes ) ) {
89 $this->mContentType = 'text/x-wiki';
90 } else {
91 $this->mContentType = $ctype;
92 }
93 }
94
95 function view() {
96 global $wgOut, $wgScript;
97
98 if( isset( $_SERVER['SCRIPT_URL'] ) ) {
99 # Normally we use PHP_SELF to get the URL to the script
100 # as it was called, minus the query string.
101 #
102 # Some sites use Apache rewrite rules to handle subdomains,
103 # and have PHP set up in a weird way that causes PHP_SELF
104 # to contain the rewritten URL instead of the one that the
105 # outside world sees.
106 #
107 # If in this mode, use SCRIPT_URL instead, which mod_rewrite
108 # provides containing the "before" URL.
109 $url = $_SERVER['SCRIPT_URL'];
110 } else {
111 $url = $_SERVER['PHP_SELF'];
112 }
113
114 $ua = @$_SERVER['HTTP_USER_AGENT'];
115 if( strcmp( $wgScript, $url ) && strpos( $ua, 'MSIE' ) !== false ) {
116 # Internet Explorer will ignore the Content-Type header if it
117 # thinks it sees a file extension it recognizes. Make sure that
118 # all raw requests are done through the script node, which will
119 # have eg '.php' and should remain safe.
120 #
121 # We used to redirect to a canonical-form URL as a general
122 # backwards-compatibility / good-citizen nice thing. However
123 # a lot of servers are set up in buggy ways, resulting in
124 # redirect loops which hang the browser until the CSS load
125 # times out.
126 #
127 # Just return a 403 Forbidden and get it over with.
128 wfHttpError( 403, 'Forbidden',
129 'Raw pages must be accessed through the primary script entry point.' );
130 return;
131 }
132
133 header( "Content-type: ".$this->mContentType.'; charset='.$this->mCharset );
134 # allow the client to cache this for 24 hours
135 $mode = $this->mPrivateCache ? 'private' : 'public';
136 header( 'Cache-Control: '.$mode.', s-maxage='.$this->mSmaxage.', max-age='.$this->mMaxage );
137 $text = $this->getRawText();
138
139 if( !wfRunHooks( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {
140 wfDebug( __METHOD__ . ': RawPageViewBeforeOutput hook broke raw page output.' );
141 }
142
143 echo $text;
144 $wgOut->disable();
145 }
146
147 function getRawText() {
148 global $wgUser, $wgOut, $wgRequest;
149 if($this->mGen) {
150 $sk = $wgUser->getSkin();
151 $sk->initPage($wgOut);
152 if($this->mGen == 'css') {
153 return $sk->getUserStylesheet();
154 } else if($this->mGen == 'js') {
155 return $sk->getUserJs();
156 }
157 } else {
158 return $this->getArticleText();
159 }
160 }
161
162 function getArticleText() {
163 $found = false;
164 $text = '';
165 if( $this->mTitle ) {
166 // If it's a MediaWiki message we can just hit the message cache
167 if ( $this->mUseMessageCache && $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
168 $key = $this->mTitle->getDBkey();
169 $text = wfMsgForContentNoTrans( $key );
170 # If the message doesn't exist, return a blank
171 if( wfEmptyMsg( $key, $text ) )
172 $text = '';
173 $found = true;
174 } else {
175 // Get it from the DB
176 $rev = Revision::newFromTitle( $this->mTitle, $this->mOldId );
177 if ( $rev ) {
178 $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
179 header( "Last-modified: $lastmod" );
180 $text = $rev->getText();
181 $found = true;
182 }
183 }
184 }
185
186 # Bad title or page does not exist
187 if( !$found && $this->mContentType == 'text/x-wiki' ) {
188 # Don't return a 404 response for CSS or JavaScript;
189 # 404s aren't generally cached and it would create
190 # extra hits when user CSS/JS are on and the user doesn't
191 # have the pages.
192 header( "HTTP/1.0 404 Not Found" );
193 }
194
195 // Special-case for empty CSS/JS
196 //
197 // Internet Explorer for Mac handles empty files badly;
198 // particularly so when keep-alive is active. It can lead
199 // to long timeouts as it seems to sit there waiting for
200 // more data that never comes.
201 //
202 // Give it a comment...
203 if( strlen( $text ) == 0 &&
204 ($this->mContentType == 'text/css' ||
205 $this->mContentType == 'text/javascript' ) ) {
206 return "/* Empty */";
207 }
208
209 return $this->parseArticleText( $text );
210 }
211
212 function parseArticleText( $text ) {
213 if ( $text === '' )
214 return '';
215 else
216 if ( $this->mExpandTemplates ) {
217 global $wgParser;
218 return $wgParser->preprocess( $text, $this->mTitle, new ParserOptions() );
219 } else
220 return $text;
221 }
222 }
223 ?>