Remove global added in 2005 by vibber (r11563). There's a new one added in r99349.
[lhc/web/wiklou.git] / includes / RawPage.php
1 <?php
2 /**
3 * Raw page text accessor
4 *
5 * Copyright © 2004 Gabriel Wicke <wicke@wikidev.net>
6 * http://wikidev.net/
7 *
8 * Based on HistoryPage and SpecialExport
9 *
10 * License: GPL (http://www.gnu.org/copyleft/gpl.html)
11 *
12 * @author Gabriel Wicke <wicke@wikidev.net>
13 * @file
14 */
15
16 /**
17 * A simple method to retrieve the plain source of an article,
18 * using "action=raw" in the GET request string.
19 */
20 class RawPage {
21 var $mArticle, $mTitle, $mRequest;
22 var $mOldId, $mGen, $mCharset, $mSection;
23 var $mSmaxage, $mMaxage;
24 var $mContentType, $mExpandTemplates;
25
26 function __construct( Page $article, $request = false ) {
27 global $wgRequest, $wgSquidMaxage, $wgJsMimeType, $wgGroupPermissions;
28
29 $allowedCTypes = array( 'text/x-wiki', $wgJsMimeType, 'text/css', 'application/x-zope-edit' );
30 $this->mArticle = $article;
31 $this->mTitle = $article->getTitle();
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' );
41 $maxage = $this->mRequest->getInt( 'maxage', $wgSquidMaxage );
42
43 $this->mExpandTemplates = $this->mRequest->getVal( 'templates' ) === 'expand';
44 $this->mUseMessageCache = $this->mRequest->getBool( 'usemsgcache' );
45
46 $this->mSection = $this->mRequest->getIntOrNull( 'section' );
47
48 $oldid = $this->mRequest->getInt( 'oldid' );
49
50 switch( $wgRequest->getText( 'direction' ) ) {
51 case 'next':
52 # output next revision, or nothing if there isn't one
53 if( $oldid ) {
54 $oldid = $this->mTitle->getNextRevisionId( $oldid );
55 }
56 $oldid = $oldid ? $oldid : -1;
57 break;
58 case 'prev':
59 # output previous revision, or nothing if there isn't one
60 if( !$oldid ) {
61 # get the current revision so we can get the penultimate one
62 $this->mArticle->getTouched();
63 $oldid = $this->mArticle->getLatest();
64 }
65 $prev = $this->mTitle->getPreviousRevisionId( $oldid );
66 $oldid = $prev ? $prev : -1 ;
67 break;
68 case 'cur':
69 $oldid = 0;
70 break;
71 }
72 $this->mOldId = $oldid;
73
74 # special case for 'generated' raw things: user css/js
75 $gen = $this->mRequest->getVal( 'gen' );
76
77 if( $gen == 'css' ) {
78 $this->mGen = $gen;
79 if( is_null( $smaxage ) ) {
80 $smaxage = $wgSquidMaxage;
81 }
82 if( $ctype == '' ) {
83 $ctype = 'text/css';
84 }
85 } elseif( $gen == 'js' ) {
86 $this->mGen = $gen;
87 if( is_null( $smaxage ) ) $smaxage = $wgSquidMaxage;
88 if($ctype == '') $ctype = $wgJsMimeType;
89 } else {
90 $this->mGen = false;
91 }
92 $this->mCharset = 'UTF-8';
93
94 # Force caching for CSS and JS raw content, default: 5 minutes
95 if( is_null( $smaxage ) && ( $ctype == 'text/css' || $ctype == $wgJsMimeType ) ) {
96 global $wgForcedRawSMaxage;
97 $this->mSmaxage = intval( $wgForcedRawSMaxage );
98 } else {
99 $this->mSmaxage = intval( $smaxage );
100 }
101 $this->mMaxage = $maxage;
102
103 # Output may contain user-specific data;
104 # vary generated content for open sessions and private wikis
105 if( $this->mGen || !$wgGroupPermissions['*']['read'] ) {
106 $this->mPrivateCache = $this->mSmaxage == 0 || session_id() != '';
107 } else {
108 $this->mPrivateCache = false;
109 }
110
111 if( $ctype == '' || !in_array( $ctype, $allowedCTypes ) ) {
112 $this->mContentType = 'text/x-wiki';
113 } else {
114 $this->mContentType = $ctype;
115 }
116 }
117
118 function view() {
119 global $wgOut, $wgRequest;
120
121 if( !$wgRequest->checkUrlExtension() ) {
122 $wgOut->disable();
123 return;
124 }
125
126 header( 'Content-type: ' . $this->mContentType . '; charset=' . $this->mCharset );
127 # allow the client to cache this for 24 hours
128 $mode = $this->mPrivateCache ? 'private' : 'public';
129 header( 'Cache-Control: ' . $mode . ', s-maxage=' . $this->mSmaxage . ', max-age=' . $this->mMaxage );
130
131 $text = $this->getRawText();
132
133 if( !wfRunHooks( 'RawPageViewBeforeOutput', array( &$this, &$text ) ) ) {
134 wfDebug( __METHOD__ . ": RawPageViewBeforeOutput hook broke raw page output.\n" );
135 }
136
137 echo $text;
138 $wgOut->disable();
139 }
140
141 function getRawText() {
142 global $wgOut, $wgUser;
143 if( $this->mGen ) {
144 $sk = $wgUser->getSkin();
145 if( !StubObject::isRealObject( $wgOut ) ) {
146 $wgOut->_unstub( 2 );
147 }
148 $sk->initPage( $wgOut );
149 if( $this->mGen == 'css' ) {
150 return $sk->generateUserStylesheet();
151 } elseif( $this->mGen == 'js' ) {
152 return $sk->generateUserJs();
153 }
154 } else {
155 return $this->getArticleText();
156 }
157 return '';
158 }
159
160 function getArticleText() {
161 $found = false;
162 $text = '';
163 if( $this->mTitle ) {
164 // If it's a MediaWiki message we can just hit the message cache
165 if( $this->mUseMessageCache && $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
166 $key = $this->mTitle->getDBkey();
167 $msg = wfMessage( $key )->inContentLanguage();
168 # If the message doesn't exist, return a blank
169 $text = !$msg->exists() ? '' : $msg->plain();
170 $found = true;
171 } else {
172 // Get it from the DB
173 $rev = Revision::newFromTitle( $this->mTitle, $this->mOldId );
174 if( $rev ) {
175 $lastmod = wfTimestamp( TS_RFC2822, $rev->getTimestamp() );
176 header( "Last-modified: $lastmod" );
177
178 if( !is_null( $this->mSection ) ) {
179 global $wgParser;
180 $text = $wgParser->getSection( $rev->getText(), $this->mSection );
181 } else {
182 $text = $rev->getText();
183 }
184 $found = true;
185 }
186 }
187 }
188
189 # Bad title or page does not exist
190 if( !$found && $this->mContentType == 'text/x-wiki' ) {
191 # Don't return a 404 response for CSS or JavaScript;
192 # 404s aren't generally cached and it would create
193 # extra hits when user CSS/JS are on and the user doesn't
194 # have the pages.
195 header( 'HTTP/1.0 404 Not Found' );
196 }
197
198 return $this->parseArticleText( $text );
199 }
200
201 /**
202 * @param $text
203 * @return string
204 */
205 function parseArticleText( $text ) {
206 if( $text === '' ) {
207 return '';
208 } else {
209 if( $this->mExpandTemplates ) {
210 global $wgParser;
211 return $wgParser->preprocess( $text, $this->mTitle, new ParserOptions() );
212 } else {
213 return $text;
214 }
215 }
216 }
217 }