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