Live hack: Skip some work on empty category/link sets
[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 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 # 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 if ( $ctype == '' or ! in_array( $ctype, $allowedCTypes ) ) {
87 $this->mContentType = 'text/x-wiki';
88 } else {
89 $this->mContentType = $ctype;
90 }
91 }
92
93 function view() {
94 global $wgOut, $wgScript;
95
96 if( isset( $_SERVER['SCRIPT_URL'] ) ) {
97 # Normally we use PHP_SELF to get the URL to the script
98 # as it was called, minus the query string.
99 #
100 # Some sites use Apache rewrite rules to handle subdomains,
101 # and have PHP set up in a weird way that causes PHP_SELF
102 # to contain the rewritten URL instead of the one that the
103 # outside world sees.
104 #
105 # If in this mode, use SCRIPT_URL instead, which mod_rewrite
106 # provides containing the "before" URL.
107 $url = $_SERVER['SCRIPT_URL'];
108 } else {
109 $url = $_SERVER['PHP_SELF'];
110 }
111
112 $ua = @$_SERVER['HTTP_USER_AGENT'];
113 if( strcmp( $wgScript, $url ) && strpos( $ua, 'MSIE' ) !== false ) {
114 # Internet Explorer will ignore the Content-Type header if it
115 # thinks it sees a file extension it recognizes. Make sure that
116 # all raw requests are done through the script node, which will
117 # have eg '.php' and should remain safe.
118 #
119 # We used to redirect to a canonical-form URL as a general
120 # backwards-compatibility / good-citizen nice thing. However
121 # a lot of servers are set up in buggy ways, resulting in
122 # redirect loops which hang the browser until the CSS load
123 # times out.
124 #
125 # Just return a 403 Forbidden and get it over with.
126 wfHttpError( 403, 'Forbidden',
127 'Raw pages must be accessed through the primary script entry point.' );
128 return;
129 }
130
131 header( "Content-type: ".$this->mContentType.'; charset='.$this->mCharset );
132 # allow the client to cache this for 24 hours
133 header( 'Cache-Control: s-maxage='.$this->mSmaxage.', max-age='.$this->mMaxage );
134 echo $this->getRawText();
135 $wgOut->disable();
136 }
137
138 function getRawText() {
139 global $wgUser, $wgOut;
140 if($this->mGen) {
141 $sk = $wgUser->getSkin();
142 $sk->initPage($wgOut);
143 if($this->mGen == 'css') {
144 return $sk->getUserStylesheet();
145 } else if($this->mGen == 'js') {
146 return $sk->getUserJs();
147 }
148 } else {
149 return $this->getArticleText();
150 }
151 }
152
153 function getArticleText() {
154 if( $this->mTitle ) {
155 $text = '';
156
157 // If it's a MediaWiki message we can just hit the message cache
158 if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
159 $text = wfMsgForContentNoTrans( $this->mTitle->getDbkey() );
160 } else {
161 // Get it from the DB
162 $rev = Revision::newFromTitle( $this->mTitle, $this->mOldId );
163 if ( $rev ) {
164 $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
165 header( "Last-modified: $lastmod" );
166 $text = $rev->getText();
167 } else
168 $text = '';
169 }
170
171 return $this->parseArticleText( $text );
172 }
173
174 # Bad title or page does not exist
175 if( $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 return '';
183 }
184
185 function parseArticleText( $text ) {
186 if ( $text === '' )
187 return '';
188 else
189 if ( $this->mExpandTemplates ) {
190 global $wgTitle;
191
192 $parser = new Parser();
193 $parser->Options( new ParserOptions() ); // We don't want this to be user-specific
194 $parser->Title( $wgTitle );
195 $parser->OutputType( OT_HTML );
196
197 return $parser->replaceVariables( $text );
198 } else
199 return $text;
200 }
201 }
202 ?>