Index: trunk/phase3/includes/api/ApiMain.php |
— | — | @@ -121,6 +121,13 @@ |
122 | 122 | } |
123 | 123 | |
124 | 124 | /** |
| 125 | + * Return true if the API was started by other PHP code using FauxRequest |
| 126 | + */ |
| 127 | + public function isInternalMode() { |
| 128 | + return $this->mInternalMode; |
| 129 | + } |
| 130 | + |
| 131 | + /** |
125 | 132 | * Return the request object that contains client's request |
126 | 133 | */ |
127 | 134 | public function getRequest() { |
— | — | @@ -188,9 +195,43 @@ |
189 | 196 | // handler will process and log it. |
190 | 197 | // |
191 | 198 | |
| 199 | + $errCode = $this->substituteResultWithError($e); |
| 200 | + |
192 | 201 | // Error results should not be cached |
193 | 202 | $this->setCacheMaxAge(0); |
194 | 203 | |
| 204 | + $headerStr = 'MediaWiki-API-Error: ' . $errCode; |
| 205 | + if ($e->getCode() === 0) |
| 206 | + header($headerStr, true); |
| 207 | + else |
| 208 | + header($headerStr, true, $e->getCode()); |
| 209 | + |
| 210 | + // Reset and print just the error message |
| 211 | + ob_clean(); |
| 212 | + |
| 213 | + // If the error occured during printing, do a printer->profileOut() |
| 214 | + $this->mPrinter->safeProfileOut(); |
| 215 | + $this->printResult(true); |
| 216 | + } |
| 217 | + |
| 218 | + // Set the cache expiration at the last moment, as any errors may change the expiration. |
| 219 | + // if $this->mSquidMaxage == 0, the expiry time is set to the first second of unix epoch |
| 220 | + $expires = $this->mSquidMaxage == 0 ? 1 : time() + $this->mSquidMaxage; |
| 221 | + header('Expires: ' . wfTimestamp(TS_RFC2822, $expires)); |
| 222 | + header('Cache-Control: s-maxage=' . $this->mSquidMaxage . ', must-revalidate, max-age=0'); |
| 223 | + |
| 224 | + if($this->mPrinter->getIsHtml()) |
| 225 | + echo wfReportTime(); |
| 226 | + |
| 227 | + ob_end_flush(); |
| 228 | + } |
| 229 | + |
| 230 | + /** |
| 231 | + * Replace the result data with the information about an exception. |
| 232 | + * Returns the error code |
| 233 | + */ |
| 234 | + protected function substituteResultWithError($e) { |
| 235 | + |
195 | 236 | // Printer may not be initialized if the extractRequestParams() fails for the main module |
196 | 237 | if (!isset ($this->mPrinter)) { |
197 | 238 | // The printer has not been created yet. Try to manually get formatter value. |
— | — | @@ -208,7 +249,8 @@ |
209 | 250 | // User entered incorrect parameters - print usage screen |
210 | 251 | // |
211 | 252 | $errMessage = array ( |
212 | | - 'code' => $e->getCodeString(), 'info' => $e->getMessage()); |
| 253 | + 'code' => $e->getCodeString(), |
| 254 | + 'info' => $e->getMessage()); |
213 | 255 | |
214 | 256 | // Only print the help message when this is for the developer, not runtime |
215 | 257 | if ($this->mPrinter->getIsHtml() || $this->mAction == 'help') |
— | — | @@ -225,32 +267,10 @@ |
226 | 268 | ApiResult :: setContent($errMessage, "\n\n{$e->getTraceAsString()}\n\n"); |
227 | 269 | } |
228 | 270 | |
229 | | - $headerStr = 'MediaWiki-API-Error: ' . $errMessage['code']; |
230 | | - if ($e->getCode() === 0) |
231 | | - header($headerStr, true); |
232 | | - else |
233 | | - header($headerStr, true, $e->getCode()); |
234 | | - |
235 | | - // Reset and print just the error message |
236 | | - ob_clean(); |
237 | 271 | $this->getResult()->reset(); |
238 | 272 | $this->getResult()->addValue(null, 'error', $errMessage); |
239 | 273 | |
240 | | - // If the error occured during printing, do a printer->profileOut() |
241 | | - $this->mPrinter->safeProfileOut(); |
242 | | - $this->printResult(true); |
243 | | - } |
244 | | - |
245 | | - // Set the cache expiration at the last moment, as any errors may change the expiration. |
246 | | - // if $this->mSquidMaxage == 0, the expiry time is set to the first second of unix epoch |
247 | | - $expires = $this->mSquidMaxage == 0 ? 1 : time() + $this->mSquidMaxage; |
248 | | - header('Expires: ' . wfTimestamp(TS_RFC2822, $expires)); |
249 | | - header('Cache-Control: s-maxage=' . $this->mSquidMaxage . ', must-revalidate, max-age=0'); |
250 | | - |
251 | | - if($this->mPrinter->getIsHtml()) |
252 | | - echo wfReportTime(); |
253 | | - |
254 | | - ob_end_flush(); |
| 274 | + return $errMessage['code']; |
255 | 275 | } |
256 | 276 | |
257 | 277 | /** |
Index: trunk/phase3/includes/api/ApiOpenSearch.php |
— | — | @@ -42,8 +42,8 @@ |
43 | 43 | } |
44 | 44 | |
45 | 45 | public function execute() { |
46 | | - $search = null; |
47 | | - extract($this->ExtractRequestParams()); |
| 46 | + $params = $this->extractRequestParams(); |
| 47 | + $search = $params['search']; |
48 | 48 | |
49 | 49 | // Open search results may be stored for a very long time |
50 | 50 | $this->getMain()->setCacheMaxAge(1200); |
— | — | @@ -53,7 +53,7 @@ |
54 | 54 | return; // Return empty result |
55 | 55 | |
56 | 56 | // Prepare nested request |
57 | | - $params = new FauxRequest(array ( |
| 57 | + $req = new FauxRequest(array ( |
58 | 58 | 'action' => 'query', |
59 | 59 | 'list' => 'allpages', |
60 | 60 | 'apnamespace' => $title->getNamespace(), |
— | — | @@ -62,7 +62,7 @@ |
63 | 63 | )); |
64 | 64 | |
65 | 65 | // Execute |
66 | | - $module = new ApiMain($params); |
| 66 | + $module = new ApiMain($req); |
67 | 67 | $module->execute(); |
68 | 68 | |
69 | 69 | // Get resulting data |
Index: trunk/phase3/includes/api/ApiBase.php |
— | — | @@ -478,6 +478,10 @@ |
479 | 479 | $this->dieUsage($this->encodeParamName($paramName) . " may not be less than $min (set to $value)", $paramName); |
480 | 480 | } |
481 | 481 | |
| 482 | + // Minimum is always validated, whereas maximum is checked only if not running in internal call mode |
| 483 | + if ($this->getMain()->isInternalMode()) |
| 484 | + return; |
| 485 | + |
482 | 486 | // Optimization: do not check user's bot status unless really needed -- skips db query |
483 | 487 | // assumes $botMax >= $max |
484 | 488 | if (!is_null($max) && $value > $max) { |