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