Reorganization of SearchEngine for legibility
[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 # Placeholders in case of DB error
35 $wgTitle = Title::newFromText( wfMsgForContent( "badtitle" ) );
36 $wgArticle = new Article($wgTitle);
37
38 $action = strtolower( trim( $action ) );
39 if ($wgRequest->getVal( "printable" ) == "yes") {
40 $wgOut->setPrintable();
41 }
42
43 if ( "" == $title && "delete" != $action ) {
44 $wgTitle = Title::newFromText( wfMsgForContent( "mainpage" ) );
45 } elseif ( $curid = $wgRequest->getInt( 'curid' ) ) {
46 # URLs like this are generated by RC, because rc_title isn't always accurate
47 $wgTitle = Title::newFromID( $curid );
48 } else {
49 $wgTitle = Title::newFromURL( $title );
50 }
51 wfProfileOut( "main-misc-setup" );
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 $search = $wgRequest->getText( 'search' );
64 if( !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 $url = $wgTitle->getFullURL();
73 # Check for a redirect loop
74 if ( !preg_match( "/^" . preg_quote( $wgServer, "/" ) . "/", $url ) && $wgTitle->isLocal() ) {
75 $wgOut->redirect( $url );
76 } else {
77 $wgTitle = Title::newFromText( wfMsgForContent( "badtitle" ) );
78 $wgOut->errorpage( "badtitle", "badtitletext" );
79 }
80 } else if ( ( $action == "view" ) &&
81 (!isset( $_GET['title'] ) || $wgTitle->getPrefixedDBKey() != $_GET['title'] ) &&
82 !count( array_diff( array_keys( $_GET ), array( 'action', 'title' ) ) ) )
83 {
84 /* redirect to canonical url, make it a 301 to allow caching */
85 $wgOut->redirect( $wgTitle->getFullURL(), '301');
86 } else if ( Namespace::getSpecial() == $wgTitle->getNamespace() ) {
87 # actions that need to be made when we have a special pages
88 require_once( 'includes/SpecialPage.php' );
89 if ( !$wgAllowSysopQueries ) {SpecialPage::removePage( 'Asksql' ); }
90 SpecialPage::executePath( $wgTitle );
91 } else {
92 if ( Namespace::getMedia() == $wgTitle->getNamespace() ) {
93 $wgTitle = Title::makeTitle( NS_IMAGE, $wgTitle->getDBkey() );
94 }
95
96 switch( $wgTitle->getNamespace() ) {
97 case NS_IMAGE:
98 require_once( "includes/ImagePage.php" );
99 $wgArticle = new ImagePage( $wgTitle );
100 break;
101 case NS_CATEGORY:
102 if ( $wgUseCategoryMagic ) {
103 require_once( "includes/CategoryPage.php" );
104 $wgArticle = new CategoryPage( $wgTitle );
105 break;
106 }
107 # NO break if wgUseCategoryMagic is false, drop through to next (default).
108 # Don't insert other cases between NS_CATEGORY and default.
109 default:
110 $wgArticle = new Article( $wgTitle );
111 }
112
113 switch( $action ) {
114 case "view":
115 $wgOut->setSquidMaxage( $wgSquidMaxage );
116 $wgArticle->view();
117 break;
118 case "watch":
119 case "unwatch":
120 case "delete":
121 case "revert":
122 case "rollback":
123 case "protect":
124 case "unprotect":
125 case "validate":
126 case "info":
127 case "markpatrolled":
128 $wgArticle->$action();
129 break;
130 case "print":
131 $wgArticle->view();
132 break;
133 case "dublincore":
134 if( !$wgEnableDublinCoreRdf ) {
135 wfHttpError( 403, "Forbidden", wfMsg( "nodublincore" ) );
136 } else {
137 require_once( "includes/Metadata.php" );
138 wfDublinCoreRdf( $wgArticle );
139 }
140 break;
141 case "creativecommons":
142 if( !$wgEnableCreativeCommonsRdf ) {
143 wfHttpError( 403, "Forbidden", wfMsg("nocreativecommons") );
144 } else {
145 require_once( "includes/Metadata.php" );
146 wfCreativeCommonsRdf( $wgArticle );
147 }
148 break;
149 case "credits":
150 require_once( "includes/Credits.php" );
151 showCreditsPage( $wgArticle );
152 break;
153 case "edit":
154 case "submit":
155 if( !$wgCommandLineMode && !$wgRequest->checkSessionCookie() ) {
156 User::SetupSession();
157 }
158 require_once( "includes/EditPage.php" );
159 $editor = new EditPage( $wgArticle );
160 $editor->submit();
161 break;
162 case "history":
163 if ($_SERVER["REQUEST_URI"] == $wgTitle->getInternalURL('action=history')) {
164 $wgOut->setSquidMaxage( $wgSquidMaxage );
165 }
166 require_once( "includes/PageHistory.php" );
167 $history = new PageHistory( $wgArticle );
168 $history->history();
169 break;
170 case "raw":
171 require_once( "includes/RawPage.php" );
172 $raw = new RawPage( $wgArticle );
173 $raw->view();
174 break;
175 case "purge":
176 wfPurgeSquidServers(array($wgTitle->getInternalURL()));
177 $wgOut->setSquidMaxage( $wgSquidMaxage );
178 $wgTitle->invalidateCache();
179 $wgArticle->view();
180 break;
181 default:
182 $wgOut->errorpage( "nosuchaction", "nosuchactiontext" );
183 }
184 }
185 wfProfileOut( "main-action" );
186
187 # Deferred updates aren't really deferred anymore. It's important to report errors to the
188 # user, and that means doing this before OutputPage::output(). Note that for page saves,
189 # the client will wait until the script exits anyway before following the redirect.
190 wfProfileIn( "main-updates" );
191 foreach ( $wgDeferredUpdateList as $up ) {
192 $up->doUpdate();
193 }
194 wfProfileOut( "main-updates" );
195
196 wfProfileIn( "main-cleanup" );
197 $wgLoadBalancer->saveMasterPos();
198
199 # Now commit any transactions, so that unreported errors after output() don't roll back the whole thing
200 $wgLoadBalancer->commitAll();
201
202 $wgOut->output();
203 wfProfileOut( "main-cleanup" );
204
205 logProfilingData();
206 $wgLoadBalancer->closeAll();
207 wfDebug( "Request ended normally\n" );
208 ?>