]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/RawPage.php
MediaWiki 1.15.4-scripts
[autoinstalls/mediawiki.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  * @file
11  */
12
13 /**
14  * A simple method to retrieve the plain source of an article,
15  * using "action=raw" in the GET request string.
16  */
17 class RawPage {
18         var $mArticle, $mTitle, $mRequest;
19         var $mOldId, $mGen, $mCharset, $mSection;
20         var $mSmaxage, $mMaxage;
21         var $mContentType, $mExpandTemplates;
22
23         function __construct( &$article, $request = false ) {
24                 global $wgRequest, $wgInputEncoding, $wgSquidMaxage, $wgJsMimeType, $wgGroupPermissions;
25
26                 $allowedCTypes = array('text/x-wiki', $wgJsMimeType, 'text/css', 'application/x-zope-edit');
27                 $this->mArticle =& $article;
28                 $this->mTitle =& $article->mTitle;
29
30                 if( $request === false ) {
31                         $this->mRequest =& $wgRequest;
32                 } else {
33                         $this->mRequest = $request;
34                 }
35
36                 $ctype = $this->mRequest->getVal( 'ctype' );
37                 $smaxage = $this->mRequest->getIntOrNull( 'smaxage' );
38                 $maxage = $this->mRequest->getInt( 'maxage', $wgSquidMaxage );
39
40                 $this->mExpandTemplates = $this->mRequest->getVal( 'templates' ) === 'expand';
41                 $this->mUseMessageCache = $this->mRequest->getBool( 'usemsgcache' );
42
43                 $this->mSection = $this->mRequest->getIntOrNull( 'section' );
44
45                 $oldid = $this->mRequest->getInt( 'oldid' );
46
47                 switch( $wgRequest->getText( 'direction' ) ) {
48                         case 'next':
49                                 # output next revision, or nothing if there isn't one
50                                 if( $oldid ) {
51                                         $oldid = $this->mTitle->getNextRevisionId( $oldid );
52                                 }
53                                 $oldid = $oldid ? $oldid : -1;
54                                 break;
55                         case 'prev':
56                                 # output previous revision, or nothing if there isn't one
57                                 if( ! $oldid ) {
58                                         # get the current revision so we can get the penultimate one
59                                         $this->mArticle->getTouched();
60                                         $oldid = $this->mArticle->mLatest;
61                                 }
62                                 $prev = $this->mTitle->getPreviousRevisionId( $oldid );
63                                 $oldid = $prev ? $prev : -1 ;
64                                 break;
65                         case 'cur':
66                                 $oldid = 0;
67                                 break;
68                 }
69                 $this->mOldId = $oldid;
70
71                 # special case for 'generated' raw things: user css/js
72                 $gen = $this->mRequest->getVal( 'gen' );
73
74                 if( $gen == 'css' ) {
75                         $this->mGen = $gen;
76                         if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
77                         if($ctype == '') $ctype = 'text/css';
78                 } elseif( $gen == 'js' ) {
79                         $this->mGen = $gen;
80                         if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
81                         if($ctype == '') $ctype = $wgJsMimeType;
82                 } else {
83                         $this->mGen = false;
84                 }
85                 $this->mCharset = $wgInputEncoding;
86
87                 # Force caching for CSS and JS raw content, default: 5 minutes
88                 if( is_null($smaxage) and ($ctype=='text/css' or $ctype==$wgJsMimeType) ) {
89                         global $wgForcedRawSMaxage;
90                         $this->mSmaxage = intval($wgForcedRawSMaxage);
91                 } else {
92                         $this->mSmaxage = intval( $smaxage );
93                 }
94                 $this->mMaxage = $maxage;
95
96                 # Output may contain user-specific data;
97                 # vary generated content for open sessions and private wikis
98                 if( $this->mGen or !$wgGroupPermissions['*']['read'] ) {
99                         $this->mPrivateCache = $this->mSmaxage == 0 || session_id() != '';
100                 } else {
101                         $this->mPrivateCache = false;
102                 }
103
104                 if( $ctype == '' or ! in_array( $ctype, $allowedCTypes ) ) {
105                         $this->mContentType = 'text/x-wiki';
106                 } else {
107                         $this->mContentType = $ctype;
108                 }
109         }
110
111         function view() {
112                 global $wgOut, $wgScript;
113
114                 if( isset( $_SERVER['SCRIPT_URL'] ) ) {
115                         # Normally we use PHP_SELF to get the URL to the script
116                         # as it was called, minus the query string.
117                         #
118                         # Some sites use Apache rewrite rules to handle subdomains,
119                         # and have PHP set up in a weird way that causes PHP_SELF
120                         # to contain the rewritten URL instead of the one that the
121                         # outside world sees.
122                         #
123                         # If in this mode, use SCRIPT_URL instead, which mod_rewrite
124                         # provides containing the "before" URL.
125                         $url = $_SERVER['SCRIPT_URL'];
126                 } else {
127                         $url = $_SERVER['PHP_SELF'];
128                 }
129
130                 if( $url == '' ) {
131                         # This will make the next check fail with a confusing error
132                         # message, so we should mention it separately.
133                         wfHttpError( 500, 'Internal Server Error',
134                                 "\$_SERVER['PHP_SELF'] is not set.  Perhaps you're using CGI" .
135                                 " and haven't set cgi.fix_pathinfo = 1 in php.ini?" );
136                         return;
137                 }
138
139                 if( strcmp( $wgScript, $url ) ) {
140                         # Internet Explorer will ignore the Content-Type header if it
141                         # thinks it sees a file extension it recognizes. Make sure that
142                         # all raw requests are done through the script node, which will
143                         # have eg '.php' and should remain safe.
144                         #
145                         # We used to redirect to a canonical-form URL as a general
146                         # backwards-compatibility / good-citizen nice thing. However
147                         # a lot of servers are set up in buggy ways, resulting in
148                         # redirect loops which hang the browser until the CSS load
149                         # times out.
150                         #
151                         # Just return a 403 Forbidden and get it over with.
152                         wfHttpError( 403, 'Forbidden',
153                                 'Raw pages must be accessed through the primary script entry point.' );
154                         return;
155                 }
156
157                 header( "Content-type: ".$this->mContentType.'; charset='.$this->mCharset );
158                 # allow the client to cache this for 24 hours
159                 $mode = $this->mPrivateCache ? 'private' : 'public';
160                 header( 'Cache-Control: '.$mode.', s-maxage='.$this->mSmaxage.', max-age='.$this->mMaxage );
161                 
162                 if( HTMLFileCache::useFileCache() ) {
163                         $cache = new HTMLFileCache( $this->mTitle, 'raw' );
164                         if( $cache->isFileCacheGood( /* Assume up to date */ ) ) {
165                                 $cache->loadFromFileCache();
166                                 $wgOut->disable();
167                                 return;
168                         } else {
169                                 ob_start( array(&$cache, 'saveToFileCache' ) );
170                         }
171                 }
172                 
173                 $text = $this->getRawText();
174
175                 if( !wfRunHooks( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {
176                         wfDebug( __METHOD__ . ": RawPageViewBeforeOutput hook broke raw page output.\n" );
177                 }
178
179                 echo $text;
180                 $wgOut->disable();
181         }
182
183         function getRawText() {
184                 global $wgUser, $wgOut, $wgRequest;
185                 if( $this->mGen ) {
186                         $sk = $wgUser->getSkin();
187                         if( !StubObject::isRealObject( $wgOut ) )
188                                 $wgOut->_unstub( 2 );
189                         $sk->initPage( $wgOut );
190                         if( $this->mGen == 'css' ) {
191                                 return $sk->generateUserStylesheet();
192                         } else if( $this->mGen == 'js' ) {
193                                 return $sk->generateUserJs();
194                         }
195                 } else {
196                         return $this->getArticleText();
197                 }
198         }
199
200         function getArticleText() {
201                 $found = false;
202                 $text = '';
203                 if( $this->mTitle ) {
204                         // If it's a MediaWiki message we can just hit the message cache
205                         if( $this->mUseMessageCache && $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
206                                 $key = $this->mTitle->getDBkey();
207                                 $text = wfMsgForContentNoTrans( $key );
208                                 # If the message doesn't exist, return a blank
209                                 if( wfEmptyMsg( $key, $text ) )
210                                         $text = '';
211                                 $found = true;
212                         } else {
213                                 // Get it from the DB
214                                 $rev = Revision::newFromTitle( $this->mTitle, $this->mOldId );
215                                 if( $rev ) {
216                                         $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
217                                         header( "Last-modified: $lastmod" );
218
219                                         if( !is_null($this->mSection ) ) {
220                                                 global $wgParser;
221                                                 $text = $wgParser->getSection ( $rev->getText(), $this->mSection );
222                                         } else
223                                                 $text = $rev->getText();
224                                         $found = true;
225                                 }
226                         }
227                 }
228
229                 # Bad title or page does not exist
230                 if( !$found && $this->mContentType == 'text/x-wiki' ) {
231                         # Don't return a 404 response for CSS or JavaScript;
232                         # 404s aren't generally cached and it would create
233                         # extra hits when user CSS/JS are on and the user doesn't
234                         # have the pages.
235                         header( "HTTP/1.0 404 Not Found" );
236                 }
237
238                 // Special-case for empty CSS/JS
239                 //
240                 // Internet Explorer for Mac handles empty files badly;
241                 // particularly so when keep-alive is active. It can lead
242                 // to long timeouts as it seems to sit there waiting for
243                 // more data that never comes.
244                 //
245                 // Give it a comment...
246                 if( strlen( $text ) == 0 &&
247                         ($this->mContentType == 'text/css' ||
248                                 $this->mContentType == 'text/javascript' ) ) {
249                         return "/* Empty */";
250                 }
251
252                 return $this->parseArticleText( $text );
253         }
254
255         function parseArticleText( $text ) {
256                 if( $text === '' )
257                         return '';
258                 else
259                         if( $this->mExpandTemplates ) {
260                                 global $wgParser;
261                                 return $wgParser->preprocess( $text, $this->mTitle, new ParserOptions() );
262                         } else
263                                 return $text;
264         }
265 }