Index: trunk/phase3/includes/api/ApiMain.php |
— | — | @@ -397,9 +397,9 @@ |
398 | 398 | } |
399 | 399 | |
400 | 400 | /** |
401 | | - * Execute the actual module, without any error handling |
| 401 | + * Set up for the execution. |
402 | 402 | */ |
403 | | - protected function executeAction() { |
| 403 | + protected function setupExecuteAction() { |
404 | 404 | // First add the id to the top element |
405 | 405 | $requestid = $this->getParameter( 'requestid' ); |
406 | 406 | if ( !is_null( $requestid ) ) { |
— | — | @@ -415,6 +415,14 @@ |
416 | 416 | $this->dieUsage( 'The API requires a valid action parameter', 'unknown_action' ); |
417 | 417 | } |
418 | 418 | |
| 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() { |
419 | 427 | // Instantiate the module requested by the user |
420 | 428 | $module = new $this->mModules[$this->mAction] ( $this, $this->mAction ); |
421 | 429 | $this->mModule = $module; |
— | — | @@ -433,7 +441,15 @@ |
434 | 442 | } |
435 | 443 | } |
436 | 444 | } |
| 445 | + return $module; |
| 446 | + } |
437 | 447 | |
| 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) { |
438 | 454 | if ( $module->shouldCheckMaxlag() && isset( $params['maxlag'] ) ) { |
439 | 455 | // Check for maxlag |
440 | 456 | global $wgShowHostnames; |
— | — | @@ -447,10 +463,18 @@ |
448 | 464 | } else { |
449 | 465 | $this->dieUsage( "Waiting for a database server: $lag seconds lagged", 'maxlag' ); |
450 | 466 | } |
451 | | - return; |
| 467 | + return false |
452 | 468 | } |
453 | 469 | } |
| 470 | + return true; |
| 471 | + } |
454 | 472 | |
| 473 | + |
| 474 | + /** |
| 475 | + * Check for sufficient permissions to execute |
| 476 | + * @param $module object An Api module |
| 477 | + */ |
| 478 | + protected function checkExecutePermissions($module) { |
455 | 479 | global $wgUser, $wgGroupPermissions; |
456 | 480 | if ( $module->isReadMode() && !$wgGroupPermissions['*']['read'] && !$wgUser->isAllowed( 'read' ) ) |
457 | 481 | { |
— | — | @@ -467,25 +491,46 @@ |
468 | 492 | $this->dieReadOnly(); |
469 | 493 | } |
470 | 494 | } |
| 495 | + } |
471 | 496 | |
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 | + } |
477 | 507 | |
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 | + } |
484 | 514 | |
485 | | - if ( $this->mPrinter->getNeedsRawData() ) { |
486 | | - $this->getResult()->setRawMode(); |
487 | | - } |
| 515 | + if ( $this->mPrinter->getNeedsRawData() ) { |
| 516 | + $this->getResult()->setRawMode(); |
488 | 517 | } |
| 518 | + } |
489 | 519 | |
| 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 | + |
490 | 535 | // Execute |
491 | 536 | $module->profileIn(); |
492 | 537 | $module->execute(); |