(bug 2585) Return proper 404 code when pages don't exist
[lhc/web/wiklou.git] / index.php
1 <?php
2 /**
3 * Main wiki script; see docs/design.txt
4 * @package MediaWiki
5 */
6
7 $wgRequestTime = microtime();
8
9 unset( $IP );
10 @ini_set( 'allow_url_fopen', 0 ); # For security...
11
12 # Valid web server entry point, enable includes.
13 # Please don't move this line to includes/Defines.php. This line essentially defines
14 # a valid entry point. If you put it in includes/Defines.php, then any script that includes
15 # it becomes an entry point, thereby defeating its purpose.
16 define( 'MEDIAWIKI', true );
17 require_once( './includes/Defines.php' );
18
19 if( !file_exists( 'LocalSettings.php' ) ) {
20 $IP = "." ;
21 require_once( 'includes/DefaultSettings.php' ); # used for printing the version
22 ?>
23 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
24 <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
25 <head>
26 <title>MediaWiki <?php echo $wgVersion ?></title>
27 <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
28 <style type='text/css' media='screen, projection'>
29 html, body {
30 color: #000;
31 background-color: #fff;
32 font-family: sans-serif;
33 text-align: center;
34 }
35
36 h1 {
37 font-size: 150%;
38 }
39 </style>
40 </head>
41 <body>
42 <img src='skins/common/images/mediawiki.png' alt='The MediaWiki logo' />
43
44 <h1>MediaWiki <?php echo $wgVersion ?></h1>
45 <div class='error'>
46 <?php
47 if ( file_exists( 'config/LocalSettings.php' ) ) {
48 echo( "To complete the installation, move <tt>config/LocalSettings.php</tt> to the parent directory." );
49 } else {
50 echo( "Please <a href='config/index.php' title='setup'>setup the wiki</a> first." );
51 }
52 ?>
53
54 </div>
55 </body>
56 </html>
57 <?php
58 die();
59 }
60
61 require_once( './LocalSettings.php' );
62 require_once( 'includes/Setup.php' );
63
64 wfProfileIn( 'main-misc-setup' );
65 OutputPage::setEncodings(); # Not really used yet
66
67 # Query string fields
68 $action = $wgRequest->getVal( 'action', 'view' );
69 $title = $wgRequest->getVal( 'title' );
70
71 if ($wgRequest->getVal( 'printable' ) == 'yes') {
72 $wgOut->setPrintable();
73 }
74
75 if ( '' == $title && 'delete' != $action ) {
76 $wgTitle = Title::newFromText( wfMsgForContent( 'mainpage' ) );
77 } elseif ( $curid = $wgRequest->getInt( 'curid' ) ) {
78 # URLs like this are generated by RC, because rc_title isn't always accurate
79 $wgTitle = Title::newFromID( $curid );
80 } else {
81 $wgTitle = Title::newFromURL( $title );
82 /* check variant links so that interwiki links don't have to worry about
83 the possible different language variants
84 */
85 if( !is_null($wgTitle) && $wgTitle->getArticleID() == 0 )
86 $wgContLang->findVariantLink( $title, $wgTitle );
87
88 }
89 wfProfileOut( 'main-misc-setup' );
90
91 # Debug statement for user levels
92 // print_r($wgUser);
93
94 # If the user is not logged in, the Namespace:title of the article must be in
95 # the Read array in order for the user to see it. (We have to check here to
96 # catch special pages etc. We check again in Article::view())
97 if ( !is_null( $wgTitle ) && !$wgTitle->userCanRead() ) {
98 $wgOut->loginToUse();
99 $wgOut->output();
100 exit;
101 }
102
103 wfProfileIn( 'main-action' );
104 $search = $wgRequest->getText( 'search' );
105 if( $wgDisableInternalSearch && !is_null( $search ) && $search !== '' ) {
106 $wgTitle = Title::makeTitle( NS_SPECIAL, 'Search' );
107 }
108
109 if( !$wgDisableInternalSearch && !is_null( $search ) && $search !== '' ) {
110 require_once( 'includes/SpecialSearch.php' );
111 $wgTitle = Title::makeTitle( NS_SPECIAL, 'Search' );
112 wfSpecialSearch();
113 } else if( !$wgTitle or $wgTitle->getDBkey() == '' ) {
114 $wgTitle = Title::newFromText( wfMsgForContent( 'badtitle' ) );
115 $wgOut->setStatusCode( 404 );
116 $wgOut->errorpage( 'badtitle', 'badtitletext' );
117 } else if ( $wgTitle->getInterwiki() != '' ) {
118 if( $rdfrom = $wgRequest->getVal( 'rdfrom' ) ) {
119 $url = $wgTitle->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
120 } else {
121 $url = $wgTitle->getFullURL();
122 }
123 # Check for a redirect loop
124 if ( !preg_match( '/^' . preg_quote( $wgServer, '/' ) . '/', $url ) && $wgTitle->isLocal() ) {
125 $wgOut->redirect( $url );
126 } else {
127 $wgTitle = Title::newFromText( wfMsgForContent( 'badtitle' ) );
128 $wgOut->setStatusCode( 404 );
129 $wgOut->errorpage( 'badtitle', 'badtitletext' );
130 }
131 } else if ( ( $action == 'view' ) &&
132 (!isset( $_GET['title'] ) || $wgTitle->getPrefixedDBKey() != $_GET['title'] ) &&
133 !count( array_diff( array_keys( $_GET ), array( 'action', 'title' ) ) ) )
134 {
135 /* redirect to canonical url, make it a 301 to allow caching */
136 $wgOut->setSquidMaxage( 1200 );
137 $wgOut->redirect( $wgTitle->getFullURL(), '301');
138 } else if ( NS_SPECIAL == $wgTitle->getNamespace() ) {
139 # actions that need to be made when we have a special pages
140 SpecialPage::executePath( $wgTitle );
141 } else {
142 if ( NS_MEDIA == $wgTitle->getNamespace() ) {
143 $wgTitle = Title::makeTitle( NS_IMAGE, $wgTitle->getDBkey() );
144 }
145
146 if ( !$wgTitle->exists() ) {
147 $wgOut->setStatusCode( 404 );
148 }
149
150 $ns = $wgTitle->getNamespace();
151
152 // Namespace might change when using redirects
153 if($action == 'view' && !$wgRequest->getVal( 'oldid' ) ) {
154 $wgArticle = new Article( $wgTitle );
155 $rTitle = Title::newFromRedirect( $wgArticle->fetchContent() );
156 if($rTitle) {
157 # Reload from the page pointed to later
158 $wgArticle->mContentLoaded = false;
159 $ns = $rTitle->getNamespace();
160 }
161 }
162
163 // Categories and images are handled by a different class
164 if ( $ns == NS_IMAGE ) {
165 unset($wgArticle);
166 require_once( 'includes/ImagePage.php' );
167 $wgArticle = new ImagePage( $wgTitle );
168 } elseif ( $wgUseCategoryMagic && $ns == NS_CATEGORY ) {
169 unset($wgArticle);
170 require_once( 'includes/CategoryPage.php' );
171 $wgArticle = new CategoryPage( $wgTitle );
172 }
173
174 if ( in_array( $action, $wgDisabledActions ) ) {
175 $wgOut->setStatusCode( 404 );
176 $wgOut->errorpage( 'nosuchaction', 'nosuchactiontext' );
177 } else {
178 switch( $action ) {
179 case 'view':
180 $wgOut->setSquidMaxage( $wgSquidMaxage );
181 $wgArticle->view();
182 break;
183 case 'watch':
184 case 'unwatch':
185 case 'delete':
186 case 'revert':
187 case 'rollback':
188 case 'protect':
189 case 'unprotect':
190 case 'info':
191 case 'markpatrolled':
192 case 'validate':
193 case 'render':
194 case 'deletetrackback':
195 $wgArticle->$action();
196 break;
197 case 'print':
198 $wgArticle->view();
199 break;
200 case 'dublincore':
201 if( !$wgEnableDublinCoreRdf ) {
202 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
203 } else {
204 require_once( 'includes/Metadata.php' );
205 wfDublinCoreRdf( $wgArticle );
206 }
207 break;
208 case 'creativecommons':
209 if( !$wgEnableCreativeCommonsRdf ) {
210 wfHttpError( 403, 'Forbidden', wfMsg('nocreativecommons') );
211 } else {
212 require_once( 'includes/Metadata.php' );
213 wfCreativeCommonsRdf( $wgArticle );
214 }
215 break;
216 case 'credits':
217 require_once( 'includes/Credits.php' );
218 showCreditsPage( $wgArticle );
219 break;
220 case 'submit':
221 if( !$wgCommandLineMode && !$wgRequest->checkSessionCookie() ) {
222 # Send a cookie so anons get talk message notifications
223 User::SetupSession();
224 }
225 # Continue...
226 case 'edit':
227 $internal = $wgRequest->getVal( 'internaledit' );
228 $external = $wgRequest->getVal( 'externaledit' );
229 $section = $wgRequest->getVal( 'section' );
230 $oldid = $wgRequest->getVal( 'oldid' );
231 if(!$wgUseExternalEditor || $action=='submit' || $internal ||
232 $section || $oldid || (!$wgUser->getOption('externaleditor') && !$external)) {
233 require_once( 'includes/EditPage.php' );
234 $editor = new EditPage( $wgArticle );
235 $editor->submit();
236 } elseif($wgUseExternalEditor && ($external || $wgUser->getOption('externaleditor'))) {
237 require_once( 'includes/ExternalEdit.php' );
238 $mode = $wgRequest->getVal( 'mode' );
239 $extedit = new ExternalEdit( $wgArticle, $mode );
240 $extedit->edit();
241 }
242 break;
243 case 'history':
244 if ($_SERVER['REQUEST_URI'] == $wgTitle->getInternalURL('action=history')) {
245 $wgOut->setSquidMaxage( $wgSquidMaxage );
246 }
247 require_once( 'includes/PageHistory.php' );
248 $history = new PageHistory( $wgArticle );
249 $history->history();
250 break;
251 case 'raw':
252 require_once( 'includes/RawPage.php' );
253 $raw = new RawPage( $wgArticle );
254 $raw->view();
255 break;
256 case 'purge':
257 wfPurgeSquidServers(array($wgTitle->getInternalURL()));
258 $wgOut->setSquidMaxage( $wgSquidMaxage );
259 $wgTitle->invalidateCache();
260 $wgArticle->view();
261 break;
262 default:
263 if (wfRunHooks('UnknownAction', array($action, $wgArticle))) {
264 $wgOut->setStatusCode( 404 );
265 $wgOut->errorpage( 'nosuchaction', 'nosuchactiontext' );
266 }
267 }
268 }
269 }
270 wfProfileOut( 'main-action' );
271
272 # Deferred updates aren't really deferred anymore. It's important to report errors to the
273 # user, and that means doing this before OutputPage::output(). Note that for page saves,
274 # the client will wait until the script exits anyway before following the redirect.
275 wfProfileIn( 'main-updates' );
276 foreach ( $wgDeferredUpdateList as $up ) {
277 $up->doUpdate();
278 }
279 wfProfileOut( 'main-updates' );
280
281 wfProfileIn( 'main-cleanup' );
282 $wgLoadBalancer->saveMasterPos();
283
284 # Now commit any transactions, so that unreported errors after output() don't roll back the whole thing
285 $wgLoadBalancer->commitAll();
286
287 $wgOut->output();
288
289 foreach ( $wgPostCommitUpdateList as $up ) {
290 $up->doUpdate();
291 }
292
293 wfProfileOut( 'main-cleanup' );
294
295 logProfilingData();
296 $wgLoadBalancer->closeAll();
297 wfDebug( "Request ended normally\n" );
298 ?>