Index: trunk/phase3/includes/Wiki.php |
— | — | @@ -22,7 +22,11 @@ |
23 | 23 | return $this->context->getOutput(); |
24 | 24 | } |
25 | 25 | |
26 | | - public function __construct( RequestContext $context ){ |
| 26 | + public function __construct( RequestContext $context = null ) { |
| 27 | + if ( !$context ) { |
| 28 | + $context = RequestContext::getMain(); |
| 29 | + } |
| 30 | + |
27 | 31 | $this->context = $context; |
28 | 32 | $this->context->setTitle( $this->parseTitle() ); |
29 | 33 | } |
— | — | @@ -473,4 +477,100 @@ |
474 | 478 | } |
475 | 479 | wfProfileOut( __METHOD__ ); |
476 | 480 | } |
| 481 | + |
| 482 | + /** |
| 483 | + * Run the current MediaWiki instance |
| 484 | + * index.php just calls this |
| 485 | + */ |
| 486 | + function run() { |
| 487 | + try { |
| 488 | + $this->checkMaxLag( true ); |
| 489 | + $this->main(); |
| 490 | + $this->finalCleanup(); |
| 491 | + $this->restInPeace(); |
| 492 | + } catch ( Exception $e ) { |
| 493 | + MWExceptionHandler::handle( $e ); |
| 494 | + } |
| 495 | + } |
| 496 | + |
| 497 | + /** |
| 498 | + * Checks if the request should abort due to a lagged server, |
| 499 | + * for given maxlag parameter. |
| 500 | + * |
| 501 | + * @param boolean $abort True if this class should abort the |
| 502 | + * script execution. False to return the result as a boolean. |
| 503 | + * @return boolean True if we passed the check, false if we surpass the maxlag |
| 504 | + */ |
| 505 | + function checkMaxLag( $abort ) { |
| 506 | + global $wgShowHostnames; |
| 507 | + |
| 508 | + wfProfileIn( __METHOD__ ); |
| 509 | + $maxLag = $this->context->getRequest()->getVal( 'maxlag' ); |
| 510 | + if ( !is_null( $maxLag ) ) { |
| 511 | + $lb = wfGetLB(); // foo()->bar() is not supported in PHP4 |
| 512 | + list( $host, $lag ) = $lb->getMaxLag(); |
| 513 | + if ( $lag > $maxLag ) { |
| 514 | + if ( $abort ) { |
| 515 | + header( 'HTTP/1.1 503 Service Unavailable' ); |
| 516 | + header( 'Retry-After: ' . max( intval( $maxLag ), 5 ) ); |
| 517 | + header( 'X-Database-Lag: ' . intval( $lag ) ); |
| 518 | + header( 'Content-Type: text/plain' ); |
| 519 | + if( $wgShowHostnames ) { |
| 520 | + echo "Waiting for $host: $lag seconds lagged\n"; |
| 521 | + } else { |
| 522 | + echo "Waiting for a database server: $lag seconds lagged\n"; |
| 523 | + } |
| 524 | + } |
| 525 | + |
| 526 | + wfProfileOut( __METHOD__ ); |
| 527 | + |
| 528 | + if ( !$abort ) |
| 529 | + return false; |
| 530 | + exit; |
| 531 | + } |
| 532 | + } |
| 533 | + wfProfileOut( __METHOD__ ); |
| 534 | + return true; |
| 535 | + } |
| 536 | + |
| 537 | + function main() { |
| 538 | + global $wgUseFileCache, $wgTitle, $wgUseAjax; |
| 539 | + |
| 540 | + # Set title from request parameters |
| 541 | + $wgTitle = $this->getTitle(); |
| 542 | + $action = $this->context->getRequest()->getVal( 'action', 'view' ); |
| 543 | + |
| 544 | + # Send Ajax requests to the Ajax dispatcher. |
| 545 | + if ( $wgUseAjax && $action == 'ajax' ) { |
| 546 | + $dispatcher = new AjaxDispatcher(); |
| 547 | + $dispatcher->performAction(); |
| 548 | + return; |
| 549 | + } |
| 550 | + |
| 551 | + if ( $wgUseFileCache && $wgTitle !== null ) { |
| 552 | + wfProfileIn( 'main-try-filecache' ); |
| 553 | + // Raw pages should handle cache control on their own, |
| 554 | + // even when using file cache. This reduces hits from clients. |
| 555 | + if ( $action != 'raw' && HTMLFileCache::useFileCache() ) { |
| 556 | + /* Try low-level file cache hit */ |
| 557 | + $cache = new HTMLFileCache( $wgTitle, $action ); |
| 558 | + if ( $cache->isFileCacheGood( /* Assume up to date */ ) ) { |
| 559 | + /* Check incoming headers to see if client has this cached */ |
| 560 | + if ( !$this->context->getOutput()->checkLastModified( $cache->fileCacheTime() ) ) { |
| 561 | + $cache->loadFromFileCache(); |
| 562 | + } |
| 563 | + # Do any stats increment/watchlist stuff |
| 564 | + $article = Article::newFromTitle( $wgTitle, $this->context ); |
| 565 | + $article->viewUpdates(); |
| 566 | + # Tell OutputPage that output is taken care of |
| 567 | + $this->context->getOutput()->disable(); |
| 568 | + wfProfileOut( 'main-try-filecache' ); |
| 569 | + return; |
| 570 | + } |
| 571 | + } |
| 572 | + wfProfileOut( 'main-try-filecache' ); |
| 573 | + } |
| 574 | + |
| 575 | + $this->performRequest(); |
| 576 | + } |
477 | 577 | } |
Index: trunk/phase3/index.php |
— | — | @@ -70,100 +70,9 @@ |
71 | 71 | require ( dirname( __FILE__ ) . '/includes/WebStart.php' ); |
72 | 72 | } |
73 | 73 | |
74 | | -try { |
75 | | - wfIndexMain(); |
76 | | -} catch ( Exception $e ) { |
77 | | - MWExceptionHandler::handle( $e ); |
78 | | -} |
| 74 | +$mediaWiki = new MediaWiki(); |
| 75 | +$mediaWiki->run(); |
79 | 76 | |
80 | | -function wfIndexMain() { |
81 | | - global $wgRequest, $wgShowHostnames, $mediaWiki, $wgTitle, $wgUseAjax, $wgUseFileCache; |
82 | | - |
83 | | - wfProfileIn( 'index.php' ); |
84 | | - wfProfileIn( 'index.php-setup' ); |
85 | | - |
86 | | - $maxLag = $wgRequest->getVal( 'maxlag' ); |
87 | | - if ( !is_null( $maxLag ) ) { |
88 | | - $lb = wfGetLB(); // foo()->bar() is not supported in PHP4 |
89 | | - list( $host, $lag ) = $lb->getMaxLag(); |
90 | | - if ( $lag > $maxLag ) { |
91 | | - header( 'HTTP/1.1 503 Service Unavailable' ); |
92 | | - header( 'Retry-After: ' . max( intval( $maxLag ), 5 ) ); |
93 | | - header( 'X-Database-Lag: ' . intval( $lag ) ); |
94 | | - header( 'Content-Type: text/plain' ); |
95 | | - if( $wgShowHostnames ) { |
96 | | - echo "Waiting for $host: $lag seconds lagged\n"; |
97 | | - } else { |
98 | | - echo "Waiting for a database server: $lag seconds lagged\n"; |
99 | | - } |
100 | | - wfProfileOut( 'index.php-setup' ); |
101 | | - wfProfileOut( 'index.php' ); |
102 | | - exit; |
103 | | - } |
104 | | - } |
105 | | - |
106 | | - # Initialize MediaWiki base class |
107 | | - $context = RequestContext::getMain(); |
108 | | - $mediaWiki = new MediaWiki( $context ); |
109 | | - |
110 | | - # Set title from request parameters |
111 | | - $wgTitle = $mediaWiki->getTitle(); |
112 | | - $action = $wgRequest->getVal( 'action', 'view' ); |
113 | | - |
114 | | - wfProfileOut( 'index.php-setup' ); |
115 | | - |
116 | | - # Send Ajax requests to the Ajax dispatcher. |
117 | | - if ( $wgUseAjax && $action == 'ajax' ) { |
118 | | - $dispatcher = new AjaxDispatcher(); |
119 | | - $dispatcher->performAction(); |
120 | | - wfProfileOut( 'index.php' ); |
121 | | - $mediaWiki->restInPeace(); |
122 | | - exit; |
123 | | - } |
124 | | - |
125 | | - if ( $wgUseFileCache && $wgTitle !== null ) { |
126 | | - wfProfileIn( 'index.php-filecache' ); |
127 | | - // Raw pages should handle cache control on their own, |
128 | | - // even when using file cache. This reduces hits from clients. |
129 | | - if ( $action != 'raw' && HTMLFileCache::useFileCache() ) { |
130 | | - /* Try low-level file cache hit */ |
131 | | - $cache = new HTMLFileCache( $wgTitle, $action ); |
132 | | - if ( $cache->isFileCacheGood( /* Assume up to date */ ) ) { |
133 | | - /* Check incoming headers to see if client has this cached */ |
134 | | - if ( !$context->getOutput()->checkLastModified( $cache->fileCacheTime() ) ) { |
135 | | - $cache->loadFromFileCache(); |
136 | | - } |
137 | | - # Do any stats increment/watchlist stuff |
138 | | - $article = Article::newFromTitle( $wgTitle, $context ); |
139 | | - $article->viewUpdates(); |
140 | | - # Tell OutputPage that output is taken care of |
141 | | - $context->getOutput()->disable(); |
142 | | - wfProfileOut( 'index.php-filecache' ); |
143 | | - $mediaWiki->finalCleanup(); |
144 | | - wfProfileOut( 'index.php' ); |
145 | | - $mediaWiki->restInPeace(); |
146 | | - exit; |
147 | | - } |
148 | | - } |
149 | | - wfProfileOut( 'index.php-filecache' ); |
150 | | - } |
151 | | - |
152 | | - /** |
153 | | - * $wgArticle is deprecated, do not use it. This will possibly be removed |
154 | | - * entirely in 1.20 or 1.21 |
155 | | - * @deprecated since 1.19 |
156 | | - */ |
157 | | - global $wgArticle; |
158 | | - |
159 | | - $wgArticle = $mediaWiki->performRequest(); |
160 | | - |
161 | | - $mediaWiki->finalCleanup(); |
162 | | - |
163 | | - wfProfileOut( 'index.php' ); |
164 | | - |
165 | | - $mediaWiki->restInPeace(); |
166 | | -} |
167 | | - |
168 | 77 | /** |
169 | 78 | * Display something vaguely comprehensible in the event of a totally unrecoverable error. |
170 | 79 | * Does not assume access to *anything*; no globals, no autloader, no database, no localisation. |