Example #1
0
 /**
  * Execute the actual module, without any error handling
  */
 protected function executeAction()
 {
     // First add the id to the top element
     if ($this->mRequest->getCheck('requestid')) {
         $this->getResult()->addValue(null, 'requestid', $this->mRequest->getVal('requestid'));
     }
     $params = $this->extractRequestParams();
     $this->mShowVersions = $params['version'];
     $this->mAction = $params['action'];
     if (!is_string($this->mAction)) {
         $this->dieUsage("The API requires a valid action parameter", 'unknown_action');
     }
     // Instantiate the module requested by the user
     $module = new $this->mModules[$this->mAction]($this, $this->mAction);
     if ($module->shouldCheckMaxlag() && isset($params['maxlag'])) {
         // Check for maxlag
         global $wgShowHostnames;
         $maxLag = $params['maxlag'];
         list($host, $lag) = wfGetLB()->getMaxLag();
         if ($lag > $maxLag) {
             header('Retry-After: ' . max(intval($maxLag), 5));
             header('X-Database-Lag: ' . intval($lag));
             // XXX: should we return a 503 HTTP error code like wfMaxlagError() does?
             if ($wgShowHostnames) {
                 ApiBase::dieUsage("Waiting for {$host}: {$lag} seconds lagged", 'maxlag');
             } else {
                 ApiBase::dieUsage("Waiting for a database server: {$lag} seconds lagged", 'maxlag');
             }
             return;
         }
     }
     if (!$this->mInternalMode) {
         // Ignore mustBePosted() for internal calls
         if ($module->mustBePosted() && !$this->mRequest->wasPosted()) {
             $this->dieUsage("The {$this->mAction} module requires a POST request", 'mustbeposted');
         }
         // 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();
         }
     }
     // Execute
     $module->profileIn();
     $module->execute();
     wfRunHooks('APIAfterExecute', array(&$module));
     $module->profileOut();
     if (!$this->mInternalMode) {
         // Print result data
         $this->printResult(false);
     }
 }
Example #2
0
 /**
  * Set up the module for response
  * @return ApiBase The module that will handle this action
  */
 protected function setupModule()
 {
     // Instantiate the module requested by the user
     // RT #1576 5.12.2008 Bartek
     if (class_exists($this->mModules[$this->mAction])) {
         $module = new $this->mModules[$this->mAction]($this, $this->mAction);
         $this->mModule = $module;
     } else {
         ApiBase::dieUsage("Trying to load a nonexistant or undefined classname");
         return;
     }
     $moduleParams = $module->extractRequestParams();
     // Die if token required, but not provided (unless there is a gettoken parameter)
     if (isset($moduleParams['gettoken'])) {
         $gettoken = $moduleParams['gettoken'];
     } else {
         $gettoken = false;
     }
     $salt = $module->getTokenSalt();
     if ($salt !== false && !$gettoken) {
         if (!isset($moduleParams['token'])) {
             $this->dieUsageMsg(array('missingparam', 'token'));
         } else {
             if (!$this->getUser()->matchEditToken($moduleParams['token'], $salt, $this->getRequest())) {
                 $this->dieUsageMsg('sessionfailure');
             }
         }
     }
     return $module;
 }
 /**
  * Execute the actual module, without any error handling
  */
 protected function executeAction()
 {
     $params = $this->extractRequestParams();
     $this->mShowVersions = $params['version'];
     $this->mAction = $params['action'];
     // Instantiate the module requested by the user
     $module = new $this->mModules[$this->mAction]($this, $this->mAction);
     if ($module->shouldCheckMaxlag() && isset($params['maxlag'])) {
         // Check for maxlag
         global $wgLoadBalancer, $wgShowHostnames;
         $maxLag = $params['maxlag'];
         list($host, $lag) = $wgLoadBalancer->getMaxLag();
         if ($lag > $maxLag) {
             if ($wgShowHostnames) {
                 ApiBase::dieUsage("Waiting for {$host}: {$lag} seconds lagged", 'maxlag');
             } else {
                 ApiBase::dieUsage("Waiting for a database server: {$lag} seconds lagged", 'maxlag');
             }
             return;
         }
     }
     if (!$this->mInternalMode) {
         // Ignore mustBePosted() for internal calls
         if ($module->mustBePosted() && !$this->mRequest->wasPosted()) {
             $this->dieUsage("The {$this->mAction} module requires a POST request", 'mustbeposted');
         }
         // 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();
         }
     }
     // Execute
     $module->profileIn();
     $module->execute();
     $module->profileOut();
     if (!$this->mInternalMode) {
         // Print result data
         $this->printResult(false);
     }
 }