From 545fdc8c8fae9c25bd5683f58c07bd793558a6ea Mon Sep 17 00:00:00 2001 From: "Mark A. Hershberger" Date: Tue, 30 Mar 2010 17:14:53 +0000 Subject: [PATCH] Refactor ApiMain to make the code more readable. --- includes/api/ApiMain.php | 79 +++++++++++++++++++++++++++++++--------- 1 file changed, 62 insertions(+), 17 deletions(-) diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index 944f96cb4a..3c8c024dd8 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -397,9 +397,9 @@ class ApiMain extends ApiBase { } /** - * Execute the actual module, without any error handling + * Set up for the execution. */ - protected function executeAction() { + protected function setupExecuteAction() { // First add the id to the top element $requestid = $this->getParameter( 'requestid' ); if ( !is_null( $requestid ) ) { @@ -415,6 +415,14 @@ class ApiMain extends ApiBase { $this->dieUsage( 'The API requires a valid action parameter', 'unknown_action' ); } + return $params; + } + + /** + * Set up the module for response + * @return Object the module that will handle this action + */ + protected function setupModule() { // Instantiate the module requested by the user $module = new $this->mModules[$this->mAction] ( $this, $this->mAction ); $this->mModule = $module; @@ -433,7 +441,15 @@ class ApiMain extends ApiBase { } } } + return $module; + } + /** + * Check the max lag if necessary + * @param $params Array an array containing the request parameters. + * @return boolean True on success, false should exit immediately + */ + protected function checkMaxLag($module, $params) { if ( $module->shouldCheckMaxlag() && isset( $params['maxlag'] ) ) { // Check for maxlag global $wgShowHostnames; @@ -447,10 +463,18 @@ class ApiMain extends ApiBase { } else { $this->dieUsage( "Waiting for a database server: $lag seconds lagged", 'maxlag' ); } - return; + return false } } + return true; + } + + /** + * Check for sufficient permissions to execute + * @param $module object An Api module + */ + protected function checkExecutePermissions($module) { global $wgUser, $wgGroupPermissions; if ( $module->isReadMode() && !$wgGroupPermissions['*']['read'] && !$wgUser->isAllowed( 'read' ) ) { @@ -467,23 +491,44 @@ class ApiMain extends ApiBase { $this->dieReadOnly(); } } + } - if ( !$this->mInternalMode ) { - // Ignore mustBePosted() for internal calls - if ( $module->mustBePosted() && !$this->mRequest->wasPosted() ) { - $this->dieUsageMsg( array( 'mustbeposted', $this->mAction ) ); - } + /** + * Check POST for external response and setup result printer + * @param $module object An Api module + * @param $params Array an array with the request parameters + */ + protected function setupExternalResponse($module) { + // Ignore mustBePosted() for internal calls + if ( $module->mustBePosted() && !$this->mRequest->wasPosted() ) { + $this->dieUsageMsg( array( 'mustbeposted', $this->mAction ) ); + } - // See if custom printer is used - $this->mPrinter = $module->getCustomPrinter(); - if ( is_null( $this->mPrinter ) ) { - // Create an appropriate printer - $this->mPrinter = $this->createPrinterByName( $params['format'] ); - } + // See if custom printer is used + $this->mPrinter = $module->getCustomPrinter(); + if ( is_null( $this->mPrinter ) ) { + // Create an appropriate printer + $this->mPrinter = $this->createPrinterByName( $params['format'] ); + } - if ( $this->mPrinter->getNeedsRawData() ) { - $this->getResult()->setRawMode(); - } + if ( $this->mPrinter->getNeedsRawData() ) { + $this->getResult()->setRawMode(); + } + } + + /** + * Execute the actual module, without any error handling + */ + protected function executeAction() { + $params = $this->setupExecuteAction(); + $module = $this->setupModule(); + + $this->checkExecutePermissions($module); + + if(!$this->checkMaxLag($module, $params)) return; + + if ( !$this->mInternalMode ) { + $this->setupExternalResponse($module, $params); } // Execute -- 2.20.1