Use Doxygen @addtogroup instead of phpdoc @package && @subpackage
[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 * @todo document
14 */
15 class RawPage {
16 var $mArticle, $mTitle, $mRequest;
17 var $mOldId, $mGen, $mCharset;
18 var $mSmaxage, $mMaxage;
19 var $mContentType, $mExpandTemplates;
20
21 function __construct( &$article, $request = false ) {
22 global $wgRequest, $wgInputEncoding, $wgSquidMaxage, $wgJsMimeType;
23 global $wgUser;
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 ( isset( $_COOKIE[ini_get( 'session.name' )] ) ||
87 $wgUser->isLoggedIn() );
88
89 if ( $ctype == '' or ! in_array( $ctype, $allowedCTypes ) ) {
90 $this->mContentType = 'text/x-wiki';
91 } else {
92 $this->mContentType = $ctype;
93 }
94 }
95
96 function view() {
97 global $wgOut, $wgScript;
98
99 if( isset( $_SERVER['SCRIPT_URL'] ) ) {
100 # Normally we use PHP_SELF to get the URL to the script
101 # as it was called, minus the query string.
102 #
103 # Some sites use Apache rewrite rules to handle subdomains,
104 # and have PHP set up in a weird way that causes PHP_SELF
105 # to contain the rewritten URL instead of the one that the
106 # outside world sees.
107 #
108 # If in this mode, use SCRIPT_URL instead, which mod_rewrite
109 # provides containing the "before" URL.
110 $url = $_SERVER['SCRIPT_URL'];
111 } else {
112 $url = $_SERVER['PHP_SELF'];
113 }
114
115 $ua = @$_SERVER['HTTP_USER_AGENT'];
116 if( strcmp( $wgScript, $url ) && strpos( $ua, 'MSIE' ) !== false ) {
117 # Internet Explorer will ignore the Content-Type header if it
118 # thinks it sees a file extension it recognizes. Make sure that
119 # all raw requests are done through the script node, which will
120 # have eg '.php' and should remain safe.
121 #
122 # We used to redirect to a canonical-form URL as a general
123 # backwards-compatibility / good-citizen nice thing. However
124 # a lot of servers are set up in buggy ways, resulting in
125 # redirect loops which hang the browser until the CSS load
126 # times out.
127 #
128 # Just return a 403 Forbidden and get it over with.
129 wfHttpError( 403, 'Forbidden',
130 'Raw pages must be accessed through the primary script entry point.' );
131 return;
132 }
133
134 header( "Content-type: ".$this->mContentType.'; charset='.$this->mCharset );
135 # allow the client to cache this for 24 hours
136 $mode = $this->mPrivateCache ? 'private' : 'public';
137 header( 'Cache-Control: '.$mode.', s-maxage='.$this->mSmaxage.', max-age='.$this->mMaxage );
138 $text = $this->getRawText();
139
140 if( !wfRunHooks( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {
141 wfDebug( __METHOD__ . ': RawPageViewBeforeOutput hook broke raw page output.' );
142 }
143
144 echo $text;
145 $wgOut->disable();
146 }
147
148 function getRawText() {
149 global $wgUser, $wgOut, $wgRequest;
150 if($this->mGen) {
151 $sk = $wgUser->getSkin();
152 $sk->initPage($wgOut);
153 if($this->mGen == 'css') {
154 return $sk->getUserStylesheet();
155 } else if($this->mGen == 'js') {
156 return $sk->getUserJs();
157 }
158 } else {
159 return $this->getArticleText();
160 }
161 }
162
163 function getArticleText() {
164 $found = false;
165 $text = '';
166 if( $this->mTitle ) {
167 // If it's a MediaWiki message we can just hit the message cache
168 if ( $this->mUseMessageCache && $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
169 $key = $this->mTitle->getDBkey();
170 $text = wfMsgForContentNoTrans( $key );
171 # If the message doesn't exist, return a blank
172 if( wfEmptyMsg( $key, $text ) )
173 $text = '';
174 $found = true;
175 } else {
176 // Get it from the DB
177 $rev = Revision::newFromTitle( $this->mTitle, $this->mOldId );
178 if ( $rev ) {
179 $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
180 header( "Last-modified: $lastmod" );
181 $text = $rev->getText();
182 $found = true;
183 }
184 }
185 }
186
187 # Bad title or page does not exist
188 if( !$found && $this->mContentType == 'text/x-wiki' ) {
189 # Don't return a 404 response for CSS or JavaScript;
190 # 404s aren't generally cached and it would create
191 # extra hits when user CSS/JS are on and the user doesn't
192 # have the pages.
193 header( "HTTP/1.0 404 Not Found" );
194 }
195
196 return $this->parseArticleText( $text );
197 }
198
199 function parseArticleText( $text ) {
200 if ( $text === '' )
201 return '';
202 else
203 if ( $this->mExpandTemplates ) {
204 global $wgParser;
205 return $wgParser->preprocess( $text, $this->mTitle, new ParserOptions() );
206 } else
207 return $text;
208 }
209 }
210 ?>