Merge "Keep headers from jumping when expire interface is shown"
[lhc/web/wiklou.git] / includes / api / ApiMain.php
1 <?php
2 /**
3 * Created on Sep 4, 2006
4 *
5 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @defgroup API API
24 */
25
26 use MediaWiki\Logger\LoggerFactory;
27 use MediaWiki\MediaWikiServices;
28 use Wikimedia\Timestamp\TimestampException;
29 use Wikimedia\Rdbms\DBQueryError;
30 use Wikimedia\Rdbms\DBError;
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 $class = preg_replace( '#^Wikimedia\\\Rdbms\\\#', '', get_class( $e ) );
1048 $code = 'internal_api_error_' . $class;
1049 if ( ( $e instanceof DBQueryError ) && !$config->get( 'ShowSQLErrors' ) ) {
1050 $params = [ 'apierror-databaseerror', WebRequest::getRequestId() ];
1051 } else {
1052 $params = [
1053 'apierror-exceptioncaught',
1054 WebRequest::getRequestId(),
1055 $e instanceof ILocalizedException
1056 ? $e->getMessageObject()
1057 : wfEscapeWikiText( $e->getMessage() )
1058 ];
1059 }
1060 $messages[] = ApiMessage::create( $params, $code );
1061 }
1062 return $messages;
1063 }
1064
1065 /**
1066 * Replace the result data with the information about an exception.
1067 * @param Exception $e
1068 * @return string[] Error codes
1069 */
1070 protected function substituteResultWithError( $e ) {
1071 $result = $this->getResult();
1072 $formatter = $this->getErrorFormatter();
1073 $config = $this->getConfig();
1074 $errorCodes = [];
1075
1076 // Remember existing warnings and errors across the reset
1077 $errors = $result->getResultData( [ 'errors' ] );
1078 $warnings = $result->getResultData( [ 'warnings' ] );
1079 $result->reset();
1080 if ( $warnings !== null ) {
1081 $result->addValue( null, 'warnings', $warnings, ApiResult::NO_SIZE_CHECK );
1082 }
1083 if ( $errors !== null ) {
1084 $result->addValue( null, 'errors', $errors, ApiResult::NO_SIZE_CHECK );
1085
1086 // Collect the copied error codes for the return value
1087 foreach ( $errors as $error ) {
1088 if ( isset( $error['code'] ) ) {
1089 $errorCodes[$error['code']] = true;
1090 }
1091 }
1092 }
1093
1094 // Add errors from the exception
1095 $modulePath = $e instanceof ApiUsageException ? $e->getModulePath() : null;
1096 foreach ( $this->errorMessagesFromException( $e, 'error' ) as $msg ) {
1097 $errorCodes[$msg->getApiCode()] = true;
1098 $formatter->addError( $modulePath, $msg );
1099 }
1100 foreach ( $this->errorMessagesFromException( $e, 'warning' ) as $msg ) {
1101 $formatter->addWarning( $modulePath, $msg );
1102 }
1103
1104 // Add additional data. Path depends on whether we're in BC mode or not.
1105 // Data depends on the type of exception.
1106 if ( $formatter instanceof ApiErrorFormatter_BackCompat ) {
1107 $path = [ 'error' ];
1108 } else {
1109 $path = null;
1110 }
1111 if ( $e instanceof ApiUsageException || $e instanceof UsageException ) {
1112 $link = wfExpandUrl( wfScript( 'api' ) );
1113 $result->addContentValue(
1114 $path,
1115 'docref',
1116 trim(
1117 $this->msg( 'api-usage-docref', $link )->inLanguage( $formatter->getLanguage() )->text()
1118 . ' '
1119 . $this->msg( 'api-usage-mailinglist-ref' )->inLanguage( $formatter->getLanguage() )->text()
1120 )
1121 );
1122 } else {
1123 if ( $config->get( 'ShowExceptionDetails' ) &&
1124 ( !$e instanceof DBError || $config->get( 'ShowDBErrorBacktrace' ) )
1125 ) {
1126 $result->addContentValue(
1127 $path,
1128 'trace',
1129 $this->msg( 'api-exception-trace',
1130 get_class( $e ),
1131 $e->getFile(),
1132 $e->getLine(),
1133 MWExceptionHandler::getRedactedTraceAsString( $e )
1134 )->inLanguage( $formatter->getLanguage() )->text()
1135 );
1136 }
1137 }
1138
1139 // Add the id and such
1140 $this->addRequestedFields( [ 'servedby' ] );
1141
1142 return array_keys( $errorCodes );
1143 }
1144
1145 /**
1146 * Add requested fields to the result
1147 * @param string[] $force Which fields to force even if not requested. Accepted values are:
1148 * - servedby
1149 */
1150 protected function addRequestedFields( $force = [] ) {
1151 $result = $this->getResult();
1152
1153 $requestid = $this->getParameter( 'requestid' );
1154 if ( $requestid !== null ) {
1155 $result->addValue( null, 'requestid', $requestid, ApiResult::NO_SIZE_CHECK );
1156 }
1157
1158 if ( $this->getConfig()->get( 'ShowHostnames' ) && (
1159 in_array( 'servedby', $force, true ) || $this->getParameter( 'servedby' )
1160 ) ) {
1161 $result->addValue( null, 'servedby', wfHostname(), ApiResult::NO_SIZE_CHECK );
1162 }
1163
1164 if ( $this->getParameter( 'curtimestamp' ) ) {
1165 $result->addValue( null, 'curtimestamp', wfTimestamp( TS_ISO_8601, time() ),
1166 ApiResult::NO_SIZE_CHECK );
1167 }
1168
1169 if ( $this->getParameter( 'responselanginfo' ) ) {
1170 $result->addValue( null, 'uselang', $this->getLanguage()->getCode(),
1171 ApiResult::NO_SIZE_CHECK );
1172 $result->addValue( null, 'errorlang', $this->getErrorFormatter()->getLanguage()->getCode(),
1173 ApiResult::NO_SIZE_CHECK );
1174 }
1175 }
1176
1177 /**
1178 * Set up for the execution.
1179 * @return array
1180 */
1181 protected function setupExecuteAction() {
1182 $this->addRequestedFields();
1183
1184 $params = $this->extractRequestParams();
1185 $this->mAction = $params['action'];
1186
1187 return $params;
1188 }
1189
1190 /**
1191 * Set up the module for response
1192 * @return ApiBase The module that will handle this action
1193 * @throws MWException
1194 * @throws ApiUsageException
1195 */
1196 protected function setupModule() {
1197 // Instantiate the module requested by the user
1198 $module = $this->mModuleMgr->getModule( $this->mAction, 'action' );
1199 if ( $module === null ) {
1200 $this->dieWithError(
1201 [ 'apierror-unknownaction', wfEscapeWikiText( $this->mAction ) ], 'unknown_action'
1202 );
1203 }
1204 $moduleParams = $module->extractRequestParams();
1205
1206 // Check token, if necessary
1207 if ( $module->needsToken() === true ) {
1208 throw new MWException(
1209 "Module '{$module->getModuleName()}' must be updated for the new token handling. " .
1210 'See documentation for ApiBase::needsToken for details.'
1211 );
1212 }
1213 if ( $module->needsToken() ) {
1214 if ( !$module->mustBePosted() ) {
1215 throw new MWException(
1216 "Module '{$module->getModuleName()}' must require POST to use tokens."
1217 );
1218 }
1219
1220 if ( !isset( $moduleParams['token'] ) ) {
1221 $module->dieWithError( [ 'apierror-missingparam', 'token' ] );
1222 }
1223
1224 $module->requirePostedParameters( [ 'token' ] );
1225
1226 if ( !$module->validateToken( $moduleParams['token'], $moduleParams ) ) {
1227 $module->dieWithError( 'apierror-badtoken' );
1228 }
1229 }
1230
1231 return $module;
1232 }
1233
1234 /**
1235 * @return array
1236 */
1237 private function getMaxLag() {
1238 $dbLag = MediaWikiServices::getInstance()->getDBLoadBalancer()->getMaxLag();
1239 $lagInfo = [
1240 'host' => $dbLag[0],
1241 'lag' => $dbLag[1],
1242 'type' => 'db'
1243 ];
1244
1245 $jobQueueLagFactor = $this->getConfig()->get( 'JobQueueIncludeInMaxLagFactor' );
1246 if ( $jobQueueLagFactor ) {
1247 // Turn total number of jobs into seconds by using the configured value
1248 $totalJobs = array_sum( JobQueueGroup::singleton()->getQueueSizes() );
1249 $jobQueueLag = $totalJobs / (float)$jobQueueLagFactor;
1250 if ( $jobQueueLag > $lagInfo['lag'] ) {
1251 $lagInfo = [
1252 'host' => wfHostname(), // XXX: Is there a better value that could be used?
1253 'lag' => $jobQueueLag,
1254 'type' => 'jobqueue',
1255 'jobs' => $totalJobs,
1256 ];
1257 }
1258 }
1259
1260 return $lagInfo;
1261 }
1262
1263 /**
1264 * Check the max lag if necessary
1265 * @param ApiBase $module Api module being used
1266 * @param array $params Array an array containing the request parameters.
1267 * @return bool True on success, false should exit immediately
1268 */
1269 protected function checkMaxLag( $module, $params ) {
1270 if ( $module->shouldCheckMaxlag() && isset( $params['maxlag'] ) ) {
1271 $maxLag = $params['maxlag'];
1272 $lagInfo = $this->getMaxLag();
1273 if ( $lagInfo['lag'] > $maxLag ) {
1274 $response = $this->getRequest()->response();
1275
1276 $response->header( 'Retry-After: ' . max( intval( $maxLag ), 5 ) );
1277 $response->header( 'X-Database-Lag: ' . intval( $lagInfo['lag'] ) );
1278
1279 if ( $this->getConfig()->get( 'ShowHostnames' ) ) {
1280 $this->dieWithError(
1281 [ 'apierror-maxlag', $lagInfo['lag'], $lagInfo['host'] ],
1282 'maxlag',
1283 $lagInfo
1284 );
1285 }
1286
1287 $this->dieWithError( [ 'apierror-maxlag-generic', $lagInfo['lag'] ], 'maxlag', $lagInfo );
1288 }
1289 }
1290
1291 return true;
1292 }
1293
1294 /**
1295 * Check selected RFC 7232 precondition headers
1296 *
1297 * RFC 7232 envisions a particular model where you send your request to "a
1298 * resource", and for write requests that you can read "the resource" by
1299 * changing the method to GET. When the API receives a GET request, it
1300 * works out even though "the resource" from RFC 7232's perspective might
1301 * be many resources from MediaWiki's perspective. But it totally fails for
1302 * a POST, since what HTTP sees as "the resource" is probably just
1303 * "/api.php" with all the interesting bits in the body.
1304 *
1305 * Therefore, we only support RFC 7232 precondition headers for GET (and
1306 * HEAD). That means we don't need to bother with If-Match and
1307 * If-Unmodified-Since since they only apply to modification requests.
1308 *
1309 * And since we don't support Range, If-Range is ignored too.
1310 *
1311 * @since 1.26
1312 * @param ApiBase $module Api module being used
1313 * @return bool True on success, false should exit immediately
1314 */
1315 protected function checkConditionalRequestHeaders( $module ) {
1316 if ( $this->mInternalMode ) {
1317 // No headers to check in internal mode
1318 return true;
1319 }
1320
1321 if ( $this->getRequest()->getMethod() !== 'GET' && $this->getRequest()->getMethod() !== 'HEAD' ) {
1322 // Don't check POSTs
1323 return true;
1324 }
1325
1326 $return304 = false;
1327
1328 $ifNoneMatch = array_diff(
1329 $this->getRequest()->getHeader( 'If-None-Match', WebRequest::GETHEADER_LIST ) ?: [],
1330 [ '' ]
1331 );
1332 if ( $ifNoneMatch ) {
1333 if ( $ifNoneMatch === [ '*' ] ) {
1334 // API responses always "exist"
1335 $etag = '*';
1336 } else {
1337 $etag = $module->getConditionalRequestData( 'etag' );
1338 }
1339 }
1340 if ( $ifNoneMatch && $etag !== null ) {
1341 $test = substr( $etag, 0, 2 ) === 'W/' ? substr( $etag, 2 ) : $etag;
1342 $match = array_map( function ( $s ) {
1343 return substr( $s, 0, 2 ) === 'W/' ? substr( $s, 2 ) : $s;
1344 }, $ifNoneMatch );
1345 $return304 = in_array( $test, $match, true );
1346 } else {
1347 $value = trim( $this->getRequest()->getHeader( 'If-Modified-Since' ) );
1348
1349 // Some old browsers sends sizes after the date, like this:
1350 // Wed, 20 Aug 2003 06:51:19 GMT; length=5202
1351 // Ignore that.
1352 $i = strpos( $value, ';' );
1353 if ( $i !== false ) {
1354 $value = trim( substr( $value, 0, $i ) );
1355 }
1356
1357 if ( $value !== '' ) {
1358 try {
1359 $ts = new MWTimestamp( $value );
1360 if (
1361 // RFC 7231 IMF-fixdate
1362 $ts->getTimestamp( TS_RFC2822 ) === $value ||
1363 // RFC 850
1364 $ts->format( 'l, d-M-y H:i:s' ) . ' GMT' === $value ||
1365 // asctime (with and without space-padded day)
1366 $ts->format( 'D M j H:i:s Y' ) === $value ||
1367 $ts->format( 'D M j H:i:s Y' ) === $value
1368 ) {
1369 $lastMod = $module->getConditionalRequestData( 'last-modified' );
1370 if ( $lastMod !== null ) {
1371 // Mix in some MediaWiki modification times
1372 $modifiedTimes = [
1373 'page' => $lastMod,
1374 'user' => $this->getUser()->getTouched(),
1375 'epoch' => $this->getConfig()->get( 'CacheEpoch' ),
1376 ];
1377 if ( $this->getConfig()->get( 'UseSquid' ) ) {
1378 // T46570: the core page itself may not change, but resources might
1379 $modifiedTimes['sepoch'] = wfTimestamp(
1380 TS_MW, time() - $this->getConfig()->get( 'SquidMaxage' )
1381 );
1382 }
1383 Hooks::run( 'OutputPageCheckLastModified', [ &$modifiedTimes, $this->getOutput() ] );
1384 $lastMod = max( $modifiedTimes );
1385 $return304 = wfTimestamp( TS_MW, $lastMod ) <= $ts->getTimestamp( TS_MW );
1386 }
1387 }
1388 } catch ( TimestampException $e ) {
1389 // Invalid timestamp, ignore it
1390 }
1391 }
1392 }
1393
1394 if ( $return304 ) {
1395 $this->getRequest()->response()->statusHeader( 304 );
1396
1397 // Avoid outputting the compressed representation of a zero-length body
1398 MediaWiki\suppressWarnings();
1399 ini_set( 'zlib.output_compression', 0 );
1400 MediaWiki\restoreWarnings();
1401 wfClearOutputBuffers();
1402
1403 return false;
1404 }
1405
1406 return true;
1407 }
1408
1409 /**
1410 * Check for sufficient permissions to execute
1411 * @param ApiBase $module An Api module
1412 */
1413 protected function checkExecutePermissions( $module ) {
1414 $user = $this->getUser();
1415 if ( $module->isReadMode() && !User::isEveryoneAllowed( 'read' ) &&
1416 !$user->isAllowed( 'read' )
1417 ) {
1418 $this->dieWithError( 'apierror-readapidenied' );
1419 }
1420
1421 if ( $module->isWriteMode() ) {
1422 if ( !$this->mEnableWrite ) {
1423 $this->dieWithError( 'apierror-noapiwrite' );
1424 } elseif ( !$user->isAllowed( 'writeapi' ) ) {
1425 $this->dieWithError( 'apierror-writeapidenied' );
1426 } elseif ( $this->getRequest()->getHeader( 'Promise-Non-Write-API-Action' ) ) {
1427 $this->dieWithError( 'apierror-promised-nonwrite-api' );
1428 }
1429
1430 $this->checkReadOnly( $module );
1431 }
1432
1433 // Allow extensions to stop execution for arbitrary reasons.
1434 $message = false;
1435 if ( !Hooks::run( 'ApiCheckCanExecute', [ $module, $user, &$message ] ) ) {
1436 $this->dieWithError( $message );
1437 }
1438 }
1439
1440 /**
1441 * Check if the DB is read-only for this user
1442 * @param ApiBase $module An Api module
1443 */
1444 protected function checkReadOnly( $module ) {
1445 if ( wfReadOnly() ) {
1446 $this->dieReadOnly();
1447 }
1448
1449 if ( $module->isWriteMode()
1450 && $this->getUser()->isBot()
1451 && wfGetLB()->getServerCount() > 1
1452 ) {
1453 $this->checkBotReadOnly();
1454 }
1455 }
1456
1457 /**
1458 * Check whether we are readonly for bots
1459 */
1460 private function checkBotReadOnly() {
1461 // Figure out how many servers have passed the lag threshold
1462 $numLagged = 0;
1463 $lagLimit = $this->getConfig()->get( 'APIMaxLagThreshold' );
1464 $laggedServers = [];
1465 $loadBalancer = wfGetLB();
1466 foreach ( $loadBalancer->getLagTimes() as $serverIndex => $lag ) {
1467 if ( $lag > $lagLimit ) {
1468 ++$numLagged;
1469 $laggedServers[] = $loadBalancer->getServerName( $serverIndex ) . " ({$lag}s)";
1470 }
1471 }
1472
1473 // If a majority of replica DBs are too lagged then disallow writes
1474 $replicaCount = wfGetLB()->getServerCount() - 1;
1475 if ( $numLagged >= ceil( $replicaCount / 2 ) ) {
1476 $laggedServers = implode( ', ', $laggedServers );
1477 wfDebugLog(
1478 'api-readonly',
1479 "Api request failed as read only because the following DBs are lagged: $laggedServers"
1480 );
1481
1482 $this->dieWithError(
1483 'readonly_lag',
1484 'readonly',
1485 [ 'readonlyreason' => "Waiting for $numLagged lagged database(s)" ]
1486 );
1487 }
1488 }
1489
1490 /**
1491 * Check asserts of the user's rights
1492 * @param array $params
1493 */
1494 protected function checkAsserts( $params ) {
1495 if ( isset( $params['assert'] ) ) {
1496 $user = $this->getUser();
1497 switch ( $params['assert'] ) {
1498 case 'user':
1499 if ( $user->isAnon() ) {
1500 $this->dieWithError( 'apierror-assertuserfailed' );
1501 }
1502 break;
1503 case 'bot':
1504 if ( !$user->isAllowed( 'bot' ) ) {
1505 $this->dieWithError( 'apierror-assertbotfailed' );
1506 }
1507 break;
1508 }
1509 }
1510 if ( isset( $params['assertuser'] ) ) {
1511 $assertUser = User::newFromName( $params['assertuser'], false );
1512 if ( !$assertUser || !$this->getUser()->equals( $assertUser ) ) {
1513 $this->dieWithError(
1514 [ 'apierror-assertnameduserfailed', wfEscapeWikiText( $params['assertuser'] ) ]
1515 );
1516 }
1517 }
1518 }
1519
1520 /**
1521 * Check POST for external response and setup result printer
1522 * @param ApiBase $module An Api module
1523 * @param array $params An array with the request parameters
1524 */
1525 protected function setupExternalResponse( $module, $params ) {
1526 $request = $this->getRequest();
1527 if ( !$request->wasPosted() && $module->mustBePosted() ) {
1528 // Module requires POST. GET request might still be allowed
1529 // if $wgDebugApi is true, otherwise fail.
1530 $this->dieWithErrorOrDebug( [ 'apierror-mustbeposted', $this->mAction ] );
1531 }
1532
1533 // See if custom printer is used
1534 $this->mPrinter = $module->getCustomPrinter();
1535 if ( is_null( $this->mPrinter ) ) {
1536 // Create an appropriate printer
1537 $this->mPrinter = $this->createPrinterByName( $params['format'] );
1538 }
1539
1540 if ( $request->getProtocol() === 'http' && (
1541 $request->getSession()->shouldForceHTTPS() ||
1542 ( $this->getUser()->isLoggedIn() &&
1543 $this->getUser()->requiresHTTPS() )
1544 ) ) {
1545 $this->addDeprecation( 'apiwarn-deprecation-httpsexpected', 'https-expected' );
1546 }
1547 }
1548
1549 /**
1550 * Execute the actual module, without any error handling
1551 */
1552 protected function executeAction() {
1553 $params = $this->setupExecuteAction();
1554 $module = $this->setupModule();
1555 $this->mModule = $module;
1556
1557 if ( !$this->mInternalMode ) {
1558 $this->setRequestExpectations( $module );
1559 }
1560
1561 $this->checkExecutePermissions( $module );
1562
1563 if ( !$this->checkMaxLag( $module, $params ) ) {
1564 return;
1565 }
1566
1567 if ( !$this->checkConditionalRequestHeaders( $module ) ) {
1568 return;
1569 }
1570
1571 if ( !$this->mInternalMode ) {
1572 $this->setupExternalResponse( $module, $params );
1573 }
1574
1575 $this->checkAsserts( $params );
1576
1577 // Execute
1578 $module->execute();
1579 Hooks::run( 'APIAfterExecute', [ &$module ] );
1580
1581 $this->reportUnusedParams();
1582
1583 if ( !$this->mInternalMode ) {
1584 // append Debug information
1585 MWDebug::appendDebugInfoToApiResult( $this->getContext(), $this->getResult() );
1586
1587 // Print result data
1588 $this->printResult();
1589 }
1590 }
1591
1592 /**
1593 * Set database connection, query, and write expectations given this module request
1594 * @param ApiBase $module
1595 */
1596 protected function setRequestExpectations( ApiBase $module ) {
1597 $limits = $this->getConfig()->get( 'TrxProfilerLimits' );
1598 $trxProfiler = Profiler::instance()->getTransactionProfiler();
1599 $trxProfiler->setLogger( LoggerFactory::getInstance( 'DBPerformance' ) );
1600 if ( $this->getRequest()->hasSafeMethod() ) {
1601 $trxProfiler->setExpectations( $limits['GET'], __METHOD__ );
1602 } elseif ( $this->getRequest()->wasPosted() && !$module->isWriteMode() ) {
1603 $trxProfiler->setExpectations( $limits['POST-nonwrite'], __METHOD__ );
1604 $this->getRequest()->markAsSafeRequest();
1605 } else {
1606 $trxProfiler->setExpectations( $limits['POST'], __METHOD__ );
1607 }
1608 }
1609
1610 /**
1611 * Log the preceding request
1612 * @param float $time Time in seconds
1613 * @param Exception $e Exception caught while processing the request
1614 */
1615 protected function logRequest( $time, $e = null ) {
1616 $request = $this->getRequest();
1617 $logCtx = [
1618 'ts' => time(),
1619 'ip' => $request->getIP(),
1620 'userAgent' => $this->getUserAgent(),
1621 'wiki' => wfWikiID(),
1622 'timeSpentBackend' => (int)round( $time * 1000 ),
1623 'hadError' => $e !== null,
1624 'errorCodes' => [],
1625 'params' => [],
1626 ];
1627
1628 if ( $e ) {
1629 foreach ( $this->errorMessagesFromException( $e ) as $msg ) {
1630 $logCtx['errorCodes'][] = $msg->getApiCode();
1631 }
1632 }
1633
1634 // Construct space separated message for 'api' log channel
1635 $msg = "API {$request->getMethod()} " .
1636 wfUrlencode( str_replace( ' ', '_', $this->getUser()->getName() ) ) .
1637 " {$logCtx['ip']} " .
1638 "T={$logCtx['timeSpentBackend']}ms";
1639
1640 $sensitive = array_flip( $this->getSensitiveParams() );
1641 foreach ( $this->getParamsUsed() as $name ) {
1642 $value = $request->getVal( $name );
1643 if ( $value === null ) {
1644 continue;
1645 }
1646
1647 if ( isset( $sensitive[$name] ) ) {
1648 $value = '[redacted]';
1649 $encValue = '[redacted]';
1650 } elseif ( strlen( $value ) > 256 ) {
1651 $value = substr( $value, 0, 256 );
1652 $encValue = $this->encodeRequestLogValue( $value ) . '[...]';
1653 } else {
1654 $encValue = $this->encodeRequestLogValue( $value );
1655 }
1656
1657 $logCtx['params'][$name] = $value;
1658 $msg .= " {$name}={$encValue}";
1659 }
1660
1661 wfDebugLog( 'api', $msg, 'private' );
1662 // ApiAction channel is for structured data consumers
1663 wfDebugLog( 'ApiAction', '', 'private', $logCtx );
1664 }
1665
1666 /**
1667 * Encode a value in a format suitable for a space-separated log line.
1668 * @param string $s
1669 * @return string
1670 */
1671 protected function encodeRequestLogValue( $s ) {
1672 static $table;
1673 if ( !$table ) {
1674 $chars = ';@$!*(),/:';
1675 $numChars = strlen( $chars );
1676 for ( $i = 0; $i < $numChars; $i++ ) {
1677 $table[rawurlencode( $chars[$i] )] = $chars[$i];
1678 }
1679 }
1680
1681 return strtr( rawurlencode( $s ), $table );
1682 }
1683
1684 /**
1685 * Get the request parameters used in the course of the preceding execute() request
1686 * @return array
1687 */
1688 protected function getParamsUsed() {
1689 return array_keys( $this->mParamsUsed );
1690 }
1691
1692 /**
1693 * Mark parameters as used
1694 * @param string|string[] $params
1695 */
1696 public function markParamsUsed( $params ) {
1697 $this->mParamsUsed += array_fill_keys( (array)$params, true );
1698 }
1699
1700 /**
1701 * Get the request parameters that should be considered sensitive
1702 * @since 1.29
1703 * @return array
1704 */
1705 protected function getSensitiveParams() {
1706 return array_keys( $this->mParamsSensitive );
1707 }
1708
1709 /**
1710 * Mark parameters as sensitive
1711 * @since 1.29
1712 * @param string|string[] $params
1713 */
1714 public function markParamsSensitive( $params ) {
1715 $this->mParamsSensitive += array_fill_keys( (array)$params, true );
1716 }
1717
1718 /**
1719 * Get a request value, and register the fact that it was used, for logging.
1720 * @param string $name
1721 * @param mixed $default
1722 * @return mixed
1723 */
1724 public function getVal( $name, $default = null ) {
1725 $this->mParamsUsed[$name] = true;
1726
1727 $ret = $this->getRequest()->getVal( $name );
1728 if ( $ret === null ) {
1729 if ( $this->getRequest()->getArray( $name ) !== null ) {
1730 // See T12262 for why we don't just implode( '|', ... ) the
1731 // array.
1732 $this->addWarning( [ 'apiwarn-unsupportedarray', $name ] );
1733 }
1734 $ret = $default;
1735 }
1736 return $ret;
1737 }
1738
1739 /**
1740 * Get a boolean request value, and register the fact that the parameter
1741 * was used, for logging.
1742 * @param string $name
1743 * @return bool
1744 */
1745 public function getCheck( $name ) {
1746 return $this->getVal( $name, null ) !== null;
1747 }
1748
1749 /**
1750 * Get a request upload, and register the fact that it was used, for logging.
1751 *
1752 * @since 1.21
1753 * @param string $name Parameter name
1754 * @return WebRequestUpload
1755 */
1756 public function getUpload( $name ) {
1757 $this->mParamsUsed[$name] = true;
1758
1759 return $this->getRequest()->getUpload( $name );
1760 }
1761
1762 /**
1763 * Report unused parameters, so the client gets a hint in case it gave us parameters we don't know,
1764 * for example in case of spelling mistakes or a missing 'g' prefix for generators.
1765 */
1766 protected function reportUnusedParams() {
1767 $paramsUsed = $this->getParamsUsed();
1768 $allParams = $this->getRequest()->getValueNames();
1769
1770 if ( !$this->mInternalMode ) {
1771 // Printer has not yet executed; don't warn that its parameters are unused
1772 $printerParams = $this->mPrinter->encodeParamName(
1773 array_keys( $this->mPrinter->getFinalParams() ?: [] )
1774 );
1775 $unusedParams = array_diff( $allParams, $paramsUsed, $printerParams );
1776 } else {
1777 $unusedParams = array_diff( $allParams, $paramsUsed );
1778 }
1779
1780 if ( count( $unusedParams ) ) {
1781 $this->addWarning( [
1782 'apierror-unrecognizedparams',
1783 Message::listParam( array_map( 'wfEscapeWikiText', $unusedParams ), 'comma' ),
1784 count( $unusedParams )
1785 ] );
1786 }
1787 }
1788
1789 /**
1790 * Print results using the current printer
1791 *
1792 * @param int $httpCode HTTP status code, or 0 to not change
1793 */
1794 protected function printResult( $httpCode = 0 ) {
1795 if ( $this->getConfig()->get( 'DebugAPI' ) !== false ) {
1796 $this->addWarning( 'apiwarn-wgDebugAPI' );
1797 }
1798
1799 $printer = $this->mPrinter;
1800 $printer->initPrinter( false );
1801 if ( $httpCode ) {
1802 $printer->setHttpStatus( $httpCode );
1803 }
1804 $printer->execute();
1805 $printer->closePrinter();
1806 }
1807
1808 /**
1809 * @return bool
1810 */
1811 public function isReadMode() {
1812 return false;
1813 }
1814
1815 /**
1816 * See ApiBase for description.
1817 *
1818 * @return array
1819 */
1820 public function getAllowedParams() {
1821 return [
1822 'action' => [
1823 ApiBase::PARAM_DFLT => 'help',
1824 ApiBase::PARAM_TYPE => 'submodule',
1825 ],
1826 'format' => [
1827 ApiBase::PARAM_DFLT => ApiMain::API_DEFAULT_FORMAT,
1828 ApiBase::PARAM_TYPE => 'submodule',
1829 ],
1830 'maxlag' => [
1831 ApiBase::PARAM_TYPE => 'integer'
1832 ],
1833 'smaxage' => [
1834 ApiBase::PARAM_TYPE => 'integer',
1835 ApiBase::PARAM_DFLT => 0
1836 ],
1837 'maxage' => [
1838 ApiBase::PARAM_TYPE => 'integer',
1839 ApiBase::PARAM_DFLT => 0
1840 ],
1841 'assert' => [
1842 ApiBase::PARAM_TYPE => [ 'user', 'bot' ]
1843 ],
1844 'assertuser' => [
1845 ApiBase::PARAM_TYPE => 'user',
1846 ],
1847 'requestid' => null,
1848 'servedby' => false,
1849 'curtimestamp' => false,
1850 'responselanginfo' => false,
1851 'origin' => null,
1852 'uselang' => [
1853 ApiBase::PARAM_DFLT => self::API_DEFAULT_USELANG,
1854 ],
1855 'errorformat' => [
1856 ApiBase::PARAM_TYPE => [ 'plaintext', 'wikitext', 'html', 'raw', 'none', 'bc' ],
1857 ApiBase::PARAM_DFLT => 'bc',
1858 ],
1859 'errorlang' => [
1860 ApiBase::PARAM_DFLT => 'uselang',
1861 ],
1862 'errorsuselocal' => [
1863 ApiBase::PARAM_DFLT => false,
1864 ],
1865 ];
1866 }
1867
1868 /** @see ApiBase::getExamplesMessages() */
1869 protected function getExamplesMessages() {
1870 return [
1871 'action=help'
1872 => 'apihelp-help-example-main',
1873 'action=help&recursivesubmodules=1'
1874 => 'apihelp-help-example-recursive',
1875 ];
1876 }
1877
1878 public function modifyHelp( array &$help, array $options, array &$tocData ) {
1879 // Wish PHP had an "array_insert_before". Instead, we have to manually
1880 // reindex the array to get 'permissions' in the right place.
1881 $oldHelp = $help;
1882 $help = [];
1883 foreach ( $oldHelp as $k => $v ) {
1884 if ( $k === 'submodules' ) {
1885 $help['permissions'] = '';
1886 }
1887 $help[$k] = $v;
1888 }
1889 $help['datatypes'] = '';
1890 $help['credits'] = '';
1891
1892 // Fill 'permissions'
1893 $help['permissions'] .= Html::openElement( 'div',
1894 [ 'class' => 'apihelp-block apihelp-permissions' ] );
1895 $m = $this->msg( 'api-help-permissions' );
1896 if ( !$m->isDisabled() ) {
1897 $help['permissions'] .= Html::rawElement( 'div', [ 'class' => 'apihelp-block-head' ],
1898 $m->numParams( count( self::$mRights ) )->parse()
1899 );
1900 }
1901 $help['permissions'] .= Html::openElement( 'dl' );
1902 foreach ( self::$mRights as $right => $rightMsg ) {
1903 $help['permissions'] .= Html::element( 'dt', null, $right );
1904
1905 $rightMsg = $this->msg( $rightMsg['msg'], $rightMsg['params'] )->parse();
1906 $help['permissions'] .= Html::rawElement( 'dd', null, $rightMsg );
1907
1908 $groups = array_map( function ( $group ) {
1909 return $group == '*' ? 'all' : $group;
1910 }, User::getGroupsWithPermission( $right ) );
1911
1912 $help['permissions'] .= Html::rawElement( 'dd', null,
1913 $this->msg( 'api-help-permissions-granted-to' )
1914 ->numParams( count( $groups ) )
1915 ->params( Message::listParam( $groups ) )
1916 ->parse()
1917 );
1918 }
1919 $help['permissions'] .= Html::closeElement( 'dl' );
1920 $help['permissions'] .= Html::closeElement( 'div' );
1921
1922 // Fill 'datatypes' and 'credits', if applicable
1923 if ( empty( $options['nolead'] ) ) {
1924 $level = $options['headerlevel'];
1925 $tocnumber = &$options['tocnumber'];
1926
1927 $header = $this->msg( 'api-help-datatypes-header' )->parse();
1928
1929 // Add an additional span with sanitized ID
1930 if ( !$this->getConfig()->get( 'ExperimentalHtmlIds' ) ) {
1931 $header = Html::element( 'span', [ 'id' => Sanitizer::escapeId( 'main/datatypes' ) ] ) .
1932 $header;
1933 }
1934 $help['datatypes'] .= Html::rawElement( 'h' . min( 6, $level ),
1935 [ 'id' => 'main/datatypes', 'class' => 'apihelp-header' ],
1936 $header
1937 );
1938 $help['datatypes'] .= $this->msg( 'api-help-datatypes' )->parseAsBlock();
1939 if ( !isset( $tocData['main/datatypes'] ) ) {
1940 $tocnumber[$level]++;
1941 $tocData['main/datatypes'] = [
1942 'toclevel' => count( $tocnumber ),
1943 'level' => $level,
1944 'anchor' => 'main/datatypes',
1945 'line' => $header,
1946 'number' => implode( '.', $tocnumber ),
1947 'index' => false,
1948 ];
1949 }
1950
1951 // Add an additional span with sanitized ID
1952 if ( !$this->getConfig()->get( 'ExperimentalHtmlIds' ) ) {
1953 $header = Html::element( 'span', [ 'id' => Sanitizer::escapeId( 'main/credits' ) ] ) .
1954 $header;
1955 }
1956 $header = $this->msg( 'api-credits-header' )->parse();
1957 $help['credits'] .= Html::rawElement( 'h' . min( 6, $level ),
1958 [ 'id' => 'main/credits', 'class' => 'apihelp-header' ],
1959 $header
1960 );
1961 $help['credits'] .= $this->msg( 'api-credits' )->useDatabase( false )->parseAsBlock();
1962 if ( !isset( $tocData['main/credits'] ) ) {
1963 $tocnumber[$level]++;
1964 $tocData['main/credits'] = [
1965 'toclevel' => count( $tocnumber ),
1966 'level' => $level,
1967 'anchor' => 'main/credits',
1968 'line' => $header,
1969 'number' => implode( '.', $tocnumber ),
1970 'index' => false,
1971 ];
1972 }
1973 }
1974 }
1975
1976 private $mCanApiHighLimits = null;
1977
1978 /**
1979 * Check whether the current user is allowed to use high limits
1980 * @return bool
1981 */
1982 public function canApiHighLimits() {
1983 if ( !isset( $this->mCanApiHighLimits ) ) {
1984 $this->mCanApiHighLimits = $this->getUser()->isAllowed( 'apihighlimits' );
1985 }
1986
1987 return $this->mCanApiHighLimits;
1988 }
1989
1990 /**
1991 * Overrides to return this instance's module manager.
1992 * @return ApiModuleManager
1993 */
1994 public function getModuleManager() {
1995 return $this->mModuleMgr;
1996 }
1997
1998 /**
1999 * Fetches the user agent used for this request
2000 *
2001 * The value will be the combination of the 'Api-User-Agent' header (if
2002 * any) and the standard User-Agent header (if any).
2003 *
2004 * @return string
2005 */
2006 public function getUserAgent() {
2007 return trim(
2008 $this->getRequest()->getHeader( 'Api-user-agent' ) . ' ' .
2009 $this->getRequest()->getHeader( 'User-agent' )
2010 );
2011 }
2012 }
2013
2014 /**
2015 * For really cool vim folding this needs to be at the end:
2016 * vim: foldmarker=@{,@} foldmethod=marker
2017 */