}
/**
- * 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 ) ) {
$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;
}
}
}
+ 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;
} 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' ) )
{
$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