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