* (bug 3000) Fall back to SCRIPT_NAME plus QUERY_STRING when REQUEST_URI is
[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 * Retrieves 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 * Initialization of ... everything
41 @return Article either the object to become $wgArticle, or NULL
42 */
43 function initialize ( &$title, &$output, &$user, $request) {
44 wfProfileIn( 'MediaWiki::initialize' );
45 $this->preliminaryChecks ( $title, $output, $request ) ;
46 $article = NULL;
47 if ( !$this->initializeSpecialCases( $title, $output, $request ) ) {
48 $article = $this->initializeArticle( $title, $request );
49 if( is_object( $article ) ) {
50 $this->performAction( $output, $article, $title, $user, $request );
51 } elseif( is_string( $article ) ) {
52 $output->redirect( $article );
53 } else {
54 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
55 }
56 }
57 wfProfileOut( 'MediaWiki::initialize' );
58 return $article;
59 }
60
61 /**
62 * Checks some initial queries
63 * Note that $title here is *not* a Title object, but a string!
64 */
65 function checkInitialQueries( $title,$action,&$output,$request, $lang) {
66 if ($request->getVal( 'printable' ) == 'yes') {
67 $output->setPrintable();
68 }
69
70 $ret = NULL ;
71
72
73 if ( '' == $title && 'delete' != $action ) {
74 $ret = Title::newMainPage();
75 } elseif ( $curid = $request->getInt( 'curid' ) ) {
76 # URLs like this are generated by RC, because rc_title isn't always accurate
77 $ret = Title::newFromID( $curid );
78 } else {
79 $ret = Title::newFromURL( $title );
80 /* check variant links so that interwiki links don't have to worry about
81 the possible different language variants
82 */
83 if( count($lang->getVariants()) > 1 && !is_null($ret) && $ret->getArticleID() == 0 )
84 $lang->findVariantLink( $title, $ret );
85
86 }
87 return $ret ;
88 }
89
90 /**
91 * Checks for search query and anon-cannot-read case
92 */
93 function preliminaryChecks ( &$title, &$output, $request ) {
94
95 # Debug statement for user levels
96 // print_r($wgUser);
97
98 $search = $request->getText( 'search' );
99 if( !is_null( $search ) && $search !== '' ) {
100 // Compatibility with old search URLs which didn't use Special:Search
101 // Do this above the read whitelist check for security...
102 $title = SpecialPage::getTitleFor( 'Search' );
103 }
104 $this->setVal( 'Search', $search );
105
106 # If the user is not logged in, the Namespace:title of the article must be in
107 # the Read array in order for the user to see it. (We have to check here to
108 # catch special pages etc. We check again in Article::view())
109 if ( !is_null( $title ) && !$title->userCanRead() ) {
110 $output->loginToUse();
111 $output->output();
112 exit;
113 }
114
115 }
116
117 /**
118 * Initialize the object to be known as $wgArticle for special cases
119 */
120 function initializeSpecialCases ( &$title, &$output, $request ) {
121 global $wgRequest;
122 wfProfileIn( 'MediaWiki::initializeSpecialCases' );
123
124 $search = $this->getVal('Search');
125 $action = $this->getVal('Action');
126 if( !$this->getVal('DisableInternalSearch') && !is_null( $search ) && $search !== '' ) {
127 require_once( 'includes/SpecialSearch.php' );
128 $title = SpecialPage::getTitleFor( 'Search' );
129 wfSpecialSearch();
130 } else if( !$title or $title->getDBkey() == '' ) {
131 $title = SpecialPage::getTitleFor( 'Badtitle' );
132 # Die now before we mess up $wgArticle and the skin stops working
133 throw new ErrorPageError( 'badtitle', 'badtitletext' );
134 } else if ( $title->getInterwiki() != '' ) {
135 if( $rdfrom = $request->getVal( 'rdfrom' ) ) {
136 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
137 } else {
138 $url = $title->getFullURL();
139 }
140 /* Check for a redirect loop */
141 if ( !preg_match( '/^' . preg_quote( $this->getVal('Server'), '/' ) . '/', $url ) && $title->isLocal() ) {
142 $output->redirect( $url );
143 } else {
144 $title = SpecialPage::getTitleFor( 'Badtitle' );
145 throw new ErrorPageError( 'badtitle', 'badtitletext' );
146 }
147 } else if ( ( $action == 'view' ) &&
148 (!isset( $this->GET['title'] ) || $title->getPrefixedDBKey() != $this->GET['title'] ) &&
149 !count( array_diff( array_keys( $this->GET ), array( 'action', 'title' ) ) ) )
150 {
151 $targetUrl = $title->getFullURL();
152 // Redirect to canonical url, make it a 301 to allow caching
153 global $wgServer, $wgUsePathInfo;
154 if( $targetUrl == $wgRequest->getFullRequestURL() ) {
155 $message = "Redirect loop detected!\n\n" .
156 "This means the wiki got confused about what page was " .
157 "requested; this sometimes happens when moving a wiki " .
158 "to a new server or changing the server configuration.\n\n";
159
160 if( $wgUsePathInfo ) {
161 $message .= "The wiki is trying to interpret the page " .
162 "title from the URL path portion (PATH_INFO), which " .
163 "sometimes fails depending on the web server. Try " .
164 "setting \"\$wgUsePathInfo = false;\" in your " .
165 "LocalSettings.php, or check that \$wgArticlePath " .
166 "is correct.";
167 } else {
168 $message .= "Your web server was detected as possibly not " .
169 "supporting URL path components (PATH_INFO) correctly; " .
170 "check your LocalSettings.php for a customized " .
171 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
172 "to true.";
173 }
174 wfHttpError( 500, "Internal error", $message );
175 return false;
176 } else {
177 $output->setSquidMaxage( 1200 );
178 $output->redirect( $targetUrl, '301');
179 }
180 } else if ( NS_SPECIAL == $title->getNamespace() ) {
181 /* actions that need to be made when we have a special pages */
182 SpecialPage::executePath( $title );
183 } else {
184 /* No match to special cases */
185 wfProfileOut( 'MediaWiki::initializeSpecialCases' );
186 return false;
187 }
188 /* Did match a special case */
189 wfProfileOut( 'MediaWiki::initializeSpecialCases' );
190 return true;
191 }
192
193 /**
194 * Create an Article object of the appropriate class for the given page.
195 * @param Title $title
196 * @return Article
197 */
198 function articleFromTitle( $title ) {
199 $article = null;
200 wfRunHooks('ArticleFromTitle', array( &$title, &$article ) );
201 if ( $article ) {
202 return $article;
203 }
204
205 if( NS_MEDIA == $title->getNamespace() ) {
206 // FIXME: where should this go?
207 $title = Title::makeTitle( NS_IMAGE, $title->getDBkey() );
208 }
209
210 switch( $title->getNamespace() ) {
211 case NS_IMAGE:
212 return new ImagePage( $title );
213 case NS_CATEGORY:
214 return new CategoryPage( $title );
215 default:
216 return new Article( $title );
217 }
218 }
219
220 /**
221 * Initialize the object to be known as $wgArticle for "standard" actions
222 * Create an Article object for the page, following redirects if needed.
223 * @param Title $title
224 * @param Request $request
225 * @param string $action
226 * @return mixed an Article, or a string to redirect to another URL
227 */
228 function initializeArticle( $title, $request ) {
229 global $wgTitle;
230 wfProfileIn( 'MediaWiki::initializeArticle' );
231
232 $action = $this->getVal('Action');
233 $article = $this->articleFromTitle( $title );
234
235 // Namespace might change when using redirects
236 if( $action == 'view' && !$request->getVal( 'oldid' ) &&
237 $request->getVal( 'redirect' ) != 'no' ) {
238
239 $dbr =& wfGetDB(DB_SLAVE);
240 $article->loadPageData($article->pageDataFromTitle($dbr, $title));
241
242 /* Follow redirects only for... redirects */
243 if ($article->mIsRedirect) {
244 $target = $article->followRedirect();
245 if( is_string( $target ) ) {
246 global $wgDisableHardRedirects;
247 if( !$wgDisableHardRedirects ) {
248 // we'll need to redirect
249 return $target;
250 }
251 }
252 if( is_object( $target ) ) {
253 /* Rewrite environment to redirected article */
254 $rarticle = $this->articleFromTitle($target);
255 $rarticle->loadPageData($rarticle->pageDataFromTitle($dbr,$target));
256 if ($rarticle->mTitle->mArticleID) {
257 $article = $rarticle;
258 $wgTitle = $target;
259 $article->setRedirectedFrom( $title );
260 } else {
261 $wgTitle = $title;
262 }
263 }
264 } else {
265 $wgTitle = $article->mTitle;
266 }
267 }
268 wfProfileOut( 'MediaWiki::initializeArticle' );
269 return $article;
270 }
271
272 /**
273 * Cleaning up by doing deferred updates, calling loadbalancer and doing the output
274 */
275 function finalCleanup ( &$deferredUpdates, &$loadBalancer, &$output ) {
276 wfProfileIn( 'MediaWiki::finalCleanup' );
277 $this->doUpdates( $deferredUpdates );
278 $this->doJobs();
279 $loadBalancer->saveMasterPos();
280 # Now commit any transactions, so that unreported errors after output() don't roll back the whole thing
281 $loadBalancer->commitAll();
282 $output->output();
283 wfProfileOut( 'MediaWiki::finalCleanup' );
284 }
285
286 /**
287 * Deferred updates aren't really deferred anymore. It's important to report errors to the
288 * user, and that means doing this before OutputPage::output(). Note that for page saves,
289 * the client will wait until the script exits anyway before following the redirect.
290 */
291 function doUpdates ( &$updates ) {
292 wfProfileIn( 'MediaWiki::doUpdates' );
293 $dbw =& wfGetDB( DB_MASTER );
294 foreach( $updates as $up ) {
295 $up->doUpdate();
296
297 # Commit after every update to prevent lock contention
298 if ( $dbw->trxLevel() ) {
299 $dbw->commit();
300 }
301 }
302 wfProfileOut( 'MediaWiki::doUpdates' );
303 }
304
305 /**
306 * Do a job from the job queue
307 */
308 function doJobs() {
309 global $wgJobRunRate;
310
311 if ( $wgJobRunRate <= 0 || wfReadOnly() ) {
312 return;
313 }
314 if ( $wgJobRunRate < 1 ) {
315 $max = mt_getrandmax();
316 if ( mt_rand( 0, $max ) > $max * $wgJobRunRate ) {
317 return;
318 }
319 $n = 1;
320 } else {
321 $n = intval( $wgJobRunRate );
322 }
323
324 while ( $n-- && false != ($job = Job::pop())) {
325 $output = $job->toString() . "\n";
326 $t = -wfTime();
327 $success = $job->run();
328 $t += wfTime();
329 $t = round( $t*1000 );
330 if ( !$success ) {
331 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
332 } else {
333 $output .= "Success, Time: $t ms\n";
334 }
335 wfDebugLog( 'jobqueue', $output );
336 }
337 }
338
339 /**
340 * Ends this task peacefully
341 */
342 function restInPeace ( &$loadBalancer ) {
343 wfLogProfilingData();
344 $loadBalancer->closeAll();
345 wfDebug( "Request ended normally\n" );
346 }
347
348 /**
349 * Perform one of the "standard" actions
350 */
351 function performAction( &$output, &$article, &$title, &$user, &$request ) {
352
353 wfProfileIn( 'MediaWiki::performAction' );
354
355 $action = $this->getVal('Action');
356 if( in_array( $action, $this->getVal('DisabledActions',array()) ) ) {
357 /* No such action; this will switch to the default case */
358 $action = 'nosuchaction';
359 }
360
361 switch( $action ) {
362 case 'view':
363 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
364 $article->view();
365 break;
366 case 'watch':
367 case 'unwatch':
368 case 'delete':
369 case 'revert':
370 case 'rollback':
371 case 'protect':
372 case 'unprotect':
373 case 'info':
374 case 'markpatrolled':
375 case 'render':
376 case 'deletetrackback':
377 case 'purge':
378 $article->$action();
379 break;
380 case 'print':
381 $article->view();
382 break;
383 case 'dublincore':
384 if( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
385 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
386 } else {
387 require_once( 'includes/Metadata.php' );
388 wfDublinCoreRdf( $article );
389 }
390 break;
391 case 'creativecommons':
392 if( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
393 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
394 } else {
395 require_once( 'includes/Metadata.php' );
396 wfCreativeCommonsRdf( $article );
397 }
398 break;
399 case 'credits':
400 require_once( 'includes/Credits.php' );
401 showCreditsPage( $article );
402 break;
403 case 'submit':
404 if( !$this->getVal( 'CommandLineMode' ) && !$request->checkSessionCookie() ) {
405 /* Send a cookie so anons get talk message notifications */
406 User::SetupSession();
407 }
408 /* Continue... */
409 case 'edit':
410 $internal = $request->getVal( 'internaledit' );
411 $external = $request->getVal( 'externaledit' );
412 $section = $request->getVal( 'section' );
413 $oldid = $request->getVal( 'oldid' );
414 if( !$this->getVal( 'UseExternalEditor' ) || $action=='submit' || $internal ||
415 $section || $oldid || ( !$user->getOption( 'externaleditor' ) && !$external ) ) {
416 $editor = new EditPage( $article );
417 $editor->submit();
418 } elseif( $this->getVal( 'UseExternalEditor' ) && ( $external || $user->getOption( 'externaleditor' ) ) ) {
419 $mode = $request->getVal( 'mode' );
420 $extedit = new ExternalEdit( $article, $mode );
421 $extedit->edit();
422 }
423 break;
424 case 'history':
425 if( $_SERVER['REQUEST_URI'] == $title->getInternalURL( 'action=history' ) ) {
426 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
427 }
428 $history = new PageHistory( $article );
429 $history->history();
430 break;
431 case 'raw':
432 $raw = new RawPage( $article );
433 $raw->view();
434 break;
435 default:
436 if( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
437 $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
438 }
439 }
440 wfProfileOut( 'MediaWiki::performAction' );
441
442
443 }
444
445 }; /* End of class MediaWiki */
446
447 ?>