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