From 380f66878f9d513b8f98f189e68d1fdc46dda28f Mon Sep 17 00:00:00 2001 From: Yuri Astrakhan Date: Sun, 15 Jul 2007 00:52:35 +0000 Subject: [PATCH] API: Removed maximum limit checking when running api in an internal mode. --- includes/api/ApiBase.php | 4 ++ includes/api/ApiMain.php | 68 ++++++++++++++++++++++------------ includes/api/ApiOpenSearch.php | 8 ++-- 3 files changed, 52 insertions(+), 28 deletions(-) diff --git a/includes/api/ApiBase.php b/includes/api/ApiBase.php index bd17725a59..536b1b478b 100644 --- a/includes/api/ApiBase.php +++ b/includes/api/ApiBase.php @@ -478,6 +478,10 @@ abstract class ApiBase { $this->dieUsage($this->encodeParamName($paramName) . " may not be less than $min (set to $value)", $paramName); } + // Minimum is always validated, whereas maximum is checked only if not running in internal call mode + if ($this->getMain()->isInternalMode()) + return; + // Optimization: do not check user's bot status unless really needed -- skips db query // assumes $botMax >= $max if (!is_null($max) && $value > $max) { diff --git a/includes/api/ApiMain.php b/includes/api/ApiMain.php index 5313c9e133..ceb16d24cd 100644 --- a/includes/api/ApiMain.php +++ b/includes/api/ApiMain.php @@ -120,6 +120,13 @@ class ApiMain extends ApiBase { $this->mSquidMaxage = 0; } + /** + * Return true if the API was started by other PHP code using FauxRequest + */ + public function isInternalMode() { + return $this->mInternalMode; + } + /** * Return the request object that contains client's request */ @@ -188,9 +195,43 @@ class ApiMain extends ApiBase { // handler will process and log it. // + $errCode = $this->substituteResultWithError($e); + // Error results should not be cached $this->setCacheMaxAge(0); + $headerStr = 'MediaWiki-API-Error: ' . $errCode; + if ($e->getCode() === 0) + header($headerStr, true); + else + header($headerStr, true, $e->getCode()); + + // Reset and print just the error message + ob_clean(); + + // If the error occured during printing, do a printer->profileOut() + $this->mPrinter->safeProfileOut(); + $this->printResult(true); + } + + // Set the cache expiration at the last moment, as any errors may change the expiration. + // if $this->mSquidMaxage == 0, the expiry time is set to the first second of unix epoch + $expires = $this->mSquidMaxage == 0 ? 1 : time() + $this->mSquidMaxage; + header('Expires: ' . wfTimestamp(TS_RFC2822, $expires)); + header('Cache-Control: s-maxage=' . $this->mSquidMaxage . ', must-revalidate, max-age=0'); + + if($this->mPrinter->getIsHtml()) + echo wfReportTime(); + + ob_end_flush(); + } + + /** + * Replace the result data with the information about an exception. + * Returns the error code + */ + protected function substituteResultWithError($e) { + // Printer may not be initialized if the extractRequestParams() fails for the main module if (!isset ($this->mPrinter)) { // The printer has not been created yet. Try to manually get formatter value. @@ -208,7 +249,8 @@ class ApiMain extends ApiBase { // User entered incorrect parameters - print usage screen // $errMessage = array ( - 'code' => $e->getCodeString(), 'info' => $e->getMessage()); + 'code' => $e->getCodeString(), + 'info' => $e->getMessage()); // Only print the help message when this is for the developer, not runtime if ($this->mPrinter->getIsHtml() || $this->mAction == 'help') @@ -225,32 +267,10 @@ class ApiMain extends ApiBase { ApiResult :: setContent($errMessage, "\n\n{$e->getTraceAsString()}\n\n"); } - $headerStr = 'MediaWiki-API-Error: ' . $errMessage['code']; - if ($e->getCode() === 0) - header($headerStr, true); - else - header($headerStr, true, $e->getCode()); - - // Reset and print just the error message - ob_clean(); $this->getResult()->reset(); $this->getResult()->addValue(null, 'error', $errMessage); - // If the error occured during printing, do a printer->profileOut() - $this->mPrinter->safeProfileOut(); - $this->printResult(true); - } - - // Set the cache expiration at the last moment, as any errors may change the expiration. - // if $this->mSquidMaxage == 0, the expiry time is set to the first second of unix epoch - $expires = $this->mSquidMaxage == 0 ? 1 : time() + $this->mSquidMaxage; - header('Expires: ' . wfTimestamp(TS_RFC2822, $expires)); - header('Cache-Control: s-maxage=' . $this->mSquidMaxage . ', must-revalidate, max-age=0'); - - if($this->mPrinter->getIsHtml()) - echo wfReportTime(); - - ob_end_flush(); + return $errMessage['code']; } /** diff --git a/includes/api/ApiOpenSearch.php b/includes/api/ApiOpenSearch.php index 18e99573d2..fe8217b704 100644 --- a/includes/api/ApiOpenSearch.php +++ b/includes/api/ApiOpenSearch.php @@ -42,8 +42,8 @@ class ApiOpenSearch extends ApiBase { } public function execute() { - $search = null; - extract($this->ExtractRequestParams()); + $params = $this->extractRequestParams(); + $search = $params['search']; // Open search results may be stored for a very long time $this->getMain()->setCacheMaxAge(1200); @@ -53,7 +53,7 @@ class ApiOpenSearch extends ApiBase { return; // Return empty result // Prepare nested request - $params = new FauxRequest(array ( + $req = new FauxRequest(array ( 'action' => 'query', 'list' => 'allpages', 'apnamespace' => $title->getNamespace(), @@ -62,7 +62,7 @@ class ApiOpenSearch extends ApiBase { )); // Execute - $module = new ApiMain($params); + $module = new ApiMain($req); $module->execute(); // Get resulting data -- 2.20.1