(29 lines skipped) [17-Jun-2011 10:03:59] PHP Notice: Use of RequestContext::__get...
[lhc/web/wiklou.git] / includes / Wiki.php
1 <?php
2 /**
3 * MediaWiki is the to-be base class for this whole project
4 *
5 * @internal documentation reviewed 15 Mar 2010
6 */
7 class MediaWiki {
8
9 /**
10 * TODO: fold $output, etc, into this
11 * @var RequestContext
12 */
13 private $context;
14
15 public function request( WebRequest $x = null ){
16 return wfSetVar( $this->context->request, $x );
17 }
18
19 public function output( OutputPage $x = null ){
20 return wfSetVar( $this->context->output, $x );
21 }
22
23 public function __construct( RequestContext $context ){
24 $this->context = $context;
25 $this->context->setTitle( $this->parseTitle() );
26 }
27
28 /**
29 * Parse $request to get the Title object
30 *
31 * @return Title object to be $wgTitle
32 */
33 private function parseTitle() {
34 global $wgContLang;
35
36 $request = $this->context->getRequest();
37 $curid = $request->getInt( 'curid' );
38 $title = $request->getVal( 'title' );
39
40 if ( $request->getCheck( 'search' ) ) {
41 // Compatibility with old search URLs which didn't use Special:Search
42 // Just check for presence here, so blank requests still
43 // show the search page when using ugly URLs (bug 8054).
44 $ret = SpecialPage::getTitleFor( 'Search' );
45 } elseif ( $curid ) {
46 // URLs like this are generated by RC, because rc_title isn't always accurate
47 $ret = Title::newFromID( $curid );
48 } elseif ( $title == '' && $this->getAction() != 'delete' ) {
49 $ret = Title::newMainPage();
50 } else {
51 $ret = Title::newFromURL( $title );
52 // check variant links so that interwiki links don't have to worry
53 // about the possible different language variants
54 if ( count( $wgContLang->getVariants() ) > 1 && !is_null( $ret ) && $ret->getArticleID() == 0 ){
55 $wgContLang->findVariantLink( $title, $ret );
56 }
57 }
58 // For non-special titles, check for implicit titles
59 if ( is_null( $ret ) || $ret->getNamespace() != NS_SPECIAL ) {
60 // We can have urls with just ?diff=,?oldid= or even just ?diff=
61 $oldid = $request->getInt( 'oldid' );
62 $oldid = $oldid ? $oldid : $request->getInt( 'diff' );
63 // Allow oldid to override a changed or missing title
64 if ( $oldid ) {
65 $rev = Revision::newFromId( $oldid );
66 $ret = $rev ? $rev->getTitle() : $ret;
67 }
68 }
69
70 if( $ret === null || ( $ret->getDBkey() == '' && $ret->getInterwiki() == '' ) ){
71 $ret = new BadTitle;
72 }
73 return $ret;
74 }
75
76 /**
77 * Get the Title object that we'll be acting on, as specified in the WebRequest
78 * @return Title
79 */
80 public function getTitle(){
81 if( $this->context->title === null ){
82 $this->context->title = $this->parseTitle();
83 }
84 return $this->context->title;
85 }
86
87 /**
88 * Performs the request.
89 * - bad titles
90 * - read restriction
91 * - local interwiki redirects
92 * - redirect loop
93 * - special pages
94 * - normal pages
95 *
96 * @return Article object
97 */
98 public function performRequest() {
99 global $wgServer, $wgUsePathInfo;
100
101 wfProfileIn( __METHOD__ );
102
103 if ( $this->context->request->getVal( 'printable' ) === 'yes' ) {
104 $this->context->output->setPrintable();
105 }
106
107 wfRunHooks( 'BeforeInitialize', array(
108 &$this->context->title,
109 null,
110 &$this->context->output,
111 &$this->context->user,
112 $this->context->request,
113 $this
114 ) );
115
116 // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty.
117 if ( $this->context->title instanceof BadTitle ) {
118 throw new ErrorPageError( 'badtitle', 'badtitletext' );
119 // If the user is not logged in, the Namespace:title of the article must be in
120 // the Read array in order for the user to see it. (We have to check here to
121 // catch special pages etc. We check again in Article::view())
122 } else if ( !$this->context->title->userCanRead() ) {
123 $this->context->output->loginToUse();
124 // Interwiki redirects
125 } else if ( $this->context->title->getInterwiki() != '' ) {
126 $rdfrom = $this->context->request->getVal( 'rdfrom' );
127 if ( $rdfrom ) {
128 $url = $this->context->title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
129 } else {
130 $query = $this->context->request->getValues();
131 unset( $query['title'] );
132 $url = $this->context->title->getFullURL( $query );
133 }
134 // Check for a redirect loop
135 if ( !preg_match( '/^' . preg_quote( $wgServer, '/' ) . '/', $url ) && $this->context->title->isLocal() ) {
136 // 301 so google et al report the target as the actual url.
137 $this->context->output->redirect( $url, 301 );
138 } else {
139 $this->context->title = new BadTitle;
140 wfProfileOut( __METHOD__ );
141 throw new ErrorPageError( 'badtitle', 'badtitletext' );
142 }
143 // Redirect loops, no title in URL, $wgUsePathInfo URLs, and URLs with a variant
144 } else if ( $this->context->request->getVal( 'action', 'view' ) == 'view' && !$this->context->request->wasPosted()
145 && ( $this->context->request->getVal( 'title' ) === null || $this->context->title->getPrefixedDBKey() != $this->context->request->getVal( 'title' ) )
146 && !count( array_diff( array_keys( $this->context->request->getValues() ), array( 'action', 'title' ) ) ) )
147 {
148 if ( $this->context->title->getNamespace() == NS_SPECIAL ) {
149 list( $name, $subpage ) = SpecialPageFactory::resolveAlias( $this->context->title->getDBkey() );
150 if ( $name ) {
151 $this->context->title = SpecialPage::getTitleFor( $name, $subpage );
152 }
153 }
154 $targetUrl = $this->context->title->getFullURL();
155 // Redirect to canonical url, make it a 301 to allow caching
156 if ( $targetUrl == $this->context->request->getFullRequestURL() ) {
157 $message = "Redirect loop detected!\n\n" .
158 "This means the wiki got confused about what page was " .
159 "requested; this sometimes happens when moving a wiki " .
160 "to a new server or changing the server configuration.\n\n";
161
162 if ( $wgUsePathInfo ) {
163 $message .= "The wiki is trying to interpret the page " .
164 "title from the URL path portion (PATH_INFO), which " .
165 "sometimes fails depending on the web server. Try " .
166 "setting \"\$wgUsePathInfo = false;\" in your " .
167 "LocalSettings.php, or check that \$wgArticlePath " .
168 "is correct.";
169 } else {
170 $message .= "Your web server was detected as possibly not " .
171 "supporting URL path components (PATH_INFO) correctly; " .
172 "check your LocalSettings.php for a customized " .
173 "\$wgArticlePath setting and/or toggle \$wgUsePathInfo " .
174 "to true.";
175 }
176 wfHttpError( 500, "Internal error", $message );
177 } else {
178 $this->context->output->setSquidMaxage( 1200 );
179 $this->context->output->redirect( $targetUrl, '301' );
180 }
181 // Special pages
182 } else if ( NS_SPECIAL == $this->context->title->getNamespace() ) {
183 // actions that need to be made when we have a special pages
184 SpecialPageFactory::executePath( $this->context->title, $this->context );
185 } else {
186 // ...otherwise treat it as an article view. The article
187 // may be a redirect to another article or URL.
188 $article = $this->initializeArticle();
189 if ( is_object( $article ) ) {
190 $this->performAction( $article );
191 wfProfileOut( __METHOD__ );
192 return $article;
193 } elseif ( is_string( $article ) ) {
194 $this->context->output->redirect( $article );
195 } else {
196 wfProfileOut( __METHOD__ );
197 throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
198 }
199 }
200 wfProfileOut( __METHOD__ );
201 }
202
203 /**
204 * Create an Article object of the appropriate class for the given page.
205 *
206 * @deprecated in 1.19; use Article::newFromTitle() instead
207 * @param $title Title
208 * @param $context RequestContext
209 * @return Article object
210 */
211 public static function articleFromTitle( $title, RequestContext $context ) {
212 return Article::newFromTitle( $title, $context );
213 }
214
215 /**
216 * Returns the action that will be executed, not necesserly the one passed
217 * passed through the "action" parameter. Actions disabled in
218 * $wgDisabledActions will be replaced by "nosuchaction"
219 *
220 * @return String: action
221 */
222 public function getAction() {
223 global $wgDisabledActions;
224
225 $action = $this->context->request->getVal( 'action', 'view' );
226
227 // Check for disabled actions
228 if ( in_array( $action, $wgDisabledActions ) ) {
229 return 'nosuchaction';
230 }
231
232 // Workaround for bug #20966: inability of IE to provide an action dependent
233 // on which submit button is clicked.
234 if ( $action === 'historysubmit' ) {
235 if ( $this->context->request->getBool( 'revisiondelete' ) ) {
236 return 'revisiondelete';
237 } else {
238 return 'view';
239 }
240 } elseif ( $action == 'editredlink' ) {
241 return 'edit';
242 }
243
244 return $action;
245 }
246
247 /**
248 * Initialize the main Article object for "standard" actions (view, etc)
249 * Create an Article object for the page, following redirects if needed.
250 *
251 * @return mixed an Article, or a string to redirect to another URL
252 */
253 private function initializeArticle() {
254 global $wgDisableHardRedirects;
255
256 wfProfileIn( __METHOD__ );
257
258 $action = $this->context->request->getVal( 'action', 'view' );
259 $article = Article::newFromTitle( $this->context->title, $this->context );
260 // NS_MEDIAWIKI has no redirects.
261 // It is also used for CSS/JS, so performance matters here...
262 if ( $this->context->title->getNamespace() == NS_MEDIAWIKI ) {
263 wfProfileOut( __METHOD__ );
264 return $article;
265 }
266 // Namespace might change when using redirects
267 // Check for redirects ...
268 $file = ( $this->context->title->getNamespace() == NS_FILE ) ? $article->getFile() : null;
269 if ( ( $action == 'view' || $action == 'render' ) // ... for actions that show content
270 && !$this->context->request->getVal( 'oldid' ) && // ... and are not old revisions
271 !$this->context->request->getVal( 'diff' ) && // ... and not when showing diff
272 $this->context->request->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to
273 // ... and the article is not a non-redirect image page with associated file
274 !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) )
275 {
276 // Give extensions a change to ignore/handle redirects as needed
277 $ignoreRedirect = $target = false;
278
279 wfRunHooks( 'InitializeArticleMaybeRedirect',
280 array( &$this->context->title, &$this->context->request, &$ignoreRedirect, &$target, &$article ) );
281
282 // Follow redirects only for... redirects.
283 // If $target is set, then a hook wanted to redirect.
284 if ( !$ignoreRedirect && ( $target || $article->isRedirect() ) ) {
285 // Is the target already set by an extension?
286 $target = $target ? $target : $article->followRedirect();
287 if ( is_string( $target ) ) {
288 if ( !$wgDisableHardRedirects ) {
289 // we'll need to redirect
290 wfProfileOut( __METHOD__ );
291 return $target;
292 }
293 }
294 if ( is_object( $target ) ) {
295 // Rewrite environment to redirected article
296 $rarticle = Article::newFromTitle( $target, $this->context );
297 $rarticle->loadPageData();
298 if ( $rarticle->exists() || ( is_object( $file ) && !$file->isLocal() ) ) {
299 $rarticle->setRedirectedFrom( $this->context->title );
300 $article = $rarticle;
301 $this->context->title = $target;
302 }
303 }
304 } else {
305 $this->context->title = $article->getTitle();
306 }
307 }
308 wfProfileOut( __METHOD__ );
309 return $article;
310 }
311
312 /**
313 * Cleaning up request by doing deferred updates, DB transaction, and the output
314 */
315 public function finalCleanup() {
316 wfProfileIn( __METHOD__ );
317 // Now commit any transactions, so that unreported errors after
318 // output() don't roll back the whole DB transaction
319 $factory = wfGetLBFactory();
320 $factory->commitMasterChanges();
321 // Output everything!
322 $this->context->output->output();
323 // Do any deferred jobs
324 wfDoUpdates( 'commit' );
325
326 $this->doJobs();
327 wfProfileOut( __METHOD__ );
328 }
329
330 /**
331 * Do a job from the job queue
332 */
333 private function doJobs() {
334 global $wgJobRunRate;
335
336 if ( $wgJobRunRate <= 0 || wfReadOnly() ) {
337 return;
338 }
339 if ( $wgJobRunRate < 1 ) {
340 $max = mt_getrandmax();
341 if ( mt_rand( 0, $max ) > $max * $wgJobRunRate ) {
342 return;
343 }
344 $n = 1;
345 } else {
346 $n = intval( $wgJobRunRate );
347 }
348
349 // Close the session so that jobs don't access the current session
350 $this->shutdownLBFactory();
351 session_write_close();
352
353 while ( $n-- && false != ( $job = Job::pop() ) ) {
354 $output = $job->toString() . "\n";
355 $t = -wfTime();
356 $success = $job->run();
357 $t += wfTime();
358 $t = round( $t * 1000 );
359 if ( !$success ) {
360 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
361 } else {
362 $output .= "Success, Time: $t ms\n";
363 }
364 wfDebugLog( 'jobqueue', $output );
365 }
366 }
367
368 /**
369 * Ends this task peacefully
370 */
371 public function restInPeace() {
372 MessageCache::logMessages();
373 wfLogProfilingData();
374 $this->shutdownLBFactory();
375 wfDebug( "Request ended normally\n" );
376 }
377
378 /**
379 * Commit pending master changes, shutdown the current loadbalancer
380 * factory and destroys the factory instance.
381 */
382 private function shutdownLBFactory() {
383 // Commit and close up!
384 $factory = LBFactory::singleton();
385 $factory->commitMasterChanges();
386 $factory->shutdown();
387 LBFactory::destroyInstance();
388 }
389
390 /**
391 * Perform one of the "standard" actions
392 *
393 * @param $article Article
394 */
395 private function performAction( $article ) {
396 global $wgSquidMaxage, $wgUseExternalEditor;
397
398 wfProfileIn( __METHOD__ );
399
400 if ( !wfRunHooks( 'MediaWikiPerformAction', array(
401 $this->context->output, $article, $this->context->title,
402 $this->context->user, $this->context->request, $this ) ) )
403 {
404 wfProfileOut( __METHOD__ );
405 return;
406 }
407
408 $act = $this->getAction();
409
410 $action = Action::factory( $act, $article );
411 if( $action instanceof Action ){
412 $action->show();
413 wfProfileOut( __METHOD__ );
414 return;
415 }
416
417 switch( $act ) {
418 case 'view':
419 $this->context->output->setSquidMaxage( $wgSquidMaxage );
420 $article->view();
421 break;
422 case 'raw': // includes JS/CSS
423 wfProfileIn( __METHOD__ . '-raw' );
424 $raw = new RawPage( $article );
425 $raw->view();
426 wfProfileOut( __METHOD__ . '-raw' );
427 break;
428 case 'delete':
429 case 'revert':
430 case 'rollback':
431 case 'protect':
432 case 'unprotect':
433 case 'info':
434 case 'render':
435 $article->$act();
436 break;
437 case 'submit':
438 if ( session_id() == '' ) {
439 // Send a cookie so anons get talk message notifications
440 wfSetupSession();
441 }
442 // Continue...
443 case 'edit':
444 if ( wfRunHooks( 'CustomEditor', array( $article, $this->context->user ) ) ) {
445 $internal = $this->context->request->getVal( 'internaledit' );
446 $external = $this->context->request->getVal( 'externaledit' );
447 $section = $this->context->request->getVal( 'section' );
448 $oldid = $this->context->request->getVal( 'oldid' );
449 if ( !$wgUseExternalEditor || $act == 'submit' || $internal ||
450 $section || $oldid || ( !$this->context->user->getOption( 'externaleditor' ) && !$external ) ) {
451 $editor = new EditPage( $article );
452 $editor->submit();
453 } elseif ( $wgUseExternalEditor && ( $external || $this->context->user->getOption( 'externaleditor' ) ) ) {
454 $mode = $this->context->request->getVal( 'mode' );
455 $extedit = new ExternalEdit( $article, $mode );
456 $extedit->edit();
457 }
458 }
459 break;
460 case 'history':
461 if ( $this->context->request->getFullRequestURL() == $this->context->title->getInternalURL( 'action=history' ) ) {
462 $this->context->output->setSquidMaxage( $wgSquidMaxage );
463 }
464 $history = new HistoryPage( $article );
465 $history->history();
466 break;
467 default:
468 if ( wfRunHooks( 'UnknownAction', array( $act, $article ) ) ) {
469 $this->context->output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
470 }
471 }
472 wfProfileOut( __METHOD__ );
473 }
474 }