Merge "API: Avoid duplicate IDs in API documentation"
[lhc/web/wiklou.git] / includes / api / ApiMain.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 4, 2006
6 *
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 * @defgroup API API
26 */
27
28 /**
29 * This is the main API class, used for both external and internal processing.
30 * When executed, it will create the requested formatter object,
31 * instantiate and execute an object associated with the needed action,
32 * and use formatter to print results.
33 * In case of an exception, an error message will be printed using the same formatter.
34 *
35 * To use API from another application, run it using FauxRequest object, in which
36 * case any internal exceptions will not be handled but passed up to the caller.
37 * After successful execution, use getResult() for the resulting data.
38 *
39 * @ingroup API
40 */
41 class ApiMain extends ApiBase {
42 /**
43 * When no format parameter is given, this format will be used
44 */
45 const API_DEFAULT_FORMAT = 'jsonfm';
46
47 /**
48 * List of available modules: action name => module class
49 */
50 private static $Modules = [
51 'login' => 'ApiLogin',
52 'clientlogin' => 'ApiClientLogin',
53 'logout' => 'ApiLogout',
54 'createaccount' => 'ApiAMCreateAccount',
55 'linkaccount' => 'ApiLinkAccount',
56 'unlinkaccount' => 'ApiRemoveAuthenticationData',
57 'changeauthenticationdata' => 'ApiChangeAuthenticationData',
58 'removeauthenticationdata' => 'ApiRemoveAuthenticationData',
59 'resetpassword' => 'ApiResetPassword',
60 'query' => 'ApiQuery',
61 'expandtemplates' => 'ApiExpandTemplates',
62 'parse' => 'ApiParse',
63 'stashedit' => 'ApiStashEdit',
64 'opensearch' => 'ApiOpenSearch',
65 'feedcontributions' => 'ApiFeedContributions',
66 'feedrecentchanges' => 'ApiFeedRecentChanges',
67 'feedwatchlist' => 'ApiFeedWatchlist',
68 'help' => 'ApiHelp',
69 'paraminfo' => 'ApiParamInfo',
70 'rsd' => 'ApiRsd',
71 'compare' => 'ApiComparePages',
72 'tokens' => 'ApiTokens',
73 'checktoken' => 'ApiCheckToken',
74
75 // Write modules
76 'purge' => 'ApiPurge',
77 'setnotificationtimestamp' => 'ApiSetNotificationTimestamp',
78 'rollback' => 'ApiRollback',
79 'delete' => 'ApiDelete',
80 'undelete' => 'ApiUndelete',
81 'protect' => 'ApiProtect',
82 'block' => 'ApiBlock',
83 'unblock' => 'ApiUnblock',
84 'move' => 'ApiMove',
85 'edit' => 'ApiEditPage',
86 'upload' => 'ApiUpload',
87 'filerevert' => 'ApiFileRevert',
88 'emailuser' => 'ApiEmailUser',
89 'watch' => 'ApiWatch',
90 'patrol' => 'ApiPatrol',
91 'import' => 'ApiImport',
92 'clearhasmsg' => 'ApiClearHasMsg',
93 'userrights' => 'ApiUserrights',
94 'options' => 'ApiOptions',
95 'imagerotate' => 'ApiImageRotate',
96 'revisiondelete' => 'ApiRevisionDelete',
97 'managetags' => 'ApiManageTags',
98 'tag' => 'ApiTag',
99 'mergehistory' => 'ApiMergeHistory',
100 ];
101
102 /**
103 * List of available formats: format name => format class
104 */
105 private static $Formats = [
106 'json' => 'ApiFormatJson',
107 'jsonfm' => 'ApiFormatJson',
108 'php' => 'ApiFormatPhp',
109 'phpfm' => 'ApiFormatPhp',
110 'xml' => 'ApiFormatXml',
111 'xmlfm' => 'ApiFormatXml',
112 'rawfm' => 'ApiFormatJson',
113 'none' => 'ApiFormatNone',
114 ];
115
116 // @codingStandardsIgnoreStart String contenation on "msg" not allowed to break long line
117 /**
118 * List of user roles that are specifically relevant to the API.
119 * array( 'right' => array ( 'msg' => 'Some message with a $1',
120 * 'params' => array ( $someVarToSubst ) ),
121 * );
122 */
123 private static $mRights = [
124 'writeapi' => [
125 'msg' => 'right-writeapi',
126 'params' => []
127 ],
128 'apihighlimits' => [
129 'msg' => 'api-help-right-apihighlimits',
130 'params' => [ ApiBase::LIMIT_SML2, ApiBase::LIMIT_BIG2 ]
131 ]
132 ];
133 // @codingStandardsIgnoreEnd
134
135 /**
136 * @var ApiFormatBase
137 */
138 private $mPrinter;
139
140 private $mModuleMgr, $mResult, $mErrorFormatter, $mContinuationManager;
141 private $mAction;
142 private $mEnableWrite;
143 private $mInternalMode, $mSquidMaxage;
144 /** @var ApiBase */
145 private $mModule;
146
147 private $mCacheMode = 'private';
148 private $mCacheControl = [];
149 private $mParamsUsed = [];
150
151 /**
152 * Constructs an instance of ApiMain that utilizes the module and format specified by $request.
153 *
154 * @param IContextSource|WebRequest $context If this is an instance of
155 * FauxRequest, errors are thrown and no printing occurs
156 * @param bool $enableWrite Should be set to true if the api may modify data
157 */
158 public function __construct( $context = null, $enableWrite = false ) {
159 if ( $context === null ) {
160 $context = RequestContext::getMain();
161 } elseif ( $context instanceof WebRequest ) {
162 // BC for pre-1.19
163 $request = $context;
164 $context = RequestContext::getMain();
165 }
166 // We set a derivative context so we can change stuff later
167 $this->setContext( new DerivativeContext( $context ) );
168
169 if ( isset( $request ) ) {
170 $this->getContext()->setRequest( $request );
171 }
172
173 $this->mInternalMode = ( $this->getRequest() instanceof FauxRequest );
174
175 // Special handling for the main module: $parent === $this
176 parent::__construct( $this, $this->mInternalMode ? 'main_int' : 'main' );
177
178 if ( !$this->mInternalMode ) {
179 // Impose module restrictions.
180 // If the current user cannot read,
181 // Remove all modules other than login
182 global $wgUser;
183
184 if ( $this->lacksSameOriginSecurity() ) {
185 // If we're in a mode that breaks the same-origin policy, strip
186 // user credentials for security.
187 wfDebug( "API: stripping user credentials when the same-origin policy is not applied\n" );
188 $wgUser = new User();
189 $this->getContext()->setUser( $wgUser );
190 }
191 }
192
193 $uselang = $this->getParameter( 'uselang' );
194 if ( $uselang === 'user' ) {
195 // Assume the parent context is going to return the user language
196 // for uselang=user (see T85635).
197 } else {
198 if ( $uselang === 'content' ) {
199 global $wgContLang;
200 $uselang = $wgContLang->getCode();
201 }
202 $code = RequestContext::sanitizeLangCode( $uselang );
203 $this->getContext()->setLanguage( $code );
204 if ( !$this->mInternalMode ) {
205 global $wgLang;
206 $wgLang = $this->getContext()->getLanguage();
207 RequestContext::getMain()->setLanguage( $wgLang );
208 }
209 }
210
211 $config = $this->getConfig();
212 $this->mModuleMgr = new ApiModuleManager( $this );
213 $this->mModuleMgr->addModules( self::$Modules, 'action' );
214 $this->mModuleMgr->addModules( $config->get( 'APIModules' ), 'action' );
215 $this->mModuleMgr->addModules( self::$Formats, 'format' );
216 $this->mModuleMgr->addModules( $config->get( 'APIFormatModules' ), 'format' );
217
218 Hooks::run( 'ApiMain::moduleManager', [ $this->mModuleMgr ] );
219
220 $this->mResult = new ApiResult( $this->getConfig()->get( 'APIMaxResultSize' ) );
221 $this->mErrorFormatter = new ApiErrorFormatter_BackCompat( $this->mResult );
222 $this->mResult->setErrorFormatter( $this->mErrorFormatter );
223 $this->mResult->setMainForContinuation( $this );
224 $this->mContinuationManager = null;
225 $this->mEnableWrite = $enableWrite;
226
227 $this->mSquidMaxage = -1; // flag for executeActionWithErrorHandling()
228 $this->mCommit = false;
229 }
230
231 /**
232 * Return true if the API was started by other PHP code using FauxRequest
233 * @return bool
234 */
235 public function isInternalMode() {
236 return $this->mInternalMode;
237 }
238
239 /**
240 * Get the ApiResult object associated with current request
241 *
242 * @return ApiResult
243 */
244 public function getResult() {
245 return $this->mResult;
246 }
247
248 /**
249 * Get the ApiErrorFormatter object associated with current request
250 * @return ApiErrorFormatter
251 */
252 public function getErrorFormatter() {
253 return $this->mErrorFormatter;
254 }
255
256 /**
257 * Get the continuation manager
258 * @return ApiContinuationManager|null
259 */
260 public function getContinuationManager() {
261 return $this->mContinuationManager;
262 }
263
264 /**
265 * Set the continuation manager
266 * @param ApiContinuationManager|null
267 */
268 public function setContinuationManager( $manager ) {
269 if ( $manager !== null ) {
270 if ( !$manager instanceof ApiContinuationManager ) {
271 throw new InvalidArgumentException( __METHOD__ . ': Was passed ' .
272 is_object( $manager ) ? get_class( $manager ) : gettype( $manager )
273 );
274 }
275 if ( $this->mContinuationManager !== null ) {
276 throw new UnexpectedValueException(
277 __METHOD__ . ': tried to set manager from ' . $manager->getSource() .
278 ' when a manager is already set from ' . $this->mContinuationManager->getSource()
279 );
280 }
281 }
282 $this->mContinuationManager = $manager;
283 }
284
285 /**
286 * Get the API module object. Only works after executeAction()
287 *
288 * @return ApiBase
289 */
290 public function getModule() {
291 return $this->mModule;
292 }
293
294 /**
295 * Get the result formatter object. Only works after setupExecuteAction()
296 *
297 * @return ApiFormatBase
298 */
299 public function getPrinter() {
300 return $this->mPrinter;
301 }
302
303 /**
304 * Set how long the response should be cached.
305 *
306 * @param int $maxage
307 */
308 public function setCacheMaxAge( $maxage ) {
309 $this->setCacheControl( [
310 'max-age' => $maxage,
311 's-maxage' => $maxage
312 ] );
313 }
314
315 /**
316 * Set the type of caching headers which will be sent.
317 *
318 * @param string $mode One of:
319 * - 'public': Cache this object in public caches, if the maxage or smaxage
320 * parameter is set, or if setCacheMaxAge() was called. If a maximum age is
321 * not provided by any of these means, the object will be private.
322 * - 'private': Cache this object only in private client-side caches.
323 * - 'anon-public-user-private': Make this object cacheable for logged-out
324 * users, but private for logged-in users. IMPORTANT: If this is set, it must be
325 * set consistently for a given URL, it cannot be set differently depending on
326 * things like the contents of the database, or whether the user is logged in.
327 *
328 * If the wiki does not allow anonymous users to read it, the mode set here
329 * will be ignored, and private caching headers will always be sent. In other words,
330 * the "public" mode is equivalent to saying that the data sent is as public as a page
331 * view.
332 *
333 * For user-dependent data, the private mode should generally be used. The
334 * anon-public-user-private mode should only be used where there is a particularly
335 * good performance reason for caching the anonymous response, but where the
336 * response to logged-in users may differ, or may contain private data.
337 *
338 * If this function is never called, then the default will be the private mode.
339 */
340 public function setCacheMode( $mode ) {
341 if ( !in_array( $mode, [ 'private', 'public', 'anon-public-user-private' ] ) ) {
342 wfDebug( __METHOD__ . ": unrecognised cache mode \"$mode\"\n" );
343
344 // Ignore for forwards-compatibility
345 return;
346 }
347
348 if ( !User::isEveryoneAllowed( 'read' ) ) {
349 // Private wiki, only private headers
350 if ( $mode !== 'private' ) {
351 wfDebug( __METHOD__ . ": ignoring request for $mode cache mode, private wiki\n" );
352
353 return;
354 }
355 }
356
357 if ( $mode === 'public' && $this->getParameter( 'uselang' ) === 'user' ) {
358 // User language is used for i18n, so we don't want to publicly
359 // cache. Anons are ok, because if they have non-default language
360 // then there's an appropriate Vary header set by whatever set
361 // their non-default language.
362 wfDebug( __METHOD__ . ": downgrading cache mode 'public' to " .
363 "'anon-public-user-private' due to uselang=user\n" );
364 $mode = 'anon-public-user-private';
365 }
366
367 wfDebug( __METHOD__ . ": setting cache mode $mode\n" );
368 $this->mCacheMode = $mode;
369 }
370
371 /**
372 * Set directives (key/value pairs) for the Cache-Control header.
373 * Boolean values will be formatted as such, by including or omitting
374 * without an equals sign.
375 *
376 * Cache control values set here will only be used if the cache mode is not
377 * private, see setCacheMode().
378 *
379 * @param array $directives
380 */
381 public function setCacheControl( $directives ) {
382 $this->mCacheControl = $directives + $this->mCacheControl;
383 }
384
385 /**
386 * Create an instance of an output formatter by its name
387 *
388 * @param string $format
389 *
390 * @return ApiFormatBase
391 */
392 public function createPrinterByName( $format ) {
393 $printer = $this->mModuleMgr->getModule( $format, 'format' );
394 if ( $printer === null ) {
395 $this->dieUsage( "Unrecognized format: {$format}", 'unknown_format' );
396 }
397
398 return $printer;
399 }
400
401 /**
402 * Execute api request. Any errors will be handled if the API was called by the remote client.
403 */
404 public function execute() {
405 if ( $this->mInternalMode ) {
406 $this->executeAction();
407 } else {
408 $this->executeActionWithErrorHandling();
409 }
410 }
411
412 /**
413 * Execute an action, and in case of an error, erase whatever partial results
414 * have been accumulated, and replace it with an error message and a help screen.
415 */
416 protected function executeActionWithErrorHandling() {
417 // Verify the CORS header before executing the action
418 if ( !$this->handleCORS() ) {
419 // handleCORS() has sent a 403, abort
420 return;
421 }
422
423 // Exit here if the request method was OPTIONS
424 // (assume there will be a followup GET or POST)
425 if ( $this->getRequest()->getMethod() === 'OPTIONS' ) {
426 return;
427 }
428
429 // In case an error occurs during data output,
430 // clear the output buffer and print just the error information
431 $obLevel = ob_get_level();
432 ob_start();
433
434 $t = microtime( true );
435 $isError = false;
436 try {
437 $this->executeAction();
438 $runTime = microtime( true ) - $t;
439 $this->logRequest( $runTime );
440 if ( $this->mModule->isWriteMode() && $this->getRequest()->wasPosted() ) {
441 $this->getStats()->timing(
442 'api.' . $this->getModuleName() . '.executeTiming', 1000 * $runTime );
443 }
444 } catch ( Exception $e ) {
445 $this->handleException( $e );
446 $this->logRequest( microtime( true ) - $t, $e );
447 $isError = true;
448 }
449
450 // Commit DBs and send any related cookies and headers
451 MediaWiki::preOutputCommit( $this->getContext() );
452
453 // Send cache headers after any code which might generate an error, to
454 // avoid sending public cache headers for errors.
455 $this->sendCacheHeaders( $isError );
456
457 // Executing the action might have already messed with the output
458 // buffers.
459 while ( ob_get_level() > $obLevel ) {
460 ob_end_flush();
461 }
462 }
463
464 /**
465 * Handle an exception as an API response
466 *
467 * @since 1.23
468 * @param Exception $e
469 */
470 protected function handleException( Exception $e ) {
471 // Bug 63145: Rollback any open database transactions
472 if ( !( $e instanceof UsageException ) ) {
473 // UsageExceptions are intentional, so don't rollback if that's the case
474 try {
475 MWExceptionHandler::rollbackMasterChangesAndLog( $e );
476 } catch ( DBError $e2 ) {
477 // Rollback threw an exception too. Log it, but don't interrupt
478 // our regularly scheduled exception handling.
479 MWExceptionHandler::logException( $e2 );
480 }
481 }
482
483 // Allow extra cleanup and logging
484 Hooks::run( 'ApiMain::onException', [ $this, $e ] );
485
486 // Log it
487 if ( !( $e instanceof UsageException ) ) {
488 MWExceptionHandler::logException( $e );
489 }
490
491 // Handle any kind of exception by outputting properly formatted error message.
492 // If this fails, an unhandled exception should be thrown so that global error
493 // handler will process and log it.
494
495 $errCode = $this->substituteResultWithError( $e );
496
497 // Error results should not be cached
498 $this->setCacheMode( 'private' );
499
500 $response = $this->getRequest()->response();
501 $headerStr = 'MediaWiki-API-Error: ' . $errCode;
502 if ( $e->getCode() === 0 ) {
503 $response->header( $headerStr );
504 } else {
505 $response->header( $headerStr, true, $e->getCode() );
506 }
507
508 // Reset and print just the error message
509 ob_clean();
510
511 // Printer may not be initialized if the extractRequestParams() fails for the main module
512 $this->createErrorPrinter();
513
514 try {
515 $this->printResult( true );
516 } catch ( UsageException $ex ) {
517 // The error printer itself is failing. Try suppressing its request
518 // parameters and redo.
519 $this->setWarning(
520 'Error printer failed (will retry without params): ' . $ex->getMessage()
521 );
522 $this->mPrinter = null;
523 $this->createErrorPrinter();
524 $this->mPrinter->forceDefaultParams();
525 $this->printResult( true );
526 }
527 }
528
529 /**
530 * Handle an exception from the ApiBeforeMain hook.
531 *
532 * This tries to print the exception as an API response, to be more
533 * friendly to clients. If it fails, it will rethrow the exception.
534 *
535 * @since 1.23
536 * @param Exception $e
537 * @throws Exception
538 */
539 public static function handleApiBeforeMainException( Exception $e ) {
540 ob_start();
541
542 try {
543 $main = new self( RequestContext::getMain(), false );
544 $main->handleException( $e );
545 $main->logRequest( 0, $e );
546 } catch ( Exception $e2 ) {
547 // Nope, even that didn't work. Punt.
548 throw $e;
549 }
550
551 // Reset cache headers
552 $main->sendCacheHeaders( true );
553
554 ob_end_flush();
555 }
556
557 /**
558 * Check the &origin= query parameter against the Origin: HTTP header and respond appropriately.
559 *
560 * If no origin parameter is present, nothing happens.
561 * If an origin parameter is present but doesn't match the Origin header, a 403 status code
562 * is set and false is returned.
563 * If the parameter and the header do match, the header is checked against $wgCrossSiteAJAXdomains
564 * and $wgCrossSiteAJAXdomainExceptions, and if the origin qualifies, the appropriate CORS
565 * headers are set.
566 * http://www.w3.org/TR/cors/#resource-requests
567 * http://www.w3.org/TR/cors/#resource-preflight-requests
568 *
569 * @return bool False if the caller should abort (403 case), true otherwise (all other cases)
570 */
571 protected function handleCORS() {
572 $originParam = $this->getParameter( 'origin' ); // defaults to null
573 if ( $originParam === null ) {
574 // No origin parameter, nothing to do
575 return true;
576 }
577
578 $request = $this->getRequest();
579 $response = $request->response();
580
581 // Origin: header is a space-separated list of origins, check all of them
582 $originHeader = $request->getHeader( 'Origin' );
583 if ( $originHeader === false ) {
584 $origins = [];
585 } else {
586 $originHeader = trim( $originHeader );
587 $origins = preg_split( '/\s+/', $originHeader );
588 }
589
590 if ( !in_array( $originParam, $origins ) ) {
591 // origin parameter set but incorrect
592 // Send a 403 response
593 $response->statusHeader( 403 );
594 $response->header( 'Cache-Control: no-cache' );
595 echo "'origin' parameter does not match Origin header\n";
596
597 return false;
598 }
599
600 $config = $this->getConfig();
601 $matchOrigin = count( $origins ) === 1 && self::matchOrigin(
602 $originParam,
603 $config->get( 'CrossSiteAJAXdomains' ),
604 $config->get( 'CrossSiteAJAXdomainExceptions' )
605 );
606
607 if ( $matchOrigin ) {
608 $requestedMethod = $request->getHeader( 'Access-Control-Request-Method' );
609 $preflight = $request->getMethod() === 'OPTIONS' && $requestedMethod !== false;
610 if ( $preflight ) {
611 // This is a CORS preflight request
612 if ( $requestedMethod !== 'POST' && $requestedMethod !== 'GET' ) {
613 // If method is not a case-sensitive match, do not set any additional headers and terminate.
614 return true;
615 }
616 // We allow the actual request to send the following headers
617 $requestedHeaders = $request->getHeader( 'Access-Control-Request-Headers' );
618 if ( $requestedHeaders !== false ) {
619 if ( !self::matchRequestedHeaders( $requestedHeaders ) ) {
620 return true;
621 }
622 $response->header( 'Access-Control-Allow-Headers: ' . $requestedHeaders );
623 }
624
625 // We only allow the actual request to be GET or POST
626 $response->header( 'Access-Control-Allow-Methods: POST, GET' );
627 }
628
629 $response->header( "Access-Control-Allow-Origin: $originHeader" );
630 $response->header( 'Access-Control-Allow-Credentials: true' );
631 // http://www.w3.org/TR/resource-timing/#timing-allow-origin
632 $response->header( "Timing-Allow-Origin: $originHeader" );
633
634 if ( !$preflight ) {
635 $response->header(
636 'Access-Control-Expose-Headers: MediaWiki-API-Error, Retry-After, X-Database-Lag'
637 );
638 }
639 }
640
641 $this->getOutput()->addVaryHeader( 'Origin' );
642 return true;
643 }
644
645 /**
646 * Attempt to match an Origin header against a set of rules and a set of exceptions
647 * @param string $value Origin header
648 * @param array $rules Set of wildcard rules
649 * @param array $exceptions Set of wildcard rules
650 * @return bool True if $value matches a rule in $rules and doesn't match
651 * any rules in $exceptions, false otherwise
652 */
653 protected static function matchOrigin( $value, $rules, $exceptions ) {
654 foreach ( $rules as $rule ) {
655 if ( preg_match( self::wildcardToRegex( $rule ), $value ) ) {
656 // Rule matches, check exceptions
657 foreach ( $exceptions as $exc ) {
658 if ( preg_match( self::wildcardToRegex( $exc ), $value ) ) {
659 return false;
660 }
661 }
662
663 return true;
664 }
665 }
666
667 return false;
668 }
669
670 /**
671 * Attempt to validate the value of Access-Control-Request-Headers against a list
672 * of headers that we allow the follow up request to send.
673 *
674 * @param string $requestedHeaders Comma seperated list of HTTP headers
675 * @return bool True if all requested headers are in the list of allowed headers
676 */
677 protected static function matchRequestedHeaders( $requestedHeaders ) {
678 if ( trim( $requestedHeaders ) === '' ) {
679 return true;
680 }
681 $requestedHeaders = explode( ',', $requestedHeaders );
682 $allowedAuthorHeaders = array_flip( [
683 /* simple headers (see spec) */
684 'accept',
685 'accept-language',
686 'content-language',
687 'content-type',
688 /* non-authorable headers in XHR, which are however requested by some UAs */
689 'accept-encoding',
690 'dnt',
691 'origin',
692 /* MediaWiki whitelist */
693 'api-user-agent',
694 ] );
695 foreach ( $requestedHeaders as $rHeader ) {
696 $rHeader = strtolower( trim( $rHeader ) );
697 if ( !isset( $allowedAuthorHeaders[$rHeader] ) ) {
698 wfDebugLog( 'api', 'CORS preflight failed on requested header: ' . $rHeader );
699 return false;
700 }
701 }
702 return true;
703 }
704
705 /**
706 * Helper function to convert wildcard string into a regex
707 * '*' => '.*?'
708 * '?' => '.'
709 *
710 * @param string $wildcard String with wildcards
711 * @return string Regular expression
712 */
713 protected static function wildcardToRegex( $wildcard ) {
714 $wildcard = preg_quote( $wildcard, '/' );
715 $wildcard = str_replace(
716 [ '\*', '\?' ],
717 [ '.*?', '.' ],
718 $wildcard
719 );
720
721 return "/^https?:\/\/$wildcard$/";
722 }
723
724 /**
725 * Send caching headers
726 * @param bool $isError Whether an error response is being output
727 * @since 1.26 added $isError parameter
728 */
729 protected function sendCacheHeaders( $isError ) {
730 $response = $this->getRequest()->response();
731 $out = $this->getOutput();
732
733 $config = $this->getConfig();
734
735 if ( $config->get( 'VaryOnXFP' ) ) {
736 $out->addVaryHeader( 'X-Forwarded-Proto' );
737 }
738
739 if ( !$isError && $this->mModule &&
740 ( $this->getRequest()->getMethod() === 'GET' || $this->getRequest()->getMethod() === 'HEAD' )
741 ) {
742 $etag = $this->mModule->getConditionalRequestData( 'etag' );
743 if ( $etag !== null ) {
744 $response->header( "ETag: $etag" );
745 }
746 $lastMod = $this->mModule->getConditionalRequestData( 'last-modified' );
747 if ( $lastMod !== null ) {
748 $response->header( 'Last-Modified: ' . wfTimestamp( TS_RFC2822, $lastMod ) );
749 }
750 }
751
752 // The logic should be:
753 // $this->mCacheControl['max-age'] is set?
754 // Use it, the module knows better than our guess.
755 // !$this->mModule || $this->mModule->isWriteMode(), and mCacheMode is private?
756 // Use 0 because we can guess caching is probably the wrong thing to do.
757 // Use $this->getParameter( 'maxage' ), which already defaults to 0.
758 $maxage = 0;
759 if ( isset( $this->mCacheControl['max-age'] ) ) {
760 $maxage = $this->mCacheControl['max-age'];
761 } elseif ( ( $this->mModule && !$this->mModule->isWriteMode() ) ||
762 $this->mCacheMode !== 'private'
763 ) {
764 $maxage = $this->getParameter( 'maxage' );
765 }
766 $privateCache = 'private, must-revalidate, max-age=' . $maxage;
767
768 if ( $this->mCacheMode == 'private' ) {
769 $response->header( "Cache-Control: $privateCache" );
770 return;
771 }
772
773 $useKeyHeader = $config->get( 'UseKeyHeader' );
774 if ( $this->mCacheMode == 'anon-public-user-private' ) {
775 $out->addVaryHeader( 'Cookie' );
776 $response->header( $out->getVaryHeader() );
777 if ( $useKeyHeader ) {
778 $response->header( $out->getKeyHeader() );
779 if ( $out->haveCacheVaryCookies() ) {
780 // Logged in, mark this request private
781 $response->header( "Cache-Control: $privateCache" );
782 return;
783 }
784 // Logged out, send normal public headers below
785 } elseif ( MediaWiki\Session\SessionManager::getGlobalSession()->isPersistent() ) {
786 // Logged in or otherwise has session (e.g. anonymous users who have edited)
787 // Mark request private
788 $response->header( "Cache-Control: $privateCache" );
789
790 return;
791 } // else no Key and anonymous, send public headers below
792 }
793
794 // Send public headers
795 $response->header( $out->getVaryHeader() );
796 if ( $useKeyHeader ) {
797 $response->header( $out->getKeyHeader() );
798 }
799
800 // If nobody called setCacheMaxAge(), use the (s)maxage parameters
801 if ( !isset( $this->mCacheControl['s-maxage'] ) ) {
802 $this->mCacheControl['s-maxage'] = $this->getParameter( 'smaxage' );
803 }
804 if ( !isset( $this->mCacheControl['max-age'] ) ) {
805 $this->mCacheControl['max-age'] = $this->getParameter( 'maxage' );
806 }
807
808 if ( !$this->mCacheControl['s-maxage'] && !$this->mCacheControl['max-age'] ) {
809 // Public cache not requested
810 // Sending a Vary header in this case is harmless, and protects us
811 // against conditional calls of setCacheMaxAge().
812 $response->header( "Cache-Control: $privateCache" );
813
814 return;
815 }
816
817 $this->mCacheControl['public'] = true;
818
819 // Send an Expires header
820 $maxAge = min( $this->mCacheControl['s-maxage'], $this->mCacheControl['max-age'] );
821 $expiryUnixTime = ( $maxAge == 0 ? 1 : time() + $maxAge );
822 $response->header( 'Expires: ' . wfTimestamp( TS_RFC2822, $expiryUnixTime ) );
823
824 // Construct the Cache-Control header
825 $ccHeader = '';
826 $separator = '';
827 foreach ( $this->mCacheControl as $name => $value ) {
828 if ( is_bool( $value ) ) {
829 if ( $value ) {
830 $ccHeader .= $separator . $name;
831 $separator = ', ';
832 }
833 } else {
834 $ccHeader .= $separator . "$name=$value";
835 $separator = ', ';
836 }
837 }
838
839 $response->header( "Cache-Control: $ccHeader" );
840 }
841
842 /**
843 * Create the printer for error output
844 */
845 private function createErrorPrinter() {
846 if ( !isset( $this->mPrinter ) ) {
847 $value = $this->getRequest()->getVal( 'format', self::API_DEFAULT_FORMAT );
848 if ( !$this->mModuleMgr->isDefined( $value, 'format' ) ) {
849 $value = self::API_DEFAULT_FORMAT;
850 }
851 $this->mPrinter = $this->createPrinterByName( $value );
852 }
853
854 // Printer may not be able to handle errors. This is particularly
855 // likely if the module returns something for getCustomPrinter().
856 if ( !$this->mPrinter->canPrintErrors() ) {
857 $this->mPrinter = $this->createPrinterByName( self::API_DEFAULT_FORMAT );
858 }
859 }
860
861 /**
862 * Create an error message for the given exception.
863 *
864 * If the exception is a UsageException then
865 * UsageException::getMessageArray() will be called to create the message.
866 *
867 * @param Exception $e
868 * @return array ['code' => 'some string', 'info' => 'some other string']
869 * @since 1.27
870 */
871 protected function errorMessageFromException( $e ) {
872 if ( $e instanceof UsageException ) {
873 // User entered incorrect parameters - generate error response
874 $errMessage = $e->getMessageArray();
875 } else {
876 $config = $this->getConfig();
877 // Something is seriously wrong
878 if ( ( $e instanceof DBQueryError ) && !$config->get( 'ShowSQLErrors' ) ) {
879 $info = 'Database query error';
880 } else {
881 $info = "Exception Caught: {$e->getMessage()}";
882 }
883
884 $errMessage = [
885 'code' => 'internal_api_error_' . get_class( $e ),
886 'info' => '[' . WebRequest::getRequestId() . '] ' . $info,
887 ];
888 }
889 return $errMessage;
890 }
891
892 /**
893 * Replace the result data with the information about an exception.
894 * Returns the error code
895 * @param Exception $e
896 * @return string
897 */
898 protected function substituteResultWithError( $e ) {
899 $result = $this->getResult();
900 $config = $this->getConfig();
901
902 $errMessage = $this->errorMessageFromException( $e );
903 if ( $e instanceof UsageException ) {
904 // User entered incorrect parameters - generate error response
905 $link = wfExpandUrl( wfScript( 'api' ) );
906 ApiResult::setContentValue( $errMessage, 'docref', "See $link for API usage" );
907 } else {
908 // Something is seriously wrong
909 if ( $config->get( 'ShowExceptionDetails' ) ) {
910 ApiResult::setContentValue(
911 $errMessage,
912 'trace',
913 MWExceptionHandler::getRedactedTraceAsString( $e )
914 );
915 }
916 }
917
918 // Remember all the warnings to re-add them later
919 $warnings = $result->getResultData( [ 'warnings' ] );
920
921 $result->reset();
922 // Re-add the id
923 $requestid = $this->getParameter( 'requestid' );
924 if ( !is_null( $requestid ) ) {
925 $result->addValue( null, 'requestid', $requestid, ApiResult::NO_SIZE_CHECK );
926 }
927 if ( $config->get( 'ShowHostnames' ) ) {
928 // servedby is especially useful when debugging errors
929 $result->addValue( null, 'servedby', wfHostname(), ApiResult::NO_SIZE_CHECK );
930 }
931 if ( $warnings !== null ) {
932 $result->addValue( null, 'warnings', $warnings, ApiResult::NO_SIZE_CHECK );
933 }
934
935 $result->addValue( null, 'error', $errMessage, ApiResult::NO_SIZE_CHECK );
936
937 return $errMessage['code'];
938 }
939
940 /**
941 * Set up for the execution.
942 * @return array
943 */
944 protected function setupExecuteAction() {
945 // First add the id to the top element
946 $result = $this->getResult();
947 $requestid = $this->getParameter( 'requestid' );
948 if ( !is_null( $requestid ) ) {
949 $result->addValue( null, 'requestid', $requestid );
950 }
951
952 if ( $this->getConfig()->get( 'ShowHostnames' ) ) {
953 $servedby = $this->getParameter( 'servedby' );
954 if ( $servedby ) {
955 $result->addValue( null, 'servedby', wfHostname() );
956 }
957 }
958
959 if ( $this->getParameter( 'curtimestamp' ) ) {
960 $result->addValue( null, 'curtimestamp', wfTimestamp( TS_ISO_8601, time() ),
961 ApiResult::NO_SIZE_CHECK );
962 }
963
964 $params = $this->extractRequestParams();
965
966 $this->mAction = $params['action'];
967
968 if ( !is_string( $this->mAction ) ) {
969 $this->dieUsage( 'The API requires a valid action parameter', 'unknown_action' );
970 }
971
972 return $params;
973 }
974
975 /**
976 * Set up the module for response
977 * @return ApiBase The module that will handle this action
978 * @throws MWException
979 * @throws UsageException
980 */
981 protected function setupModule() {
982 // Instantiate the module requested by the user
983 $module = $this->mModuleMgr->getModule( $this->mAction, 'action' );
984 if ( $module === null ) {
985 $this->dieUsage( 'The API requires a valid action parameter', 'unknown_action' );
986 }
987 $moduleParams = $module->extractRequestParams();
988
989 // Check token, if necessary
990 if ( $module->needsToken() === true ) {
991 throw new MWException(
992 "Module '{$module->getModuleName()}' must be updated for the new token handling. " .
993 'See documentation for ApiBase::needsToken for details.'
994 );
995 }
996 if ( $module->needsToken() ) {
997 if ( !$module->mustBePosted() ) {
998 throw new MWException(
999 "Module '{$module->getModuleName()}' must require POST to use tokens."
1000 );
1001 }
1002
1003 if ( !isset( $moduleParams['token'] ) ) {
1004 $this->dieUsageMsg( [ 'missingparam', 'token' ] );
1005 }
1006
1007 if ( !$this->getConfig()->get( 'DebugAPI' ) &&
1008 array_key_exists(
1009 $module->encodeParamName( 'token' ),
1010 $this->getRequest()->getQueryValues()
1011 )
1012 ) {
1013 $this->dieUsage(
1014 "The '{$module->encodeParamName( 'token' )}' parameter was " .
1015 'found in the query string, but must be in the POST body',
1016 'mustposttoken'
1017 );
1018 }
1019
1020 if ( !$module->validateToken( $moduleParams['token'], $moduleParams ) ) {
1021 $this->dieUsageMsg( 'sessionfailure' );
1022 }
1023 }
1024
1025 return $module;
1026 }
1027
1028 /**
1029 * Check the max lag if necessary
1030 * @param ApiBase $module Api module being used
1031 * @param array $params Array an array containing the request parameters.
1032 * @return bool True on success, false should exit immediately
1033 */
1034 protected function checkMaxLag( $module, $params ) {
1035 if ( $module->shouldCheckMaxlag() && isset( $params['maxlag'] ) ) {
1036 $maxLag = $params['maxlag'];
1037 list( $host, $lag ) = wfGetLB()->getMaxLag();
1038 if ( $lag > $maxLag ) {
1039 $response = $this->getRequest()->response();
1040
1041 $response->header( 'Retry-After: ' . max( intval( $maxLag ), 5 ) );
1042 $response->header( 'X-Database-Lag: ' . intval( $lag ) );
1043
1044 if ( $this->getConfig()->get( 'ShowHostnames' ) ) {
1045 $this->dieUsage( "Waiting for $host: $lag seconds lagged", 'maxlag' );
1046 }
1047
1048 $this->dieUsage( "Waiting for a database server: $lag seconds lagged", 'maxlag' );
1049 }
1050 }
1051
1052 return true;
1053 }
1054
1055 /**
1056 * Check selected RFC 7232 precondition headers
1057 *
1058 * RFC 7232 envisions a particular model where you send your request to "a
1059 * resource", and for write requests that you can read "the resource" by
1060 * changing the method to GET. When the API receives a GET request, it
1061 * works out even though "the resource" from RFC 7232's perspective might
1062 * be many resources from MediaWiki's perspective. But it totally fails for
1063 * a POST, since what HTTP sees as "the resource" is probably just
1064 * "/api.php" with all the interesting bits in the body.
1065 *
1066 * Therefore, we only support RFC 7232 precondition headers for GET (and
1067 * HEAD). That means we don't need to bother with If-Match and
1068 * If-Unmodified-Since since they only apply to modification requests.
1069 *
1070 * And since we don't support Range, If-Range is ignored too.
1071 *
1072 * @since 1.26
1073 * @param ApiBase $module Api module being used
1074 * @return bool True on success, false should exit immediately
1075 */
1076 protected function checkConditionalRequestHeaders( $module ) {
1077 if ( $this->mInternalMode ) {
1078 // No headers to check in internal mode
1079 return true;
1080 }
1081
1082 if ( $this->getRequest()->getMethod() !== 'GET' && $this->getRequest()->getMethod() !== 'HEAD' ) {
1083 // Don't check POSTs
1084 return true;
1085 }
1086
1087 $return304 = false;
1088
1089 $ifNoneMatch = array_diff(
1090 $this->getRequest()->getHeader( 'If-None-Match', WebRequest::GETHEADER_LIST ) ?: [],
1091 [ '' ]
1092 );
1093 if ( $ifNoneMatch ) {
1094 if ( $ifNoneMatch === [ '*' ] ) {
1095 // API responses always "exist"
1096 $etag = '*';
1097 } else {
1098 $etag = $module->getConditionalRequestData( 'etag' );
1099 }
1100 }
1101 if ( $ifNoneMatch && $etag !== null ) {
1102 $test = substr( $etag, 0, 2 ) === 'W/' ? substr( $etag, 2 ) : $etag;
1103 $match = array_map( function ( $s ) {
1104 return substr( $s, 0, 2 ) === 'W/' ? substr( $s, 2 ) : $s;
1105 }, $ifNoneMatch );
1106 $return304 = in_array( $test, $match, true );
1107 } else {
1108 $value = trim( $this->getRequest()->getHeader( 'If-Modified-Since' ) );
1109
1110 // Some old browsers sends sizes after the date, like this:
1111 // Wed, 20 Aug 2003 06:51:19 GMT; length=5202
1112 // Ignore that.
1113 $i = strpos( $value, ';' );
1114 if ( $i !== false ) {
1115 $value = trim( substr( $value, 0, $i ) );
1116 }
1117
1118 if ( $value !== '' ) {
1119 try {
1120 $ts = new MWTimestamp( $value );
1121 if (
1122 // RFC 7231 IMF-fixdate
1123 $ts->getTimestamp( TS_RFC2822 ) === $value ||
1124 // RFC 850
1125 $ts->format( 'l, d-M-y H:i:s' ) . ' GMT' === $value ||
1126 // asctime (with and without space-padded day)
1127 $ts->format( 'D M j H:i:s Y' ) === $value ||
1128 $ts->format( 'D M j H:i:s Y' ) === $value
1129 ) {
1130 $lastMod = $module->getConditionalRequestData( 'last-modified' );
1131 if ( $lastMod !== null ) {
1132 // Mix in some MediaWiki modification times
1133 $modifiedTimes = [
1134 'page' => $lastMod,
1135 'user' => $this->getUser()->getTouched(),
1136 'epoch' => $this->getConfig()->get( 'CacheEpoch' ),
1137 ];
1138 if ( $this->getConfig()->get( 'UseSquid' ) ) {
1139 // T46570: the core page itself may not change, but resources might
1140 $modifiedTimes['sepoch'] = wfTimestamp(
1141 TS_MW, time() - $this->getConfig()->get( 'SquidMaxage' )
1142 );
1143 }
1144 Hooks::run( 'OutputPageCheckLastModified', [ &$modifiedTimes, $this->getOutput() ] );
1145 $lastMod = max( $modifiedTimes );
1146 $return304 = wfTimestamp( TS_MW, $lastMod ) <= $ts->getTimestamp( TS_MW );
1147 }
1148 }
1149 } catch ( TimestampException $e ) {
1150 // Invalid timestamp, ignore it
1151 }
1152 }
1153 }
1154
1155 if ( $return304 ) {
1156 $this->getRequest()->response()->statusHeader( 304 );
1157
1158 // Avoid outputting the compressed representation of a zero-length body
1159 MediaWiki\suppressWarnings();
1160 ini_set( 'zlib.output_compression', 0 );
1161 MediaWiki\restoreWarnings();
1162 wfClearOutputBuffers();
1163
1164 return false;
1165 }
1166
1167 return true;
1168 }
1169
1170 /**
1171 * Check for sufficient permissions to execute
1172 * @param ApiBase $module An Api module
1173 */
1174 protected function checkExecutePermissions( $module ) {
1175 $user = $this->getUser();
1176 if ( $module->isReadMode() && !User::isEveryoneAllowed( 'read' ) &&
1177 !$user->isAllowed( 'read' )
1178 ) {
1179 $this->dieUsageMsg( 'readrequired' );
1180 }
1181
1182 if ( $module->isWriteMode() ) {
1183 if ( !$this->mEnableWrite ) {
1184 $this->dieUsageMsg( 'writedisabled' );
1185 } elseif ( !$user->isAllowed( 'writeapi' ) ) {
1186 $this->dieUsageMsg( 'writerequired' );
1187 } elseif ( $this->getRequest()->getHeader( 'Promise-Non-Write-API-Action' ) ) {
1188 $this->dieUsage(
1189 'Promise-Non-Write-API-Action HTTP header cannot be sent to write API modules',
1190 'promised-nonwrite-api'
1191 );
1192 }
1193
1194 $this->checkReadOnly( $module );
1195 }
1196
1197 // Allow extensions to stop execution for arbitrary reasons.
1198 $message = false;
1199 if ( !Hooks::run( 'ApiCheckCanExecute', [ $module, $user, &$message ] ) ) {
1200 $this->dieUsageMsg( $message );
1201 }
1202 }
1203
1204 /**
1205 * Check if the DB is read-only for this user
1206 * @param ApiBase $module An Api module
1207 */
1208 protected function checkReadOnly( $module ) {
1209 if ( wfReadOnly() ) {
1210 $this->dieReadOnly();
1211 }
1212
1213 if ( $module->isWriteMode()
1214 && in_array( 'bot', $this->getUser()->getGroups() )
1215 && wfGetLB()->getServerCount() > 1
1216 ) {
1217 $this->checkBotReadOnly();
1218 }
1219 }
1220
1221 /**
1222 * Check whether we are readonly for bots
1223 */
1224 private function checkBotReadOnly() {
1225 // Figure out how many servers have passed the lag threshold
1226 $numLagged = 0;
1227 $lagLimit = $this->getConfig()->get( 'APIMaxLagThreshold' );
1228 $laggedServers = [];
1229 $loadBalancer = wfGetLB();
1230 foreach ( $loadBalancer->getLagTimes() as $serverIndex => $lag ) {
1231 if ( $lag > $lagLimit ) {
1232 ++$numLagged;
1233 $laggedServers[] = $loadBalancer->getServerName( $serverIndex ) . " ({$lag}s)";
1234 }
1235 }
1236
1237 // If a majority of slaves are too lagged then disallow writes
1238 $slaveCount = wfGetLB()->getServerCount() - 1;
1239 if ( $numLagged >= ceil( $slaveCount / 2 ) ) {
1240 $laggedServers = implode( ', ', $laggedServers );
1241 wfDebugLog(
1242 'api-readonly',
1243 "Api request failed as read only because the following DBs are lagged: $laggedServers"
1244 );
1245
1246 $parsed = $this->parseMsg( [ 'readonlytext' ] );
1247 $this->dieUsage(
1248 $parsed['info'],
1249 $parsed['code'],
1250 /* http error */
1251 0,
1252 [ 'readonlyreason' => "Waiting for $numLagged lagged database(s)" ]
1253 );
1254 }
1255 }
1256
1257 /**
1258 * Check asserts of the user's rights
1259 * @param array $params
1260 */
1261 protected function checkAsserts( $params ) {
1262 if ( isset( $params['assert'] ) ) {
1263 $user = $this->getUser();
1264 switch ( $params['assert'] ) {
1265 case 'user':
1266 if ( $user->isAnon() ) {
1267 $this->dieUsage( 'Assertion that the user is logged in failed', 'assertuserfailed' );
1268 }
1269 break;
1270 case 'bot':
1271 if ( !$user->isAllowed( 'bot' ) ) {
1272 $this->dieUsage( 'Assertion that the user has the bot right failed', 'assertbotfailed' );
1273 }
1274 break;
1275 }
1276 }
1277 }
1278
1279 /**
1280 * Check POST for external response and setup result printer
1281 * @param ApiBase $module An Api module
1282 * @param array $params An array with the request parameters
1283 */
1284 protected function setupExternalResponse( $module, $params ) {
1285 $request = $this->getRequest();
1286 if ( !$request->wasPosted() && $module->mustBePosted() ) {
1287 // Module requires POST. GET request might still be allowed
1288 // if $wgDebugApi is true, otherwise fail.
1289 $this->dieUsageMsgOrDebug( [ 'mustbeposted', $this->mAction ] );
1290 }
1291
1292 // See if custom printer is used
1293 $this->mPrinter = $module->getCustomPrinter();
1294 if ( is_null( $this->mPrinter ) ) {
1295 // Create an appropriate printer
1296 $this->mPrinter = $this->createPrinterByName( $params['format'] );
1297 }
1298
1299 if ( $request->getProtocol() === 'http' && (
1300 $request->getSession()->shouldForceHTTPS() ||
1301 ( $this->getUser()->isLoggedIn() &&
1302 $this->getUser()->requiresHTTPS() )
1303 ) ) {
1304 $this->logFeatureUsage( 'https-expected' );
1305 $this->setWarning( 'HTTP used when HTTPS was expected' );
1306 }
1307 }
1308
1309 /**
1310 * Execute the actual module, without any error handling
1311 */
1312 protected function executeAction() {
1313 $params = $this->setupExecuteAction();
1314 $module = $this->setupModule();
1315 $this->mModule = $module;
1316
1317 if ( !$this->mInternalMode ) {
1318 $this->setRequestExpectations( $module );
1319 }
1320
1321 $this->checkExecutePermissions( $module );
1322
1323 if ( !$this->checkMaxLag( $module, $params ) ) {
1324 return;
1325 }
1326
1327 if ( !$this->checkConditionalRequestHeaders( $module ) ) {
1328 return;
1329 }
1330
1331 if ( !$this->mInternalMode ) {
1332 $this->setupExternalResponse( $module, $params );
1333 }
1334
1335 $this->checkAsserts( $params );
1336
1337 // Execute
1338 $module->execute();
1339 Hooks::run( 'APIAfterExecute', [ &$module ] );
1340
1341 $this->reportUnusedParams();
1342
1343 if ( !$this->mInternalMode ) {
1344 // append Debug information
1345 MWDebug::appendDebugInfoToApiResult( $this->getContext(), $this->getResult() );
1346
1347 // Print result data
1348 $this->printResult( false );
1349 }
1350 }
1351
1352 /**
1353 * Set database connection, query, and write expectations given this module request
1354 * @param ApiBase $module
1355 */
1356 protected function setRequestExpectations( ApiBase $module ) {
1357 $limits = $this->getConfig()->get( 'TrxProfilerLimits' );
1358 $trxProfiler = Profiler::instance()->getTransactionProfiler();
1359 if ( $this->getRequest()->wasPosted() ) {
1360 if ( $module->isWriteMode() ) {
1361 $trxProfiler->setExpectations( $limits['POST'], __METHOD__ );
1362 } else {
1363 $trxProfiler->setExpectations( $limits['POST-nonwrite'], __METHOD__ );
1364 $this->getRequest()->markAsSafeRequest();
1365 }
1366 } else {
1367 $trxProfiler->setExpectations( $limits['GET'], __METHOD__ );
1368 }
1369 }
1370
1371 /**
1372 * Log the preceding request
1373 * @param float $time Time in seconds
1374 * @param Exception $e Exception caught while processing the request
1375 */
1376 protected function logRequest( $time, $e = null ) {
1377 $request = $this->getRequest();
1378 $logCtx = [
1379 'ts' => time(),
1380 'ip' => $request->getIP(),
1381 'userAgent' => $this->getUserAgent(),
1382 'wiki' => wfWikiID(),
1383 'timeSpentBackend' => (int) round( $time * 1000 ),
1384 'hadError' => $e !== null,
1385 'errorCodes' => [],
1386 'params' => [],
1387 ];
1388
1389 if ( $e ) {
1390 $logCtx['errorCodes'][] = $this->errorMessageFromException( $e )['code'];
1391 }
1392
1393 // Construct space separated message for 'api' log channel
1394 $msg = "API {$request->getMethod()} " .
1395 wfUrlencode( str_replace( ' ', '_', $this->getUser()->getName() ) ) .
1396 " {$logCtx['ip']} " .
1397 "T={$logCtx['timeSpentBackend']}ms";
1398
1399 foreach ( $this->getParamsUsed() as $name ) {
1400 $value = $request->getVal( $name );
1401 if ( $value === null ) {
1402 continue;
1403 }
1404
1405 if ( strlen( $value ) > 256 ) {
1406 $value = substr( $value, 0, 256 );
1407 $encValue = $this->encodeRequestLogValue( $value ) . '[...]';
1408 } else {
1409 $encValue = $this->encodeRequestLogValue( $value );
1410 }
1411
1412 $logCtx['params'][$name] = $value;
1413 $msg .= " {$name}={$encValue}";
1414 }
1415
1416 wfDebugLog( 'api', $msg, 'private' );
1417 // ApiAction channel is for structured data consumers
1418 wfDebugLog( 'ApiAction', '', 'private', $logCtx );
1419 }
1420
1421 /**
1422 * Encode a value in a format suitable for a space-separated log line.
1423 * @param string $s
1424 * @return string
1425 */
1426 protected function encodeRequestLogValue( $s ) {
1427 static $table;
1428 if ( !$table ) {
1429 $chars = ';@$!*(),/:';
1430 $numChars = strlen( $chars );
1431 for ( $i = 0; $i < $numChars; $i++ ) {
1432 $table[rawurlencode( $chars[$i] )] = $chars[$i];
1433 }
1434 }
1435
1436 return strtr( rawurlencode( $s ), $table );
1437 }
1438
1439 /**
1440 * Get the request parameters used in the course of the preceding execute() request
1441 * @return array
1442 */
1443 protected function getParamsUsed() {
1444 return array_keys( $this->mParamsUsed );
1445 }
1446
1447 /**
1448 * Mark parameters as used
1449 * @param string|string[] $params
1450 */
1451 public function markParamsUsed( $params ) {
1452 $this->mParamsUsed += array_fill_keys( (array)$params, true );
1453 }
1454
1455 /**
1456 * Get a request value, and register the fact that it was used, for logging.
1457 * @param string $name
1458 * @param mixed $default
1459 * @return mixed
1460 */
1461 public function getVal( $name, $default = null ) {
1462 $this->mParamsUsed[$name] = true;
1463
1464 $ret = $this->getRequest()->getVal( $name );
1465 if ( $ret === null ) {
1466 if ( $this->getRequest()->getArray( $name ) !== null ) {
1467 // See bug 10262 for why we don't just implode( '|', ... ) the
1468 // array.
1469 $this->setWarning(
1470 "Parameter '$name' uses unsupported PHP array syntax"
1471 );
1472 }
1473 $ret = $default;
1474 }
1475 return $ret;
1476 }
1477
1478 /**
1479 * Get a boolean request value, and register the fact that the parameter
1480 * was used, for logging.
1481 * @param string $name
1482 * @return bool
1483 */
1484 public function getCheck( $name ) {
1485 return $this->getVal( $name, null ) !== null;
1486 }
1487
1488 /**
1489 * Get a request upload, and register the fact that it was used, for logging.
1490 *
1491 * @since 1.21
1492 * @param string $name Parameter name
1493 * @return WebRequestUpload
1494 */
1495 public function getUpload( $name ) {
1496 $this->mParamsUsed[$name] = true;
1497
1498 return $this->getRequest()->getUpload( $name );
1499 }
1500
1501 /**
1502 * Report unused parameters, so the client gets a hint in case it gave us parameters we don't know,
1503 * for example in case of spelling mistakes or a missing 'g' prefix for generators.
1504 */
1505 protected function reportUnusedParams() {
1506 $paramsUsed = $this->getParamsUsed();
1507 $allParams = $this->getRequest()->getValueNames();
1508
1509 if ( !$this->mInternalMode ) {
1510 // Printer has not yet executed; don't warn that its parameters are unused
1511 $printerParams = array_map(
1512 [ $this->mPrinter, 'encodeParamName' ],
1513 array_keys( $this->mPrinter->getFinalParams() ?: [] )
1514 );
1515 $unusedParams = array_diff( $allParams, $paramsUsed, $printerParams );
1516 } else {
1517 $unusedParams = array_diff( $allParams, $paramsUsed );
1518 }
1519
1520 if ( count( $unusedParams ) ) {
1521 $s = count( $unusedParams ) > 1 ? 's' : '';
1522 $this->setWarning( "Unrecognized parameter$s: '" . implode( $unusedParams, "', '" ) . "'" );
1523 }
1524 }
1525
1526 /**
1527 * Print results using the current printer
1528 *
1529 * @param bool $isError
1530 */
1531 protected function printResult( $isError ) {
1532 if ( $this->getConfig()->get( 'DebugAPI' ) !== false ) {
1533 $this->setWarning( 'SECURITY WARNING: $wgDebugAPI is enabled' );
1534 }
1535
1536 $printer = $this->mPrinter;
1537 $printer->initPrinter( false );
1538 $printer->execute();
1539 $printer->closePrinter();
1540 }
1541
1542 /**
1543 * @return bool
1544 */
1545 public function isReadMode() {
1546 return false;
1547 }
1548
1549 /**
1550 * See ApiBase for description.
1551 *
1552 * @return array
1553 */
1554 public function getAllowedParams() {
1555 return [
1556 'action' => [
1557 ApiBase::PARAM_DFLT => 'help',
1558 ApiBase::PARAM_TYPE => 'submodule',
1559 ],
1560 'format' => [
1561 ApiBase::PARAM_DFLT => ApiMain::API_DEFAULT_FORMAT,
1562 ApiBase::PARAM_TYPE => 'submodule',
1563 ],
1564 'maxlag' => [
1565 ApiBase::PARAM_TYPE => 'integer'
1566 ],
1567 'smaxage' => [
1568 ApiBase::PARAM_TYPE => 'integer',
1569 ApiBase::PARAM_DFLT => 0
1570 ],
1571 'maxage' => [
1572 ApiBase::PARAM_TYPE => 'integer',
1573 ApiBase::PARAM_DFLT => 0
1574 ],
1575 'assert' => [
1576 ApiBase::PARAM_TYPE => [ 'user', 'bot' ]
1577 ],
1578 'requestid' => null,
1579 'servedby' => false,
1580 'curtimestamp' => false,
1581 'origin' => null,
1582 'uselang' => [
1583 ApiBase::PARAM_DFLT => 'user',
1584 ],
1585 ];
1586 }
1587
1588 /** @see ApiBase::getExamplesMessages() */
1589 protected function getExamplesMessages() {
1590 return [
1591 'action=help'
1592 => 'apihelp-help-example-main',
1593 'action=help&recursivesubmodules=1'
1594 => 'apihelp-help-example-recursive',
1595 ];
1596 }
1597
1598 public function modifyHelp( array &$help, array $options, array &$tocData ) {
1599 // Wish PHP had an "array_insert_before". Instead, we have to manually
1600 // reindex the array to get 'permissions' in the right place.
1601 $oldHelp = $help;
1602 $help = [];
1603 foreach ( $oldHelp as $k => $v ) {
1604 if ( $k === 'submodules' ) {
1605 $help['permissions'] = '';
1606 }
1607 $help[$k] = $v;
1608 }
1609 $help['datatypes'] = '';
1610 $help['credits'] = '';
1611
1612 // Fill 'permissions'
1613 $help['permissions'] .= Html::openElement( 'div',
1614 [ 'class' => 'apihelp-block apihelp-permissions' ] );
1615 $m = $this->msg( 'api-help-permissions' );
1616 if ( !$m->isDisabled() ) {
1617 $help['permissions'] .= Html::rawElement( 'div', [ 'class' => 'apihelp-block-head' ],
1618 $m->numParams( count( self::$mRights ) )->parse()
1619 );
1620 }
1621 $help['permissions'] .= Html::openElement( 'dl' );
1622 foreach ( self::$mRights as $right => $rightMsg ) {
1623 $help['permissions'] .= Html::element( 'dt', null, $right );
1624
1625 $rightMsg = $this->msg( $rightMsg['msg'], $rightMsg['params'] )->parse();
1626 $help['permissions'] .= Html::rawElement( 'dd', null, $rightMsg );
1627
1628 $groups = array_map( function ( $group ) {
1629 return $group == '*' ? 'all' : $group;
1630 }, User::getGroupsWithPermission( $right ) );
1631
1632 $help['permissions'] .= Html::rawElement( 'dd', null,
1633 $this->msg( 'api-help-permissions-granted-to' )
1634 ->numParams( count( $groups ) )
1635 ->params( $this->getLanguage()->commaList( $groups ) )
1636 ->parse()
1637 );
1638 }
1639 $help['permissions'] .= Html::closeElement( 'dl' );
1640 $help['permissions'] .= Html::closeElement( 'div' );
1641
1642 // Fill 'datatypes' and 'credits', if applicable
1643 if ( empty( $options['nolead'] ) ) {
1644 $level = $options['headerlevel'];
1645 $tocnumber = &$options['tocnumber'];
1646
1647 $header = $this->msg( 'api-help-datatypes-header' )->parse();
1648
1649 // Add an additional span with sanitized ID
1650 if ( !$this->getConfig()->get( 'ExperimentalHtmlIds' ) ) {
1651 $header = Html::element( 'span', [ 'id' => Sanitizer::escapeId( 'main/datatypes' ) ] ) .
1652 $header;
1653 }
1654 $help['datatypes'] .= Html::rawElement( 'h' . min( 6, $level ),
1655 [ 'id' => 'main/datatypes', 'class' => 'apihelp-header' ],
1656 $header
1657 );
1658 $help['datatypes'] .= $this->msg( 'api-help-datatypes' )->parseAsBlock();
1659 if ( !isset( $tocData['main/datatypes'] ) ) {
1660 $tocnumber[$level]++;
1661 $tocData['main/datatypes'] = [
1662 'toclevel' => count( $tocnumber ),
1663 'level' => $level,
1664 'anchor' => 'main/datatypes',
1665 'line' => $header,
1666 'number' => implode( '.', $tocnumber ),
1667 'index' => false,
1668 ];
1669 }
1670
1671 // Add an additional span with sanitized ID
1672 if ( !$this->getConfig()->get( 'ExperimentalHtmlIds' ) ) {
1673 $header = Html::element( 'span', [ 'id' => Sanitizer::escapeId( 'main/credits' ) ] ) .
1674 $header;
1675 }
1676 $header = $this->msg( 'api-credits-header' )->parse();
1677 $help['credits'] .= Html::rawElement( 'h' . min( 6, $level ),
1678 [ 'id' => 'main/credits', 'class' => 'apihelp-header' ],
1679 $header
1680 );
1681 $help['credits'] .= $this->msg( 'api-credits' )->useDatabase( false )->parseAsBlock();
1682 if ( !isset( $tocData['main/credits'] ) ) {
1683 $tocnumber[$level]++;
1684 $tocData['main/credits'] = [
1685 'toclevel' => count( $tocnumber ),
1686 'level' => $level,
1687 'anchor' => 'main/credits',
1688 'line' => $header,
1689 'number' => implode( '.', $tocnumber ),
1690 'index' => false,
1691 ];
1692 }
1693 }
1694 }
1695
1696 private $mCanApiHighLimits = null;
1697
1698 /**
1699 * Check whether the current user is allowed to use high limits
1700 * @return bool
1701 */
1702 public function canApiHighLimits() {
1703 if ( !isset( $this->mCanApiHighLimits ) ) {
1704 $this->mCanApiHighLimits = $this->getUser()->isAllowed( 'apihighlimits' );
1705 }
1706
1707 return $this->mCanApiHighLimits;
1708 }
1709
1710 /**
1711 * Overrides to return this instance's module manager.
1712 * @return ApiModuleManager
1713 */
1714 public function getModuleManager() {
1715 return $this->mModuleMgr;
1716 }
1717
1718 /**
1719 * Fetches the user agent used for this request
1720 *
1721 * The value will be the combination of the 'Api-User-Agent' header (if
1722 * any) and the standard User-Agent header (if any).
1723 *
1724 * @return string
1725 */
1726 public function getUserAgent() {
1727 return trim(
1728 $this->getRequest()->getHeader( 'Api-user-agent' ) . ' ' .
1729 $this->getRequest()->getHeader( 'User-agent' )
1730 );
1731 }
1732
1733 /************************************************************************//**
1734 * @name Deprecated
1735 * @{
1736 */
1737
1738 /**
1739 * Sets whether the pretty-printer should format *bold* and $italics$
1740 *
1741 * @deprecated since 1.25
1742 * @param bool $help
1743 */
1744 public function setHelp( $help = true ) {
1745 wfDeprecated( __METHOD__, '1.25' );
1746 $this->mPrinter->setHelp( $help );
1747 }
1748
1749 /**
1750 * Override the parent to generate help messages for all available modules.
1751 *
1752 * @deprecated since 1.25
1753 * @return string
1754 */
1755 public function makeHelpMsg() {
1756 wfDeprecated( __METHOD__, '1.25' );
1757
1758 $this->setHelp();
1759 $cacheHelpTimeout = $this->getConfig()->get( 'APICacheHelpTimeout' );
1760
1761 return ObjectCache::getMainWANInstance()->getWithSetCallback(
1762 wfMemcKey(
1763 'apihelp',
1764 $this->getModuleName(),
1765 str_replace( ' ', '_', SpecialVersion::getVersion( 'nodb' ) )
1766 ),
1767 $cacheHelpTimeout > 0 ? $cacheHelpTimeout : WANObjectCache::TTL_UNCACHEABLE,
1768 [ $this, 'reallyMakeHelpMsg' ]
1769 );
1770 }
1771
1772 /**
1773 * @deprecated since 1.25
1774 * @return mixed|string
1775 */
1776 public function reallyMakeHelpMsg() {
1777 wfDeprecated( __METHOD__, '1.25' );
1778 $this->setHelp();
1779
1780 // Use parent to make default message for the main module
1781 $msg = parent::makeHelpMsg();
1782
1783 $asterisks = str_repeat( '*** ', 14 );
1784 $msg .= "\n\n$asterisks Modules $asterisks\n\n";
1785
1786 foreach ( $this->mModuleMgr->getNames( 'action' ) as $name ) {
1787 $module = $this->mModuleMgr->getModule( $name );
1788 $msg .= self::makeHelpMsgHeader( $module, 'action' );
1789
1790 $msg2 = $module->makeHelpMsg();
1791 if ( $msg2 !== false ) {
1792 $msg .= $msg2;
1793 }
1794 $msg .= "\n";
1795 }
1796
1797 $msg .= "\n$asterisks Permissions $asterisks\n\n";
1798 foreach ( self::$mRights as $right => $rightMsg ) {
1799 $rightsMsg = $this->msg( $rightMsg['msg'], $rightMsg['params'] )
1800 ->useDatabase( false )
1801 ->inLanguage( 'en' )
1802 ->text();
1803 $groups = User::getGroupsWithPermission( $right );
1804 $msg .= '* ' . $right . " *\n $rightsMsg" .
1805 "\nGranted to:\n " . str_replace( '*', 'all', implode( ', ', $groups ) ) . "\n\n";
1806 }
1807
1808 $msg .= "\n$asterisks Formats $asterisks\n\n";
1809 foreach ( $this->mModuleMgr->getNames( 'format' ) as $name ) {
1810 $module = $this->mModuleMgr->getModule( $name );
1811 $msg .= self::makeHelpMsgHeader( $module, 'format' );
1812 $msg2 = $module->makeHelpMsg();
1813 if ( $msg2 !== false ) {
1814 $msg .= $msg2;
1815 }
1816 $msg .= "\n";
1817 }
1818
1819 $credits = $this->msg( 'api-credits' )->useDatabase( 'false' )->inLanguage( 'en' )->text();
1820 $credits = str_replace( "\n", "\n ", $credits );
1821 $msg .= "\n*** Credits: ***\n $credits\n";
1822
1823 return $msg;
1824 }
1825
1826 /**
1827 * @deprecated since 1.25
1828 * @param ApiBase $module
1829 * @param string $paramName What type of request is this? e.g. action,
1830 * query, list, prop, meta, format
1831 * @return string
1832 */
1833 public static function makeHelpMsgHeader( $module, $paramName ) {
1834 wfDeprecated( __METHOD__, '1.25' );
1835 $modulePrefix = $module->getModulePrefix();
1836 if ( strval( $modulePrefix ) !== '' ) {
1837 $modulePrefix = "($modulePrefix) ";
1838 }
1839
1840 return "* $paramName={$module->getModuleName()} $modulePrefix*";
1841 }
1842
1843 /**@}*/
1844
1845 }
1846
1847 /**
1848 * This exception will be thrown when dieUsage is called to stop module execution.
1849 *
1850 * @ingroup API
1851 */
1852 class UsageException extends MWException {
1853
1854 private $mCodestr;
1855
1856 /**
1857 * @var null|array
1858 */
1859 private $mExtraData;
1860
1861 /**
1862 * @param string $message
1863 * @param string $codestr
1864 * @param int $code
1865 * @param array|null $extradata
1866 */
1867 public function __construct( $message, $codestr, $code = 0, $extradata = null ) {
1868 parent::__construct( $message, $code );
1869 $this->mCodestr = $codestr;
1870 $this->mExtraData = $extradata;
1871 }
1872
1873 /**
1874 * @return string
1875 */
1876 public function getCodeString() {
1877 return $this->mCodestr;
1878 }
1879
1880 /**
1881 * @return array
1882 */
1883 public function getMessageArray() {
1884 $result = [
1885 'code' => $this->mCodestr,
1886 'info' => $this->getMessage()
1887 ];
1888 if ( is_array( $this->mExtraData ) ) {
1889 $result = array_merge( $result, $this->mExtraData );
1890 }
1891
1892 return $result;
1893 }
1894
1895 /**
1896 * @return string
1897 */
1898 public function __toString() {
1899 return "{$this->getCodeString()}: {$this->getMessage()}";
1900 }
1901 }
1902
1903 /**
1904 * For really cool vim folding this needs to be at the end:
1905 * vim: foldmarker=@{,@} foldmethod=marker
1906 */