* (bug 563) Fix UTF-8 interwiki URL redirects via Latin-1 wikis
[lhc/web/wiklou.git] / index.php
1 <?php
2 #apd_set_pprof_trace();
3 # Main wiki script; see design.doc
4 #
5 $wgRequestTime = microtime();
6
7 unset( $IP );
8 @ini_set( 'allow_url_fopen', 0 ); # For security...
9 if( !file_exists( 'LocalSettings.php' ) ) {
10 if ( file_exists( 'config/LocalSettings.php' ) ) {
11 die( "To complete the installation, move <tt>config/LocalSettings.php</tt> to the parent directory.\n" );
12 } else {
13 die( "You'll have to <a href='config/index.php'>set the wiki up</a> first!" );
14 }
15 }
16
17 # Valid web server entry point, enable includes.
18 # Please don't move this line to includes/Defines.php. This line essentially defines
19 # a valid entry point. If you put it in includes/Defines.php, then any script that includes
20 # it becomes an entry point, thereby defeating its purpose.
21 define( 'MEDIAWIKI', true );
22
23 require_once( './includes/Defines.php' );
24 require_once( './LocalSettings.php' );
25 require_once( 'includes/Setup.php' );
26
27 wfProfileIn( 'main-misc-setup' );
28 OutputPage::setEncodings(); # Not really used yet
29
30 # Query string fields
31 $action = $wgRequest->getVal( 'action', 'view' );
32 $title = $wgRequest->getVal( 'title' );
33
34 $action = strtolower( trim( $action ) );
35 if ($wgRequest->getVal( 'printable' ) == 'yes') {
36 $wgOut->setPrintable();
37 }
38
39 if ( '' == $title && 'delete' != $action ) {
40 $wgTitle = Title::newFromText( wfMsgForContent( 'mainpage' ) );
41 } elseif ( $curid = $wgRequest->getInt( 'curid' ) ) {
42 # URLs like this are generated by RC, because rc_title isn't always accurate
43 $wgTitle = Title::newFromID( $curid );
44 } else {
45 $wgTitle = Title::newFromURL( $title );
46 }
47 wfProfileOut( 'main-misc-setup' );
48
49 # Debug statement for user levels
50 // print_r($wgUser);
51
52 # If the user is not logged in, the Namespace:title of the article must be in
53 # the Read array in order for the user to see it. (We have to check here to
54 # catch special pages etc. We check again in Article::view())
55 if ( !is_null( $wgTitle ) && !$wgTitle->userCanRead() ) {
56 $wgOut->loginToUse();
57 $wgOut->output();
58 exit;
59 }
60
61 wfProfileIn( 'main-action' );
62
63 $search = $wgRequest->getText( 'search' );
64 if( !$wgDisableInternalSearch && !is_null( $search ) && $search !== '' ) {
65 require_once( 'includes/SpecialSearch.php' );
66 $wgTitle = Title::makeTitle( NS_SPECIAL, 'Search' );
67 wfSpecialSearch();
68 } else if( !$wgTitle or $wgTitle->getDBkey() == '' ) {
69 $wgTitle = Title::newFromText( wfMsgForContent( 'badtitle' ) );
70 $wgOut->errorpage( 'badtitle', 'badtitletext' );
71 } else if ( $wgTitle->getInterwiki() != '' ) {
72 if( $wgUseLatin1 ) {
73 # Conversion from UTF-8 may truncate or corrupt non-Latin links.
74 # Grab a fresh copy without doing the automated conversion checks.
75 $interwiki = Title::newFromUrl( $_REQUEST['title'] );
76 if( !is_null( $interwiki ) ) $wgTitle = $interwiki;
77 }
78 $url = $wgTitle->getFullURL();
79 # Check for a redirect loop
80 if ( !preg_match( '/^' . preg_quote( $wgServer, '/' ) . '/', $url ) && $wgTitle->isLocal() ) {
81 $wgOut->redirect( $url );
82 } else {
83 $wgTitle = Title::newFromText( wfMsgForContent( 'badtitle' ) );
84 $wgOut->errorpage( 'badtitle', 'badtitletext' );
85 }
86 } else if ( ( $action == 'view' ) &&
87 (!isset( $_GET['title'] ) || $wgTitle->getPrefixedDBKey() != $_GET['title'] ) &&
88 !count( array_diff( array_keys( $_GET ), array( 'action', 'title' ) ) ) )
89 {
90 /* redirect to canonical url, make it a 301 to allow caching */
91 $wgOut->setSquidMaxage( 1200 );
92 $wgOut->redirect( $wgTitle->getFullURL(), '301');
93 } else if ( NS_SPECIAL == $wgTitle->getNamespace() ) {
94 # actions that need to be made when we have a special pages
95 require_once( 'includes/SpecialPage.php' );
96 SpecialPage::executePath( $wgTitle );
97 } else {
98 if ( NS_MEDIA == $wgTitle->getNamespace() ) {
99 $wgTitle = Title::makeTitle( NS_IMAGE, $wgTitle->getDBkey() );
100 }
101
102 switch( $wgTitle->getNamespace() ) {
103 case NS_IMAGE:
104 require_once( 'includes/ImagePage.php' );
105 $wgArticle = new ImagePage( $wgTitle );
106 break;
107 case NS_CATEGORY:
108 if ( $wgUseCategoryMagic ) {
109 require_once( 'includes/CategoryPage.php' );
110 $wgArticle = new CategoryPage( $wgTitle );
111 break;
112 }
113 # NO break if wgUseCategoryMagic is false, drop through to next (default).
114 # Don't insert other cases between NS_CATEGORY and default.
115 default:
116 $wgArticle = new Article( $wgTitle );
117 }
118
119 switch( $action ) {
120 case 'view':
121 $wgOut->setSquidMaxage( $wgSquidMaxage );
122 $wgArticle->view();
123 break;
124 case 'watch':
125 case 'unwatch':
126 case 'delete':
127 case 'revert':
128 case 'rollback':
129 case 'protect':
130 case 'unprotect':
131 case 'info':
132 case 'markpatrolled':
133 $wgArticle->$action();
134 break;
135 case 'print':
136 $wgArticle->view();
137 break;
138 case 'dublincore':
139 if( !$wgEnableDublinCoreRdf ) {
140 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
141 } else {
142 require_once( 'includes/Metadata.php' );
143 wfDublinCoreRdf( $wgArticle );
144 }
145 break;
146 case 'creativecommons':
147 if( !$wgEnableCreativeCommonsRdf ) {
148 wfHttpError( 403, 'Forbidden', wfMsg('nocreativecommons') );
149 } else {
150 require_once( 'includes/Metadata.php' );
151 wfCreativeCommonsRdf( $wgArticle );
152 }
153 break;
154 case 'credits':
155 require_once( 'includes/Credits.php' );
156 showCreditsPage( $wgArticle );
157 break;
158 case 'submit':
159 if( !$wgCommandLineMode && !$wgRequest->checkSessionCookie() ) {
160 # Send a cookie so anons get talk message notifications
161 User::SetupSession();
162 }
163 # Continue...
164 case 'edit':
165 require_once( 'includes/EditPage.php' );
166 $editor = new EditPage( $wgArticle );
167 $editor->submit();
168 break;
169 case 'history':
170 if ($_SERVER['REQUEST_URI'] == $wgTitle->getInternalURL('action=history')) {
171 $wgOut->setSquidMaxage( $wgSquidMaxage );
172 }
173 require_once( 'includes/PageHistory.php' );
174 $history = new PageHistory( $wgArticle );
175 $history->history();
176 break;
177 case 'raw':
178 require_once( 'includes/RawPage.php' );
179 $raw = new RawPage( $wgArticle );
180 $raw->view();
181 break;
182 case 'purge':
183 wfPurgeSquidServers(array($wgTitle->getInternalURL()));
184 $wgOut->setSquidMaxage( $wgSquidMaxage );
185 $wgTitle->invalidateCache();
186 $wgArticle->view();
187 break;
188 default:
189 if (wfRunHooks('UnknownAction', $action, $wgArticle)) {
190 $wgOut->errorpage( 'nosuchaction', 'nosuchactiontext' );
191 }
192 }
193 }
194 wfProfileOut( 'main-action' );
195
196 # Deferred updates aren't really deferred anymore. It's important to report errors to the
197 # user, and that means doing this before OutputPage::output(). Note that for page saves,
198 # the client will wait until the script exits anyway before following the redirect.
199 wfProfileIn( 'main-updates' );
200 foreach ( $wgDeferredUpdateList as $up ) {
201 $up->doUpdate();
202 }
203 wfProfileOut( 'main-updates' );
204
205 wfProfileIn( 'main-cleanup' );
206 $wgLoadBalancer->saveMasterPos();
207
208 # Now commit any transactions, so that unreported errors after output() don't roll back the whole thing
209 $wgLoadBalancer->commitAll();
210
211 $wgOut->output();
212 wfProfileOut( 'main-cleanup' );
213
214 logProfilingData();
215 $wgLoadBalancer->closeAll();
216 wfDebug( "Request ended normally\n" );
217 ?>