Further cleanup of index.php
[lhc/web/wiklou.git] / includes / Wiki.php
1 <?php
2 /**
3 * MediaWiki is the to-be base class for this whole project
4 */
5
6 class MediaWiki {
7
8 var $GET; /* Stores the $_GET variables at time of creation, can be changed */
9 var $params = array();
10
11 /**
12 * Constructor
13 */
14 function MediaWiki () {
15 $this->GET = $_GET;
16 }
17
18 /**
19 * Stores key/value pairs to circumvent global variables
20 * Note that keys are case-insensitive!
21 */
22 function setVal( $key, &$value ) {
23 $key = strtolower( $key );
24 $this->params[$key] =& $value;
25 }
26
27 /**
28 * Retieves key/value pairs to circumvent global variables
29 * Note that keys are case-insensitive!
30 */
31 function getVal( $key, $default = "" ) {
32 $key = strtolower( $key );
33 if( isset( $this->params[$key] ) ) {
34 return $this->params[$key];
35 }
36 return $default;
37 }
38
39 /**
40 * Checks if the wiki is set up at all, or configured but not activated
41 */
42 function checkSetup() {
43 if ( file_exists( './LocalSettings.php' ) ) {
44 /* LocalSettings exists, commerce normally */
45 return;
46 }
47
48 /* LocalSettings is not in the right place, do something */
49 $IP = ".";
50 require_once( 'includes/DefaultSettings.php' ); # used for printing the version
51 $out = file_get_contents( "./setup_message.html" );
52 $out = str_replace( "$1", $wgVersion, $out );
53 if ( file_exists( 'config/LocalSettings.php' ) ) {
54 $msg = "To complete the installation, move <tt>config/LocalSettings.php</tt> to the parent directory.";
55 } else {
56 $msg = "Please <a href='config/index.php' title='setup'>setup the wiki</a> first.";
57 }
58 $out = str_replace( "$2", $msg, $out );
59 echo $out ;
60 die();
61 }
62
63 /**
64 * Initialization of ... everything
65 @return Article either the object to become $wgArticle, or NULL
66 */
67 function initialize ( &$title, &$output, &$user, $request) {
68 wfProfileIn( 'MediaWiki::initialize' );
69 $this->preliminaryChecks ( $title, $output, $request ) ;
70 $article = NULL;
71 if ( !$this->initializeSpecialCases( $title, $output, $request ) ) {
72 $article = $this->initializeArticle( $title, $request );
73 if( is_object( $article ) ) {
74 $this->performAction( $output, $article, $title, $user, $request );
75 } elseif( is_string( $article ) ) {
76 $output->redirect( $article );
77 } else {
78 wfDebugDieBacktrace( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
79 }
80 }
81 wfProfileOut( 'MediaWiki::initialize' );
82 return $article;
83 }
84
85 /**
86 * Checks some initial queries
87 * Note that $title here is *not* a Title object, but a string!
88 */
89 function checkInitialQueries( $title,$action,&$output,$request, $lang) {
90 wfProfileIn( 'MediaWiki::checkInitialQueries' );
91 if ($request->getVal( 'printable' ) == 'yes') {
92 $output->setPrintable();
93 }
94
95 $ret = NULL ;
96
97
98 if ( '' == $title && 'delete' != $action ) {
99 $ret = Title::newFromText( wfMsgForContent( 'mainpage' ) );
100 } elseif ( $curid = $request->getInt( 'curid' ) ) {
101 # URLs like this are generated by RC, because rc_title isn't always accurate
102 $ret = Title::newFromID( $curid );
103 } else {
104 $ret = Title::newFromURL( $title );
105 /* check variant links so that interwiki links don't have to worry about
106 the possible different language variants
107 */
108 if( count($lang->getVariants()) > 1 && !is_null($ret) && $ret->getArticleID() == 0 )
109 $lang->findVariantLink( $title, $ret );
110
111 }
112 wfProfileOut( 'MediaWiki::checkInitialQueries' );
113 return $ret ;
114 }
115
116 /**
117 * Checks for search query and anon-cannot-read case
118 */
119 function preliminaryChecks ( &$title, &$output, $request ) {
120
121 # Debug statement for user levels
122 // print_r($wgUser);
123
124 $search = $request->getText( 'search' );
125 if( !is_null( $search ) && $search !== '' ) {
126 // Compatibility with old search URLs which didn't use Special:Search
127 // Do this above the read whitelist check for security...
128 $title = Title::makeTitle( NS_SPECIAL, 'Search' );
129 }
130 $this->setVal( "Search", $search );
131
132 # If the user is not logged in, the Namespace:title of the article must be in
133 # the Read array in order for the user to see it. (We have to check here to
134 # catch special pages etc. We check again in Article::view())
135 if ( !is_null( $title ) && !$title->userCanRead() ) {
136 $output->loginToUse();
137 $output->output();
138 exit;
139 }
140
141 }
142
143 /**
144 * Initialize the object to be known as $wgArticle for special cases
145 */
146 function initializeSpecialCases ( &$title, &$output, $request ) {
147
148 wfProfileIn( 'MediaWiki::initializeSpecialCases' );
149
150 $search = $this->getVal('Search');
151 $action = $this->getVal('Action');
152 if( !$this->getVal('DisableInternalSearch') && !is_null( $search ) && $search !== '' ) {
153 require_once( 'includes/SpecialSearch.php' );
154 $title = Title::makeTitle( NS_SPECIAL, 'Search' );
155 wfSpecialSearch();
156 } else if( !$title or $title->getDBkey() == '' ) {
157 $title = Title::newFromText( wfMsgForContent( 'badtitle' ) );
158 $output->errorpage( 'badtitle', 'badtitletext' );
159 } else if ( $title->getInterwiki() != '' ) {
160 if( $rdfrom = $request->getVal( 'rdfrom' ) ) {
161 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
162 } else {
163 $url = $title->getFullURL();
164 }
165 /* Check for a redirect loop */
166 if ( !preg_match( '/^' . preg_quote( $this->getVal('Server'), '/' ) . '/', $url ) && $title->isLocal() ) {
167 $output->redirect( $url );
168 } else {
169 $title = Title::newFromText( wfMsgForContent( 'badtitle' ) );
170 $output->errorpage( 'badtitle', 'badtitletext' );
171 }
172 } else if ( ( $action == 'view' ) &&
173 (!isset( $this->GET['title'] ) || $title->getPrefixedDBKey() != $this->GET['title'] ) &&
174 !count( array_diff( array_keys( $this->GET ), array( 'action', 'title' ) ) ) )
175 {
176 /* Redirect to canonical url, make it a 301 to allow caching */
177 $output->setSquidMaxage( 1200 );
178 $output->redirect( $title->getFullURL(), '301');
179 } else if ( NS_SPECIAL == $title->getNamespace() ) {
180 /* actions that need to be made when we have a special pages */
181 SpecialPage::executePath( $title );
182 } else {
183 /* No match to special cases */
184 wfProfileOut( 'MediaWiki::initializeSpecialCases' );
185 return false;
186 }
187 /* Did match a special case */
188 wfProfileOut( 'MediaWiki::initializeSpecialCases' );
189 return true;
190 }
191
192 /**
193 * Create an Article object of the appropriate class for the given page.
194 * @param Title $title
195 * @return Article
196 */
197 function articleFromTitle( $title ) {
198 if( NS_MEDIA == $title->getNamespace() ) {
199 // FIXME: where should this go?
200 $title = Title::makeTitle( NS_IMAGE, $title->getDBkey() );
201 }
202
203 switch( $title->getNamespace() ) {
204 case NS_IMAGE:
205 require_once( 'includes/ImagePage.php' );
206 return new ImagePage( $title );
207 case NS_CATEGORY:
208 require_once( 'includes/CategoryPage.php' );
209 return new CategoryPage( $title );
210 default:
211 return new Article( $title );
212 }
213 }
214
215 /**
216 * Initialize the object to be known as $wgArticle for "standard" actions
217 * Create an Article object for the page, following redirects if needed.
218 * @param Title $title
219 * @param Request $request
220 * @param string $action
221 * @return mixed an Article, or a string to redirect to another URL
222 */
223 function initializeArticle( $title, $request ) {
224 wfProfileIn( 'MediaWiki::initializeArticle' );
225
226 $action = $this->getVal('Action');
227 $article = $this->articleFromTitle( $title );
228
229 // Namespace might change when using redirects
230 if( $action == 'view' && !$request->getVal( 'oldid' ) && $request->getVal( 'redirect' ) != 'no' ) {
231 $target = $article->followRedirect();
232 if( is_string( $target ) ) {
233 global $wgDisableHardRedirects;
234 if( !$wgDisableHardRedirects ) {
235 // we'll need to redirect
236 return $target;
237 }
238 }
239 if( is_object( $target ) ) {
240 // evil globals hack!
241 global $wgTitle;
242 $wgTitle = $target;
243
244 $article = $this->articleFromTitle( $target );
245 $article->setRedirectedFrom( $title );
246 }
247 }
248 wfProfileOut( 'MediaWiki::initializeArticle' );
249 return $article;
250 }
251
252 /**
253 * Cleaning up by doing deferred updates, calling loadbalancer and doing the output
254 */
255 function finalCleanup ( &$deferredUpdates, &$loadBalancer, &$output ) {
256 wfProfileIn( 'MediaWiki::finalCleanup' );
257 $this->doUpdates( $deferredUpdates );
258 $loadBalancer->saveMasterPos();
259 # Now commit any transactions, so that unreported errors after output() don't roll back the whole thing
260 $loadBalancer->commitAll();
261 $output->output();
262 wfProfileOut( 'MediaWiki::finalCleanup' );
263 }
264
265 /**
266 * Deferred updates aren't really deferred anymore. It's important to report errors to the
267 * user, and that means doing this before OutputPage::output(). Note that for page saves,
268 * the client will wait until the script exits anyway before following the redirect.
269 */
270 function doUpdates ( &$updates ) {
271 wfProfileIn( 'MediaWiki::doUpdates' );
272 foreach( $updates as $up ) {
273 $up->doUpdate();
274 }
275 wfProfileOut( 'MediaWiki::doUpdates' );
276 }
277
278 /**
279 * Ends this task peacefully
280 */
281 function restInPeace ( &$loadBalancer ) {
282 wfProfileClose();
283 logProfilingData();
284 $loadBalancer->closeAll();
285 wfDebug( "Request ended normally\n" );
286 }
287
288 /**
289 * Perform one of the "standard" actions
290 */
291 function performAction( &$output, &$article, &$title, &$user, &$request ) {
292
293 wfProfileIn( 'MediaWiki::performAction' );
294
295 $action = $this->getVal('Action');
296 if( in_array( $action, $this->getVal('DisabledActions',array()) ) ) {
297 /* No such action; this will switch to the default case */
298 $action = "nosuchaction";
299 }
300
301 switch( $action ) {
302 case 'view':
303 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
304 $article->view();
305 break;
306 case 'watch':
307 case 'unwatch':
308 case 'delete':
309 case 'revert':
310 case 'rollback':
311 case 'protect':
312 case 'unprotect':
313 case 'info':
314 case 'markpatrolled':
315 case 'validate':
316 case 'render':
317 case 'deletetrackback':
318 case 'purge':
319 $article->$action();
320 break;
321 case 'print':
322 $article->view();
323 break;
324 case 'dublincore':
325 if( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
326 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
327 } else {
328 require_once( 'includes/Metadata.php' );
329 wfDublinCoreRdf( $article );
330 }
331 break;
332 case 'creativecommons':
333 if( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
334 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
335 } else {
336 require_once( 'includes/Metadata.php' );
337 wfCreativeCommonsRdf( $article );
338 }
339 break;
340 case 'credits':
341 require_once( 'includes/Credits.php' );
342 showCreditsPage( $article );
343 break;
344 case 'submit':
345 if( !$this->getVal( 'CommandLineMode' ) && !$request->checkSessionCookie() ) {
346 /* Send a cookie so anons get talk message notifications */
347 User::SetupSession();
348 }
349 /* Continue... */
350 case 'edit':
351 $internal = $request->getVal( 'internaledit' );
352 $external = $request->getVal( 'externaledit' );
353 $section = $request->getVal( 'section' );
354 $oldid = $request->getVal( 'oldid' );
355 if( !$this->getVal( 'UseExternalEditor' ) || $action=='submit' || $internal ||
356 $section || $oldid || ( !$user->getOption( 'externaleditor' ) && !$external ) ) {
357 require_once( 'includes/EditPage.php' );
358 $editor = new EditPage( $article );
359 $editor->submit();
360 } elseif( $this->getVal( 'UseExternalEditor' ) && ( $external || $user->getOption( 'externaleditor' ) ) ) {
361 require_once( 'includes/ExternalEdit.php' );
362 $mode = $request->getVal( 'mode' );
363 $extedit = new ExternalEdit( $article, $mode );
364 $extedit->edit();
365 }
366 break;
367 case 'history':
368 if( $_SERVER['REQUEST_URI'] == $title->getInternalURL( 'action=history' ) ) {
369 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
370 }
371 require_once( 'includes/PageHistory.php' );
372 $history = new PageHistory( $article );
373 $history->history();
374 break;
375 case 'raw':
376 require_once( 'includes/RawPage.php' );
377 $raw = new RawPage( $article );
378 $raw->view();
379 break;
380 default:
381 if( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
382 $output->errorpage( 'nosuchaction', 'nosuchactiontext' );
383 }
384 wfProfileOut( 'MediaWiki::performAction' );
385 }
386
387
388 }
389
390 }; /* End of class MediaWiki */
391
392 ?>