r85259 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r85258‎ | r85259 | r85260 >
Date:15:56, 3 April 2011
Author:happy-melon
Status:ok
Tags:
Comment:
Cleanup in Wiki.php and index.php:
* Add visibility to MediaWiki::*() methods.
* Refactor out MediaWiki::preliminaryChecks(), was actually just one preliminary check :D
* maxlag is a property of the database, not the wiki, so MediaWiki::checkMaxLag() doesn't belong. Since it was only called in index.php I just expanded it there, it's only a trivial wrapper anyway.
* Run stylize.php over code.
Modified paths:
  • /trunk/phase3/includes/Wiki.php (modified) (history)
  • /trunk/phase3/index.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Wiki.php
@@ -5,16 +5,23 @@
66 * @internal documentation reviewed 15 Mar 2010
77 */
88 class MediaWiki {
9 - var $params = array();
109
1110 /**
 11+ * Array of options which may or may not be used
 12+ * FIXME: this seems currently to be a messy halfway-house between globals
 13+ * and a config object. Pick one and run with it
 14+ * @var array
 15+ */
 16+ private $params = array();
 17+
 18+ /**
1219 * Stores key/value pairs to circumvent global variables
1320 * Note that keys are case-insensitive!
1421 *
1522 * @param $key String: key to store
1623 * @param $value Mixed: value to put for the key
1724 */
18 - function setVal( $key, &$value ) {
 25+ public function setVal( $key, &$value ) {
1926 $key = strtolower( $key );
2027 $this->params[$key] =& $value;
2128 }
@@ -27,9 +34,9 @@
2835 * @param $default string default value, defaults to empty string
2936 * @return $default Mixed: default value if if the key doesn't exist
3037 */
31 - function getVal( $key, $default = '' ) {
 38+ public function getVal( $key, $default = '' ) {
3239 $key = strtolower( $key );
33 - if( isset( $this->params[$key] ) ) {
 40+ if ( isset( $this->params[$key] ) ) {
3441 return $this->params[$key];
3542 }
3643 return $default;
@@ -45,29 +52,36 @@
4653 * @param $user User
4754 * @param $request WebRequest
4855 */
49 - function performRequestForTitle( &$title, &$article, &$output, &$user, $request ) {
 56+ public function performRequestForTitle( &$title, &$article, &$output, &$user, $request ) {
5057 wfProfileIn( __METHOD__ );
5158
5259 $output->setTitle( $title );
53 - if( $request->getVal( 'printable' ) === 'yes' ) {
 60+ if ( $request->getVal( 'printable' ) === 'yes' ) {
5461 $output->setPrintable();
5562 }
5663
5764 wfRunHooks( 'BeforeInitialize', array( &$title, &$article, &$output, &$user, $request, $this ) );
5865
59 - if( !$this->preliminaryChecks( $title, $output ) ) {
 66+ // If the user is not logged in, the Namespace:title of the article must be in
 67+ // the Read array in order for the user to see it. (We have to check here to
 68+ // catch special pages etc. We check again in Article::view())
 69+ if ( !is_null( $title ) && !$title->userCanRead() ) {
 70+ $output->loginToUse();
 71+ $this->finalCleanup( $output );
 72+ $output->disable();
6073 wfProfileOut( __METHOD__ );
61 - return;
 74+ return false;
6275 }
 76+
6377 // Call handleSpecialCases() to deal with all special requests...
64 - if( !$this->handleSpecialCases( $title, $output, $request ) ) {
 78+ if ( !$this->handleSpecialCases( $title, $output, $request ) ) {
6579 // ...otherwise treat it as an article view. The article
6680 // may be a redirect to another article or URL.
6781 $new_article = $this->initializeArticle( $title, $output, $request );
68 - if( is_object( $new_article ) ) {
 82+ if ( is_object( $new_article ) ) {
6983 $article = $new_article;
7084 $this->performAction( $output, $article, $title, $user, $request );
71 - } elseif( is_string( $new_article ) ) {
 85+ } elseif ( is_string( $new_article ) ) {
7286 $output->redirect( $new_article );
7387 } else {
7488 wfProfileOut( __METHOD__ );
@@ -78,59 +92,42 @@
7993 }
8094
8195 /**
82 - * Check if the maximum lag of database slaves is higher that $maxLag, and
83 - * if it's the case, output an error message
84 - *
85 - * @param $maxLag int: maximum lag allowed for the request, as supplied by
86 - * the client
87 - * @return bool true if the request can continue
88 - */
89 - function checkMaxLag( $maxLag ) {
90 - list( $host, $lag ) = wfGetLB()->getMaxLag();
91 - if( $lag > $maxLag ) {
92 - wfMaxlagError( $host, $lag, $maxLag );
93 - return false;
94 - } else {
95 - return true;
96 - }
97 - }
98 -
99 - /**
10096 * Checks some initial queries
 97+ * FIXME: rename to parseTitle() ?
10198 *
10299 * @param $request WebRequest
103100 * @return Title object to be $wgTitle
104101 */
105 - function checkInitialQueries( WebRequest $request ) {
 102+ /* private */ function checkInitialQueries( WebRequest $request ) {
106103 global $wgContLang;
107104
108105 $curid = $request->getInt( 'curid' );
109106 $title = $request->getVal( 'title' );
110107
111 - if( $request->getCheck( 'search' ) ) {
 108+ if ( $request->getCheck( 'search' ) ) {
112109 // Compatibility with old search URLs which didn't use Special:Search
113110 // Just check for presence here, so blank requests still
114111 // show the search page when using ugly URLs (bug 8054).
115112 $ret = SpecialPage::getTitleFor( 'Search' );
116 - } elseif( $curid ) {
 113+ } elseif ( $curid ) {
117114 // URLs like this are generated by RC, because rc_title isn't always accurate
118115 $ret = Title::newFromID( $curid );
119 - } elseif( $title == '' && $this->getAction( $request ) != 'delete' ) {
 116+ } elseif ( $title == '' && $this->getAction( $request ) != 'delete' ) {
120117 $ret = Title::newMainPage();
121118 } else {
122119 $ret = Title::newFromURL( $title );
123120 // check variant links so that interwiki links don't have to worry
124121 // about the possible different language variants
125 - if( count( $wgContLang->getVariants() ) > 1 && !is_null( $ret ) && $ret->getArticleID() == 0 )
 122+ if ( count( $wgContLang->getVariants() ) > 1 && !is_null( $ret ) && $ret->getArticleID() == 0 )
126123 $wgContLang->findVariantLink( $title, $ret );
127124 }
128125 // For non-special titles, check for implicit titles
129 - if( is_null( $ret ) || $ret->getNamespace() != NS_SPECIAL ) {
 126+ if ( is_null( $ret ) || $ret->getNamespace() != NS_SPECIAL ) {
130127 // We can have urls with just ?diff=,?oldid= or even just ?diff=
131128 $oldid = $request->getInt( 'oldid' );
132129 $oldid = $oldid ? $oldid : $request->getInt( 'diff' );
133130 // Allow oldid to override a changed or missing title
134 - if( $oldid ) {
 131+ if ( $oldid ) {
135132 $rev = Revision::newFromId( $oldid );
136133 $ret = $rev ? $rev->getTitle() : $ret;
137134 }
@@ -139,26 +136,6 @@
140137 }
141138
142139 /**
143 - * Checks for anon-cannot-read case
144 - *
145 - * @param $title Title
146 - * @param $output OutputPage
147 - * @return boolean true if successful
148 - */
149 - function preliminaryChecks( &$title, &$output ) {
150 - // If the user is not logged in, the Namespace:title of the article must be in
151 - // the Read array in order for the user to see it. (We have to check here to
152 - // catch special pages etc. We check again in Article::view())
153 - if( !is_null( $title ) && !$title->userCanRead() ) {
154 - $output->loginToUse();
155 - $this->finalCleanup( $output );
156 - $output->disable();
157 - return false;
158 - }
159 - return true;
160 - }
161 -
162 - /**
163140 * Initialize some special cases:
164141 * - bad titles
165142 * - local interwiki redirects
@@ -170,20 +147,20 @@
171148 * @param $request WebRequest
172149 * @return bool true if the request is already executed
173150 */
174 - function handleSpecialCases( &$title, &$output, $request ) {
 151+ private function handleSpecialCases( &$title, &$output, $request ) {
175152 wfProfileIn( __METHOD__ );
176153
177154 // Invalid titles. Bug 21776: The interwikis must redirect even if the page name is empty.
178 - if( is_null($title) || ( ( $title->getDBkey() == '' ) && ( $title->getInterwiki() == '' ) ) ) {
 155+ if ( is_null( $title ) || ( ( $title->getDBkey() == '' ) && ( $title->getInterwiki() == '' ) ) ) {
179156 $title = SpecialPage::getTitleFor( 'Badtitle' );
180157 $output->setTitle( $title ); // bug 21456
181158 // Die now before we mess up $wgArticle and the skin stops working
182159 throw new ErrorPageError( 'badtitle', 'badtitletext' );
183160
184161 // Interwiki redirects
185 - } else if( $title->getInterwiki() != '' ) {
 162+ } else if ( $title->getInterwiki() != '' ) {
186163 $rdfrom = $request->getVal( 'rdfrom' );
187 - if( $rdfrom ) {
 164+ if ( $rdfrom ) {
188165 $url = $title->getFullURL( 'rdfrom=' . urlencode( $rdfrom ) );
189166 } else {
190167 $query = $request->getValues();
@@ -191,7 +168,7 @@
192169 $url = $title->getFullURL( $query );
193170 }
194171 /* Check for a redirect loop */
195 - if( !preg_match( '/^' . preg_quote( $this->getVal('Server'), '/' ) . '/', $url ) && $title->isLocal() ) {
 172+ if ( !preg_match( '/^' . preg_quote( $this->getVal( 'Server' ), '/' ) . '/', $url ) && $title->isLocal() ) {
196173 // 301 so google et al report the target as the actual url.
197174 $output->redirect( $url, 301 );
198175 } else {
@@ -213,13 +190,13 @@
214191 }
215192 $targetUrl = $title->getFullURL();
216193 // Redirect to canonical url, make it a 301 to allow caching
217 - if( $targetUrl == $request->getFullRequestURL() ) {
 194+ if ( $targetUrl == $request->getFullRequestURL() ) {
218195 $message = "Redirect loop detected!\n\n" .
219196 "This means the wiki got confused about what page was " .
220197 "requested; this sometimes happens when moving a wiki " .
221198 "to a new server or changing the server configuration.\n\n";
222199
223 - if( $this->getVal( 'UsePathInfo' ) ) {
 200+ if ( $this->getVal( 'UsePathInfo' ) ) {
224201 $message .= "The wiki is trying to interpret the page " .
225202 "title from the URL path portion (PATH_INFO), which " .
226203 "sometimes fails depending on the web server. Try " .
@@ -241,7 +218,7 @@
242219 $output->redirect( $targetUrl, '301' );
243220 }
244221 // Special pages
245 - } else if( NS_SPECIAL == $title->getNamespace() ) {
 222+ } else if ( NS_SPECIAL == $title->getNamespace() ) {
246223 /* actions that need to be made when we have a special pages */
247224 SpecialPage::executePath( $title );
248225 } else {
@@ -260,15 +237,15 @@
261238 * @param $title Title
262239 * @return Article object
263240 */
264 - static function articleFromTitle( &$title ) {
265 - if( NS_MEDIA == $title->getNamespace() ) {
 241+ public static function articleFromTitle( &$title ) {
 242+ if ( NS_MEDIA == $title->getNamespace() ) {
266243 // FIXME: where should this go?
267244 $title = Title::makeTitle( NS_FILE, $title->getDBkey() );
268245 }
269246
270247 $article = null;
271248 wfRunHooks( 'ArticleFromTitle', array( &$title, &$article ) );
272 - if( $article ) {
 249+ if ( $article ) {
273250 return $article;
274251 }
275252
@@ -296,7 +273,7 @@
297274 $action = $request->getVal( 'action', 'view' );
298275
299276 // Check for disabled actions
300 - if( in_array( $action, $wgDisabledActions ) ) {
 277+ if ( in_array( $action, $wgDisabledActions ) ) {
301278 return 'nosuchaction';
302279 }
303280
@@ -326,21 +303,21 @@
327304 * @param $request WebRequest ($wgRequest)
328305 * @return mixed an Article, or a string to redirect to another URL
329306 */
330 - function initializeArticle( &$title, &$output, $request ) {
 307+ private function initializeArticle( &$title, &$output, $request ) {
331308 wfProfileIn( __METHOD__ );
332309
333310 $action = $request->getVal( 'action', 'view' );
334311 $article = self::articleFromTitle( $title );
335312 // NS_MEDIAWIKI has no redirects.
336313 // It is also used for CSS/JS, so performance matters here...
337 - if( $title->getNamespace() == NS_MEDIAWIKI ) {
 314+ if ( $title->getNamespace() == NS_MEDIAWIKI ) {
338315 wfProfileOut( __METHOD__ );
339316 return $article;
340317 }
341318 // Namespace might change when using redirects
342319 // Check for redirects ...
343 - $file = ($title->getNamespace() == NS_FILE) ? $article->getFile() : null;
344 - if( ( $action == 'view' || $action == 'render' ) // ... for actions that show content
 320+ $file = ( $title->getNamespace() == NS_FILE ) ? $article->getFile() : null;
 321+ if ( ( $action == 'view' || $action == 'render' ) // ... for actions that show content
345322 && !$request->getVal( 'oldid' ) && // ... and are not old revisions
346323 !$request->getVal( 'diff' ) && // ... and not when showing diff
347324 $request->getVal( 'redirect' ) != 'no' && // ... unless explicitly told not to
@@ -351,25 +328,25 @@
352329 $ignoreRedirect = $target = false;
353330
354331 wfRunHooks( 'InitializeArticleMaybeRedirect',
355 - array(&$title,&$request,&$ignoreRedirect,&$target,&$article) );
 332+ array( &$title, &$request, &$ignoreRedirect, &$target, &$article ) );
356333
357334 // Follow redirects only for... redirects.
358335 // If $target is set, then a hook wanted to redirect.
359 - if( !$ignoreRedirect && ($target || $article->isRedirect()) ) {
 336+ if ( !$ignoreRedirect && ( $target || $article->isRedirect() ) ) {
360337 // Is the target already set by an extension?
361338 $target = $target ? $target : $article->followRedirect();
362 - if( is_string( $target ) ) {
363 - if( !$this->getVal( 'DisableHardRedirects' ) ) {
 339+ if ( is_string( $target ) ) {
 340+ if ( !$this->getVal( 'DisableHardRedirects' ) ) {
364341 // we'll need to redirect
365342 wfProfileOut( __METHOD__ );
366343 return $target;
367344 }
368345 }
369 - if( is_object($target) ) {
 346+ if ( is_object( $target ) ) {
370347 // Rewrite environment to redirected article
371348 $rarticle = self::articleFromTitle( $target );
372349 $rarticle->loadPageData();
373 - if( $rarticle->exists() || ( is_object( $file ) && !$file->isLocal() ) ) {
 350+ if ( $rarticle->exists() || ( is_object( $file ) && !$file->isLocal() ) ) {
374351 $rarticle->setRedirectedFrom( $title );
375352 $article = $rarticle;
376353 $title = $target;
@@ -385,12 +362,11 @@
386363 }
387364
388365 /**
389 - * Cleaning up request by doing:
390 - ** deferred updates, DB transaction, and the output
 366+ * Cleaning up request by doing deferred updates, DB transaction, and the output
391367 *
392368 * @param $output OutputPage
393369 */
394 - function finalCleanup( &$output ) {
 370+ public function finalCleanup( &$output ) {
395371 wfProfileIn( __METHOD__ );
396372 // Now commit any transactions, so that unreported errors after
397373 // output() don't roll back the whole DB transaction
@@ -409,15 +385,15 @@
410386 /**
411387 * Do a job from the job queue
412388 */
413 - function doJobs() {
 389+ private function doJobs() {
414390 global $wgJobRunRate;
415391
416 - if( $wgJobRunRate <= 0 || wfReadOnly() ) {
 392+ if ( $wgJobRunRate <= 0 || wfReadOnly() ) {
417393 return;
418394 }
419 - if( $wgJobRunRate < 1 ) {
 395+ if ( $wgJobRunRate < 1 ) {
420396 $max = mt_getrandmax();
421 - if( mt_rand( 0, $max ) > $max * $wgJobRunRate ) {
 397+ if ( mt_rand( 0, $max ) > $max * $wgJobRunRate ) {
422398 return;
423399 }
424400 $n = 1;
@@ -430,8 +406,8 @@
431407 $t = -wfTime();
432408 $success = $job->run();
433409 $t += wfTime();
434 - $t = round( $t*1000 );
435 - if( !$success ) {
 410+ $t = round( $t * 1000 );
 411+ if ( !$success ) {
436412 $output .= "Error: " . $job->getLastError() . ", Time: $t ms\n";
437413 } else {
438414 $output .= "Success, Time: $t ms\n";
@@ -443,7 +419,7 @@
444420 /**
445421 * Ends this task peacefully
446422 */
447 - function restInPeace() {
 423+ public function restInPeace() {
448424 MessageCache::logMessages();
449425 wfLogProfilingData();
450426 // Commit and close up!
@@ -462,10 +438,10 @@
463439 * @param $user User
464440 * @param $request WebRequest
465441 */
466 - function performAction( &$output, &$article, &$title, &$user, &$request ) {
 442+ private function performAction( &$output, &$article, &$title, &$user, &$request ) {
467443 wfProfileIn( __METHOD__ );
468444
469 - if( !wfRunHooks( 'MediaWikiPerformAction', array( $output, $article, $title, $user, $request, $this ) ) ) {
 445+ if ( !wfRunHooks( 'MediaWikiPerformAction', array( $output, $article, $title, $user, $request, $this ) ) ) {
470446 wfProfileOut( __METHOD__ );
471447 return;
472448 }
@@ -478,10 +454,10 @@
479455 $article->view();
480456 break;
481457 case 'raw': // includes JS/CSS
482 - wfProfileIn( __METHOD__.'-raw' );
 458+ wfProfileIn( __METHOD__ . '-raw' );
483459 $raw = new RawPage( $article );
484460 $raw->view();
485 - wfProfileOut( __METHOD__.'-raw' );
 461+ wfProfileOut( __METHOD__ . '-raw' );
486462 break;
487463 case 'watch':
488464 case 'unwatch':
@@ -501,7 +477,7 @@
502478 $article->view();
503479 break;
504480 case 'dublincore':
505 - if( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
 481+ if ( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
506482 wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
507483 } else {
508484 $rdf = new DublinCoreRdf( $article );
@@ -509,7 +485,7 @@
510486 }
511487 break;
512488 case 'creativecommons':
513 - if( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
 489+ if ( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
514490 wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
515491 } else {
516492 $rdf = new CreativeCommonsRdf( $article );
@@ -520,22 +496,22 @@
521497 Credits::showPage( $article );
522498 break;
523499 case 'submit':
524 - if( session_id() == '' ) {
 500+ if ( session_id() == '' ) {
525501 /* Send a cookie so anons get talk message notifications */
526502 wfSetupSession();
527503 }
528504 /* Continue... */
529505 case 'edit':
530 - if( wfRunHooks( 'CustomEditor', array( $article, $user ) ) ) {
 506+ if ( wfRunHooks( 'CustomEditor', array( $article, $user ) ) ) {
531507 $internal = $request->getVal( 'internaledit' );
532508 $external = $request->getVal( 'externaledit' );
533509 $section = $request->getVal( 'section' );
534510 $oldid = $request->getVal( 'oldid' );
535 - if( !$this->getVal( 'UseExternalEditor' ) || $action=='submit' || $internal ||
 511+ if ( !$this->getVal( 'UseExternalEditor' ) || $action == 'submit' || $internal ||
536512 $section || $oldid || ( !$user->getOption( 'externaleditor' ) && !$external ) ) {
537513 $editor = new EditPage( $article );
538514 $editor->submit();
539 - } elseif( $this->getVal( 'UseExternalEditor' ) && ( $external || $user->getOption( 'externaleditor' ) ) ) {
 515+ } elseif ( $this->getVal( 'UseExternalEditor' ) && ( $external || $user->getOption( 'externaleditor' ) ) ) {
540516 $mode = $request->getVal( 'mode' );
541517 $extedit = new ExternalEdit( $article, $mode );
542518 $extedit->edit();
@@ -543,7 +519,7 @@
544520 }
545521 break;
546522 case 'history':
547 - if( $request->getFullRequestURL() == $title->getInternalURL( 'action=history' ) ) {
 523+ if ( $request->getFullRequestURL() == $title->getInternalURL( 'action=history' ) ) {
548524 $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
549525 }
550526 $history = new HistoryPage( $article );
@@ -560,12 +536,10 @@
561537 $special->execute( '' );
562538 break;
563539 default:
564 - if( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
 540+ if ( wfRunHooks( 'UnknownAction', array( $action, $article ) ) ) {
565541 $output->showErrorPage( 'nosuchaction', 'nosuchactiontext' );
566542 }
567543 }
568544 wfProfileOut( __METHOD__ );
569 -
570545 }
571 -
572546 }
Index: trunk/phase3/index.php
@@ -47,8 +47,12 @@
4848 $mediaWiki = new MediaWiki();
4949
5050 $maxLag = $wgRequest->getVal( 'maxlag' );
51 -if( !is_null( $maxLag ) && !$mediaWiki->checkMaxLag( $maxLag ) ) {
52 - exit;
 51+if ( !is_null( $maxLag ) ) {
 52+ list( $host, $lag ) = wfGetLB()->getMaxLag();
 53+ if ( $lag > $maxLag ) {
 54+ wfMaxlagError( $host, $lag, $maxLag );
 55+ exit;
 56+ }
5357 }
5458
5559 # Set title from request parameters
@@ -58,7 +62,7 @@
5963 wfProfileOut( 'index.php-setup' );
6064
6165 # Send Ajax requests to the Ajax dispatcher.
62 -if( $wgUseAjax && $action == 'ajax' ) {
 66+if ( $wgUseAjax && $action == 'ajax' ) {
6367 $dispatcher = new AjaxDispatcher();
6468 $dispatcher->performAction();
6569 wfProfileOut( 'index.php' );
@@ -66,16 +70,16 @@
6771 exit;
6872 }
6973
70 -if( $wgUseFileCache && $wgTitle !== null ) {
 74+if ( $wgUseFileCache && $wgTitle !== null ) {
7175 wfProfileIn( 'index.php-filecache' );
7276 // Raw pages should handle cache control on their own,
7377 // even when using file cache. This reduces hits from clients.
74 - if( $action != 'raw' && HTMLFileCache::useFileCache() ) {
 78+ if ( $action != 'raw' && HTMLFileCache::useFileCache() ) {
7579 /* Try low-level file cache hit */
7680 $cache = new HTMLFileCache( $wgTitle, $action );
77 - if( $cache->isFileCacheGood( /* Assume up to date */ ) ) {
 81+ if ( $cache->isFileCacheGood( /* Assume up to date */ ) ) {
7882 /* Check incoming headers to see if client has this cached */
79 - if( !$wgOut->checkLastModified( $cache->fileCacheTime() ) ) {
 83+ if ( !$wgOut->checkLastModified( $cache->fileCacheTime() ) ) {
8084 $cache->loadFromFileCache();
8185 }
8286 # Do any stats increment/watchlist stuff

Status & tagging log