(bug 11353) Add ability to retrieve raw section content via 'action=raw' by specifyin...
[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, $mSection;
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 $this->mSection = $this->mRequest->getIntOrNull( 'section' );
42
43 $oldid = $this->mRequest->getInt( 'oldid' );
44
45 switch ( $wgRequest->getText( 'direction' ) ) {
46 case 'next':
47 # output next revision, or nothing if there isn't one
48 if ( $oldid ) {
49 $oldid = $this->mTitle->getNextRevisionId( $oldid );
50 }
51 $oldid = $oldid ? $oldid : -1;
52 break;
53 case 'prev':
54 # output previous revision, or nothing if there isn't one
55 if ( ! $oldid ) {
56 # get the current revision so we can get the penultimate one
57 $this->mArticle->getTouched();
58 $oldid = $this->mArticle->mLatest;
59 }
60 $prev = $this->mTitle->getPreviousRevisionId( $oldid );
61 $oldid = $prev ? $prev : -1 ;
62 break;
63 case 'cur':
64 $oldid = 0;
65 break;
66 }
67 $this->mOldId = $oldid;
68
69 # special case for 'generated' raw things: user css/js
70 $gen = $this->mRequest->getVal( 'gen' );
71
72 if($gen == 'css') {
73 $this->mGen = $gen;
74 if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
75 if($ctype == '') $ctype = 'text/css';
76 } elseif ($gen == 'js') {
77 $this->mGen = $gen;
78 if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
79 if($ctype == '') $ctype = $wgJsMimeType;
80 } else {
81 $this->mGen = false;
82 }
83 $this->mCharset = $wgInputEncoding;
84 $this->mSmaxage = intval( $smaxage );
85 $this->mMaxage = $maxage;
86
87 // Output may contain user-specific data; vary for open sessions
88 $this->mPrivateCache = ( $this->mSmaxage == 0 ) ||
89 ( session_id() != '' );
90
91 if ( $ctype == '' or ! in_array( $ctype, $allowedCTypes ) ) {
92 $this->mContentType = 'text/x-wiki';
93 } else {
94 $this->mContentType = $ctype;
95 }
96 }
97
98 function view() {
99 global $wgOut, $wgScript;
100
101 if( isset( $_SERVER['SCRIPT_URL'] ) ) {
102 # Normally we use PHP_SELF to get the URL to the script
103 # as it was called, minus the query string.
104 #
105 # Some sites use Apache rewrite rules to handle subdomains,
106 # and have PHP set up in a weird way that causes PHP_SELF
107 # to contain the rewritten URL instead of the one that the
108 # outside world sees.
109 #
110 # If in this mode, use SCRIPT_URL instead, which mod_rewrite
111 # provides containing the "before" URL.
112 $url = $_SERVER['SCRIPT_URL'];
113 } else {
114 $url = $_SERVER['PHP_SELF'];
115 }
116
117 $ua = @$_SERVER['HTTP_USER_AGENT'];
118 if( strcmp( $wgScript, $url ) && strpos( $ua, 'MSIE' ) !== false ) {
119 # Internet Explorer will ignore the Content-Type header if it
120 # thinks it sees a file extension it recognizes. Make sure that
121 # all raw requests are done through the script node, which will
122 # have eg '.php' and should remain safe.
123 #
124 # We used to redirect to a canonical-form URL as a general
125 # backwards-compatibility / good-citizen nice thing. However
126 # a lot of servers are set up in buggy ways, resulting in
127 # redirect loops which hang the browser until the CSS load
128 # times out.
129 #
130 # Just return a 403 Forbidden and get it over with.
131 wfHttpError( 403, 'Forbidden',
132 'Raw pages must be accessed through the primary script entry point.' );
133 return;
134 }
135
136 header( "Content-type: ".$this->mContentType.'; charset='.$this->mCharset );
137 # allow the client to cache this for 24 hours
138 $mode = $this->mPrivateCache ? 'private' : 'public';
139 header( 'Cache-Control: '.$mode.', s-maxage='.$this->mSmaxage.', max-age='.$this->mMaxage );
140 $text = $this->getRawText();
141
142 if( !wfRunHooks( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {
143 wfDebug( __METHOD__ . ': RawPageViewBeforeOutput hook broke raw page output.' );
144 }
145
146 echo $text;
147 $wgOut->disable();
148 }
149
150 function getRawText() {
151 global $wgUser, $wgOut, $wgRequest;
152 if($this->mGen) {
153 $sk = $wgUser->getSkin();
154 $sk->initPage($wgOut);
155 if($this->mGen == 'css') {
156 return $sk->getUserStylesheet();
157 } else if($this->mGen == 'js') {
158 return $sk->getUserJs();
159 }
160 } else {
161 return $this->getArticleText();
162 }
163 }
164
165 function getArticleText() {
166 $found = false;
167 $text = '';
168 if( $this->mTitle ) {
169 // If it's a MediaWiki message we can just hit the message cache
170 if ( $this->mUseMessageCache && $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
171 $key = $this->mTitle->getDBkey();
172 $text = wfMsgForContentNoTrans( $key );
173 # If the message doesn't exist, return a blank
174 if( wfEmptyMsg( $key, $text ) )
175 $text = '';
176 $found = true;
177 } else {
178 // Get it from the DB
179 $rev = Revision::newFromTitle( $this->mTitle, $this->mOldId );
180 if ( $rev ) {
181 $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
182 header( "Last-modified: $lastmod" );
183
184 if ( !is_null($this->mSection) && $this->mSection != '' ) {
185 global $wgParser;
186 return $wgParser->getSection ( $rev->getText(), $this->mSection );
187 } else
188 $text = $rev->getText();
189 $found = true;
190 }
191 }
192 }
193
194 # Bad title or page does not exist
195 if( !$found && $this->mContentType == 'text/x-wiki' ) {
196 # Don't return a 404 response for CSS or JavaScript;
197 # 404s aren't generally cached and it would create
198 # extra hits when user CSS/JS are on and the user doesn't
199 # have the pages.
200 header( "HTTP/1.0 404 Not Found" );
201 }
202
203 // Special-case for empty CSS/JS
204 //
205 // Internet Explorer for Mac handles empty files badly;
206 // particularly so when keep-alive is active. It can lead
207 // to long timeouts as it seems to sit there waiting for
208 // more data that never comes.
209 //
210 // Give it a comment...
211 if( strlen( $text ) == 0 &&
212 ($this->mContentType == 'text/css' ||
213 $this->mContentType == 'text/javascript' ) ) {
214 return "/* Empty */";
215 }
216
217 return $this->parseArticleText( $text );
218 }
219
220 function parseArticleText( $text ) {
221 if ( $text === '' )
222 return '';
223 else
224 if ( $this->mExpandTemplates ) {
225 global $wgParser;
226 return $wgParser->preprocess( $text, $this->mTitle, new ParserOptions() );
227 } else
228 return $text;
229 }
230 }
231