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