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