r24099 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r24098‎ | r24099 | r24100 >
Date:00:52, 15 July 2007
Author:yurik
Status:old
Tags:
Comment:
API: Removed maximum limit checking when running api in an internal mode.
Modified paths:
  • /trunk/phase3/includes/api/ApiBase.php (modified) (history)
  • /trunk/phase3/includes/api/ApiMain.php (modified) (history)
  • /trunk/phase3/includes/api/ApiOpenSearch.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiMain.php
@@ -121,6 +121,13 @@
122122 }
123123
124124 /**
 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+ /**
125132 * Return the request object that contains client's request
126133 */
127134 public function getRequest() {
@@ -188,9 +195,43 @@
189196 // handler will process and log it.
190197 //
191198
 199+ $errCode = $this->substituteResultWithError($e);
 200+
192201 // Error results should not be cached
193202 $this->setCacheMaxAge(0);
194203
 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+
195236 // Printer may not be initialized if the extractRequestParams() fails for the main module
196237 if (!isset ($this->mPrinter)) {
197238 // The printer has not been created yet. Try to manually get formatter value.
@@ -208,7 +249,8 @@
209250 // User entered incorrect parameters - print usage screen
210251 //
211252 $errMessage = array (
212 - 'code' => $e->getCodeString(), 'info' => $e->getMessage());
 253+ 'code' => $e->getCodeString(),
 254+ 'info' => $e->getMessage());
213255
214256 // Only print the help message when this is for the developer, not runtime
215257 if ($this->mPrinter->getIsHtml() || $this->mAction == 'help')
@@ -225,32 +267,10 @@
226268 ApiResult :: setContent($errMessage, "\n\n{$e->getTraceAsString()}\n\n");
227269 }
228270
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();
237271 $this->getResult()->reset();
238272 $this->getResult()->addValue(null, 'error', $errMessage);
239273
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'];
255275 }
256276
257277 /**
Index: trunk/phase3/includes/api/ApiOpenSearch.php
@@ -42,8 +42,8 @@
4343 }
4444
4545 public function execute() {
46 - $search = null;
47 - extract($this->ExtractRequestParams());
 46+ $params = $this->extractRequestParams();
 47+ $search = $params['search'];
4848
4949 // Open search results may be stored for a very long time
5050 $this->getMain()->setCacheMaxAge(1200);
@@ -53,7 +53,7 @@
5454 return; // Return empty result
5555
5656 // Prepare nested request
57 - $params = new FauxRequest(array (
 57+ $req = new FauxRequest(array (
5858 'action' => 'query',
5959 'list' => 'allpages',
6060 'apnamespace' => $title->getNamespace(),
@@ -62,7 +62,7 @@
6363 ));
6464
6565 // Execute
66 - $module = new ApiMain($params);
 66+ $module = new ApiMain($req);
6767 $module->execute();
6868
6969 // Get resulting data
Index: trunk/phase3/includes/api/ApiBase.php
@@ -478,6 +478,10 @@
479479 $this->dieUsage($this->encodeParamName($paramName) . " may not be less than $min (set to $value)", $paramName);
480480 }
481481
 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+
482486 // Optimization: do not check user's bot status unless really needed -- skips db query
483487 // assumes $botMax >= $max
484488 if (!is_null($max) && $value > $max) {

Follow-up revisions

RevisionCommit summaryAuthorDate
r24215Merged revisions 24095-24212 via svnmerge from...david21:19, 17 July 2007

Status & tagging log