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