MediaWiki  1.32.0
ApiMain.php
Go to the documentation of this file.
1 <?php
26 use Wikimedia\Timestamp\TimestampException;
27 
41 class ApiMain extends ApiBase {
45  const API_DEFAULT_FORMAT = 'jsonfm';
46 
50  const API_DEFAULT_USELANG = 'user';
51 
55  private static $Modules = [
56  'login' => ApiLogin::class,
57  'clientlogin' => ApiClientLogin::class,
58  'logout' => ApiLogout::class,
59  'createaccount' => ApiAMCreateAccount::class,
60  'linkaccount' => ApiLinkAccount::class,
61  'unlinkaccount' => ApiRemoveAuthenticationData::class,
62  'changeauthenticationdata' => ApiChangeAuthenticationData::class,
63  'removeauthenticationdata' => ApiRemoveAuthenticationData::class,
64  'resetpassword' => ApiResetPassword::class,
65  'query' => ApiQuery::class,
66  'expandtemplates' => ApiExpandTemplates::class,
67  'parse' => ApiParse::class,
68  'stashedit' => ApiStashEdit::class,
69  'opensearch' => ApiOpenSearch::class,
70  'feedcontributions' => ApiFeedContributions::class,
71  'feedrecentchanges' => ApiFeedRecentChanges::class,
72  'feedwatchlist' => ApiFeedWatchlist::class,
73  'help' => ApiHelp::class,
74  'paraminfo' => ApiParamInfo::class,
75  'rsd' => ApiRsd::class,
76  'compare' => ApiComparePages::class,
77  'tokens' => ApiTokens::class,
78  'checktoken' => ApiCheckToken::class,
79  'cspreport' => ApiCSPReport::class,
80  'validatepassword' => ApiValidatePassword::class,
81 
82  // Write modules
83  'purge' => ApiPurge::class,
84  'setnotificationtimestamp' => ApiSetNotificationTimestamp::class,
85  'rollback' => ApiRollback::class,
86  'delete' => ApiDelete::class,
87  'undelete' => ApiUndelete::class,
88  'protect' => ApiProtect::class,
89  'block' => ApiBlock::class,
90  'unblock' => ApiUnblock::class,
91  'move' => ApiMove::class,
92  'edit' => ApiEditPage::class,
93  'upload' => ApiUpload::class,
94  'filerevert' => ApiFileRevert::class,
95  'emailuser' => ApiEmailUser::class,
96  'watch' => ApiWatch::class,
97  'patrol' => ApiPatrol::class,
98  'import' => ApiImport::class,
99  'clearhasmsg' => ApiClearHasMsg::class,
100  'userrights' => ApiUserrights::class,
101  'options' => ApiOptions::class,
102  'imagerotate' => ApiImageRotate::class,
103  'revisiondelete' => ApiRevisionDelete::class,
104  'managetags' => ApiManageTags::class,
105  'tag' => ApiTag::class,
106  'mergehistory' => ApiMergeHistory::class,
107  'setpagelanguage' => ApiSetPageLanguage::class,
108  ];
109 
113  private static $Formats = [
114  'json' => ApiFormatJson::class,
115  'jsonfm' => ApiFormatJson::class,
116  'php' => ApiFormatPhp::class,
117  'phpfm' => ApiFormatPhp::class,
118  'xml' => ApiFormatXml::class,
119  'xmlfm' => ApiFormatXml::class,
120  'rawfm' => ApiFormatJson::class,
121  'none' => ApiFormatNone::class,
122  ];
123 
130  private static $mRights = [
131  'writeapi' => [
132  'msg' => 'right-writeapi',
133  'params' => []
134  ],
135  'apihighlimits' => [
136  'msg' => 'api-help-right-apihighlimits',
138  ]
139  ];
140 
144  private $mPrinter;
145 
149  private $mAction;
150  private $mEnableWrite;
153  private $mModule;
154 
155  private $mCacheMode = 'private';
156  private $mCacheControl = [];
157  private $mParamsUsed = [];
158  private $mParamsSensitive = [];
159 
161  private $lacksSameOriginSecurity = null;
162 
170  public function __construct( $context = null, $enableWrite = false ) {
171  if ( $context === null ) {
173  } elseif ( $context instanceof WebRequest ) {
174  // BC for pre-1.19
175  $request = $context;
177  }
178  // We set a derivative context so we can change stuff later
179  $this->setContext( new DerivativeContext( $context ) );
180 
181  if ( isset( $request ) ) {
182  $this->getContext()->setRequest( $request );
183  } else {
184  $request = $this->getRequest();
185  }
186 
187  $this->mInternalMode = ( $request instanceof FauxRequest );
188 
189  // Special handling for the main module: $parent === $this
190  parent::__construct( $this, $this->mInternalMode ? 'main_int' : 'main' );
191 
192  $config = $this->getConfig();
193 
194  if ( !$this->mInternalMode ) {
195  // Log if a request with a non-whitelisted Origin header is seen
196  // with session cookies.
197  $originHeader = $request->getHeader( 'Origin' );
198  if ( $originHeader === false ) {
199  $origins = [];
200  } else {
201  $originHeader = trim( $originHeader );
202  $origins = preg_split( '/\s+/', $originHeader );
203  }
204  $sessionCookies = array_intersect(
205  array_keys( $_COOKIE ),
206  MediaWiki\Session\SessionManager::singleton()->getVaryCookies()
207  );
208  if ( $origins && $sessionCookies && (
209  count( $origins ) !== 1 || !self::matchOrigin(
210  $origins[0],
211  $config->get( 'CrossSiteAJAXdomains' ),
212  $config->get( 'CrossSiteAJAXdomainExceptions' )
213  )
214  ) ) {
215  LoggerFactory::getInstance( 'cors' )->warning(
216  'Non-whitelisted CORS request with session cookies', [
217  'origin' => $originHeader,
218  'cookies' => $sessionCookies,
219  'ip' => $request->getIP(),
220  'userAgent' => $this->getUserAgent(),
221  'wiki' => wfWikiID(),
222  ]
223  );
224  }
225 
226  // If we're in a mode that breaks the same-origin policy, strip
227  // user credentials for security.
228  if ( $this->lacksSameOriginSecurity() ) {
229  global $wgUser;
230  wfDebug( "API: stripping user credentials when the same-origin policy is not applied\n" );
231  $wgUser = new User();
232  $this->getContext()->setUser( $wgUser );
233  $request->response()->header( 'MediaWiki-Login-Suppressed: true' );
234  }
235  }
236 
237  $this->mResult = new ApiResult( $this->getConfig()->get( 'APIMaxResultSize' ) );
238 
239  // Setup uselang. This doesn't use $this->getParameter()
240  // because we're not ready to handle errors yet.
241  $uselang = $request->getVal( 'uselang', self::API_DEFAULT_USELANG );
242  if ( $uselang === 'user' ) {
243  // Assume the parent context is going to return the user language
244  // for uselang=user (see T85635).
245  } else {
246  if ( $uselang === 'content' ) {
247  $uselang = MediaWikiServices::getInstance()->getContentLanguage()->getCode();
248  }
250  $this->getContext()->setLanguage( $code );
251  if ( !$this->mInternalMode ) {
252  global $wgLang;
253  $wgLang = $this->getContext()->getLanguage();
254  RequestContext::getMain()->setLanguage( $wgLang );
255  }
256  }
257 
258  // Set up the error formatter. This doesn't use $this->getParameter()
259  // because we're not ready to handle errors yet.
260  $errorFormat = $request->getVal( 'errorformat', 'bc' );
261  $errorLangCode = $request->getVal( 'errorlang', 'uselang' );
262  $errorsUseDB = $request->getCheck( 'errorsuselocal' );
263  if ( in_array( $errorFormat, [ 'plaintext', 'wikitext', 'html', 'raw', 'none' ], true ) ) {
264  if ( $errorLangCode === 'uselang' ) {
265  $errorLang = $this->getLanguage();
266  } elseif ( $errorLangCode === 'content' ) {
267  $errorLang = MediaWikiServices::getInstance()->getContentLanguage();
268  } else {
269  $errorLangCode = RequestContext::sanitizeLangCode( $errorLangCode );
270  $errorLang = Language::factory( $errorLangCode );
271  }
272  $this->mErrorFormatter = new ApiErrorFormatter(
273  $this->mResult, $errorLang, $errorFormat, $errorsUseDB
274  );
275  } else {
276  $this->mErrorFormatter = new ApiErrorFormatter_BackCompat( $this->mResult );
277  }
278  $this->mResult->setErrorFormatter( $this->getErrorFormatter() );
279 
280  $this->mModuleMgr = new ApiModuleManager( $this );
281  $this->mModuleMgr->addModules( self::$Modules, 'action' );
282  $this->mModuleMgr->addModules( $config->get( 'APIModules' ), 'action' );
283  $this->mModuleMgr->addModules( self::$Formats, 'format' );
284  $this->mModuleMgr->addModules( $config->get( 'APIFormatModules' ), 'format' );
285 
286  Hooks::run( 'ApiMain::moduleManager', [ $this->mModuleMgr ] );
287 
288  $this->mContinuationManager = null;
289  $this->mEnableWrite = $enableWrite;
290 
291  $this->mSquidMaxage = -1; // flag for executeActionWithErrorHandling()
292  $this->mCommit = false;
293  }
294 
299  public function isInternalMode() {
300  return $this->mInternalMode;
301  }
302 
308  public function getResult() {
309  return $this->mResult;
310  }
311 
316  public function lacksSameOriginSecurity() {
317  if ( $this->lacksSameOriginSecurity !== null ) {
319  }
320 
321  $request = $this->getRequest();
322 
323  // JSONP mode
324  if ( $request->getVal( 'callback' ) !== null ) {
325  $this->lacksSameOriginSecurity = true;
326  return true;
327  }
328 
329  // Anonymous CORS
330  if ( $request->getVal( 'origin' ) === '*' ) {
331  $this->lacksSameOriginSecurity = true;
332  return true;
333  }
334 
335  // Header to be used from XMLHTTPRequest when the request might
336  // otherwise be used for XSS.
337  if ( $request->getHeader( 'Treat-as-Untrusted' ) !== false ) {
338  $this->lacksSameOriginSecurity = true;
339  return true;
340  }
341 
342  // Allow extensions to override.
343  $this->lacksSameOriginSecurity = !Hooks::run( 'RequestHasSameOriginSecurity', [ $request ] );
345  }
346 
351  public function getErrorFormatter() {
352  return $this->mErrorFormatter;
353  }
354 
359  public function getContinuationManager() {
361  }
362 
367  public function setContinuationManager( ApiContinuationManager $manager = null ) {
368  if ( $manager !== null && $this->mContinuationManager !== null ) {
369  throw new UnexpectedValueException(
370  __METHOD__ . ': tried to set manager from ' . $manager->getSource() .
371  ' when a manager is already set from ' . $this->mContinuationManager->getSource()
372  );
373  }
374  $this->mContinuationManager = $manager;
375  }
376 
382  public function getModule() {
383  return $this->mModule;
384  }
385 
391  public function getPrinter() {
392  return $this->mPrinter;
393  }
394 
400  public function setCacheMaxAge( $maxage ) {
401  $this->setCacheControl( [
402  'max-age' => $maxage,
403  's-maxage' => $maxage
404  ] );
405  }
406 
432  public function setCacheMode( $mode ) {
433  if ( !in_array( $mode, [ 'private', 'public', 'anon-public-user-private' ] ) ) {
434  wfDebug( __METHOD__ . ": unrecognised cache mode \"$mode\"\n" );
435 
436  // Ignore for forwards-compatibility
437  return;
438  }
439 
440  if ( !User::isEveryoneAllowed( 'read' ) ) {
441  // Private wiki, only private headers
442  if ( $mode !== 'private' ) {
443  wfDebug( __METHOD__ . ": ignoring request for $mode cache mode, private wiki\n" );
444 
445  return;
446  }
447  }
448 
449  if ( $mode === 'public' && $this->getParameter( 'uselang' ) === 'user' ) {
450  // User language is used for i18n, so we don't want to publicly
451  // cache. Anons are ok, because if they have non-default language
452  // then there's an appropriate Vary header set by whatever set
453  // their non-default language.
454  wfDebug( __METHOD__ . ": downgrading cache mode 'public' to " .
455  "'anon-public-user-private' due to uselang=user\n" );
456  $mode = 'anon-public-user-private';
457  }
458 
459  wfDebug( __METHOD__ . ": setting cache mode $mode\n" );
460  $this->mCacheMode = $mode;
461  }
462 
473  public function setCacheControl( $directives ) {
474  $this->mCacheControl = $directives + $this->mCacheControl;
475  }
476 
484  public function createPrinterByName( $format ) {
485  $printer = $this->mModuleMgr->getModule( $format, 'format', /* $ignoreCache */ true );
486  if ( $printer === null ) {
487  $this->dieWithError(
488  [ 'apierror-unknownformat', wfEscapeWikiText( $format ) ], 'unknown_format'
489  );
490  }
491 
492  return $printer;
493  }
494 
498  public function execute() {
499  if ( $this->mInternalMode ) {
500  $this->executeAction();
501  } else {
503  }
504  }
505 
510  protected function executeActionWithErrorHandling() {
511  // Verify the CORS header before executing the action
512  if ( !$this->handleCORS() ) {
513  // handleCORS() has sent a 403, abort
514  return;
515  }
516 
517  // Exit here if the request method was OPTIONS
518  // (assume there will be a followup GET or POST)
519  if ( $this->getRequest()->getMethod() === 'OPTIONS' ) {
520  return;
521  }
522 
523  // In case an error occurs during data output,
524  // clear the output buffer and print just the error information
525  $obLevel = ob_get_level();
526  ob_start();
527 
528  $t = microtime( true );
529  $isError = false;
530  try {
531  $this->executeAction();
532  $runTime = microtime( true ) - $t;
533  $this->logRequest( $runTime );
534  MediaWikiServices::getInstance()->getStatsdDataFactory()->timing(
535  'api.' . $this->mModule->getModuleName() . '.executeTiming', 1000 * $runTime
536  );
537  } catch ( Exception $e ) { // @todo Remove this block when HHVM is no longer supported
538  $this->handleException( $e );
539  $this->logRequest( microtime( true ) - $t, $e );
540  $isError = true;
541  } catch ( Throwable $e ) {
542  $this->handleException( $e );
543  $this->logRequest( microtime( true ) - $t, $e );
544  $isError = true;
545  }
546 
547  // Commit DBs and send any related cookies and headers
549 
550  // Send cache headers after any code which might generate an error, to
551  // avoid sending public cache headers for errors.
552  $this->sendCacheHeaders( $isError );
553 
554  // Executing the action might have already messed with the output
555  // buffers.
556  while ( ob_get_level() > $obLevel ) {
557  ob_end_flush();
558  }
559  }
560 
567  protected function handleException( $e ) {
568  // T65145: Rollback any open database transactions
569  if ( !$e instanceof ApiUsageException ) {
570  // ApiUsageExceptions are intentional, so don't rollback if that's the case
572  }
573 
574  // Allow extra cleanup and logging
575  Hooks::run( 'ApiMain::onException', [ $this, $e ] );
576 
577  // Handle any kind of exception by outputting properly formatted error message.
578  // If this fails, an unhandled exception should be thrown so that global error
579  // handler will process and log it.
580 
581  $errCodes = $this->substituteResultWithError( $e );
582 
583  // Error results should not be cached
584  $this->setCacheMode( 'private' );
585 
586  $response = $this->getRequest()->response();
587  $headerStr = 'MediaWiki-API-Error: ' . implode( ', ', $errCodes );
588  $response->header( $headerStr );
589 
590  // Reset and print just the error message
591  ob_clean();
592 
593  // Printer may not be initialized if the extractRequestParams() fails for the main module
594  $this->createErrorPrinter();
595 
596  $failed = false;
597  try {
598  $this->printResult( $e->getCode() );
599  } catch ( ApiUsageException $ex ) {
600  // The error printer itself is failing. Try suppressing its request
601  // parameters and redo.
602  $failed = true;
603  $this->addWarning( 'apiwarn-errorprinterfailed' );
604  foreach ( $ex->getStatusValue()->getErrors() as $error ) {
605  try {
606  $this->mPrinter->addWarning( $error );
607  } catch ( Exception $ex2 ) { // @todo Remove this block when HHVM is no longer supported
608  // WTF?
609  $this->addWarning( $error );
610  } catch ( Throwable $ex2 ) {
611  // WTF?
612  $this->addWarning( $error );
613  }
614  }
615  }
616  if ( $failed ) {
617  $this->mPrinter = null;
618  $this->createErrorPrinter();
619  $this->mPrinter->forceDefaultParams();
620  if ( $e->getCode() ) {
621  $response->statusHeader( 200 ); // Reset in case the fallback doesn't want a non-200
622  }
623  $this->printResult( $e->getCode() );
624  }
625  }
626 
637  public static function handleApiBeforeMainException( $e ) {
638  ob_start();
639 
640  try {
641  $main = new self( RequestContext::getMain(), false );
642  $main->handleException( $e );
643  $main->logRequest( 0, $e );
644  } catch ( Exception $e2 ) { // @todo Remove this block when HHVM is no longer supported
645  // Nope, even that didn't work. Punt.
646  throw $e;
647  } catch ( Throwable $e2 ) {
648  // Nope, even that didn't work. Punt.
649  throw $e;
650  }
651 
652  // Reset cache headers
653  $main->sendCacheHeaders( true );
654 
655  ob_end_flush();
656  }
657 
672  protected function handleCORS() {
673  $originParam = $this->getParameter( 'origin' ); // defaults to null
674  if ( $originParam === null ) {
675  // No origin parameter, nothing to do
676  return true;
677  }
678 
679  $request = $this->getRequest();
680  $response = $request->response();
681 
682  $matchedOrigin = false;
683  $allowTiming = false;
684  $varyOrigin = true;
685 
686  if ( $originParam === '*' ) {
687  // Request for anonymous CORS
688  // Technically we should check for the presence of an Origin header
689  // and not process it as CORS if it's not set, but that would
690  // require us to vary on Origin for all 'origin=*' requests which
691  // we don't want to do.
692  $matchedOrigin = true;
693  $allowOrigin = '*';
694  $allowCredentials = 'false';
695  $varyOrigin = false; // No need to vary
696  } else {
697  // Non-anonymous CORS, check we allow the domain
698 
699  // Origin: header is a space-separated list of origins, check all of them
700  $originHeader = $request->getHeader( 'Origin' );
701  if ( $originHeader === false ) {
702  $origins = [];
703  } else {
704  $originHeader = trim( $originHeader );
705  $origins = preg_split( '/\s+/', $originHeader );
706  }
707 
708  if ( !in_array( $originParam, $origins ) ) {
709  // origin parameter set but incorrect
710  // Send a 403 response
711  $response->statusHeader( 403 );
712  $response->header( 'Cache-Control: no-cache' );
713  echo "'origin' parameter does not match Origin header\n";
714 
715  return false;
716  }
717 
718  $config = $this->getConfig();
719  $matchedOrigin = count( $origins ) === 1 && self::matchOrigin(
720  $originParam,
721  $config->get( 'CrossSiteAJAXdomains' ),
722  $config->get( 'CrossSiteAJAXdomainExceptions' )
723  );
724 
725  $allowOrigin = $originHeader;
726  $allowCredentials = 'true';
727  $allowTiming = $originHeader;
728  }
729 
730  if ( $matchedOrigin ) {
731  $requestedMethod = $request->getHeader( 'Access-Control-Request-Method' );
732  $preflight = $request->getMethod() === 'OPTIONS' && $requestedMethod !== false;
733  if ( $preflight ) {
734  // This is a CORS preflight request
735  if ( $requestedMethod !== 'POST' && $requestedMethod !== 'GET' ) {
736  // If method is not a case-sensitive match, do not set any additional headers and terminate.
737  $response->header( 'MediaWiki-CORS-Rejection: Unsupported method requested in preflight' );
738  return true;
739  }
740  // We allow the actual request to send the following headers
741  $requestedHeaders = $request->getHeader( 'Access-Control-Request-Headers' );
742  if ( $requestedHeaders !== false ) {
743  if ( !self::matchRequestedHeaders( $requestedHeaders ) ) {
744  $response->header( 'MediaWiki-CORS-Rejection: Unsupported header requested in preflight' );
745  return true;
746  }
747  $response->header( 'Access-Control-Allow-Headers: ' . $requestedHeaders );
748  }
749 
750  // We only allow the actual request to be GET or POST
751  $response->header( 'Access-Control-Allow-Methods: POST, GET' );
752  } elseif ( $request->getMethod() !== 'POST' && $request->getMethod() !== 'GET' ) {
753  // Unsupported non-preflight method, don't handle it as CORS
754  $response->header(
755  'MediaWiki-CORS-Rejection: Unsupported method for simple request or actual request'
756  );
757  return true;
758  }
759 
760  $response->header( "Access-Control-Allow-Origin: $allowOrigin" );
761  $response->header( "Access-Control-Allow-Credentials: $allowCredentials" );
762  // https://www.w3.org/TR/resource-timing/#timing-allow-origin
763  if ( $allowTiming !== false ) {
764  $response->header( "Timing-Allow-Origin: $allowTiming" );
765  }
766 
767  if ( !$preflight ) {
768  $response->header(
769  'Access-Control-Expose-Headers: MediaWiki-API-Error, Retry-After, X-Database-Lag, '
770  . 'MediaWiki-Login-Suppressed'
771  );
772  }
773  } else {
774  $response->header( 'MediaWiki-CORS-Rejection: Origin mismatch' );
775  }
776 
777  if ( $varyOrigin ) {
778  $this->getOutput()->addVaryHeader( 'Origin' );
779  }
780 
781  return true;
782  }
783 
792  protected static function matchOrigin( $value, $rules, $exceptions ) {
793  foreach ( $rules as $rule ) {
794  if ( preg_match( self::wildcardToRegex( $rule ), $value ) ) {
795  // Rule matches, check exceptions
796  foreach ( $exceptions as $exc ) {
797  if ( preg_match( self::wildcardToRegex( $exc ), $value ) ) {
798  return false;
799  }
800  }
801 
802  return true;
803  }
804  }
805 
806  return false;
807  }
808 
816  protected static function matchRequestedHeaders( $requestedHeaders ) {
817  if ( trim( $requestedHeaders ) === '' ) {
818  return true;
819  }
820  $requestedHeaders = explode( ',', $requestedHeaders );
821  $allowedAuthorHeaders = array_flip( [
822  /* simple headers (see spec) */
823  'accept',
824  'accept-language',
825  'content-language',
826  'content-type',
827  /* non-authorable headers in XHR, which are however requested by some UAs */
828  'accept-encoding',
829  'dnt',
830  'origin',
831  /* MediaWiki whitelist */
832  'api-user-agent',
833  ] );
834  foreach ( $requestedHeaders as $rHeader ) {
835  $rHeader = strtolower( trim( $rHeader ) );
836  if ( !isset( $allowedAuthorHeaders[$rHeader] ) ) {
837  wfDebugLog( 'api', 'CORS preflight failed on requested header: ' . $rHeader );
838  return false;
839  }
840  }
841  return true;
842  }
843 
852  protected static function wildcardToRegex( $wildcard ) {
853  $wildcard = preg_quote( $wildcard, '/' );
854  $wildcard = str_replace(
855  [ '\*', '\?' ],
856  [ '.*?', '.' ],
857  $wildcard
858  );
859 
860  return "/^https?:\/\/$wildcard$/";
861  }
862 
868  protected function sendCacheHeaders( $isError ) {
869  $response = $this->getRequest()->response();
870  $out = $this->getOutput();
871 
872  $out->addVaryHeader( 'Treat-as-Untrusted' );
873 
874  $config = $this->getConfig();
875 
876  if ( $config->get( 'VaryOnXFP' ) ) {
877  $out->addVaryHeader( 'X-Forwarded-Proto' );
878  }
879 
880  if ( !$isError && $this->mModule &&
881  ( $this->getRequest()->getMethod() === 'GET' || $this->getRequest()->getMethod() === 'HEAD' )
882  ) {
883  $etag = $this->mModule->getConditionalRequestData( 'etag' );
884  if ( $etag !== null ) {
885  $response->header( "ETag: $etag" );
886  }
887  $lastMod = $this->mModule->getConditionalRequestData( 'last-modified' );
888  if ( $lastMod !== null ) {
889  $response->header( 'Last-Modified: ' . wfTimestamp( TS_RFC2822, $lastMod ) );
890  }
891  }
892 
893  // The logic should be:
894  // $this->mCacheControl['max-age'] is set?
895  // Use it, the module knows better than our guess.
896  // !$this->mModule || $this->mModule->isWriteMode(), and mCacheMode is private?
897  // Use 0 because we can guess caching is probably the wrong thing to do.
898  // Use $this->getParameter( 'maxage' ), which already defaults to 0.
899  $maxage = 0;
900  if ( isset( $this->mCacheControl['max-age'] ) ) {
901  $maxage = $this->mCacheControl['max-age'];
902  } elseif ( ( $this->mModule && !$this->mModule->isWriteMode() ) ||
903  $this->mCacheMode !== 'private'
904  ) {
905  $maxage = $this->getParameter( 'maxage' );
906  }
907  $privateCache = 'private, must-revalidate, max-age=' . $maxage;
908 
909  if ( $this->mCacheMode == 'private' ) {
910  $response->header( "Cache-Control: $privateCache" );
911  return;
912  }
913 
914  $useKeyHeader = $config->get( 'UseKeyHeader' );
915  if ( $this->mCacheMode == 'anon-public-user-private' ) {
916  $out->addVaryHeader( 'Cookie' );
917  $response->header( $out->getVaryHeader() );
918  if ( $useKeyHeader ) {
919  $response->header( $out->getKeyHeader() );
920  if ( $out->haveCacheVaryCookies() ) {
921  // Logged in, mark this request private
922  $response->header( "Cache-Control: $privateCache" );
923  return;
924  }
925  // Logged out, send normal public headers below
926  } elseif ( MediaWiki\Session\SessionManager::getGlobalSession()->isPersistent() ) {
927  // Logged in or otherwise has session (e.g. anonymous users who have edited)
928  // Mark request private
929  $response->header( "Cache-Control: $privateCache" );
930 
931  return;
932  } // else no Key and anonymous, send public headers below
933  }
934 
935  // Send public headers
936  $response->header( $out->getVaryHeader() );
937  if ( $useKeyHeader ) {
938  $response->header( $out->getKeyHeader() );
939  }
940 
941  // If nobody called setCacheMaxAge(), use the (s)maxage parameters
942  if ( !isset( $this->mCacheControl['s-maxage'] ) ) {
943  $this->mCacheControl['s-maxage'] = $this->getParameter( 'smaxage' );
944  }
945  if ( !isset( $this->mCacheControl['max-age'] ) ) {
946  $this->mCacheControl['max-age'] = $this->getParameter( 'maxage' );
947  }
948 
949  if ( !$this->mCacheControl['s-maxage'] && !$this->mCacheControl['max-age'] ) {
950  // Public cache not requested
951  // Sending a Vary header in this case is harmless, and protects us
952  // against conditional calls of setCacheMaxAge().
953  $response->header( "Cache-Control: $privateCache" );
954 
955  return;
956  }
957 
958  $this->mCacheControl['public'] = true;
959 
960  // Send an Expires header
961  $maxAge = min( $this->mCacheControl['s-maxage'], $this->mCacheControl['max-age'] );
962  $expiryUnixTime = ( $maxAge == 0 ? 1 : time() + $maxAge );
963  $response->header( 'Expires: ' . wfTimestamp( TS_RFC2822, $expiryUnixTime ) );
964 
965  // Construct the Cache-Control header
966  $ccHeader = '';
967  $separator = '';
968  foreach ( $this->mCacheControl as $name => $value ) {
969  if ( is_bool( $value ) ) {
970  if ( $value ) {
971  $ccHeader .= $separator . $name;
972  $separator = ', ';
973  }
974  } else {
975  $ccHeader .= $separator . "$name=$value";
976  $separator = ', ';
977  }
978  }
979 
980  $response->header( "Cache-Control: $ccHeader" );
981  }
982 
986  private function createErrorPrinter() {
987  if ( !isset( $this->mPrinter ) ) {
988  $value = $this->getRequest()->getVal( 'format', self::API_DEFAULT_FORMAT );
989  if ( !$this->mModuleMgr->isDefined( $value, 'format' ) ) {
991  }
992  $this->mPrinter = $this->createPrinterByName( $value );
993  }
994 
995  // Printer may not be able to handle errors. This is particularly
996  // likely if the module returns something for getCustomPrinter().
997  if ( !$this->mPrinter->canPrintErrors() ) {
998  $this->mPrinter = $this->createPrinterByName( self::API_DEFAULT_FORMAT );
999  }
1000  }
1001 
1017  protected function errorMessagesFromException( $e, $type = 'error' ) {
1018  $messages = [];
1019  if ( $e instanceof ApiUsageException ) {
1020  foreach ( $e->getStatusValue()->getErrorsByType( $type ) as $error ) {
1021  $messages[] = ApiMessage::create( $error );
1022  }
1023  } elseif ( $type !== 'error' ) {
1024  // None of the rest have any messages for non-error types
1025  } else {
1026  // Something is seriously wrong
1027  $config = $this->getConfig();
1028  $class = preg_replace( '#^Wikimedia\\\Rdbms\\\#', '', get_class( $e ) );
1029  $code = 'internal_api_error_' . $class;
1030  if ( $config->get( 'ShowExceptionDetails' ) ) {
1031  if ( $e instanceof ILocalizedException ) {
1032  $msg = $e->getMessageObject();
1033  } elseif ( $e instanceof MessageSpecifier ) {
1034  $msg = Message::newFromSpecifier( $e );
1035  } else {
1036  $msg = wfEscapeWikiText( $e->getMessage() );
1037  }
1038  $params = [ 'apierror-exceptioncaught', WebRequest::getRequestId(), $msg ];
1039  } else {
1040  $params = [ 'apierror-exceptioncaughttype', WebRequest::getRequestId(), get_class( $e ) ];
1041  }
1042 
1044  }
1045  return $messages;
1046  }
1047 
1053  protected function substituteResultWithError( $e ) {
1054  $result = $this->getResult();
1055  $formatter = $this->getErrorFormatter();
1056  $config = $this->getConfig();
1057  $errorCodes = [];
1058 
1059  // Remember existing warnings and errors across the reset
1060  $errors = $result->getResultData( [ 'errors' ] );
1061  $warnings = $result->getResultData( [ 'warnings' ] );
1062  $result->reset();
1063  if ( $warnings !== null ) {
1064  $result->addValue( null, 'warnings', $warnings, ApiResult::NO_SIZE_CHECK );
1065  }
1066  if ( $errors !== null ) {
1067  $result->addValue( null, 'errors', $errors, ApiResult::NO_SIZE_CHECK );
1068 
1069  // Collect the copied error codes for the return value
1070  foreach ( $errors as $error ) {
1071  if ( isset( $error['code'] ) ) {
1072  $errorCodes[$error['code']] = true;
1073  }
1074  }
1075  }
1076 
1077  // Add errors from the exception
1078  $modulePath = $e instanceof ApiUsageException ? $e->getModulePath() : null;
1079  foreach ( $this->errorMessagesFromException( $e, 'error' ) as $msg ) {
1080  $errorCodes[$msg->getApiCode()] = true;
1081  $formatter->addError( $modulePath, $msg );
1082  }
1083  foreach ( $this->errorMessagesFromException( $e, 'warning' ) as $msg ) {
1084  $formatter->addWarning( $modulePath, $msg );
1085  }
1086 
1087  // Add additional data. Path depends on whether we're in BC mode or not.
1088  // Data depends on the type of exception.
1089  if ( $formatter instanceof ApiErrorFormatter_BackCompat ) {
1090  $path = [ 'error' ];
1091  } else {
1092  $path = null;
1093  }
1094  if ( $e instanceof ApiUsageException ) {
1095  $link = wfExpandUrl( wfScript( 'api' ) );
1096  $result->addContentValue(
1097  $path,
1098  'docref',
1099  trim(
1100  $this->msg( 'api-usage-docref', $link )->inLanguage( $formatter->getLanguage() )->text()
1101  . ' '
1102  . $this->msg( 'api-usage-mailinglist-ref' )->inLanguage( $formatter->getLanguage() )->text()
1103  )
1104  );
1105  } else {
1106  if ( $config->get( 'ShowExceptionDetails' ) ) {
1107  $result->addContentValue(
1108  $path,
1109  'trace',
1110  $this->msg( 'api-exception-trace',
1111  get_class( $e ),
1112  $e->getFile(),
1113  $e->getLine(),
1115  )->inLanguage( $formatter->getLanguage() )->text()
1116  );
1117  }
1118  }
1119 
1120  // Add the id and such
1121  $this->addRequestedFields( [ 'servedby' ] );
1122 
1123  return array_keys( $errorCodes );
1124  }
1125 
1131  protected function addRequestedFields( $force = [] ) {
1132  $result = $this->getResult();
1133 
1134  $requestid = $this->getParameter( 'requestid' );
1135  if ( $requestid !== null ) {
1136  $result->addValue( null, 'requestid', $requestid, ApiResult::NO_SIZE_CHECK );
1137  }
1138 
1139  if ( $this->getConfig()->get( 'ShowHostnames' ) && (
1140  in_array( 'servedby', $force, true ) || $this->getParameter( 'servedby' )
1141  ) ) {
1142  $result->addValue( null, 'servedby', wfHostname(), ApiResult::NO_SIZE_CHECK );
1143  }
1144 
1145  if ( $this->getParameter( 'curtimestamp' ) ) {
1146  $result->addValue( null, 'curtimestamp', wfTimestamp( TS_ISO_8601, time() ),
1148  }
1149 
1150  if ( $this->getParameter( 'responselanginfo' ) ) {
1151  $result->addValue( null, 'uselang', $this->getLanguage()->getCode(),
1153  $result->addValue( null, 'errorlang', $this->getErrorFormatter()->getLanguage()->getCode(),
1155  }
1156  }
1157 
1162  protected function setupExecuteAction() {
1163  $this->addRequestedFields();
1164 
1165  $params = $this->extractRequestParams();
1166  $this->mAction = $params['action'];
1167 
1168  return $params;
1169  }
1170 
1177  protected function setupModule() {
1178  // Instantiate the module requested by the user
1179  $module = $this->mModuleMgr->getModule( $this->mAction, 'action' );
1180  if ( $module === null ) {
1181  // Probably can't happen
1182  // @codeCoverageIgnoreStart
1183  $this->dieWithError(
1184  [ 'apierror-unknownaction', wfEscapeWikiText( $this->mAction ) ], 'unknown_action'
1185  );
1186  // @codeCoverageIgnoreEnd
1187  }
1188  $moduleParams = $module->extractRequestParams();
1189 
1190  // Check token, if necessary
1191  if ( $module->needsToken() === true ) {
1192  throw new MWException(
1193  "Module '{$module->getModuleName()}' must be updated for the new token handling. " .
1194  'See documentation for ApiBase::needsToken for details.'
1195  );
1196  }
1197  if ( $module->needsToken() ) {
1198  if ( !$module->mustBePosted() ) {
1199  throw new MWException(
1200  "Module '{$module->getModuleName()}' must require POST to use tokens."
1201  );
1202  }
1203 
1204  if ( !isset( $moduleParams['token'] ) ) {
1205  // Probably can't happen
1206  // @codeCoverageIgnoreStart
1207  $module->dieWithError( [ 'apierror-missingparam', 'token' ] );
1208  // @codeCoverageIgnoreEnd
1209  }
1210 
1211  $module->requirePostedParameters( [ 'token' ] );
1212 
1213  if ( !$module->validateToken( $moduleParams['token'], $moduleParams ) ) {
1214  $module->dieWithError( 'apierror-badtoken' );
1215  }
1216  }
1217 
1218  return $module;
1219  }
1220 
1224  private function getMaxLag() {
1225  $dbLag = MediaWikiServices::getInstance()->getDBLoadBalancer()->getMaxLag();
1226  $lagInfo = [
1227  'host' => $dbLag[0],
1228  'lag' => $dbLag[1],
1229  'type' => 'db'
1230  ];
1231 
1232  $jobQueueLagFactor = $this->getConfig()->get( 'JobQueueIncludeInMaxLagFactor' );
1233  if ( $jobQueueLagFactor ) {
1234  // Turn total number of jobs into seconds by using the configured value
1235  $totalJobs = array_sum( JobQueueGroup::singleton()->getQueueSizes() );
1236  $jobQueueLag = $totalJobs / (float)$jobQueueLagFactor;
1237  if ( $jobQueueLag > $lagInfo['lag'] ) {
1238  $lagInfo = [
1239  'host' => wfHostname(), // XXX: Is there a better value that could be used?
1240  'lag' => $jobQueueLag,
1241  'type' => 'jobqueue',
1242  'jobs' => $totalJobs,
1243  ];
1244  }
1245  }
1246 
1247  Hooks::runWithoutAbort( 'ApiMaxLagInfo', [ &$lagInfo ] );
1248 
1249  return $lagInfo;
1250  }
1251 
1258  protected function checkMaxLag( $module, $params ) {
1259  if ( $module->shouldCheckMaxlag() && isset( $params['maxlag'] ) ) {
1260  $maxLag = $params['maxlag'];
1261  $lagInfo = $this->getMaxLag();
1262  if ( $lagInfo['lag'] > $maxLag ) {
1263  $response = $this->getRequest()->response();
1264 
1265  $response->header( 'Retry-After: ' . max( intval( $maxLag ), 5 ) );
1266  $response->header( 'X-Database-Lag: ' . intval( $lagInfo['lag'] ) );
1267 
1268  if ( $this->getConfig()->get( 'ShowHostnames' ) ) {
1269  $this->dieWithError(
1270  [ 'apierror-maxlag', $lagInfo['lag'], $lagInfo['host'] ],
1271  'maxlag',
1272  $lagInfo
1273  );
1274  }
1275 
1276  $this->dieWithError( [ 'apierror-maxlag-generic', $lagInfo['lag'] ], 'maxlag', $lagInfo );
1277  }
1278  }
1279 
1280  return true;
1281  }
1282 
1304  protected function checkConditionalRequestHeaders( $module ) {
1305  if ( $this->mInternalMode ) {
1306  // No headers to check in internal mode
1307  return true;
1308  }
1309 
1310  if ( $this->getRequest()->getMethod() !== 'GET' && $this->getRequest()->getMethod() !== 'HEAD' ) {
1311  // Don't check POSTs
1312  return true;
1313  }
1314 
1315  $return304 = false;
1316 
1317  $ifNoneMatch = array_diff(
1318  $this->getRequest()->getHeader( 'If-None-Match', WebRequest::GETHEADER_LIST ) ?: [],
1319  [ '' ]
1320  );
1321  if ( $ifNoneMatch ) {
1322  if ( $ifNoneMatch === [ '*' ] ) {
1323  // API responses always "exist"
1324  $etag = '*';
1325  } else {
1326  $etag = $module->getConditionalRequestData( 'etag' );
1327  }
1328  }
1329  if ( $ifNoneMatch && $etag !== null ) {
1330  $test = substr( $etag, 0, 2 ) === 'W/' ? substr( $etag, 2 ) : $etag;
1331  $match = array_map( function ( $s ) {
1332  return substr( $s, 0, 2 ) === 'W/' ? substr( $s, 2 ) : $s;
1333  }, $ifNoneMatch );
1334  $return304 = in_array( $test, $match, true );
1335  } else {
1336  $value = trim( $this->getRequest()->getHeader( 'If-Modified-Since' ) );
1337 
1338  // Some old browsers sends sizes after the date, like this:
1339  // Wed, 20 Aug 2003 06:51:19 GMT; length=5202
1340  // Ignore that.
1341  $i = strpos( $value, ';' );
1342  if ( $i !== false ) {
1343  $value = trim( substr( $value, 0, $i ) );
1344  }
1345 
1346  if ( $value !== '' ) {
1347  try {
1348  $ts = new MWTimestamp( $value );
1349  if (
1350  // RFC 7231 IMF-fixdate
1351  $ts->getTimestamp( TS_RFC2822 ) === $value ||
1352  // RFC 850
1353  $ts->format( 'l, d-M-y H:i:s' ) . ' GMT' === $value ||
1354  // asctime (with and without space-padded day)
1355  $ts->format( 'D M j H:i:s Y' ) === $value ||
1356  $ts->format( 'D M j H:i:s Y' ) === $value
1357  ) {
1358  $lastMod = $module->getConditionalRequestData( 'last-modified' );
1359  if ( $lastMod !== null ) {
1360  // Mix in some MediaWiki modification times
1361  $modifiedTimes = [
1362  'page' => $lastMod,
1363  'user' => $this->getUser()->getTouched(),
1364  'epoch' => $this->getConfig()->get( 'CacheEpoch' ),
1365  ];
1366  if ( $this->getConfig()->get( 'UseSquid' ) ) {
1367  // T46570: the core page itself may not change, but resources might
1368  $modifiedTimes['sepoch'] = wfTimestamp(
1369  TS_MW, time() - $this->getConfig()->get( 'SquidMaxage' )
1370  );
1371  }
1372  Hooks::run( 'OutputPageCheckLastModified', [ &$modifiedTimes, $this->getOutput() ] );
1373  $lastMod = max( $modifiedTimes );
1374  $return304 = wfTimestamp( TS_MW, $lastMod ) <= $ts->getTimestamp( TS_MW );
1375  }
1376  }
1377  } catch ( TimestampException $e ) {
1378  // Invalid timestamp, ignore it
1379  }
1380  }
1381  }
1382 
1383  if ( $return304 ) {
1384  $this->getRequest()->response()->statusHeader( 304 );
1385 
1386  // Avoid outputting the compressed representation of a zero-length body
1387  Wikimedia\suppressWarnings();
1388  ini_set( 'zlib.output_compression', 0 );
1389  Wikimedia\restoreWarnings();
1391 
1392  return false;
1393  }
1394 
1395  return true;
1396  }
1397 
1402  protected function checkExecutePermissions( $module ) {
1403  $user = $this->getUser();
1404  if ( $module->isReadMode() && !User::isEveryoneAllowed( 'read' ) &&
1405  !$user->isAllowed( 'read' )
1406  ) {
1407  $this->dieWithError( 'apierror-readapidenied' );
1408  }
1409 
1410  if ( $module->isWriteMode() ) {
1411  if ( !$this->mEnableWrite ) {
1412  $this->dieWithError( 'apierror-noapiwrite' );
1413  } elseif ( !$user->isAllowed( 'writeapi' ) ) {
1414  $this->dieWithError( 'apierror-writeapidenied' );
1415  } elseif ( $this->getRequest()->getHeader( 'Promise-Non-Write-API-Action' ) ) {
1416  $this->dieWithError( 'apierror-promised-nonwrite-api' );
1417  }
1418 
1419  $this->checkReadOnly( $module );
1420  }
1421 
1422  // Allow extensions to stop execution for arbitrary reasons.
1423  $message = 'hookaborted';
1424  if ( !Hooks::run( 'ApiCheckCanExecute', [ $module, $user, &$message ] ) ) {
1425  $this->dieWithError( $message );
1426  }
1427  }
1428 
1433  protected function checkReadOnly( $module ) {
1434  if ( wfReadOnly() ) {
1435  $this->dieReadOnly();
1436  }
1437 
1438  if ( $module->isWriteMode()
1439  && $this->getUser()->isBot()
1440  && MediaWikiServices::getInstance()->getDBLoadBalancer()->getServerCount() > 1
1441  ) {
1442  $this->checkBotReadOnly();
1443  }
1444  }
1445 
1449  private function checkBotReadOnly() {
1450  // Figure out how many servers have passed the lag threshold
1451  $numLagged = 0;
1452  $lagLimit = $this->getConfig()->get( 'APIMaxLagThreshold' );
1453  $laggedServers = [];
1454  $loadBalancer = MediaWikiServices::getInstance()->getDBLoadBalancer();
1455  foreach ( $loadBalancer->getLagTimes() as $serverIndex => $lag ) {
1456  if ( $lag > $lagLimit ) {
1457  ++$numLagged;
1458  $laggedServers[] = $loadBalancer->getServerName( $serverIndex ) . " ({$lag}s)";
1459  }
1460  }
1461 
1462  // If a majority of replica DBs are too lagged then disallow writes
1463  $replicaCount = $loadBalancer->getServerCount() - 1;
1464  if ( $numLagged >= ceil( $replicaCount / 2 ) ) {
1465  $laggedServers = implode( ', ', $laggedServers );
1466  wfDebugLog(
1467  'api-readonly',
1468  "Api request failed as read only because the following DBs are lagged: $laggedServers"
1469  );
1470 
1471  $this->dieWithError(
1472  'readonly_lag',
1473  'readonly',
1474  [ 'readonlyreason' => "Waiting for $numLagged lagged database(s)" ]
1475  );
1476  }
1477  }
1478 
1483  protected function checkAsserts( $params ) {
1484  if ( isset( $params['assert'] ) ) {
1485  $user = $this->getUser();
1486  switch ( $params['assert'] ) {
1487  case 'user':
1488  if ( $user->isAnon() ) {
1489  $this->dieWithError( 'apierror-assertuserfailed' );
1490  }
1491  break;
1492  case 'bot':
1493  if ( !$user->isAllowed( 'bot' ) ) {
1494  $this->dieWithError( 'apierror-assertbotfailed' );
1495  }
1496  break;
1497  }
1498  }
1499  if ( isset( $params['assertuser'] ) ) {
1500  $assertUser = User::newFromName( $params['assertuser'], false );
1501  if ( !$assertUser || !$this->getUser()->equals( $assertUser ) ) {
1502  $this->dieWithError(
1503  [ 'apierror-assertnameduserfailed', wfEscapeWikiText( $params['assertuser'] ) ]
1504  );
1505  }
1506  }
1507  }
1508 
1514  protected function setupExternalResponse( $module, $params ) {
1515  $request = $this->getRequest();
1516  if ( !$request->wasPosted() && $module->mustBePosted() ) {
1517  // Module requires POST. GET request might still be allowed
1518  // if $wgDebugApi is true, otherwise fail.
1519  $this->dieWithErrorOrDebug( [ 'apierror-mustbeposted', $this->mAction ] );
1520  }
1521 
1522  // See if custom printer is used
1523  $this->mPrinter = $module->getCustomPrinter();
1524  if ( is_null( $this->mPrinter ) ) {
1525  // Create an appropriate printer
1526  $this->mPrinter = $this->createPrinterByName( $params['format'] );
1527  }
1528 
1529  if ( $request->getProtocol() === 'http' && (
1530  $request->getSession()->shouldForceHTTPS() ||
1531  ( $this->getUser()->isLoggedIn() &&
1532  $this->getUser()->requiresHTTPS() )
1533  ) ) {
1534  $this->addDeprecation( 'apiwarn-deprecation-httpsexpected', 'https-expected' );
1535  }
1536  }
1537 
1541  protected function executeAction() {
1542  $params = $this->setupExecuteAction();
1543 
1544  // Check asserts early so e.g. errors in parsing a module's parameters due to being
1545  // logged out don't override the client's intended "am I logged in?" check.
1546  $this->checkAsserts( $params );
1547 
1548  $module = $this->setupModule();
1549  $this->mModule = $module;
1550 
1551  if ( !$this->mInternalMode ) {
1552  $this->setRequestExpectations( $module );
1553  }
1554 
1555  $this->checkExecutePermissions( $module );
1556 
1557  if ( !$this->checkMaxLag( $module, $params ) ) {
1558  return;
1559  }
1560 
1561  if ( !$this->checkConditionalRequestHeaders( $module ) ) {
1562  return;
1563  }
1564 
1565  if ( !$this->mInternalMode ) {
1566  $this->setupExternalResponse( $module, $params );
1567  }
1568 
1569  // Execute
1570  $module->execute();
1571  Hooks::run( 'APIAfterExecute', [ &$module ] );
1572 
1573  $this->reportUnusedParams();
1574 
1575  if ( !$this->mInternalMode ) {
1576  // append Debug information
1578 
1579  // Print result data
1580  $this->printResult();
1581  }
1582  }
1583 
1588  protected function setRequestExpectations( ApiBase $module ) {
1589  $limits = $this->getConfig()->get( 'TrxProfilerLimits' );
1590  $trxProfiler = Profiler::instance()->getTransactionProfiler();
1591  $trxProfiler->setLogger( LoggerFactory::getInstance( 'DBPerformance' ) );
1592  if ( $this->getRequest()->hasSafeMethod() ) {
1593  $trxProfiler->setExpectations( $limits['GET'], __METHOD__ );
1594  } elseif ( $this->getRequest()->wasPosted() && !$module->isWriteMode() ) {
1595  $trxProfiler->setExpectations( $limits['POST-nonwrite'], __METHOD__ );
1596  $this->getRequest()->markAsSafeRequest();
1597  } else {
1598  $trxProfiler->setExpectations( $limits['POST'], __METHOD__ );
1599  }
1600  }
1601 
1607  protected function logRequest( $time, $e = null ) {
1608  $request = $this->getRequest();
1609  $logCtx = [
1610  'ts' => time(),
1611  'ip' => $request->getIP(),
1612  'userAgent' => $this->getUserAgent(),
1613  'wiki' => wfWikiID(),
1614  'timeSpentBackend' => (int)round( $time * 1000 ),
1615  'hadError' => $e !== null,
1616  'errorCodes' => [],
1617  'params' => [],
1618  ];
1619 
1620  if ( $e ) {
1621  foreach ( $this->errorMessagesFromException( $e ) as $msg ) {
1622  $logCtx['errorCodes'][] = $msg->getApiCode();
1623  }
1624  }
1625 
1626  // Construct space separated message for 'api' log channel
1627  $msg = "API {$request->getMethod()} " .
1628  wfUrlencode( str_replace( ' ', '_', $this->getUser()->getName() ) ) .
1629  " {$logCtx['ip']} " .
1630  "T={$logCtx['timeSpentBackend']}ms";
1631 
1632  $sensitive = array_flip( $this->getSensitiveParams() );
1633  foreach ( $this->getParamsUsed() as $name ) {
1634  $value = $request->getVal( $name );
1635  if ( $value === null ) {
1636  continue;
1637  }
1638 
1639  if ( isset( $sensitive[$name] ) ) {
1640  $value = '[redacted]';
1641  $encValue = '[redacted]';
1642  } elseif ( strlen( $value ) > 256 ) {
1643  $value = substr( $value, 0, 256 );
1644  $encValue = $this->encodeRequestLogValue( $value ) . '[...]';
1645  } else {
1646  $encValue = $this->encodeRequestLogValue( $value );
1647  }
1648 
1649  $logCtx['params'][$name] = $value;
1650  $msg .= " {$name}={$encValue}";
1651  }
1652 
1653  wfDebugLog( 'api', $msg, 'private' );
1654  // ApiAction channel is for structured data consumers
1655  wfDebugLog( 'ApiAction', '', 'private', $logCtx );
1656  }
1657 
1663  protected function encodeRequestLogValue( $s ) {
1664  static $table;
1665  if ( !$table ) {
1666  $chars = ';@$!*(),/:';
1667  $numChars = strlen( $chars );
1668  for ( $i = 0; $i < $numChars; $i++ ) {
1669  $table[rawurlencode( $chars[$i] )] = $chars[$i];
1670  }
1671  }
1672 
1673  return strtr( rawurlencode( $s ), $table );
1674  }
1675 
1680  protected function getParamsUsed() {
1681  return array_keys( $this->mParamsUsed );
1682  }
1683 
1688  public function markParamsUsed( $params ) {
1689  $this->mParamsUsed += array_fill_keys( (array)$params, true );
1690  }
1691 
1697  protected function getSensitiveParams() {
1698  return array_keys( $this->mParamsSensitive );
1699  }
1700 
1706  public function markParamsSensitive( $params ) {
1707  $this->mParamsSensitive += array_fill_keys( (array)$params, true );
1708  }
1709 
1716  public function getVal( $name, $default = null ) {
1717  $this->mParamsUsed[$name] = true;
1718 
1719  $ret = $this->getRequest()->getVal( $name );
1720  if ( $ret === null ) {
1721  if ( $this->getRequest()->getArray( $name ) !== null ) {
1722  // See T12262 for why we don't just implode( '|', ... ) the
1723  // array.
1724  $this->addWarning( [ 'apiwarn-unsupportedarray', $name ] );
1725  }
1726  $ret = $default;
1727  }
1728  return $ret;
1729  }
1730 
1737  public function getCheck( $name ) {
1738  return $this->getVal( $name, null ) !== null;
1739  }
1740 
1748  public function getUpload( $name ) {
1749  $this->mParamsUsed[$name] = true;
1750 
1751  return $this->getRequest()->getUpload( $name );
1752  }
1753 
1758  protected function reportUnusedParams() {
1759  $paramsUsed = $this->getParamsUsed();
1760  $allParams = $this->getRequest()->getValueNames();
1761 
1762  if ( !$this->mInternalMode ) {
1763  // Printer has not yet executed; don't warn that its parameters are unused
1764  $printerParams = $this->mPrinter->encodeParamName(
1765  array_keys( $this->mPrinter->getFinalParams() ?: [] )
1766  );
1767  $unusedParams = array_diff( $allParams, $paramsUsed, $printerParams );
1768  } else {
1769  $unusedParams = array_diff( $allParams, $paramsUsed );
1770  }
1771 
1772  if ( count( $unusedParams ) ) {
1773  $this->addWarning( [
1774  'apierror-unrecognizedparams',
1775  Message::listParam( array_map( 'wfEscapeWikiText', $unusedParams ), 'comma' ),
1776  count( $unusedParams )
1777  ] );
1778  }
1779  }
1780 
1786  protected function printResult( $httpCode = 0 ) {
1787  if ( $this->getConfig()->get( 'DebugAPI' ) !== false ) {
1788  $this->addWarning( 'apiwarn-wgDebugAPI' );
1789  }
1790 
1791  $printer = $this->mPrinter;
1792  $printer->initPrinter( false );
1793  if ( $httpCode ) {
1794  $printer->setHttpStatus( $httpCode );
1795  }
1796  $printer->execute();
1797  $printer->closePrinter();
1798  }
1799 
1803  public function isReadMode() {
1804  return false;
1805  }
1806 
1812  public function getAllowedParams() {
1813  return [
1814  'action' => [
1815  ApiBase::PARAM_DFLT => 'help',
1816  ApiBase::PARAM_TYPE => 'submodule',
1817  ],
1818  'format' => [
1820  ApiBase::PARAM_TYPE => 'submodule',
1821  ],
1822  'maxlag' => [
1823  ApiBase::PARAM_TYPE => 'integer'
1824  ],
1825  'smaxage' => [
1826  ApiBase::PARAM_TYPE => 'integer',
1827  ApiBase::PARAM_DFLT => 0
1828  ],
1829  'maxage' => [
1830  ApiBase::PARAM_TYPE => 'integer',
1831  ApiBase::PARAM_DFLT => 0
1832  ],
1833  'assert' => [
1834  ApiBase::PARAM_TYPE => [ 'user', 'bot' ]
1835  ],
1836  'assertuser' => [
1837  ApiBase::PARAM_TYPE => 'user',
1838  ],
1839  'requestid' => null,
1840  'servedby' => false,
1841  'curtimestamp' => false,
1842  'responselanginfo' => false,
1843  'origin' => null,
1844  'uselang' => [
1846  ],
1847  'errorformat' => [
1848  ApiBase::PARAM_TYPE => [ 'plaintext', 'wikitext', 'html', 'raw', 'none', 'bc' ],
1849  ApiBase::PARAM_DFLT => 'bc',
1850  ],
1851  'errorlang' => [
1852  ApiBase::PARAM_DFLT => 'uselang',
1853  ],
1854  'errorsuselocal' => [
1855  ApiBase::PARAM_DFLT => false,
1856  ],
1857  ];
1858  }
1859 
1861  protected function getExamplesMessages() {
1862  return [
1863  'action=help'
1864  => 'apihelp-help-example-main',
1865  'action=help&recursivesubmodules=1'
1866  => 'apihelp-help-example-recursive',
1867  ];
1868  }
1869 
1870  public function modifyHelp( array &$help, array $options, array &$tocData ) {
1871  // Wish PHP had an "array_insert_before". Instead, we have to manually
1872  // reindex the array to get 'permissions' in the right place.
1873  $oldHelp = $help;
1874  $help = [];
1875  foreach ( $oldHelp as $k => $v ) {
1876  if ( $k === 'submodules' ) {
1877  $help['permissions'] = '';
1878  }
1879  $help[$k] = $v;
1880  }
1881  $help['datatypes'] = '';
1882  $help['templatedparams'] = '';
1883  $help['credits'] = '';
1884 
1885  // Fill 'permissions'
1886  $help['permissions'] .= Html::openElement( 'div',
1887  [ 'class' => 'apihelp-block apihelp-permissions' ] );
1888  $m = $this->msg( 'api-help-permissions' );
1889  if ( !$m->isDisabled() ) {
1890  $help['permissions'] .= Html::rawElement( 'div', [ 'class' => 'apihelp-block-head' ],
1891  $m->numParams( count( self::$mRights ) )->parse()
1892  );
1893  }
1894  $help['permissions'] .= Html::openElement( 'dl' );
1895  foreach ( self::$mRights as $right => $rightMsg ) {
1896  $help['permissions'] .= Html::element( 'dt', null, $right );
1897 
1898  $rightMsg = $this->msg( $rightMsg['msg'], $rightMsg['params'] )->parse();
1899  $help['permissions'] .= Html::rawElement( 'dd', null, $rightMsg );
1900 
1901  $groups = array_map( function ( $group ) {
1902  return $group == '*' ? 'all' : $group;
1903  }, User::getGroupsWithPermission( $right ) );
1904 
1905  $help['permissions'] .= Html::rawElement( 'dd', null,
1906  $this->msg( 'api-help-permissions-granted-to' )
1907  ->numParams( count( $groups ) )
1908  ->params( Message::listParam( $groups ) )
1909  ->parse()
1910  );
1911  }
1912  $help['permissions'] .= Html::closeElement( 'dl' );
1913  $help['permissions'] .= Html::closeElement( 'div' );
1914 
1915  // Fill 'datatypes', 'templatedparams', and 'credits', if applicable
1916  if ( empty( $options['nolead'] ) ) {
1917  $level = $options['headerlevel'];
1918  $tocnumber = &$options['tocnumber'];
1919 
1920  $header = $this->msg( 'api-help-datatypes-header' )->parse();
1921 
1922  $id = Sanitizer::escapeIdForAttribute( 'main/datatypes', Sanitizer::ID_PRIMARY );
1923  $idFallback = Sanitizer::escapeIdForAttribute( 'main/datatypes', Sanitizer::ID_FALLBACK );
1924  $headline = Linker::makeHeadline( min( 6, $level ),
1925  ' class="apihelp-header">',
1926  $id,
1927  $header,
1928  '',
1929  $idFallback
1930  );
1931  // Ensure we have a sane anchor
1932  if ( $id !== 'main/datatypes' && $idFallback !== 'main/datatypes' ) {
1933  $headline = '<div id="main/datatypes"></div>' . $headline;
1934  }
1935  $help['datatypes'] .= $headline;
1936  $help['datatypes'] .= $this->msg( 'api-help-datatypes' )->parseAsBlock();
1937  if ( !isset( $tocData['main/datatypes'] ) ) {
1938  $tocnumber[$level]++;
1939  $tocData['main/datatypes'] = [
1940  'toclevel' => count( $tocnumber ),
1941  'level' => $level,
1942  'anchor' => 'main/datatypes',
1943  'line' => $header,
1944  'number' => implode( '.', $tocnumber ),
1945  'index' => false,
1946  ];
1947  }
1948 
1949  $header = $this->msg( 'api-help-templatedparams-header' )->parse();
1950 
1951  $id = Sanitizer::escapeIdForAttribute( 'main/templatedparams', Sanitizer::ID_PRIMARY );
1952  $idFallback = Sanitizer::escapeIdForAttribute( 'main/templatedparams', Sanitizer::ID_FALLBACK );
1953  $headline = Linker::makeHeadline( min( 6, $level ),
1954  ' class="apihelp-header">',
1955  $id,
1956  $header,
1957  '',
1958  $idFallback
1959  );
1960  // Ensure we have a sane anchor
1961  if ( $id !== 'main/templatedparams' && $idFallback !== 'main/templatedparams' ) {
1962  $headline = '<div id="main/templatedparams"></div>' . $headline;
1963  }
1964  $help['templatedparams'] .= $headline;
1965  $help['templatedparams'] .= $this->msg( 'api-help-templatedparams' )->parseAsBlock();
1966  if ( !isset( $tocData['main/templatedparams'] ) ) {
1967  $tocnumber[$level]++;
1968  $tocData['main/templatedparams'] = [
1969  'toclevel' => count( $tocnumber ),
1970  'level' => $level,
1971  'anchor' => 'main/templatedparams',
1972  'line' => $header,
1973  'number' => implode( '.', $tocnumber ),
1974  'index' => false,
1975  ];
1976  }
1977 
1978  $header = $this->msg( 'api-credits-header' )->parse();
1979  $id = Sanitizer::escapeIdForAttribute( 'main/credits', Sanitizer::ID_PRIMARY );
1980  $idFallback = Sanitizer::escapeIdForAttribute( 'main/credits', Sanitizer::ID_FALLBACK );
1981  $headline = Linker::makeHeadline( min( 6, $level ),
1982  ' class="apihelp-header">',
1983  $id,
1984  $header,
1985  '',
1986  $idFallback
1987  );
1988  // Ensure we have a sane anchor
1989  if ( $id !== 'main/credits' && $idFallback !== 'main/credits' ) {
1990  $headline = '<div id="main/credits"></div>' . $headline;
1991  }
1992  $help['credits'] .= $headline;
1993  $help['credits'] .= $this->msg( 'api-credits' )->useDatabase( false )->parseAsBlock();
1994  if ( !isset( $tocData['main/credits'] ) ) {
1995  $tocnumber[$level]++;
1996  $tocData['main/credits'] = [
1997  'toclevel' => count( $tocnumber ),
1998  'level' => $level,
1999  'anchor' => 'main/credits',
2000  'line' => $header,
2001  'number' => implode( '.', $tocnumber ),
2002  'index' => false,
2003  ];
2004  }
2005  }
2006  }
2007 
2008  private $mCanApiHighLimits = null;
2009 
2014  public function canApiHighLimits() {
2015  if ( !isset( $this->mCanApiHighLimits ) ) {
2016  $this->mCanApiHighLimits = $this->getUser()->isAllowed( 'apihighlimits' );
2017  }
2018 
2019  return $this->mCanApiHighLimits;
2020  }
2021 
2026  public function getModuleManager() {
2027  return $this->mModuleMgr;
2028  }
2029 
2038  public function getUserAgent() {
2039  return trim(
2040  $this->getRequest()->getHeader( 'Api-user-agent' ) . ' ' .
2041  $this->getRequest()->getHeader( 'User-agent' )
2042  );
2043  }
2044 }
2045 
ApiUsageException\getStatusValue
getStatusValue()
Fetch the error status.
Definition: ApiUsageException.php:101
ApiMain\executeActionWithErrorHandling
executeActionWithErrorHandling()
Execute an action, and in case of an error, erase whatever partial results have been accumulated,...
Definition: ApiMain.php:510
MWTimestamp
Library for creating and parsing MW-style timestamps.
Definition: MWTimestamp.php:32
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:41
ContextSource\$context
IContextSource $context
Definition: ContextSource.php:33
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:63
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:244
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:33
ApiUsageException
Exception used to abort API execution with an error.
Definition: ApiUsageException.php:28
ContextSource\getContext
getContext()
Get the base IContextSource object.
Definition: ContextSource.php:40
ApiMain\checkReadOnly
checkReadOnly( $module)
Check if the DB is read-only for this user.
Definition: ApiMain.php:1433
ApiBase\addWarning
addWarning( $msg, $code=null, $data=null)
Add a warning for this module.
Definition: ApiBase.php:1906
RequestContext\sanitizeLangCode
static sanitizeLangCode( $code)
Accepts a language code and ensures it's sane.
Definition: RequestContext.php:279
ApiMain\$mParamsSensitive
$mParamsSensitive
Definition: ApiMain.php:158
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
ApiMain\$mAction
$mAction
Definition: ApiMain.php:149
ApiErrorFormatter_BackCompat
Format errors and warnings in the old style, for backwards compatibility.
Definition: ApiErrorFormatter.php:378
ApiMain\$mSquidMaxage
$mSquidMaxage
Definition: ApiMain.php:151
ApiMain\getParamsUsed
getParamsUsed()
Get the request parameters used in the course of the preceding execute() request.
Definition: ApiMain.php:1680
Profiler\instance
static instance()
Singleton.
Definition: Profiler.php:62
ApiMain\createErrorPrinter
createErrorPrinter()
Create the printer for error output.
Definition: ApiMain.php:986
ApiMain\getErrorFormatter
getErrorFormatter()
Get the ApiErrorFormatter object associated with current request.
Definition: ApiMain.php:351
ApiMain\getVal
getVal( $name, $default=null)
Get a request value, and register the fact that it was used, for logging.
Definition: ApiMain.php:1716
captcha-old.count
count
Definition: captcha-old.py:249
ApiContinuationManager
This manages continuation state.
Definition: ApiContinuationManager.php:26
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1987
ContextSource\msg
msg( $key)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: ContextSource.php:168
ApiMain\sendCacheHeaders
sendCacheHeaders( $isError)
Send caching headers.
Definition: ApiMain.php:868
$result
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message. Please note the header message cannot receive/use parameters. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item. Return false to stop further processing of the tag $reader:XMLReader object $logInfo:Array of information 'ImportHandlePageXMLTag':When parsing a XML tag in a page. Return false to stop further processing of the tag $reader:XMLReader object & $pageInfo:Array of information 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision. Return false to stop further processing of the tag $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information 'ImportHandleToplevelXMLTag':When parsing a top level XML tag. Return false to stop further processing of the tag $reader:XMLReader object 'ImportHandleUnknownUser':When a user doesn 't exist locally, this hook is called to give extensions an opportunity to auto-create it. If the auto-creation is successful, return false. $name:User name 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload. Return false to stop further processing of the tag $reader:XMLReader object $revisionInfo:Array of information 'ImportLogInterwikiLink':Hook to change the interwiki link used in log entries and edit summaries for transwiki imports. & $fullInterwikiPrefix:Interwiki prefix, may contain colons. & $pageTitle:String that contains page title. 'ImportSources':Called when reading from the $wgImportSources configuration variable. Can be used to lazy-load the import sources list. & $importSources:The value of $wgImportSources. Modify as necessary. See the comment in DefaultSettings.php for the detail of how to structure this array. 'InfoAction':When building information to display on the action=info page. $context:IContextSource object & $pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect. & $title:Title object for the current page & $request:WebRequest & $ignoreRedirect:boolean to skip redirect check & $target:Title/string of redirect target & $article:Article object 'InternalParseBeforeLinks':during Parser 's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InternalParseBeforeSanitize':during Parser 's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings. Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not. Return true without providing an interwiki to continue interwiki search. $prefix:interwiki prefix we are looking for. & $iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InvalidateEmailComplete':Called after a user 's email has been invalidated successfully. $user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification. Callee may modify $url and $query, URL will be constructed as $url . $query & $url:URL to index.php & $query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) & $article:article(object) being checked 'IsTrustedProxy':Override the result of IP::isTrustedProxy() & $ip:IP being check & $result:Change this value to override the result of IP::isTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from & $allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of Sanitizer::validateEmail(), for instance to return false if the domain name doesn 't match your organization. $addr:The e-mail address entered by the user & $result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user & $result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we 're looking for a messages file for & $file:The messages file path, you can override this to change the location. 'LanguageGetMagic':DEPRECATED since 1.16! Use $magicWords in a file listed in $wgExtensionMessagesFiles instead. Use this to define synonyms of magic words depending of the language & $magicExtensions:associative array of magic words synonyms $lang:language code(string) 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces. Do not use this hook to add namespaces. Use CanonicalNamespaces for that. & $namespaces:Array of namespaces indexed by their numbers 'LanguageGetSpecialPageAliases':DEPRECATED! Use $specialPageAliases in a file listed in $wgExtensionMessagesFiles instead. Use to define aliases of special pages names depending of the language & $specialPageAliases:associative array of magic words synonyms $lang:language code(string) 'LanguageGetTranslatedLanguageNames':Provide translated language names. & $names:array of language code=> language name $code:language of the preferred translations 'LanguageLinks':Manipulate a page 's language links. This is called in various places to allow extensions to define the effective language links for a page. $title:The page 's Title. & $links:Array with elements of the form "language:title" in the order that they will be output. & $linkFlags:Associative array mapping prefixed links to arrays of flags. Currently unused, but planned to provide support for marking individual language links in the UI, e.g. for featured articles. 'LanguageSelector':Hook to change the language selector available on a page. $out:The output page. $cssClassName:CSS class name of the language selector. 'LinkBegin':DEPRECATED since 1.28! Use HtmlPageLinkRendererBegin instead. Used when generating internal and interwiki links in Linker::link(), before processing starts. Return false to skip default processing and return $ret. See documentation for Linker::link() for details on the expected meanings of parameters. $skin:the Skin object $target:the Title that the link is pointing to & $html:the contents that the< a > tag should have(raw HTML) $result
Definition: hooks.txt:2034
ApiMain\$Modules
static $Modules
List of available modules: action name => module class.
Definition: ApiMain.php:55
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1954
ApiBase\PARAM_TYPE
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition: ApiBase.php:87
ApiFormatBase
This is the abstract base class for API formatters.
Definition: ApiFormatBase.php:28
ApiMain\logRequest
logRequest( $time, $e=null)
Log the preceding request.
Definition: ApiMain.php:1607
MessageSpecifier
Definition: MessageSpecifier.php:21
wfUrlencode
wfUrlencode( $s)
We want some things to be included as literal characters in our title URLs for prettiness,...
Definition: GlobalFunctions.php:331
ApiMain\isReadMode
isReadMode()
Definition: ApiMain.php:1803
ApiMain\handleCORS
handleCORS()
Check the &origin= query parameter against the Origin: HTTP header and respond appropriately.
Definition: ApiMain.php:672
ApiMain\checkConditionalRequestHeaders
checkConditionalRequestHeaders( $module)
Check selected RFC 7232 precondition headers.
Definition: ApiMain.php:1304
ApiBase\dieWithErrorOrDebug
dieWithErrorOrDebug( $msg, $code=null, $data=null, $httpCode=null)
Will only set a warning instead of failing if the global $wgDebugAPI is set to true.
Definition: ApiBase.php:2138
$params
$params
Definition: styleTest.css.php:44
ApiMain\lacksSameOriginSecurity
lacksSameOriginSecurity()
Get the security flag for the current request.
Definition: ApiMain.php:316
wfHostname
wfHostname()
Fetch server name for use in error reporting etc.
Definition: GlobalFunctions.php:1392
wfReadOnly
wfReadOnly()
Check whether the wiki is in read-only mode.
Definition: GlobalFunctions.php:1237
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:592
$s
$s
Definition: mergeMessageFileList.php:187
ApiResult\NO_SIZE_CHECK
const NO_SIZE_CHECK
For addValue() and similar functions, do not check size while adding a value Don't use this unless yo...
Definition: ApiResult.php:58
ContextSource\getRequest
getRequest()
Definition: ContextSource.php:71
User
User
Definition: All_system_messages.txt:425
ApiMain\encodeRequestLogValue
encodeRequestLogValue( $s)
Encode a value in a format suitable for a space-separated log line.
Definition: ApiMain.php:1663
ContextSource\getUser
getUser()
Definition: ContextSource.php:120
ApiMain\getMaxLag
getMaxLag()
Definition: ApiMain.php:1224
$messages
$messages
Definition: LogTests.i18n.php:8
ApiMain\$mContinuationManager
ApiContinuationManager null $mContinuationManager
Definition: ApiMain.php:148
ApiMain\matchOrigin
static matchOrigin( $value, $rules, $exceptions)
Attempt to match an Origin header against a set of rules and a set of exceptions.
Definition: ApiMain.php:792
wfDebugLog
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.
Definition: GlobalFunctions.php:1082
ApiMain\$mRights
static $mRights
List of user roles that are specifically relevant to the API.
Definition: ApiMain.php:130
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:37
ContextSource\getLanguage
getLanguage()
Definition: ContextSource.php:128
Html\closeElement
static closeElement( $element)
Returns "</$element>".
Definition: Html.php:310
ApiMain\getModule
getModule()
Get the API module object.
Definition: ApiMain.php:382
DerivativeContext
An IContextSource implementation which will inherit context from another source but allow individual ...
Definition: DerivativeContext.php:30
ApiMain\markParamsUsed
markParamsUsed( $params)
Mark parameters as used.
Definition: ApiMain.php:1688
MWException
MediaWiki exception.
Definition: MWException.php:26
ApiMain\setupExecuteAction
setupExecuteAction()
Set up for the execution.
Definition: ApiMain.php:1162
wfScript
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
Definition: GlobalFunctions.php:2771
ApiResult
This class represents the result of the API operations.
Definition: ApiResult.php:35
ApiMain\$mResult
$mResult
Definition: ApiMain.php:146
ApiMain\$lacksSameOriginSecurity
bool null $lacksSameOriginSecurity
Cached return value from self::lacksSameOriginSecurity()
Definition: ApiMain.php:161
ContextSource\getOutput
getOutput()
Definition: ContextSource.php:112
$wgLang
$wgLang
Definition: Setup.php:902
MWExceptionHandler\rollbackMasterChangesAndLog
static rollbackMasterChangesAndLog( $e)
Roll back any open database transactions and log the stack trace of the exception.
Definition: MWExceptionHandler.php:116
Linker\makeHeadline
static makeHeadline( $level, $attribs, $anchor, $html, $link, $fallbackAnchor=false)
Create a headline for content.
Definition: Linker.php:1641
ApiMain\getResult
getResult()
Get the ApiResult object associated with current request.
Definition: ApiMain.php:308
ApiMain\getSensitiveParams
getSensitiveParams()
Get the request parameters that should be considered sensitive.
Definition: ApiMain.php:1697
MediaWiki
A helper class for throttling authentication attempts.
ApiMain\checkMaxLag
checkMaxLag( $module, $params)
Check the max lag if necessary.
Definition: ApiMain.php:1258
ApiMain\API_DEFAULT_USELANG
const API_DEFAULT_USELANG
When no uselang parameter is given, this language will be used.
Definition: ApiMain.php:50
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$code
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
Definition: hooks.txt:813
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:770
ApiMain\getExamplesMessages
getExamplesMessages()
@inheritDoc
Definition: ApiMain.php:1861
ApiMain\setCacheControl
setCacheControl( $directives)
Set directives (key/value pairs) for the Cache-Control header.
Definition: ApiMain.php:473
ApiMain\getAllowedParams
getAllowedParams()
See ApiBase for description.
Definition: ApiMain.php:1812
ApiMain\setupExternalResponse
setupExternalResponse( $module, $params)
Check POST for external response and setup result printer.
Definition: ApiMain.php:1514
$time
see documentation in includes Linker php for Linker::makeImageLink & $time
Definition: hooks.txt:1841
ApiMain\createPrinterByName
createPrinterByName( $format)
Create an instance of an output formatter by its name.
Definition: ApiMain.php:484
ApiMain\getModuleManager
getModuleManager()
Overrides to return this instance's module manager.
Definition: ApiMain.php:2026
ApiMain\$mCanApiHighLimits
$mCanApiHighLimits
Definition: ApiMain.php:2008
ApiMessage\create
static create( $msg, $code=null, array $data=null)
Create an IApiMessage for the message.
Definition: ApiMessage.php:40
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
ApiMain\matchRequestedHeaders
static matchRequestedHeaders( $requestedHeaders)
Attempt to validate the value of Access-Control-Request-Headers against a list of headers that we all...
Definition: ApiMain.php:816
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:988
ApiMain\addRequestedFields
addRequestedFields( $force=[])
Add requested fields to the result.
Definition: ApiMain.php:1131
ContextSource\setContext
setContext(IContextSource $context)
Definition: ContextSource.php:55
ILocalizedException
Interface for MediaWiki-localized exceptions.
Definition: LocalizedException.php:27
ApiMain\canApiHighLimits
canApiHighLimits()
Check whether the current user is allowed to use high limits.
Definition: ApiMain.php:2014
ApiMain\$mPrinter
ApiFormatBase $mPrinter
Definition: ApiMain.php:144
ApiMain\checkBotReadOnly
checkBotReadOnly()
Check whether we are readonly for bots.
Definition: ApiMain.php:1449
ApiMain\getContinuationManager
getContinuationManager()
Get the continuation manager.
Definition: ApiMain.php:359
$request
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2675
ApiModuleManager
This class holds a list of modules and handles instantiation.
Definition: ApiModuleManager.php:30
$name
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:302
ApiMain\checkAsserts
checkAsserts( $params)
Check asserts of the user's rights.
Definition: ApiMain.php:1483
wfWikiID
wfWikiID()
Get an ASCII string identifying this wiki This is used as a prefix in memcached keys.
Definition: GlobalFunctions.php:2644
Hooks\runWithoutAbort
static runWithoutAbort( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:231
ApiMain\$mCacheMode
$mCacheMode
Definition: ApiMain.php:155
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2213
MWDebug\appendDebugInfoToApiResult
static appendDebugInfoToApiResult(IContextSource $context, ApiResult $result)
Append the debug info to given ApiResult.
Definition: MWDebug.php:482
ApiMain\handleException
handleException( $e)
Handle an exception as an API response.
Definition: ApiMain.php:567
wfClearOutputBuffers
wfClearOutputBuffers()
More legible than passing a 'false' parameter to wfResetOutputBuffers():
Definition: GlobalFunctions.php:1816
ApiMain\$mErrorFormatter
$mErrorFormatter
Definition: ApiMain.php:146
ApiMain\$mInternalMode
$mInternalMode
Definition: ApiMain.php:151
$value
$value
Definition: styleTest.css.php:49
ApiMain\setContinuationManager
setContinuationManager(ApiContinuationManager $manager=null)
Set the continuation manager.
Definition: ApiMain.php:367
MediaWiki\preOutputCommit
static preOutputCommit(IContextSource $context, callable $postCommitWork=null)
This function commits all DB changes as needed before the user can receive a response (in case commit...
Definition: MediaWiki.php:579
ApiMain\wildcardToRegex
static wildcardToRegex( $wildcard)
Helper function to convert wildcard string into a regex '*' => '.
Definition: ApiMain.php:852
ApiMain\checkExecutePermissions
checkExecutePermissions( $module)
Check for sufficient permissions to execute.
Definition: ApiMain.php:1402
ApiBase\addDeprecation
addDeprecation( $msg, $feature, $data=[])
Add a deprecation warning for this module.
Definition: ApiBase.php:1920
$header
$header
Definition: updateCredits.php:35
ApiBase\LIMIT_SML2
const LIMIT_SML2
Slow query, apihighlimits limit.
Definition: ApiBase.php:258
ApiBase\dieReadOnly
dieReadOnly()
Helper function for readonly errors.
Definition: ApiBase.php:2079
ApiMain\__construct
__construct( $context=null, $enableWrite=false)
Constructs an instance of ApiMain that utilizes the module and format specified by $request.
Definition: ApiMain.php:170
ApiMain\reportUnusedParams
reportUnusedParams()
Report unused parameters, so the client gets a hint in case it gave us parameters we don't know,...
Definition: ApiMain.php:1758
ApiMain\execute
execute()
Execute api request.
Definition: ApiMain.php:498
ApiBase\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiBase.php:446
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1617
$ret
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:2036
ApiMain\$Formats
static $Formats
List of available formats: format name => format class.
Definition: ApiMain.php:113
ApiMain\markParamsSensitive
markParamsSensitive( $params)
Mark parameters as sensitive.
Definition: ApiMain.php:1706
ApiMain\setRequestExpectations
setRequestExpectations(ApiBase $module)
Set database connection, query, and write expectations given this module request.
Definition: ApiMain.php:1588
ApiMain\substituteResultWithError
substituteResultWithError( $e)
Replace the result data with the information about an exception.
Definition: ApiMain.php:1053
RequestContext\getMain
static getMain()
Get the RequestContext object associated with the main request.
Definition: RequestContext.php:432
ApiMain\getCheck
getCheck( $name)
Get a boolean request value, and register the fact that the parameter was used, for logging.
Definition: ApiMain.php:1737
ApiMain\printResult
printResult( $httpCode=0)
Print results using the current printer.
Definition: ApiMain.php:1786
$response
this hook is for auditing only $response
Definition: hooks.txt:813
WebRequest
The WebRequest class encapsulates getting at data passed in the URL or via a POSTed form stripping il...
Definition: WebRequest.php:41
ApiErrorFormatter
Formats errors and warnings for the API, and add them to the associated ApiResult.
Definition: ApiErrorFormatter.php:30
text
This list may contain false positives That usually means there is additional text with links below the first Each row contains links to the first and second as well as the first line of the second redirect text
Definition: All_system_messages.txt:1267
ApiMain\$mEnableWrite
$mEnableWrite
Definition: ApiMain.php:150
User\isEveryoneAllowed
static isEveryoneAllowed( $right)
Check if all users may be assumed to have the given permission.
Definition: User.php:5034
ApiBase\LIMIT_BIG2
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:254
ApiMain\API_DEFAULT_FORMAT
const API_DEFAULT_FORMAT
When no format parameter is given, this format will be used.
Definition: ApiMain.php:45
WebRequest\GETHEADER_LIST
const GETHEADER_LIST
Flag to make WebRequest::getHeader return an array of values.
Definition: WebRequest.php:48
$options
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition: hooks.txt:2036
ApiMain\getUpload
getUpload( $name)
Get a request upload, and register the fact that it was used, for logging.
Definition: ApiMain.php:1748
ApiMain\modifyHelp
modifyHelp(array &$help, array $options, array &$tocData)
Called from ApiHelp before the pieces are joined together and returned.
Definition: ApiMain.php:1870
WebRequest\getRequestId
static getRequestId()
Get the unique request ID.
Definition: WebRequest.php:275
JobQueueGroup\singleton
static singleton( $wiki=false)
Definition: JobQueueGroup.php:69
$path
$path
Definition: NoLocalSettings.php:25
ApiBase\PARAM_DFLT
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:48
ApiBase\getParameter
getParameter( $paramName, $parseLimit=true)
Get a value for the given parameter.
Definition: ApiBase.php:884
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Html\openElement
static openElement( $element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/'...
Definition: Html.php:252
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:210
ApiMain\executeAction
executeAction()
Execute the actual module, without any error handling.
Definition: ApiMain.php:1541
$link
usually copyright or history_copyright This message must be in HTML not wikitext & $link
Definition: hooks.txt:3090
LoggerFactory
MediaWiki Logger LoggerFactory implements a PSR[0] compatible message logging system Named Psr Log LoggerInterface instances can be obtained from the MediaWiki Logger LoggerFactory::getInstance() static method. MediaWiki\Logger\LoggerFactory expects a class implementing the MediaWiki\Logger\Spi interface to act as a factory for new Psr\Log\LoggerInterface instances. The "Spi" in MediaWiki\Logger\Spi stands for "service provider interface". An SPI is an API intended to be implemented or extended by a third party. This software design pattern is intended to enable framework extension and replaceable components. It is specifically used in the MediaWiki\Logger\LoggerFactory service to allow alternate PSR-3 logging implementations to be easily integrated with MediaWiki. The service provider interface allows the backend logging library to be implemented in multiple ways. The $wgMWLoggerDefaultSpi global provides the classname of the default MediaWiki\Logger\Spi implementation to be loaded at runtime. This can either be the name of a class implementing the MediaWiki\Logger\Spi with a zero argument const ructor or a callable that will return an MediaWiki\Logger\Spi instance. Alternately the MediaWiki\Logger\LoggerFactory MediaWiki Logger LoggerFactory
Definition: logger.txt:5
ApiMain\setupModule
setupModule()
Set up the module for response.
Definition: ApiMain.php:1177
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:2036
$help
$help
Definition: mcc.php:32
Language\factory
static factory( $code)
Get a cached or new language object for a given language code.
Definition: Language.php:214
ApiMain\$mCacheControl
$mCacheControl
Definition: ApiMain.php:156
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
$t
$t
Definition: testCompression.php:69
ApiMain\handleApiBeforeMainException
static handleApiBeforeMainException( $e)
Handle an exception from the ApiBeforeMain hook.
Definition: ApiMain.php:637
Html\element
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:232
ApiMain\isInternalMode
isInternalMode()
Return true if the API was started by other PHP code using FauxRequest.
Definition: ApiMain.php:299
ApiMain\$mParamsUsed
$mParamsUsed
Definition: ApiMain.php:157
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
ApiMain\$mModuleMgr
$mModuleMgr
Definition: ApiMain.php:146
ApiMain\getUserAgent
getUserAgent()
Fetches the user agent used for this request.
Definition: ApiMain.php:2038
ApiMain\setCacheMaxAge
setCacheMaxAge( $maxage)
Set how long the response should be cached.
Definition: ApiMain.php:400
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:200
ApiMain\errorMessagesFromException
errorMessagesFromException( $e, $type='error')
Create an error message for the given exception.
Definition: ApiMain.php:1017
ApiFormatBase\initPrinter
initPrinter( $unused=false)
Initialize the printer function and prepare the output headers.
Definition: ApiFormatBase.php:189
MWExceptionHandler\getRedactedTraceAsString
static getRedactedTraceAsString( $e)
Generate a string representation of an exception's stack trace.
Definition: MWExceptionHandler.php:381
ApiMain\setCacheMode
setCacheMode( $mode)
Set the type of caching headers which will be sent.
Definition: ApiMain.php:432
ApiMain\getPrinter
getPrinter()
Get the result formatter object.
Definition: ApiMain.php:391
wfExpandUrl
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
Definition: GlobalFunctions.php:512
User\getGroupsWithPermission
static getGroupsWithPermission( $role)
Get all the groups who have a given permission.
Definition: User.php:4991
$out
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output $out
Definition: hooks.txt:813
$type
$type
Definition: testCompression.php:48
ApiMain\$mModule
ApiBase $mModule
Definition: ApiMain.php:153