r64397 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r64396‎ | r64397 | r64398 >
Date:17:14, 30 March 2010
Author:mah
Status:resolved (Comments)
Tags:
Comment:
Refactor ApiMain to make the code more readable.
Modified paths:
  • /trunk/phase3/includes/api/ApiMain.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiMain.php
@@ -397,9 +397,9 @@
398398 }
399399
400400 /**
401 - * Execute the actual module, without any error handling
 401+ * Set up for the execution.
402402 */
403 - protected function executeAction() {
 403+ protected function setupExecuteAction() {
404404 // First add the id to the top element
405405 $requestid = $this->getParameter( 'requestid' );
406406 if ( !is_null( $requestid ) ) {
@@ -415,6 +415,14 @@
416416 $this->dieUsage( 'The API requires a valid action parameter', 'unknown_action' );
417417 }
418418
 419+ return $params;
 420+ }
 421+
 422+ /**
 423+ * Set up the module for response
 424+ * @return Object the module that will handle this action
 425+ */
 426+ protected function setupModule() {
419427 // Instantiate the module requested by the user
420428 $module = new $this->mModules[$this->mAction] ( $this, $this->mAction );
421429 $this->mModule = $module;
@@ -433,7 +441,15 @@
434442 }
435443 }
436444 }
 445+ return $module;
 446+ }
437447
 448+ /**
 449+ * Check the max lag if necessary
 450+ * @param $params Array an array containing the request parameters.
 451+ * @return boolean True on success, false should exit immediately
 452+ */
 453+ protected function checkMaxLag($module, $params) {
438454 if ( $module->shouldCheckMaxlag() && isset( $params['maxlag'] ) ) {
439455 // Check for maxlag
440456 global $wgShowHostnames;
@@ -447,10 +463,18 @@
448464 } else {
449465 $this->dieUsage( "Waiting for a database server: $lag seconds lagged", 'maxlag' );
450466 }
451 - return;
 467+ return false
452468 }
453469 }
 470+ return true;
 471+ }
454472
 473+
 474+ /**
 475+ * Check for sufficient permissions to execute
 476+ * @param $module object An Api module
 477+ */
 478+ protected function checkExecutePermissions($module) {
455479 global $wgUser, $wgGroupPermissions;
456480 if ( $module->isReadMode() && !$wgGroupPermissions['*']['read'] && !$wgUser->isAllowed( 'read' ) )
457481 {
@@ -467,25 +491,46 @@
468492 $this->dieReadOnly();
469493 }
470494 }
 495+ }
471496
472 - if ( !$this->mInternalMode ) {
473 - // Ignore mustBePosted() for internal calls
474 - if ( $module->mustBePosted() && !$this->mRequest->wasPosted() ) {
475 - $this->dieUsageMsg( array( 'mustbeposted', $this->mAction ) );
476 - }
 497+ /**
 498+ * Check POST for external response and setup result printer
 499+ * @param $module object An Api module
 500+ * @param $params Array an array with the request parameters
 501+ */
 502+ protected function setupExternalResponse($module) {
 503+ // Ignore mustBePosted() for internal calls
 504+ if ( $module->mustBePosted() && !$this->mRequest->wasPosted() ) {
 505+ $this->dieUsageMsg( array( 'mustbeposted', $this->mAction ) );
 506+ }
477507
478 - // See if custom printer is used
479 - $this->mPrinter = $module->getCustomPrinter();
480 - if ( is_null( $this->mPrinter ) ) {
481 - // Create an appropriate printer
482 - $this->mPrinter = $this->createPrinterByName( $params['format'] );
483 - }
 508+ // See if custom printer is used
 509+ $this->mPrinter = $module->getCustomPrinter();
 510+ if ( is_null( $this->mPrinter ) ) {
 511+ // Create an appropriate printer
 512+ $this->mPrinter = $this->createPrinterByName( $params['format'] );
 513+ }
484514
485 - if ( $this->mPrinter->getNeedsRawData() ) {
486 - $this->getResult()->setRawMode();
487 - }
 515+ if ( $this->mPrinter->getNeedsRawData() ) {
 516+ $this->getResult()->setRawMode();
488517 }
 518+ }
489519
 520+ /**
 521+ * Execute the actual module, without any error handling
 522+ */
 523+ protected function executeAction() {
 524+ $params = $this->setupExecuteAction();
 525+ $module = $this->setupModule();
 526+
 527+ $this->checkExecutePermissions($module);
 528+
 529+ if(!$this->checkMaxLag($module, $params)) return;
 530+
 531+ if ( !$this->mInternalMode ) {
 532+ $this->setupExternalResponse($module, $params);
 533+ }
 534+
490535 // Execute
491536 $module->profileIn();
492537 $module->execute();

Follow-up revisions

RevisionCommit summaryAuthorDate
r64942Fix param names mismatch in code/doc from r64852 and r64397ialex19:10, 11 April 2010

Comments

#Comment by Bryan (talk | contribs)   16:58, 31 March 2010

Please test your code before comitting. If you would have opened api.php in your browser, you would have very easily noticed that a syntax error slipped in.

Status & tagging log