r85285 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r85284‎ | r85285 | r85286 >
Date:21:32, 3 April 2011
Author:happy-melon
Status:ok
Tags:
Comment:
Follow-up r85278, r85240:

* Internalise $title in MediaWiki base class
* Fix access fatal in SpecialPage by getting the context from the MediaWiki base class rather than the OutputPage
* Fix a couple of typos in RequestContext which would have thrown errors/fatals if anyone had ever called them.
Modified paths:
  • /trunk/phase3/includes/RequestContext.php (modified) (history)
  • /trunk/phase3/includes/SpecialPage.php (modified) (history)
  • /trunk/phase3/includes/Wiki.php (modified) (history)
  • /trunk/phase3/index.php (modified) (history)

Diff [purge]

Index: trunk/phase3/index.php
@@ -66,7 +66,7 @@
6767 $mediaWiki = new MediaWiki( $wgRequest, $wgOut );
6868
6969 # Set title from request parameters
70 -$wgTitle = $mediaWiki->checkInitialQueries();
 70+$wgTitle = $mediaWiki->getTitle();
7171 $action = $wgRequest->getVal( 'action', 'view' );
7272
7373 wfProfileOut( 'index.php-setup' );
@@ -116,7 +116,7 @@
117117 $mediaWiki->setVal( 'UseExternalEditor', $wgUseExternalEditor );
118118 $mediaWiki->setVal( 'UsePathInfo', $wgUsePathInfo );
119119
120 -$mediaWiki->performRequestForTitle( $wgTitle, $wgArticle, $wgUser );
 120+$mediaWiki->performRequestForTitle( $wgArticle, $wgUser );
121121 $mediaWiki->finalCleanup();
122122
123123 wfProfileOut( 'index.php' );
Index: trunk/phase3/includes/SpecialPage.php
@@ -519,10 +519,11 @@
520520 * Returns a title object if the page is redirected, false if there was no such special
521521 * page, and true if it was successful.
522522 *
523 - * @param $title a title object
524 - * @param $including output is being captured for use in {{special:whatever}}
 523+ * @param $title Title object
 524+ * @param $context RequestContext
 525+ * @param $including Bool output is being captured for use in {{special:whatever}}
525526 */
526 - static function executePath( &$title, $including = false ) {
 527+ static function executePath( &$title, RequestContext $context, $including = false ) {
527528 global $wgOut, $wgTitle, $wgRequest;
528529 wfProfileIn( __METHOD__ );
529530
@@ -546,7 +547,7 @@
547548 }
548549
549550 # Page exists, set the context
550 - $page->setContext( $wgOut->getContext() );
 551+ $page->setContext( $context );
551552
552553 # Check for redirect
553554 if ( !$including ) {
@@ -624,7 +625,7 @@
625626 $context->setTitle( $title );
626627 $wgOut = $context->getOutput();
627628
628 - $ret = SpecialPage::executePath( $title, true );
 629+ $ret = SpecialPage::executePath( $title, $context, true );
629630 if ( $ret === true ) {
630631 $ret = $wgOut->getHTML();
631632 }
Index: trunk/phase3/includes/RequestContext.php
@@ -10,7 +10,7 @@
1111 class RequestContext {
1212 private $request; /// WebRequest object
1313 private $title; /// Title object
14 - private $out; /// OutputPage object
 14+ private $output; /// OutputPage object
1515 private $user; /// User object
1616 private $lang; /// Language object
1717 private $skin; /// Skin object
@@ -60,6 +60,15 @@
6161 }
6262
6363 /**
 64+ * Set the OutputPage object
 65+ *
 66+ * @param $u OutputPage
 67+ */
 68+ public function setOutput( OutputPage $u ) {
 69+ $this->output = $u;
 70+ }
 71+
 72+ /**
6473 * Get the OutputPage object
6574 *
6675 * @return OutputPage object
@@ -160,8 +169,8 @@
161170 * @return Message object
162171 */
163172 public function msg() {
164 - $args = function_get_args();
165 - return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() )->outputPage( $this->getOut() );
 173+ $args = func_get_args();
 174+ return call_user_func_array( 'wfMessage', $args )->inLanguage( $this->getLang() );
166175 }
167176
168177 /** Static methods **/
@@ -178,6 +187,5 @@
179188 }
180189 return $instance;
181190 }
182 -
183191 }
184192
Index: trunk/phase3/includes/Wiki.php
@@ -29,6 +29,18 @@
3030 private $output;
3131
3232 /**
 33+ * The Title we'll be working on
 34+ * @var Title
 35+ */
 36+ private $title;
 37+
 38+ /**
 39+ * TODO: fold $output, etc, into this
 40+ * @var RequestContext
 41+ */
 42+ private $context;
 43+
 44+ /**
3345 * Stores key/value pairs to circumvent global variables
3446 * Note that keys are case-insensitive!
3547 *
@@ -67,26 +79,31 @@
6880 public function __construct( WebRequest &$request, /*OutputPage*/ &$output ){
6981 $this->request =& $request;
7082 $this->output =& $output;
 83+ $this->title = $this->parseTitle();
 84+
 85+ $this->context = new RequestContext();
 86+ $this->context->setRequest( $this->request );
 87+ $this->context->setOutput( $this->output );
 88+ $this->context->setTitle( $this->title );
7189 }
7290
7391 /**
7492 * Initialization of ... everything
7593 * Performs the request too
7694 *
77 - * @param $title Title ($wgTitle)
7895 * @param $article Article
7996 * @param $user User
8097 */
81 - public function performRequestForTitle( &$title, &$article, &$user ) {
 98+ public function performRequestForTitle( &$article, &$user ) {
8299 wfProfileIn( __METHOD__ );
83100
84 - $this->output->setTitle( $title );
 101+ $this->output->setTitle( $this->title );
85102 if ( $this->request->getVal( 'printable' ) === 'yes' ) {
86103 $this->output->setPrintable();
87104 }
88105
89106 wfRunHooks( 'BeforeInitialize', array(
90 - &$title,
 107+ &$this->title,
91108 &$article,
92109 &$this->output,
93110 &$user,
@@ -97,7 +114,7 @@
98115 // If the user is not logged in, the Namespace:title of the article must be in
99116 // the Read array in order for the user to see it. (We have to check here to
100117 // catch special pages etc. We check again in Article::view())
101 - if ( !is_null( $title ) && !$title->userCanRead() ) {
 118+ if ( !is_null( $this->title ) && !$this->title->userCanRead() ) {
102119 $this->output->loginToUse();
103120 $this->finalCleanup();
104121 $this->output->disable();
@@ -106,13 +123,13 @@
107124 }
108125
109126 // Call handleSpecialCases() to deal with all special requests...
110 - if ( !$this->handleSpecialCases( $title ) ) {
 127+ if ( !$this->handleSpecialCases() ) {
111128 // ...otherwise treat it as an article view. The article
112129 // may be a redirect to another article or URL.
113 - $new_article = $this->initializeArticle( $title );
 130+ $new_article = $this->initializeArticle();
114131 if ( is_object( $new_article ) ) {
115132 $article = $new_article;
116 - $this->performAction( $article, $title, $user );
 133+ $this->performAction( $article, $user );
117134 } elseif ( is_string( $new_article ) ) {
118135 $this->output->redirect( $new_article );
119136 } else {
@@ -124,12 +141,11 @@
125142 }
126143
127144 /**
128 - * Checks some initial queries
129 - * FIXME: rename to parseTitle() ?
 145+ * Parse $request to get the Title object
130146 *
131147 * @return Title object to be $wgTitle
132148 */
133 - /* private */ function checkInitialQueries() {
 149+ private function parseTitle() {
134150 global $wgContLang;
135151
136152 $curid = $this->request->getInt( 'curid' );
@@ -167,57 +183,67 @@
168184 }
169185
170186 /**
 187+ * Get the Title object that we'll be acting on, as specified in the WebRequest
 188+ * @return Title
 189+ */
 190+ public function getTitle(){
 191+ if( $this->title === null ){
 192+ $this->title = $this->parseTitle();
 193+ }
 194+ return $this->title;
 195+ }
 196+
 197+ /**
171198 * Initialize some special cases:
172199 * - bad titles
173200 * - local interwiki redirects
174201 * - redirect loop
175202 * - special pages
176203 *
177 - * @param $title Title
178204 * @return bool true if the request is already executed
179205 */
180 - private function handleSpecialCases( &$title ) {
 206+ private function handleSpecialCases() {
181207 wfProfileIn( __METHOD__ );
182208
183209 // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty.
184 - if ( is_null( $title ) || ( ( $title->getDBkey() == '' ) && ( $title->getInterwiki() == '' ) ) ) {
185 - $title = SpecialPage::getTitleFor( 'Badtitle' );
186 - $this->output->setTitle( $title ); // bug 21456
 210+ if ( is_null( $this->title ) || ( ( $this->title->getDBkey() == '' ) && ( $this->title->getInterwiki() == '' ) ) ) {
 211+ $this->title = SpecialPage::getTitleFor( 'Badtitle' );
 212+ $this->output->setTitle( $this->title ); // bug 21456
187213 // Die now before we mess up $wgArticle and the skin stops working
188214 throw new ErrorPageError( 'badtitle', 'badtitletext' );
189215
190216 // Interwiki redirects
191 - } else if ( $title->getInterwiki() != '' ) {
 217+ } else if ( $this->title->getInterwiki() != '' ) {
192218 $rdfrom = $this->request->getVal( 'rdfrom' );
193219 if ( $rdfrom ) {
194 - $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
 220+ $url = $this->title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
195221 } else {
196222 $query = $this->request->getValues();
197223 unset( $query['title'] );
198 - $url = $title->getFullURL( $query );
 224+ $url = $this->title->getFullURL( $query );
199225 }
200226 /* Check for a redirect loop */
201 - if ( !preg_match( '/^' . preg_quote( $this->getVal( 'Server' ), '/' ) . '/', $url ) && $title->isLocal() ) {
 227+ if ( !preg_match( '/^' . preg_quote( $this->getVal( 'Server' ), '/' ) . '/', $url ) && $this->title->isLocal() ) {
202228 // 301 so google et al report the target as the actual url.
203229 $this->output->redirect( $url, 301 );
204230 } else {
205 - $title = SpecialPage::getTitleFor( 'Badtitle' );
206 - $this->output->setTitle( $title ); // bug 21456
 231+ $this->title = SpecialPage::getTitleFor( 'Badtitle' );
 232+ $this->output->setTitle( $this->title ); // bug 21456
207233 wfProfileOut( __METHOD__ );
208234 throw new ErrorPageError( 'badtitle', 'badtitletext' );
209235 }
210236 // Redirect loops, no title in URL, $wgUsePathInfo URLs, and URLs with a variant
211237 } else if ( $this->request->getVal( 'action', 'view' ) == 'view' && !$this->request->wasPosted()
212 - && ( $this->request->getVal( 'title' ) === null || $title->getPrefixedDBKey() != $this->request->getVal( 'title' ) )
 238+ && ( $this->request->getVal( 'title' ) === null || $this->title->getPrefixedDBKey() != $this->request->getVal( 'title' ) )
213239 && !count( array_diff( array_keys( $this->request->getValues() ), array( 'action', 'title' ) ) ) )
214240 {
215 - if ( $title->getNamespace() == NS_SPECIAL ) {
216 - list( $name, $subpage ) = SpecialPage::resolveAliasWithSubpage( $title->getDBkey() );
 241+ if ( $this->title->getNamespace() == NS_SPECIAL ) {
 242+ list( $name, $subpage ) = SpecialPage::resolveAliasWithSubpage( $this->title->getDBkey() );
217243 if ( $name ) {
218 - $title = SpecialPage::getTitleFor( $name, $subpage );
 244+ $this->title = SpecialPage::getTitleFor( $name, $subpage );
219245 }
220246 }
221 - $targetUrl = $title->getFullURL();
 247+ $targetUrl = $this->title->getFullURL();
222248 // Redirect to canonical url, make it a 301 to allow caching
223249 if ( $targetUrl == $this->request->getFullRequestURL() ) {
224250 $message = "Redirect loop detected!\n\n" .
@@ -247,9 +273,9 @@
248274 $this->output->redirect( $targetUrl, '301' );
249275 }
250276 // Special pages
251 - } else if ( NS_SPECIAL == $title->getNamespace() ) {
 277+ } else if ( NS_SPECIAL == $this->title->getNamespace() ) {
252278 /* actions that need to be made when we have a special pages */
253 - SpecialPage::executePath( $title );
 279+ SpecialPage::executePath( $this->title, $this->context );
254280 } else {
255281 /* No match to special cases */
256282 wfProfileOut( __METHOD__ );
@@ -326,23 +352,22 @@
327353 * Initialize the object to be known as $wgArticle for "standard" actions
328354 * Create an Article object for the page, following redirects if needed.
329355 *
330 - * @param $title Title ($wgTitle)
331356 * @return mixed an Article, or a string to redirect to another URL
332357 */
333 - private function initializeArticle( &$title ) {
 358+ private function initializeArticle() {
334359 wfProfileIn( __METHOD__ );
335360
336361 $action = $this->request->getVal( 'action', 'view' );
337 - $article = self::articleFromTitle( $title );
 362+ $article = self::articleFromTitle( $this->title );
338363 // NS_MEDIAWIKI has no redirects.
339364 // It is also used for CSS/JS, so performance matters here...
340 - if ( $title->getNamespace() == NS_MEDIAWIKI ) {
 365+ if ( $this->title->getNamespace() == NS_MEDIAWIKI ) {
341366 wfProfileOut( __METHOD__ );
342367 return $article;
343368 }
344369 // Namespace might change when using redirects
345370 // Check for redirects ...
346 - $file = ( $title->getNamespace() == NS_FILE ) ? $article->getFile() : null;
 371+ $file = ( $this->title->getNamespace() == NS_FILE ) ? $article->getFile() : null;
347372 if ( ( $action == 'view' || $action == 'render' ) // ... for actions that show content
348373 && !$this->request->getVal( 'oldid' ) && // ... and are not old revisions
349374 !$this->request->getVal( 'diff' ) && // ... and not when showing diff
@@ -354,7 +379,7 @@
355380 $ignoreRedirect = $target = false;
356381
357382 wfRunHooks( 'InitializeArticleMaybeRedirect',
358 - array( &$title, &$this->request, &$ignoreRedirect, &$target, &$article ) );
 383+ array( &$this->title, &$this->request, &$ignoreRedirect, &$target, &$article ) );
359384
360385 // Follow redirects only for... redirects.
361386 // If $target is set, then a hook wanted to redirect.
@@ -373,14 +398,14 @@
374399 $rarticle = self::articleFromTitle( $target );
375400 $rarticle->loadPageData();
376401 if ( $rarticle->exists() || ( is_object( $file ) && !$file->isLocal() ) ) {
377 - $rarticle->setRedirectedFrom( $title );
 402+ $rarticle->setRedirectedFrom( $this->title );
378403 $article = $rarticle;
379 - $title = $target;
380 - $this->output->setTitle( $title );
 404+ $this->title = $target;
 405+ $this->output->setTitle( $this->title );
381406 }
382407 }
383408 } else {
384 - $title = $article->getTitle();
 409+ $this->title = $article->getTitle();
385410 }
386411 }
387412 wfProfileOut( __METHOD__ );
@@ -457,13 +482,15 @@
458483 * Perform one of the "standard" actions
459484 *
460485 * @param $article Article
461 - * @param $title Title
462486 * @param $user User
463487 */
464 - private function performAction( &$article, &$title, &$user ) {
 488+ private function performAction( &$article, &$user ) {
465489 wfProfileIn( __METHOD__ );
466490
467 - if ( !wfRunHooks( 'MediaWikiPerformAction', array( $this->output, $article, $title, $user, $this->request, $this ) ) ) {
 491+ if ( !wfRunHooks( 'MediaWikiPerformAction', array(
 492+ $this->output, $article, $this->title,
 493+ $user, $this->request, $this ) ) )
 494+ {
468495 wfProfileOut( __METHOD__ );
469496 return;
470497 }
@@ -541,7 +568,7 @@
542569 }
543570 break;
544571 case 'history':
545 - if ( $this->request->getFullRequestURL() == $title->getInternalURL( 'action=history' ) ) {
 572+ if ( $this->request->getFullRequestURL() == $this->title->getInternalURL( 'action=history' ) ) {
546573 $this->output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
547574 }
548575 $history = new HistoryPage( $article );

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r85240Implement the RequestContext class. Some credit to IAlex, ;) other credit for...dantman10:41, 3 April 2011
r85278Follow-up to r85240:...happy-melon20:40, 3 April 2011

Status & tagging log