MediaWiki  1.28.0
ApiBase.php
Go to the documentation of this file.
1 <?php
39 abstract class ApiBase extends ContextSource {
40 
50  const PARAM_DFLT = 0;
51 
53  const PARAM_ISMULTI = 1;
54 
88  const PARAM_TYPE = 2;
89 
91  const PARAM_MAX = 3;
92 
97  const PARAM_MAX2 = 4;
98 
100  const PARAM_MIN = 5;
101 
104 
106  const PARAM_DEPRECATED = 7;
107 
112  const PARAM_REQUIRED = 8;
113 
119 
125  const PARAM_HELP_MSG = 10;
126 
133 
143 
149  const PARAM_VALUE_LINKS = 13;
150 
158 
166 
173 
177  const LIMIT_BIG1 = 500;
179  const LIMIT_BIG2 = 5000;
181  const LIMIT_SML1 = 50;
183  const LIMIT_SML2 = 500;
184 
191 
193  private static $extensionInfo = null;
194 
196  private $mMainModule;
199  private $mSlaveDB = null;
200  private $mParamCache = [];
202  private $mModuleSource = false;
203 
209  public function __construct( ApiMain $mainModule, $moduleName, $modulePrefix = '' ) {
210  $this->mMainModule = $mainModule;
211  $this->mModuleName = $moduleName;
212  $this->mModulePrefix = $modulePrefix;
213 
214  if ( !$this->isMain() ) {
215  $this->setContext( $mainModule->getContext() );
216  }
217  }
218 
219  /************************************************************************/
240  abstract public function execute();
241 
247  public function getModuleManager() {
248  return null;
249  }
250 
260  public function getCustomPrinter() {
261  return null;
262  }
263 
275  protected function getExamplesMessages() {
276  // Fall back to old non-localised method
277  $ret = [];
278 
279  $examples = $this->getExamples();
280  if ( $examples ) {
281  if ( !is_array( $examples ) ) {
282  $examples = [ $examples ];
283  } elseif ( $examples && ( count( $examples ) & 1 ) == 0 &&
284  array_keys( $examples ) === range( 0, count( $examples ) - 1 ) &&
285  !preg_match( '/^\s*api\.php\?/', $examples[0] )
286  ) {
287  // Fix up the ugly "even numbered elements are description, odd
288  // numbered elemts are the link" format (see doc for self::getExamples)
289  $tmp = [];
290  $examplesCount = count( $examples );
291  for ( $i = 0; $i < $examplesCount; $i += 2 ) {
292  $tmp[$examples[$i + 1]] = $examples[$i];
293  }
294  $examples = $tmp;
295  }
296 
297  foreach ( $examples as $k => $v ) {
298  if ( is_numeric( $k ) ) {
299  $qs = $v;
300  $msg = '';
301  } else {
302  $qs = $k;
303  $msg = self::escapeWikiText( $v );
304  if ( is_array( $msg ) ) {
305  $msg = implode( ' ', $msg );
306  }
307  }
308 
309  $qs = preg_replace( '/^\s*api\.php\?/', '', $qs );
310  $ret[$qs] = $this->msg( 'api-help-fallback-example', [ $msg ] );
311  }
312  }
313 
314  return $ret;
315  }
316 
322  public function getHelpUrls() {
323  return [];
324  }
325 
338  protected function getAllowedParams( /* $flags = 0 */ ) {
339  // int $flags is not declared because it causes "Strict standards"
340  // warning. Most derived classes do not implement it.
341  return [];
342  }
343 
348  public function shouldCheckMaxlag() {
349  return true;
350  }
351 
356  public function isReadMode() {
357  return true;
358  }
359 
364  public function isWriteMode() {
365  return false;
366  }
367 
372  public function mustBePosted() {
373  return $this->needsToken() !== false;
374  }
375 
381  public function isDeprecated() {
382  return false;
383  }
384 
391  public function isInternal() {
392  return false;
393  }
394 
413  public function needsToken() {
414  return false;
415  }
416 
426  protected function getWebUITokenSalt( array $params ) {
427  return null;
428  }
429 
442  public function getConditionalRequestData( $condition ) {
443  return null;
444  }
445 
448  /************************************************************************/
457  public function getModuleName() {
458  return $this->mModuleName;
459  }
460 
465  public function getModulePrefix() {
466  return $this->mModulePrefix;
467  }
468 
473  public function getMain() {
474  return $this->mMainModule;
475  }
476 
482  public function isMain() {
483  return $this === $this->mMainModule;
484  }
485 
491  public function getParent() {
492  return $this->isMain() ? null : $this->getMain();
493  }
494 
505  public function lacksSameOriginSecurity() {
506  // Main module has this method overridden
507  // Safety - avoid infinite loop:
508  if ( $this->isMain() ) {
509  ApiBase::dieDebug( __METHOD__, 'base method was called on main module.' );
510  }
511 
512  return $this->getMain()->lacksSameOriginSecurity();
513  }
514 
521  public function getModulePath() {
522  if ( $this->isMain() ) {
523  return 'main';
524  } elseif ( $this->getParent()->isMain() ) {
525  return $this->getModuleName();
526  } else {
527  return $this->getParent()->getModulePath() . '+' . $this->getModuleName();
528  }
529  }
530 
539  public function getModuleFromPath( $path ) {
540  $module = $this->getMain();
541  if ( $path === 'main' ) {
542  return $module;
543  }
544 
545  $parts = explode( '+', $path );
546  if ( count( $parts ) === 1 ) {
547  // In case the '+' was typed into URL, it resolves as a space
548  $parts = explode( ' ', $path );
549  }
550 
551  $count = count( $parts );
552  for ( $i = 0; $i < $count; $i++ ) {
553  $parent = $module;
554  $manager = $parent->getModuleManager();
555  if ( $manager === null ) {
556  $errorPath = implode( '+', array_slice( $parts, 0, $i ) );
557  $this->dieUsage( "The module \"$errorPath\" has no submodules", 'badmodule' );
558  }
559  $module = $manager->getModule( $parts[$i] );
560 
561  if ( $module === null ) {
562  $errorPath = $i ? implode( '+', array_slice( $parts, 0, $i ) ) : $parent->getModuleName();
563  $this->dieUsage(
564  "The module \"$errorPath\" does not have a submodule \"{$parts[$i]}\"",
565  'badmodule'
566  );
567  }
568  }
569 
570  return $module;
571  }
572 
577  public function getResult() {
578  // Main module has getResult() method overridden
579  // Safety - avoid infinite loop:
580  if ( $this->isMain() ) {
581  ApiBase::dieDebug( __METHOD__, 'base method was called on main module. ' );
582  }
583 
584  return $this->getMain()->getResult();
585  }
586 
591  public function getErrorFormatter() {
592  // Main module has getErrorFormatter() method overridden
593  // Safety - avoid infinite loop:
594  if ( $this->isMain() ) {
595  ApiBase::dieDebug( __METHOD__, 'base method was called on main module. ' );
596  }
597 
598  return $this->getMain()->getErrorFormatter();
599  }
600 
605  protected function getDB() {
606  if ( !isset( $this->mSlaveDB ) ) {
607  $this->mSlaveDB = wfGetDB( DB_REPLICA, 'api' );
608  }
609 
610  return $this->mSlaveDB;
611  }
612 
617  public function getContinuationManager() {
618  // Main module has getContinuationManager() method overridden
619  // Safety - avoid infinite loop:
620  if ( $this->isMain() ) {
621  ApiBase::dieDebug( __METHOD__, 'base method was called on main module. ' );
622  }
623 
624  return $this->getMain()->getContinuationManager();
625  }
626 
631  public function setContinuationManager( $manager ) {
632  // Main module has setContinuationManager() method overridden
633  // Safety - avoid infinite loop:
634  if ( $this->isMain() ) {
635  ApiBase::dieDebug( __METHOD__, 'base method was called on main module. ' );
636  }
637 
638  $this->getMain()->setContinuationManager( $manager );
639  }
640 
643  /************************************************************************/
655  public function dynamicParameterDocumentation() {
656  return null;
657  }
658 
665  public function encodeParamName( $paramName ) {
666  return $this->mModulePrefix . $paramName;
667  }
668 
678  public function extractRequestParams( $parseLimit = true ) {
679  // Cache parameters, for performance and to avoid bug 24564.
680  if ( !isset( $this->mParamCache[$parseLimit] ) ) {
681  $params = $this->getFinalParams();
682  $results = [];
683 
684  if ( $params ) { // getFinalParams() can return false
685  foreach ( $params as $paramName => $paramSettings ) {
686  $results[$paramName] = $this->getParameterFromSettings(
687  $paramName, $paramSettings, $parseLimit );
688  }
689  }
690  $this->mParamCache[$parseLimit] = $results;
691  }
692 
693  return $this->mParamCache[$parseLimit];
694  }
695 
702  protected function getParameter( $paramName, $parseLimit = true ) {
703  $paramSettings = $this->getFinalParams()[$paramName];
704 
705  return $this->getParameterFromSettings( $paramName, $paramSettings, $parseLimit );
706  }
707 
714  public function requireOnlyOneParameter( $params, $required /*...*/ ) {
715  $required = func_get_args();
716  array_shift( $required );
717  $p = $this->getModulePrefix();
718 
719  $intersection = array_intersect( array_keys( array_filter( $params,
720  [ $this, 'parameterNotEmpty' ] ) ), $required );
721 
722  if ( count( $intersection ) > 1 ) {
723  $this->dieUsage(
724  "The parameters {$p}" . implode( ", {$p}", $intersection ) . ' can not be used together',
725  'invalidparammix' );
726  } elseif ( count( $intersection ) == 0 ) {
727  $this->dieUsage(
728  "One of the parameters {$p}" . implode( ", {$p}", $required ) . ' is required',
729  'missingparam'
730  );
731  }
732  }
733 
740  public function requireMaxOneParameter( $params, $required /*...*/ ) {
741  $required = func_get_args();
742  array_shift( $required );
743  $p = $this->getModulePrefix();
744 
745  $intersection = array_intersect( array_keys( array_filter( $params,
746  [ $this, 'parameterNotEmpty' ] ) ), $required );
747 
748  if ( count( $intersection ) > 1 ) {
749  $this->dieUsage(
750  "The parameters {$p}" . implode( ", {$p}", $intersection ) . ' can not be used together',
751  'invalidparammix'
752  );
753  }
754  }
755 
763  public function requireAtLeastOneParameter( $params, $required /*...*/ ) {
764  $required = func_get_args();
765  array_shift( $required );
766  $p = $this->getModulePrefix();
767 
768  $intersection = array_intersect(
769  array_keys( array_filter( $params, [ $this, 'parameterNotEmpty' ] ) ),
770  $required
771  );
772 
773  if ( count( $intersection ) == 0 ) {
774  $this->dieUsage( "At least one of the parameters {$p}" .
775  implode( ", {$p}", $required ) . ' is required', "{$p}missingparam" );
776  }
777  }
778 
786  public function requirePostedParameters( $params, $prefix = 'prefix' ) {
787  // Skip if $wgDebugAPI is set or we're in internal mode
788  if ( $this->getConfig()->get( 'DebugAPI' ) || $this->getMain()->isInternalMode() ) {
789  return;
790  }
791 
792  $queryValues = $this->getRequest()->getQueryValues();
793  $badParams = [];
794  foreach ( $params as $param ) {
795  if ( $prefix !== 'noprefix' ) {
796  $param = $this->encodeParamName( $param );
797  }
798  if ( array_key_exists( $param, $queryValues ) ) {
799  $badParams[] = $param;
800  }
801  }
802 
803  if ( $badParams ) {
804  $this->dieUsage(
805  'The following parameters were found in the query string, but must be in the POST body: '
806  . join( ', ', $badParams ),
807  'mustpostparams'
808  );
809  }
810  }
811 
818  private function parameterNotEmpty( $x ) {
819  return !is_null( $x ) && $x !== false;
820  }
821 
833  public function getTitleOrPageId( $params, $load = false ) {
834  $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
835 
836  $pageObj = null;
837  if ( isset( $params['title'] ) ) {
838  $titleObj = Title::newFromText( $params['title'] );
839  if ( !$titleObj || $titleObj->isExternal() ) {
840  $this->dieUsageMsg( [ 'invalidtitle', $params['title'] ] );
841  }
842  if ( !$titleObj->canExist() ) {
843  $this->dieUsage( "Namespace doesn't allow actual pages", 'pagecannotexist' );
844  }
845  $pageObj = WikiPage::factory( $titleObj );
846  if ( $load !== false ) {
847  $pageObj->loadPageData( $load );
848  }
849  } elseif ( isset( $params['pageid'] ) ) {
850  if ( $load === false ) {
851  $load = 'fromdb';
852  }
853  $pageObj = WikiPage::newFromID( $params['pageid'], $load );
854  if ( !$pageObj ) {
855  $this->dieUsageMsg( [ 'nosuchpageid', $params['pageid'] ] );
856  }
857  }
858 
859  return $pageObj;
860  }
861 
870  protected function getWatchlistValue( $watchlist, $titleObj, $userOption = null ) {
871 
872  $userWatching = $this->getUser()->isWatched( $titleObj, User::IGNORE_USER_RIGHTS );
873 
874  switch ( $watchlist ) {
875  case 'watch':
876  return true;
877 
878  case 'unwatch':
879  return false;
880 
881  case 'preferences':
882  # If the user is already watching, don't bother checking
883  if ( $userWatching ) {
884  return true;
885  }
886  # If no user option was passed, use watchdefault and watchcreations
887  if ( is_null( $userOption ) ) {
888  return $this->getUser()->getBoolOption( 'watchdefault' ) ||
889  $this->getUser()->getBoolOption( 'watchcreations' ) && !$titleObj->exists();
890  }
891 
892  # Watch the article based on the user preference
893  return $this->getUser()->getBoolOption( $userOption );
894 
895  case 'nochange':
896  return $userWatching;
897 
898  default:
899  return $userWatching;
900  }
901  }
902 
912  protected function getParameterFromSettings( $paramName, $paramSettings, $parseLimit ) {
913  // Some classes may decide to change parameter names
914  $encParamName = $this->encodeParamName( $paramName );
915 
916  if ( !is_array( $paramSettings ) ) {
917  $default = $paramSettings;
918  $multi = false;
919  $type = gettype( $paramSettings );
920  $dupes = false;
921  $deprecated = false;
922  $required = false;
923  } else {
924  $default = isset( $paramSettings[self::PARAM_DFLT] )
925  ? $paramSettings[self::PARAM_DFLT]
926  : null;
927  $multi = isset( $paramSettings[self::PARAM_ISMULTI] )
928  ? $paramSettings[self::PARAM_ISMULTI]
929  : false;
930  $type = isset( $paramSettings[self::PARAM_TYPE] )
931  ? $paramSettings[self::PARAM_TYPE]
932  : null;
933  $dupes = isset( $paramSettings[self::PARAM_ALLOW_DUPLICATES] )
934  ? $paramSettings[self::PARAM_ALLOW_DUPLICATES]
935  : false;
936  $deprecated = isset( $paramSettings[self::PARAM_DEPRECATED] )
937  ? $paramSettings[self::PARAM_DEPRECATED]
938  : false;
939  $required = isset( $paramSettings[self::PARAM_REQUIRED] )
940  ? $paramSettings[self::PARAM_REQUIRED]
941  : false;
942 
943  // When type is not given, and no choices, the type is the same as $default
944  if ( !isset( $type ) ) {
945  if ( isset( $default ) ) {
946  $type = gettype( $default );
947  } else {
948  $type = 'NULL'; // allow everything
949  }
950  }
951  }
952 
953  if ( $type == 'boolean' ) {
954  if ( isset( $default ) && $default !== false ) {
955  // Having a default value of anything other than 'false' is not allowed
957  __METHOD__,
958  "Boolean param $encParamName's default is set to '$default'. " .
959  'Boolean parameters must default to false.'
960  );
961  }
962 
963  $value = $this->getMain()->getCheck( $encParamName );
964  } elseif ( $type == 'upload' ) {
965  if ( isset( $default ) ) {
966  // Having a default value is not allowed
968  __METHOD__,
969  "File upload param $encParamName's default is set to " .
970  "'$default'. File upload parameters may not have a default." );
971  }
972  if ( $multi ) {
973  ApiBase::dieDebug( __METHOD__, "Multi-values not supported for $encParamName" );
974  }
975  $value = $this->getMain()->getUpload( $encParamName );
976  if ( !$value->exists() ) {
977  // This will get the value without trying to normalize it
978  // (because trying to normalize a large binary file
979  // accidentally uploaded as a field fails spectacularly)
980  $value = $this->getMain()->getRequest()->unsetVal( $encParamName );
981  if ( $value !== null ) {
982  $this->dieUsage(
983  "File upload param $encParamName is not a file upload; " .
984  'be sure to use multipart/form-data for your POST and include ' .
985  'a filename in the Content-Disposition header.',
986  "badupload_{$encParamName}"
987  );
988  }
989  }
990  } else {
991  $value = $this->getMain()->getVal( $encParamName, $default );
992 
993  if ( isset( $value ) && $type == 'namespace' ) {
995  }
996  if ( isset( $value ) && $type == 'submodule' ) {
997  if ( isset( $paramSettings[self::PARAM_SUBMODULE_MAP] ) ) {
998  $type = array_keys( $paramSettings[self::PARAM_SUBMODULE_MAP] );
999  } else {
1000  $type = $this->getModuleManager()->getNames( $paramName );
1001  }
1002  }
1003 
1004  $request = $this->getMain()->getRequest();
1005  $rawValue = $request->getRawVal( $encParamName );
1006  if ( $rawValue === null ) {
1007  $rawValue = $default;
1008  }
1009 
1010  // Preserve U+001F for self::parseMultiValue(), or error out if that won't be called
1011  if ( isset( $value ) && substr( $rawValue, 0, 1 ) === "\x1f" ) {
1012  if ( $multi ) {
1013  // This loses the potential $wgContLang->checkTitleEncoding() transformation
1014  // done by WebRequest for $_GET. Let's call that a feature.
1015  $value = join( "\x1f", $request->normalizeUnicode( explode( "\x1f", $rawValue ) ) );
1016  } else {
1017  $this->dieUsage(
1018  "U+001F multi-value separation may only be used for multi-valued parameters.",
1019  'badvalue_notmultivalue'
1020  );
1021  }
1022  }
1023 
1024  // Check for NFC normalization, and warn
1025  if ( $rawValue !== $value ) {
1026  $this->handleParamNormalization( $paramName, $value, $rawValue );
1027  }
1028  }
1029 
1030  if ( isset( $value ) && ( $multi || is_array( $type ) ) ) {
1031  $value = $this->parseMultiValue(
1032  $encParamName,
1033  $value,
1034  $multi,
1035  is_array( $type ) ? $type : null
1036  );
1037  }
1038 
1039  // More validation only when choices were not given
1040  // choices were validated in parseMultiValue()
1041  if ( isset( $value ) ) {
1042  if ( !is_array( $type ) ) {
1043  switch ( $type ) {
1044  case 'NULL': // nothing to do
1045  break;
1046  case 'string':
1047  case 'text':
1048  case 'password':
1049  if ( $required && $value === '' ) {
1050  $this->dieUsageMsg( [ 'missingparam', $paramName ] );
1051  }
1052  break;
1053  case 'integer': // Force everything using intval() and optionally validate limits
1054  $min = isset( $paramSettings[self::PARAM_MIN] ) ? $paramSettings[self::PARAM_MIN] : null;
1055  $max = isset( $paramSettings[self::PARAM_MAX] ) ? $paramSettings[self::PARAM_MAX] : null;
1056  $enforceLimits = isset( $paramSettings[self::PARAM_RANGE_ENFORCE] )
1057  ? $paramSettings[self::PARAM_RANGE_ENFORCE] : false;
1058 
1059  if ( is_array( $value ) ) {
1060  $value = array_map( 'intval', $value );
1061  if ( !is_null( $min ) || !is_null( $max ) ) {
1062  foreach ( $value as &$v ) {
1063  $this->validateLimit( $paramName, $v, $min, $max, null, $enforceLimits );
1064  }
1065  }
1066  } else {
1067  $value = intval( $value );
1068  if ( !is_null( $min ) || !is_null( $max ) ) {
1069  $this->validateLimit( $paramName, $value, $min, $max, null, $enforceLimits );
1070  }
1071  }
1072  break;
1073  case 'limit':
1074  if ( !$parseLimit ) {
1075  // Don't do any validation whatsoever
1076  break;
1077  }
1078  if ( !isset( $paramSettings[self::PARAM_MAX] )
1079  || !isset( $paramSettings[self::PARAM_MAX2] )
1080  ) {
1082  __METHOD__,
1083  "MAX1 or MAX2 are not defined for the limit $encParamName"
1084  );
1085  }
1086  if ( $multi ) {
1087  ApiBase::dieDebug( __METHOD__, "Multi-values not supported for $encParamName" );
1088  }
1089  $min = isset( $paramSettings[self::PARAM_MIN] ) ? $paramSettings[self::PARAM_MIN] : 0;
1090  if ( $value == 'max' ) {
1091  $value = $this->getMain()->canApiHighLimits()
1092  ? $paramSettings[self::PARAM_MAX2]
1093  : $paramSettings[self::PARAM_MAX];
1094  $this->getResult()->addParsedLimit( $this->getModuleName(), $value );
1095  } else {
1096  $value = intval( $value );
1097  $this->validateLimit(
1098  $paramName,
1099  $value,
1100  $min,
1101  $paramSettings[self::PARAM_MAX],
1102  $paramSettings[self::PARAM_MAX2]
1103  );
1104  }
1105  break;
1106  case 'boolean':
1107  if ( $multi ) {
1108  ApiBase::dieDebug( __METHOD__, "Multi-values not supported for $encParamName" );
1109  }
1110  break;
1111  case 'timestamp':
1112  if ( is_array( $value ) ) {
1113  foreach ( $value as $key => $val ) {
1114  $value[$key] = $this->validateTimestamp( $val, $encParamName );
1115  }
1116  } else {
1117  $value = $this->validateTimestamp( $value, $encParamName );
1118  }
1119  break;
1120  case 'user':
1121  if ( is_array( $value ) ) {
1122  foreach ( $value as $key => $val ) {
1123  $value[$key] = $this->validateUser( $val, $encParamName );
1124  }
1125  } else {
1126  $value = $this->validateUser( $value, $encParamName );
1127  }
1128  break;
1129  case 'upload': // nothing to do
1130  break;
1131  case 'tags':
1132  // If change tagging was requested, check that the tags are valid.
1133  if ( !is_array( $value ) && !$multi ) {
1134  $value = [ $value ];
1135  }
1137  if ( !$tagsStatus->isGood() ) {
1138  $this->dieStatus( $tagsStatus );
1139  }
1140  break;
1141  default:
1142  ApiBase::dieDebug( __METHOD__, "Param $encParamName's type is unknown - $type" );
1143  }
1144  }
1145 
1146  // Throw out duplicates if requested
1147  if ( !$dupes && is_array( $value ) ) {
1148  $value = array_unique( $value );
1149  }
1150 
1151  // Set a warning if a deprecated parameter has been passed
1152  if ( $deprecated && $value !== false ) {
1153  $this->setWarning( "The $encParamName parameter has been deprecated." );
1154 
1155  $feature = $encParamName;
1156  $m = $this;
1157  while ( !$m->isMain() ) {
1158  $p = $m->getParent();
1159  $name = $m->getModuleName();
1160  $param = $p->encodeParamName( $p->getModuleManager()->getModuleGroup( $name ) );
1161  $feature = "{$param}={$name}&{$feature}";
1162  $m = $p;
1163  }
1164  $this->logFeatureUsage( $feature );
1165  }
1166  } elseif ( $required ) {
1167  $this->dieUsageMsg( [ 'missingparam', $paramName ] );
1168  }
1169 
1170  return $value;
1171  }
1172 
1180  protected function handleParamNormalization( $paramName, $value, $rawValue ) {
1181  $encParamName = $this->encodeParamName( $paramName );
1182  $this->setWarning(
1183  "The value passed for '$encParamName' contains invalid or non-normalized data. "
1184  . 'Textual data should be valid, NFC-normalized Unicode without '
1185  . 'C0 control characters other than HT (\\t), LF (\\n), and CR (\\r).'
1186  );
1187  }
1188 
1196  protected function explodeMultiValue( $value, $limit ) {
1197  if ( substr( $value, 0, 1 ) === "\x1f" ) {
1198  $sep = "\x1f";
1199  $value = substr( $value, 1 );
1200  } else {
1201  $sep = '|';
1202  }
1203 
1204  return explode( $sep, $value, $limit );
1205  }
1206 
1220  protected function parseMultiValue( $valueName, $value, $allowMultiple, $allowedValues ) {
1221  if ( ( trim( $value ) === '' || trim( $value ) === "\x1f" ) && $allowMultiple ) {
1222  return [];
1223  }
1224 
1225  // This is a bit awkward, but we want to avoid calling canApiHighLimits()
1226  // because it unstubs $wgUser
1227  $valuesList = $this->explodeMultiValue( $value, self::LIMIT_SML2 + 1 );
1228  $sizeLimit = count( $valuesList ) > self::LIMIT_SML1 && $this->mMainModule->canApiHighLimits()
1229  ? self::LIMIT_SML2
1230  : self::LIMIT_SML1;
1231 
1232  if ( self::truncateArray( $valuesList, $sizeLimit ) ) {
1233  $this->logFeatureUsage( "too-many-$valueName-for-{$this->getModulePath()}" );
1234  $this->setWarning( "Too many values supplied for parameter '$valueName': " .
1235  "the limit is $sizeLimit" );
1236  }
1237 
1238  if ( !$allowMultiple && count( $valuesList ) != 1 ) {
1239  // Bug 33482 - Allow entries with | in them for non-multiple values
1240  if ( in_array( $value, $allowedValues, true ) ) {
1241  return $value;
1242  }
1243 
1244  $possibleValues = is_array( $allowedValues )
1245  ? "of '" . implode( "', '", $allowedValues ) . "'"
1246  : '';
1247  $this->dieUsage(
1248  "Only one $possibleValues is allowed for parameter '$valueName'",
1249  "multival_$valueName"
1250  );
1251  }
1252 
1253  if ( is_array( $allowedValues ) ) {
1254  // Check for unknown values
1255  $unknown = array_diff( $valuesList, $allowedValues );
1256  if ( count( $unknown ) ) {
1257  if ( $allowMultiple ) {
1258  $s = count( $unknown ) > 1 ? 's' : '';
1259  $vals = implode( ', ', $unknown );
1260  $this->setWarning( "Unrecognized value$s for parameter '$valueName': $vals" );
1261  } else {
1262  $this->dieUsage(
1263  "Unrecognized value for parameter '$valueName': {$valuesList[0]}",
1264  "unknown_$valueName"
1265  );
1266  }
1267  }
1268  // Now throw them out
1269  $valuesList = array_intersect( $valuesList, $allowedValues );
1270  }
1271 
1272  return $allowMultiple ? $valuesList : $valuesList[0];
1273  }
1274 
1285  protected function validateLimit( $paramName, &$value, $min, $max, $botMax = null,
1286  $enforceLimits = false
1287  ) {
1288  if ( !is_null( $min ) && $value < $min ) {
1289  $msg = $this->encodeParamName( $paramName ) . " may not be less than $min (set to $value)";
1290  $this->warnOrDie( $msg, $enforceLimits );
1291  $value = $min;
1292  }
1293 
1294  // Minimum is always validated, whereas maximum is checked only if not
1295  // running in internal call mode
1296  if ( $this->getMain()->isInternalMode() ) {
1297  return;
1298  }
1299 
1300  // Optimization: do not check user's bot status unless really needed -- skips db query
1301  // assumes $botMax >= $max
1302  if ( !is_null( $max ) && $value > $max ) {
1303  if ( !is_null( $botMax ) && $this->getMain()->canApiHighLimits() ) {
1304  if ( $value > $botMax ) {
1305  $msg = $this->encodeParamName( $paramName ) .
1306  " may not be over $botMax (set to $value) for bots or sysops";
1307  $this->warnOrDie( $msg, $enforceLimits );
1308  $value = $botMax;
1309  }
1310  } else {
1311  $msg = $this->encodeParamName( $paramName ) . " may not be over $max (set to $value) for users";
1312  $this->warnOrDie( $msg, $enforceLimits );
1313  $value = $max;
1314  }
1315  }
1316  }
1317 
1324  protected function validateTimestamp( $value, $encParamName ) {
1325  // Confusing synonyms for the current time accepted by wfTimestamp()
1326  // (wfTimestamp() also accepts various non-strings and the string of 14
1327  // ASCII NUL bytes, but those can't get here)
1328  if ( !$value ) {
1329  $this->logFeatureUsage( 'unclear-"now"-timestamp' );
1330  $this->setWarning(
1331  "Passing '$value' for timestamp parameter $encParamName has been deprecated." .
1332  ' If for some reason you need to explicitly specify the current time without' .
1333  ' calculating it client-side, use "now".'
1334  );
1335  return wfTimestamp( TS_MW );
1336  }
1337 
1338  // Explicit synonym for the current time
1339  if ( $value === 'now' ) {
1340  return wfTimestamp( TS_MW );
1341  }
1342 
1343  $unixTimestamp = wfTimestamp( TS_UNIX, $value );
1344  if ( $unixTimestamp === false ) {
1345  $this->dieUsage(
1346  "Invalid value '$value' for timestamp parameter $encParamName",
1347  "badtimestamp_{$encParamName}"
1348  );
1349  }
1350 
1351  return wfTimestamp( TS_MW, $unixTimestamp );
1352  }
1353 
1363  final public function validateToken( $token, array $params ) {
1364  $tokenType = $this->needsToken();
1366  if ( !isset( $salts[$tokenType] ) ) {
1367  throw new MWException(
1368  "Module '{$this->getModuleName()}' tried to use token type '$tokenType' " .
1369  'without registering it'
1370  );
1371  }
1372 
1373  $tokenObj = ApiQueryTokens::getToken(
1374  $this->getUser(), $this->getRequest()->getSession(), $salts[$tokenType]
1375  );
1376  if ( $tokenObj->match( $token ) ) {
1377  return true;
1378  }
1379 
1380  $webUiSalt = $this->getWebUITokenSalt( $params );
1381  if ( $webUiSalt !== null && $this->getUser()->matchEditToken(
1382  $token,
1383  $webUiSalt,
1384  $this->getRequest()
1385  ) ) {
1386  return true;
1387  }
1388 
1389  return false;
1390  }
1391 
1398  private function validateUser( $value, $encParamName ) {
1400  if ( $title === null || $title->hasFragment() ) {
1401  $this->dieUsage(
1402  "Invalid value '$value' for user parameter $encParamName",
1403  "baduser_{$encParamName}"
1404  );
1405  }
1406 
1407  return $title->getText();
1408  }
1409 
1412  /************************************************************************/
1423  protected function setWatch( $watch, $titleObj, $userOption = null ) {
1424  $value = $this->getWatchlistValue( $watch, $titleObj, $userOption );
1425  if ( $value === null ) {
1426  return;
1427  }
1428 
1429  WatchAction::doWatchOrUnwatch( $value, $titleObj, $this->getUser() );
1430  }
1431 
1438  public static function truncateArray( &$arr, $limit ) {
1439  $modified = false;
1440  while ( count( $arr ) > $limit ) {
1441  array_pop( $arr );
1442  $modified = true;
1443  }
1444 
1445  return $modified;
1446  }
1447 
1454  public function getWatchlistUser( $params ) {
1455  if ( !is_null( $params['owner'] ) && !is_null( $params['token'] ) ) {
1456  $user = User::newFromName( $params['owner'], false );
1457  if ( !( $user && $user->getId() ) ) {
1458  $this->dieUsage( 'Specified user does not exist', 'bad_wlowner' );
1459  }
1460  $token = $user->getOption( 'watchlisttoken' );
1461  if ( $token == '' || !hash_equals( $token, $params['token'] ) ) {
1462  $this->dieUsage(
1463  'Incorrect watchlist token provided -- please set a correct token in Special:Preferences',
1464  'bad_wltoken'
1465  );
1466  }
1467  } else {
1468  if ( !$this->getUser()->isLoggedIn() ) {
1469  $this->dieUsage( 'You must be logged-in to have a watchlist', 'notloggedin' );
1470  }
1471  if ( !$this->getUser()->isAllowed( 'viewmywatchlist' ) ) {
1472  $this->dieUsage( 'You don\'t have permission to view your watchlist', 'permissiondenied' );
1473  }
1474  $user = $this->getUser();
1475  }
1476 
1477  return $user;
1478  }
1479 
1487  private static function escapeWikiText( $v ) {
1488  if ( is_array( $v ) ) {
1489  return array_map( 'self::escapeWikiText', $v );
1490  } else {
1491  return strtr( $v, [
1492  '__' => '_&#95;', '{' => '&#123;', '}' => '&#125;',
1493  '[[Category:' => '[[:Category:',
1494  '[[File:' => '[[:File:', '[[Image:' => '[[:Image:',
1495  ] );
1496  }
1497  }
1498 
1511  public static function makeMessage( $msg, IContextSource $context, array $params = null ) {
1512  if ( is_string( $msg ) ) {
1513  $msg = wfMessage( $msg );
1514  } elseif ( is_array( $msg ) ) {
1515  $msg = call_user_func_array( 'wfMessage', $msg );
1516  }
1517  if ( !$msg instanceof Message ) {
1518  return null;
1519  }
1520 
1521  $msg->setContext( $context );
1522  if ( $params ) {
1523  $msg->params( $params );
1524  }
1525 
1526  return $msg;
1527  }
1528 
1531  /************************************************************************/
1543  public function setWarning( $warning ) {
1544  $msg = new ApiRawMessage( $warning, 'warning' );
1545  $this->getErrorFormatter()->addWarning( $this->getModuleName(), $msg );
1546  }
1547 
1554  private function warnOrDie( $msg, $enforceLimits = false ) {
1555  if ( $enforceLimits ) {
1556  $this->dieUsage( $msg, 'integeroutofrange' );
1557  }
1558 
1559  $this->setWarning( $msg );
1560  }
1561 
1574  public function dieUsage( $description, $errorCode, $httpRespCode = 0, $extradata = null ) {
1575  throw new UsageException(
1576  $description,
1577  $this->encodeParamName( $errorCode ),
1578  $httpRespCode,
1579  $extradata
1580  );
1581  }
1582 
1591  public function dieBlocked( Block $block ) {
1592  // Die using the appropriate message depending on block type
1593  if ( $block->getType() == Block::TYPE_AUTO ) {
1594  $this->dieUsage(
1595  'Your IP address has been blocked automatically, because it was used by a blocked user',
1596  'autoblocked',
1597  0,
1598  [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $block ) ]
1599  );
1600  } else {
1601  $this->dieUsage(
1602  'You have been blocked from editing',
1603  'blocked',
1604  0,
1605  [ 'blockinfo' => ApiQueryUserInfo::getBlockInfo( $block ) ]
1606  );
1607  }
1608  }
1609 
1619  public function getErrorFromStatus( $status, &$extraData = null ) {
1620  if ( $status->isGood() ) {
1621  throw new MWException( 'Successful status passed to ApiBase::dieStatus' );
1622  }
1623 
1624  $errors = $status->getErrorsByType( 'error' );
1625  if ( !$errors ) {
1626  // No errors? Assume the warnings should be treated as errors
1627  $errors = $status->getErrorsByType( 'warning' );
1628  }
1629  if ( !$errors ) {
1630  // Still no errors? Punt
1631  $errors = [ [ 'message' => 'unknownerror-nocode', 'params' => [] ] ];
1632  }
1633 
1634  // Cannot use dieUsageMsg() because extensions might return custom
1635  // error messages.
1636  if ( $errors[0]['message'] instanceof Message ) {
1637  $msg = $errors[0]['message'];
1638  if ( $msg instanceof IApiMessage ) {
1639  $extraData = $msg->getApiData();
1640  $code = $msg->getApiCode();
1641  } else {
1642  $code = $msg->getKey();
1643  }
1644  } else {
1645  $code = $errors[0]['message'];
1646  $msg = wfMessage( $code, $errors[0]['params'] );
1647  }
1648  if ( isset( ApiBase::$messageMap[$code] ) ) {
1649  // Translate message to code, for backwards compatibility
1650  $code = ApiBase::$messageMap[$code]['code'];
1651  }
1652 
1653  return [ $code, $msg->inLanguage( 'en' )->useDatabase( false )->plain() ];
1654  }
1655 
1663  public function dieStatus( $status ) {
1664  $extraData = null;
1665  list( $code, $msg ) = $this->getErrorFromStatus( $status, $extraData );
1666  $this->dieUsage( $msg, $code, 0, $extraData );
1667  }
1668 
1669  // @codingStandardsIgnoreStart Allow long lines. Cannot split these.
1673  public static $messageMap = [
1674  // This one MUST be present, or dieUsageMsg() will recurse infinitely
1675  'unknownerror' => [ 'code' => 'unknownerror', 'info' => "Unknown error: \"\$1\"" ],
1676  'unknownerror-nocode' => [ 'code' => 'unknownerror', 'info' => 'Unknown error' ],
1677 
1678  // Messages from Title::getUserPermissionsErrors()
1679  'ns-specialprotected' => [
1680  'code' => 'unsupportednamespace',
1681  'info' => "Pages in the Special namespace can't be edited"
1682  ],
1683  'protectedinterface' => [
1684  'code' => 'protectednamespace-interface',
1685  'info' => "You're not allowed to edit interface messages"
1686  ],
1687  'namespaceprotected' => [
1688  'code' => 'protectednamespace',
1689  'info' => "You're not allowed to edit pages in the \"\$1\" namespace"
1690  ],
1691  'customcssprotected' => [
1692  'code' => 'customcssprotected',
1693  'info' => "You're not allowed to edit custom CSS pages"
1694  ],
1695  'customjsprotected' => [
1696  'code' => 'customjsprotected',
1697  'info' => "You're not allowed to edit custom JavaScript pages"
1698  ],
1699  'cascadeprotected' => [
1700  'code' => 'cascadeprotected',
1701  'info' => "The page you're trying to edit is protected because it's included in a cascade-protected page"
1702  ],
1703  'protectedpagetext' => [
1704  'code' => 'protectedpage',
1705  'info' => "The \"\$1\" right is required to edit this page"
1706  ],
1707  'protect-cantedit' => [
1708  'code' => 'cantedit',
1709  'info' => "You can't protect this page because you can't edit it"
1710  ],
1711  'deleteprotected' => [
1712  'code' => 'cantedit',
1713  'info' => "You can't delete this page because it has been protected"
1714  ],
1715  'badaccess-group0' => [
1716  'code' => 'permissiondenied',
1717  'info' => 'Permission denied'
1718  ], // Generic permission denied message
1719  'badaccess-groups' => [
1720  'code' => 'permissiondenied',
1721  'info' => 'Permission denied'
1722  ],
1723  'titleprotected' => [
1724  'code' => 'protectedtitle',
1725  'info' => 'This title has been protected from creation'
1726  ],
1727  'nocreate-loggedin' => [
1728  'code' => 'cantcreate',
1729  'info' => "You don't have permission to create new pages"
1730  ],
1731  'nocreatetext' => [
1732  'code' => 'cantcreate-anon',
1733  'info' => "Anonymous users can't create new pages"
1734  ],
1735  'movenologintext' => [
1736  'code' => 'cantmove-anon',
1737  'info' => "Anonymous users can't move pages"
1738  ],
1739  'movenotallowed' => [
1740  'code' => 'cantmove',
1741  'info' => "You don't have permission to move pages"
1742  ],
1743  'confirmedittext' => [
1744  'code' => 'confirmemail',
1745  'info' => 'You must confirm your email address before you can edit'
1746  ],
1747  'blockedtext' => [
1748  'code' => 'blocked',
1749  'info' => 'You have been blocked from editing'
1750  ],
1751  'autoblockedtext' => [
1752  'code' => 'autoblocked',
1753  'info' => 'Your IP address has been blocked automatically, because it was used by a blocked user'
1754  ],
1755 
1756  // Miscellaneous interface messages
1757  'actionthrottledtext' => [
1758  'code' => 'ratelimited',
1759  'info' => "You've exceeded your rate limit. Please wait some time and try again"
1760  ],
1761  'alreadyrolled' => [
1762  'code' => 'alreadyrolled',
1763  'info' => 'The page you tried to rollback was already rolled back'
1764  ],
1765  'cantrollback' => [
1766  'code' => 'onlyauthor',
1767  'info' => 'The page you tried to rollback only has one author'
1768  ],
1769  'readonlytext' => [
1770  'code' => 'readonly',
1771  'info' => 'The wiki is currently in read-only mode'
1772  ],
1773  'sessionfailure' => [
1774  'code' => 'badtoken',
1775  'info' => 'Invalid token' ],
1776  'cannotdelete' => [
1777  'code' => 'cantdelete',
1778  'info' => "Couldn't delete \"\$1\". Maybe it was deleted already by someone else"
1779  ],
1780  'notanarticle' => [
1781  'code' => 'missingtitle',
1782  'info' => "The page you requested doesn't exist"
1783  ],
1784  'selfmove' => [ 'code' => 'selfmove', 'info' => "Can't move a page to itself"
1785  ],
1786  'immobile_namespace' => [
1787  'code' => 'immobilenamespace',
1788  'info' => 'You tried to move pages from or to a namespace that is protected from moving'
1789  ],
1790  'articleexists' => [
1791  'code' => 'articleexists',
1792  'info' => 'The destination article already exists and is not a redirect to the source article'
1793  ],
1794  'protectedpage' => [
1795  'code' => 'protectedpage',
1796  'info' => "You don't have permission to perform this move"
1797  ],
1798  'hookaborted' => [
1799  'code' => 'hookaborted',
1800  'info' => 'The modification you tried to make was aborted by an extension hook'
1801  ],
1802  'cantmove-titleprotected' => [
1803  'code' => 'protectedtitle',
1804  'info' => 'The destination article has been protected from creation'
1805  ],
1806  'imagenocrossnamespace' => [
1807  'code' => 'nonfilenamespace',
1808  'info' => "Can't move a file to a non-file namespace"
1809  ],
1810  'imagetypemismatch' => [
1811  'code' => 'filetypemismatch',
1812  'info' => "The new file extension doesn't match its type"
1813  ],
1814  // 'badarticleerror' => shouldn't happen
1815  // 'badtitletext' => shouldn't happen
1816  'ip_range_invalid' => [ 'code' => 'invalidrange', 'info' => 'Invalid IP range' ],
1817  'range_block_disabled' => [
1818  'code' => 'rangedisabled',
1819  'info' => 'Blocking IP ranges has been disabled'
1820  ],
1821  'nosuchusershort' => [
1822  'code' => 'nosuchuser',
1823  'info' => "The user you specified doesn't exist"
1824  ],
1825  'badipaddress' => [ 'code' => 'invalidip', 'info' => 'Invalid IP address specified' ],
1826  'ipb_expiry_invalid' => [ 'code' => 'invalidexpiry', 'info' => 'Invalid expiry time' ],
1827  'ipb_already_blocked' => [
1828  'code' => 'alreadyblocked',
1829  'info' => 'The user you tried to block was already blocked'
1830  ],
1831  'ipb_blocked_as_range' => [
1832  'code' => 'blockedasrange',
1833  'info' => "IP address \"\$1\" was blocked as part of range \"\$2\". You can't unblock the IP individually, but you can unblock the range as a whole."
1834  ],
1835  'ipb_cant_unblock' => [
1836  'code' => 'cantunblock',
1837  'info' => 'The block you specified was not found. It may have been unblocked already'
1838  ],
1839  'mailnologin' => [
1840  'code' => 'cantsend',
1841  'info' => 'You are not logged in, you do not have a confirmed email address, or you are not allowed to send email to other users, so you cannot send email'
1842  ],
1843  'ipbblocked' => [
1844  'code' => 'ipbblocked',
1845  'info' => 'You cannot block or unblock users while you are yourself blocked'
1846  ],
1847  'ipbnounblockself' => [
1848  'code' => 'ipbnounblockself',
1849  'info' => 'You are not allowed to unblock yourself'
1850  ],
1851  'usermaildisabled' => [
1852  'code' => 'usermaildisabled',
1853  'info' => 'User email has been disabled'
1854  ],
1855  'blockedemailuser' => [
1856  'code' => 'blockedfrommail',
1857  'info' => 'You have been blocked from sending email'
1858  ],
1859  'notarget' => [
1860  'code' => 'notarget',
1861  'info' => 'You have not specified a valid target for this action'
1862  ],
1863  'noemail' => [
1864  'code' => 'noemail',
1865  'info' => 'The user has not specified a valid email address, or has chosen not to receive email from other users'
1866  ],
1867  'rcpatroldisabled' => [
1868  'code' => 'patroldisabled',
1869  'info' => 'Patrolling is disabled on this wiki'
1870  ],
1871  'markedaspatrollederror-noautopatrol' => [
1872  'code' => 'noautopatrol',
1873  'info' => "You don't have permission to patrol your own changes"
1874  ],
1875  'delete-toobig' => [
1876  'code' => 'bigdelete',
1877  'info' => "You can't delete this page because it has more than \$1 revisions"
1878  ],
1879  'movenotallowedfile' => [
1880  'code' => 'cantmovefile',
1881  'info' => "You don't have permission to move files"
1882  ],
1883  'userrights-no-interwiki' => [
1884  'code' => 'nointerwikiuserrights',
1885  'info' => "You don't have permission to change user rights on other wikis"
1886  ],
1887  'userrights-nodatabase' => [
1888  'code' => 'nosuchdatabase',
1889  'info' => "Database \"\$1\" does not exist or is not local"
1890  ],
1891  'nouserspecified' => [ 'code' => 'invaliduser', 'info' => "Invalid username \"\$1\"" ],
1892  'noname' => [ 'code' => 'invaliduser', 'info' => "Invalid username \"\$1\"" ],
1893  'summaryrequired' => [ 'code' => 'summaryrequired', 'info' => 'Summary required' ],
1894  'import-rootpage-invalid' => [
1895  'code' => 'import-rootpage-invalid',
1896  'info' => 'Root page is an invalid title'
1897  ],
1898  'import-rootpage-nosubpage' => [
1899  'code' => 'import-rootpage-nosubpage',
1900  'info' => 'Namespace "$1" of the root page does not allow subpages'
1901  ],
1902 
1903  // API-specific messages
1904  'readrequired' => [
1905  'code' => 'readapidenied',
1906  'info' => 'You need read permission to use this module'
1907  ],
1908  'writedisabled' => [
1909  'code' => 'noapiwrite',
1910  'info' => "Editing of this wiki through the API is disabled. Make sure the \$wgEnableWriteAPI=true; statement is included in the wiki's LocalSettings.php file"
1911  ],
1912  'writerequired' => [
1913  'code' => 'writeapidenied',
1914  'info' => "You're not allowed to edit this wiki through the API"
1915  ],
1916  'missingparam' => [ 'code' => 'no$1', 'info' => "The \$1 parameter must be set" ],
1917  'invalidtitle' => [ 'code' => 'invalidtitle', 'info' => "Bad title \"\$1\"" ],
1918  'nosuchpageid' => [ 'code' => 'nosuchpageid', 'info' => "There is no page with ID \$1" ],
1919  'nosuchrevid' => [ 'code' => 'nosuchrevid', 'info' => "There is no revision with ID \$1" ],
1920  'nosuchuser' => [ 'code' => 'nosuchuser', 'info' => "User \"\$1\" doesn't exist" ],
1921  'invaliduser' => [ 'code' => 'invaliduser', 'info' => "Invalid username \"\$1\"" ],
1922  'invalidexpiry' => [ 'code' => 'invalidexpiry', 'info' => "Invalid expiry time \"\$1\"" ],
1923  'pastexpiry' => [ 'code' => 'pastexpiry', 'info' => "Expiry time \"\$1\" is in the past" ],
1924  'create-titleexists' => [
1925  'code' => 'create-titleexists',
1926  'info' => "Existing titles can't be protected with 'create'"
1927  ],
1928  'missingtitle-createonly' => [
1929  'code' => 'missingtitle-createonly',
1930  'info' => "Missing titles can only be protected with 'create'"
1931  ],
1932  'cantblock' => [ 'code' => 'cantblock',
1933  'info' => "You don't have permission to block users"
1934  ],
1935  'canthide' => [
1936  'code' => 'canthide',
1937  'info' => "You don't have permission to hide user names from the block log"
1938  ],
1939  'cantblock-email' => [
1940  'code' => 'cantblock-email',
1941  'info' => "You don't have permission to block users from sending email through the wiki"
1942  ],
1943  'unblock-notarget' => [
1944  'code' => 'notarget',
1945  'info' => 'Either the id or the user parameter must be set'
1946  ],
1947  'unblock-idanduser' => [
1948  'code' => 'idanduser',
1949  'info' => "The id and user parameters can't be used together"
1950  ],
1951  'cantunblock' => [
1952  'code' => 'permissiondenied',
1953  'info' => "You don't have permission to unblock users"
1954  ],
1955  'cannotundelete' => [
1956  'code' => 'cantundelete',
1957  'info' => "Couldn't undelete: the requested revisions may not exist, or may have been undeleted already"
1958  ],
1959  'permdenied-undelete' => [
1960  'code' => 'permissiondenied',
1961  'info' => "You don't have permission to restore deleted revisions"
1962  ],
1963  'createonly-exists' => [
1964  'code' => 'articleexists',
1965  'info' => 'The article you tried to create has been created already'
1966  ],
1967  'nocreate-missing' => [
1968  'code' => 'missingtitle',
1969  'info' => "The article you tried to edit doesn't exist"
1970  ],
1971  'cantchangecontentmodel' => [
1972  'code' => 'cantchangecontentmodel',
1973  'info' => "You don't have permission to change the content model of a page"
1974  ],
1975  'nosuchrcid' => [
1976  'code' => 'nosuchrcid',
1977  'info' => "There is no change with rcid \"\$1\""
1978  ],
1979  'nosuchlogid' => [
1980  'code' => 'nosuchlogid',
1981  'info' => "There is no log entry with ID \"\$1\""
1982  ],
1983  'protect-invalidaction' => [
1984  'code' => 'protect-invalidaction',
1985  'info' => "Invalid protection type \"\$1\""
1986  ],
1987  'protect-invalidlevel' => [
1988  'code' => 'protect-invalidlevel',
1989  'info' => "Invalid protection level \"\$1\""
1990  ],
1991  'toofewexpiries' => [
1992  'code' => 'toofewexpiries',
1993  'info' => "\$1 expiry timestamps were provided where \$2 were needed"
1994  ],
1995  'cantimport' => [
1996  'code' => 'cantimport',
1997  'info' => "You don't have permission to import pages"
1998  ],
1999  'cantimport-upload' => [
2000  'code' => 'cantimport-upload',
2001  'info' => "You don't have permission to import uploaded pages"
2002  ],
2003  'importnofile' => [ 'code' => 'nofile', 'info' => "You didn't upload a file" ],
2004  'importuploaderrorsize' => [
2005  'code' => 'filetoobig',
2006  'info' => 'The file you uploaded is bigger than the maximum upload size'
2007  ],
2008  'importuploaderrorpartial' => [
2009  'code' => 'partialupload',
2010  'info' => 'The file was only partially uploaded'
2011  ],
2012  'importuploaderrortemp' => [
2013  'code' => 'notempdir',
2014  'info' => 'The temporary upload directory is missing'
2015  ],
2016  'importcantopen' => [
2017  'code' => 'cantopenfile',
2018  'info' => "Couldn't open the uploaded file"
2019  ],
2020  'import-noarticle' => [
2021  'code' => 'badinterwiki',
2022  'info' => 'Invalid interwiki title specified'
2023  ],
2024  'importbadinterwiki' => [
2025  'code' => 'badinterwiki',
2026  'info' => 'Invalid interwiki title specified'
2027  ],
2028  'import-unknownerror' => [
2029  'code' => 'import-unknownerror',
2030  'info' => "Unknown error on import: \"\$1\""
2031  ],
2032  'cantoverwrite-sharedfile' => [
2033  'code' => 'cantoverwrite-sharedfile',
2034  'info' => 'The target file exists on a shared repository and you do not have permission to override it'
2035  ],
2036  'sharedfile-exists' => [
2037  'code' => 'fileexists-sharedrepo-perm',
2038  'info' => 'The target file exists on a shared repository. Use the ignorewarnings parameter to override it.'
2039  ],
2040  'mustbeposted' => [
2041  'code' => 'mustbeposted',
2042  'info' => "The \$1 module requires a POST request"
2043  ],
2044  'show' => [
2045  'code' => 'show',
2046  'info' => 'Incorrect parameter - mutually exclusive values may not be supplied'
2047  ],
2048  'specialpage-cantexecute' => [
2049  'code' => 'specialpage-cantexecute',
2050  'info' => "You don't have permission to view the results of this special page"
2051  ],
2052  'invalidoldimage' => [
2053  'code' => 'invalidoldimage',
2054  'info' => 'The oldimage parameter has invalid format'
2055  ],
2056  'nodeleteablefile' => [
2057  'code' => 'nodeleteablefile',
2058  'info' => 'No such old version of the file'
2059  ],
2060  'fileexists-forbidden' => [
2061  'code' => 'fileexists-forbidden',
2062  'info' => 'A file with name "$1" already exists, and cannot be overwritten.'
2063  ],
2064  'fileexists-shared-forbidden' => [
2065  'code' => 'fileexists-shared-forbidden',
2066  'info' => 'A file with name "$1" already exists in the shared file repository, and cannot be overwritten.'
2067  ],
2068  'filerevert-badversion' => [
2069  'code' => 'filerevert-badversion',
2070  'info' => 'There is no previous local version of this file with the provided timestamp.'
2071  ],
2072 
2073  // ApiEditPage messages
2074  'noimageredirect-anon' => [
2075  'code' => 'noimageredirect-anon',
2076  'info' => "Anonymous users can't create image redirects"
2077  ],
2078  'noimageredirect-logged' => [
2079  'code' => 'noimageredirect',
2080  'info' => "You don't have permission to create image redirects"
2081  ],
2082  'spamdetected' => [
2083  'code' => 'spamdetected',
2084  'info' => "Your edit was refused because it contained a spam fragment: \"\$1\""
2085  ],
2086  'contenttoobig' => [
2087  'code' => 'contenttoobig',
2088  'info' => "The content you supplied exceeds the article size limit of \$1 kilobytes"
2089  ],
2090  'noedit-anon' => [ 'code' => 'noedit-anon', 'info' => "Anonymous users can't edit pages" ],
2091  'noedit' => [ 'code' => 'noedit', 'info' => "You don't have permission to edit pages" ],
2092  'wasdeleted' => [
2093  'code' => 'pagedeleted',
2094  'info' => 'The page has been deleted since you fetched its timestamp'
2095  ],
2096  'blankpage' => [
2097  'code' => 'emptypage',
2098  'info' => 'Creating new, empty pages is not allowed'
2099  ],
2100  'editconflict' => [ 'code' => 'editconflict', 'info' => 'Edit conflict detected' ],
2101  'hashcheckfailed' => [ 'code' => 'badmd5', 'info' => 'The supplied MD5 hash was incorrect' ],
2102  'missingtext' => [
2103  'code' => 'notext',
2104  'info' => 'One of the text, appendtext, prependtext and undo parameters must be set'
2105  ],
2106  'emptynewsection' => [
2107  'code' => 'emptynewsection',
2108  'info' => 'Creating empty new sections is not possible.'
2109  ],
2110  'revwrongpage' => [
2111  'code' => 'revwrongpage',
2112  'info' => "r\$1 is not a revision of \"\$2\""
2113  ],
2114  'undo-failure' => [
2115  'code' => 'undofailure',
2116  'info' => 'Undo failed due to conflicting intermediate edits'
2117  ],
2118  'content-not-allowed-here' => [
2119  'code' => 'contentnotallowedhere',
2120  'info' => 'Content model "$1" is not allowed at title "$2"'
2121  ],
2122 
2123  // Messages from WikiPage::doEit(]
2124  'edit-hook-aborted' => [
2125  'code' => 'edit-hook-aborted',
2126  'info' => 'Your edit was aborted by an ArticleSave hook'
2127  ],
2128  'edit-gone-missing' => [
2129  'code' => 'edit-gone-missing',
2130  'info' => "The page you tried to edit doesn't seem to exist anymore"
2131  ],
2132  'edit-conflict' => [ 'code' => 'editconflict', 'info' => 'Edit conflict detected' ],
2133  'edit-already-exists' => [
2134  'code' => 'edit-already-exists',
2135  'info' => 'It seems the page you tried to create already exist'
2136  ],
2137 
2138  // uploadMsgs
2139  'invalid-file-key' => [ 'code' => 'invalid-file-key', 'info' => 'Not a valid file key' ],
2140  'nouploadmodule' => [ 'code' => 'nouploadmodule', 'info' => 'No upload module set' ],
2141  'uploaddisabled' => [
2142  'code' => 'uploaddisabled',
2143  'info' => 'Uploads are not enabled. Make sure $wgEnableUploads is set to true in LocalSettings.php and the PHP ini setting file_uploads is true'
2144  ],
2145  'copyuploaddisabled' => [
2146  'code' => 'copyuploaddisabled',
2147  'info' => 'Uploads by URL is not enabled. Make sure $wgAllowCopyUploads is set to true in LocalSettings.php.'
2148  ],
2149  'copyuploadbaddomain' => [
2150  'code' => 'copyuploadbaddomain',
2151  'info' => 'Uploads by URL are not allowed from this domain.'
2152  ],
2153  'copyuploadbadurl' => [
2154  'code' => 'copyuploadbadurl',
2155  'info' => 'Upload not allowed from this URL.'
2156  ],
2157 
2158  'filename-tooshort' => [
2159  'code' => 'filename-tooshort',
2160  'info' => 'The filename is too short'
2161  ],
2162  'filename-toolong' => [ 'code' => 'filename-toolong', 'info' => 'The filename is too long' ],
2163  'illegal-filename' => [
2164  'code' => 'illegal-filename',
2165  'info' => 'The filename is not allowed'
2166  ],
2167  'filetype-missing' => [
2168  'code' => 'filetype-missing',
2169  'info' => 'The file is missing an extension'
2170  ],
2171 
2172  'mustbeloggedin' => [ 'code' => 'mustbeloggedin', 'info' => 'You must be logged in to $1.' ]
2173  ];
2174  // @codingStandardsIgnoreEnd
2175 
2181  public function dieReadOnly() {
2182  $parsed = $this->parseMsg( [ 'readonlytext' ] );
2183  $this->dieUsage( $parsed['info'], $parsed['code'], /* http error */ 0,
2184  [ 'readonlyreason' => wfReadOnlyReason() ] );
2185  }
2186 
2192  public function dieUsageMsg( $error ) {
2193  # most of the time we send a 1 element, so we might as well send it as
2194  # a string and make this an array here.
2195  if ( is_string( $error ) ) {
2196  $error = [ $error ];
2197  }
2198  $parsed = $this->parseMsg( $error );
2199  $extraData = isset( $parsed['data'] ) ? $parsed['data'] : null;
2200  $this->dieUsage( $parsed['info'], $parsed['code'], 0, $extraData );
2201  }
2202 
2210  public function dieUsageMsgOrDebug( $error ) {
2211  if ( $this->getConfig()->get( 'DebugAPI' ) !== true ) {
2212  $this->dieUsageMsg( $error );
2213  }
2214 
2215  if ( is_string( $error ) ) {
2216  $error = [ $error ];
2217  }
2218  $parsed = $this->parseMsg( $error );
2219  $this->setWarning( '$wgDebugAPI: ' . $parsed['code'] . ' - ' . $parsed['info'] );
2220  }
2221 
2229  protected function dieContinueUsageIf( $condition ) {
2230  if ( $condition ) {
2231  $this->dieUsage(
2232  'Invalid continue param. You should pass the original value returned by the previous query',
2233  'badcontinue' );
2234  }
2235  }
2236 
2242  public function parseMsg( $error ) {
2243  // Check whether someone passed the whole array, instead of one element as
2244  // documented. This breaks if it's actually an array of fallback keys, but
2245  // that's long-standing misbehavior introduced in r87627 to incorrectly
2246  // fix T30797.
2247  if ( is_array( $error ) ) {
2248  $first = reset( $error );
2249  if ( is_array( $first ) ) {
2250  wfDebug( __METHOD__ . ' was passed an array of arrays. ' . wfGetAllCallers( 5 ) );
2251  $error = $first;
2252  }
2253  }
2254 
2255  $msg = Message::newFromSpecifier( $error );
2256 
2257  if ( $msg instanceof IApiMessage ) {
2258  return [
2259  'code' => $msg->getApiCode(),
2260  'info' => $msg->inLanguage( 'en' )->useDatabase( false )->text(),
2261  'data' => $msg->getApiData()
2262  ];
2263  }
2264 
2265  $key = $msg->getKey();
2266  if ( isset( self::$messageMap[$key] ) ) {
2267  $params = $msg->getParams();
2268  return [
2269  'code' => wfMsgReplaceArgs( self::$messageMap[$key]['code'], $params ),
2270  'info' => wfMsgReplaceArgs( self::$messageMap[$key]['info'], $params )
2271  ];
2272  }
2273 
2274  // If the key isn't present, throw an "unknown error"
2275  return $this->parseMsg( [ 'unknownerror', $key ] );
2276  }
2277 
2284  protected static function dieDebug( $method, $message ) {
2285  throw new MWException( "Internal error in $method: $message" );
2286  }
2287 
2293  public function logFeatureUsage( $feature ) {
2294  $request = $this->getRequest();
2295  $s = '"' . addslashes( $feature ) . '"' .
2296  ' "' . wfUrlencode( str_replace( ' ', '_', $this->getUser()->getName() ) ) . '"' .
2297  ' "' . $request->getIP() . '"' .
2298  ' "' . addslashes( $request->getHeader( 'Referer' ) ) . '"' .
2299  ' "' . addslashes( $this->getMain()->getUserAgent() ) . '"';
2300  wfDebugLog( 'api-feature-usage', $s, 'private' );
2301  }
2302 
2305  /************************************************************************/
2315  protected function getDescriptionMessage() {
2316  return "apihelp-{$this->getModulePath()}-description";
2317  }
2318 
2326  public function getFinalDescription() {
2327  $desc = $this->getDescription();
2328  Hooks::run( 'APIGetDescription', [ &$this, &$desc ] );
2329  $desc = self::escapeWikiText( $desc );
2330  if ( is_array( $desc ) ) {
2331  $desc = implode( "\n", $desc );
2332  } else {
2333  $desc = (string)$desc;
2334  }
2335 
2336  $msg = ApiBase::makeMessage( $this->getDescriptionMessage(), $this->getContext(), [
2337  $this->getModulePrefix(),
2338  $this->getModuleName(),
2339  $this->getModulePath(),
2340  ] );
2341  if ( !$msg->exists() ) {
2342  $msg = $this->msg( 'api-help-fallback-description', $desc );
2343  }
2344  $msgs = [ $msg ];
2345 
2346  Hooks::run( 'APIGetDescriptionMessages', [ $this, &$msgs ] );
2347 
2348  return $msgs;
2349  }
2350 
2359  public function getFinalParams( $flags = 0 ) {
2360  $params = $this->getAllowedParams( $flags );
2361  if ( !$params ) {
2362  $params = [];
2363  }
2364 
2365  if ( $this->needsToken() ) {
2366  $params['token'] = [
2367  ApiBase::PARAM_TYPE => 'string',
2368  ApiBase::PARAM_REQUIRED => true,
2370  'api-help-param-token',
2371  $this->needsToken(),
2372  ],
2373  ] + ( isset( $params['token'] ) ? $params['token'] : [] );
2374  }
2375 
2376  Hooks::run( 'APIGetAllowedParams', [ &$this, &$params, $flags ] );
2377 
2378  return $params;
2379  }
2380 
2388  public function getFinalParamDescription() {
2389  $prefix = $this->getModulePrefix();
2390  $name = $this->getModuleName();
2391  $path = $this->getModulePath();
2392 
2393  $desc = $this->getParamDescription();
2394  Hooks::run( 'APIGetParamDescription', [ &$this, &$desc ] );
2395 
2396  if ( !$desc ) {
2397  $desc = [];
2398  }
2399  $desc = self::escapeWikiText( $desc );
2400 
2402  $msgs = [];
2403  foreach ( $params as $param => $settings ) {
2404  if ( !is_array( $settings ) ) {
2405  $settings = [];
2406  }
2407 
2408  $d = isset( $desc[$param] ) ? $desc[$param] : '';
2409  if ( is_array( $d ) ) {
2410  // Special handling for prop parameters
2411  $d = array_map( function ( $line ) {
2412  if ( preg_match( '/^\s+(\S+)\s+-\s+(.+)$/', $line, $m ) ) {
2413  $line = "\n;{$m[1]}:{$m[2]}";
2414  }
2415  return $line;
2416  }, $d );
2417  $d = implode( ' ', $d );
2418  }
2419 
2420  if ( isset( $settings[ApiBase::PARAM_HELP_MSG] ) ) {
2421  $msg = $settings[ApiBase::PARAM_HELP_MSG];
2422  } else {
2423  $msg = $this->msg( "apihelp-{$path}-param-{$param}" );
2424  if ( !$msg->exists() ) {
2425  $msg = $this->msg( 'api-help-fallback-parameter', $d );
2426  }
2427  }
2428  $msg = ApiBase::makeMessage( $msg, $this->getContext(),
2429  [ $prefix, $param, $name, $path ] );
2430  if ( !$msg ) {
2431  self::dieDebug( __METHOD__,
2432  'Value in ApiBase::PARAM_HELP_MSG is not valid' );
2433  }
2434  $msgs[$param] = [ $msg ];
2435 
2436  if ( isset( $settings[ApiBase::PARAM_HELP_MSG_PER_VALUE] ) ) {
2437  if ( !is_array( $settings[ApiBase::PARAM_HELP_MSG_PER_VALUE] ) ) {
2438  self::dieDebug( __METHOD__,
2439  'ApiBase::PARAM_HELP_MSG_PER_VALUE is not valid' );
2440  }
2441  if ( !is_array( $settings[ApiBase::PARAM_TYPE] ) ) {
2442  self::dieDebug( __METHOD__,
2443  'ApiBase::PARAM_HELP_MSG_PER_VALUE may only be used when ' .
2444  'ApiBase::PARAM_TYPE is an array' );
2445  }
2446 
2447  $valueMsgs = $settings[ApiBase::PARAM_HELP_MSG_PER_VALUE];
2448  foreach ( $settings[ApiBase::PARAM_TYPE] as $value ) {
2449  if ( isset( $valueMsgs[$value] ) ) {
2450  $msg = $valueMsgs[$value];
2451  } else {
2452  $msg = "apihelp-{$path}-paramvalue-{$param}-{$value}";
2453  }
2454  $m = ApiBase::makeMessage( $msg, $this->getContext(),
2455  [ $prefix, $param, $name, $path, $value ] );
2456  if ( $m ) {
2457  $m = new ApiHelpParamValueMessage(
2458  $value,
2459  [ $m->getKey(), 'api-help-param-no-description' ],
2460  $m->getParams()
2461  );
2462  $msgs[$param][] = $m->setContext( $this->getContext() );
2463  } else {
2464  self::dieDebug( __METHOD__,
2465  "Value in ApiBase::PARAM_HELP_MSG_PER_VALUE for $value is not valid" );
2466  }
2467  }
2468  }
2469 
2470  if ( isset( $settings[ApiBase::PARAM_HELP_MSG_APPEND] ) ) {
2471  if ( !is_array( $settings[ApiBase::PARAM_HELP_MSG_APPEND] ) ) {
2472  self::dieDebug( __METHOD__,
2473  'Value for ApiBase::PARAM_HELP_MSG_APPEND is not an array' );
2474  }
2475  foreach ( $settings[ApiBase::PARAM_HELP_MSG_APPEND] as $m ) {
2476  $m = ApiBase::makeMessage( $m, $this->getContext(),
2477  [ $prefix, $param, $name, $path ] );
2478  if ( $m ) {
2479  $msgs[$param][] = $m;
2480  } else {
2481  self::dieDebug( __METHOD__,
2482  'Value in ApiBase::PARAM_HELP_MSG_APPEND is not valid' );
2483  }
2484  }
2485  }
2486  }
2487 
2488  Hooks::run( 'APIGetParamDescriptionMessages', [ $this, &$msgs ] );
2489 
2490  return $msgs;
2491  }
2492 
2502  protected function getHelpFlags() {
2503  $flags = [];
2504 
2505  if ( $this->isDeprecated() ) {
2506  $flags[] = 'deprecated';
2507  }
2508  if ( $this->isInternal() ) {
2509  $flags[] = 'internal';
2510  }
2511  if ( $this->isReadMode() ) {
2512  $flags[] = 'readrights';
2513  }
2514  if ( $this->isWriteMode() ) {
2515  $flags[] = 'writerights';
2516  }
2517  if ( $this->mustBePosted() ) {
2518  $flags[] = 'mustbeposted';
2519  }
2520 
2521  return $flags;
2522  }
2523 
2535  protected function getModuleSourceInfo() {
2536  global $IP;
2537 
2538  if ( $this->mModuleSource !== false ) {
2539  return $this->mModuleSource;
2540  }
2541 
2542  // First, try to find where the module comes from...
2543  $rClass = new ReflectionClass( $this );
2544  $path = $rClass->getFileName();
2545  if ( !$path ) {
2546  // No path known?
2547  $this->mModuleSource = null;
2548  return null;
2549  }
2550  $path = realpath( $path ) ?: $path;
2551 
2552  // Build map of extension directories to extension info
2553  if ( self::$extensionInfo === null ) {
2554  $extDir = $this->getConfig()->get( 'ExtensionDirectory' );
2555  self::$extensionInfo = [
2556  realpath( __DIR__ ) ?: __DIR__ => [
2557  'path' => $IP,
2558  'name' => 'MediaWiki',
2559  'license-name' => 'GPL-2.0+',
2560  ],
2561  realpath( "$IP/extensions" ) ?: "$IP/extensions" => null,
2562  realpath( $extDir ) ?: $extDir => null,
2563  ];
2564  $keep = [
2565  'path' => null,
2566  'name' => null,
2567  'namemsg' => null,
2568  'license-name' => null,
2569  ];
2570  foreach ( $this->getConfig()->get( 'ExtensionCredits' ) as $group ) {
2571  foreach ( $group as $ext ) {
2572  if ( !isset( $ext['path'] ) || !isset( $ext['name'] ) ) {
2573  // This shouldn't happen, but does anyway.
2574  continue;
2575  }
2576 
2577  $extpath = $ext['path'];
2578  if ( !is_dir( $extpath ) ) {
2579  $extpath = dirname( $extpath );
2580  }
2581  self::$extensionInfo[realpath( $extpath ) ?: $extpath] =
2582  array_intersect_key( $ext, $keep );
2583  }
2584  }
2585  foreach ( ExtensionRegistry::getInstance()->getAllThings() as $ext ) {
2586  $extpath = $ext['path'];
2587  if ( !is_dir( $extpath ) ) {
2588  $extpath = dirname( $extpath );
2589  }
2590  self::$extensionInfo[realpath( $extpath ) ?: $extpath] =
2591  array_intersect_key( $ext, $keep );
2592  }
2593  }
2594 
2595  // Now traverse parent directories until we find a match or run out of
2596  // parents.
2597  do {
2598  if ( array_key_exists( $path, self::$extensionInfo ) ) {
2599  // Found it!
2600  $this->mModuleSource = self::$extensionInfo[$path];
2601  return $this->mModuleSource;
2602  }
2603 
2604  $oldpath = $path;
2605  $path = dirname( $path );
2606  } while ( $path !== $oldpath );
2607 
2608  // No idea what extension this might be.
2609  $this->mModuleSource = null;
2610  return null;
2611  }
2612 
2624  public function modifyHelp( array &$help, array $options, array &$tocData ) {
2625  }
2626 
2629  /************************************************************************/
2643  protected function getDescription() {
2644  return false;
2645  }
2646 
2659  protected function getParamDescription() {
2660  return [];
2661  }
2662 
2679  protected function getExamples() {
2680  return false;
2681  }
2682 
2688  public function getModuleProfileName( $db = false ) {
2689  wfDeprecated( __METHOD__, '1.25' );
2690  return '';
2691  }
2692 
2696  public function profileIn() {
2697  // No wfDeprecated() yet because extensions call this and might need to
2698  // keep doing so for BC.
2699  }
2700 
2704  public function profileOut() {
2705  // No wfDeprecated() yet because extensions call this and might need to
2706  // keep doing so for BC.
2707  }
2708 
2712  public function safeProfileOut() {
2713  wfDeprecated( __METHOD__, '1.25' );
2714  }
2715 
2720  public function getProfileTime() {
2721  wfDeprecated( __METHOD__, '1.25' );
2722  return 0;
2723  }
2724 
2728  public function profileDBIn() {
2729  wfDeprecated( __METHOD__, '1.25' );
2730  }
2731 
2735  public function profileDBOut() {
2736  wfDeprecated( __METHOD__, '1.25' );
2737  }
2738 
2743  public function getProfileDBTime() {
2744  wfDeprecated( __METHOD__, '1.25' );
2745  return 0;
2746  }
2747 
2752  protected function useTransactionalTimeLimit() {
2753  if ( $this->getRequest()->wasPosted() ) {
2755  }
2756  }
2757 
2759 }
2760 
static newFromName($name, $validate= 'valid')
Static factory method for creation from username.
Definition: User.php:525
dieUsageMsgOrDebug($error)
Will only set a warning instead of failing if the global $wgDebugAPI is set to true.
Definition: ApiBase.php:2210
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:115
setContext(IContextSource $context)
Set the IContextSource object.
const PARAM_VALUE_LINKS
(string[]) When PARAM_TYPE is an array, this may be an array mapping those values to page titles whic...
Definition: ApiBase.php:149
getFinalParamDescription()
Get final parameter descriptions, after hooks have had a chance to tweak it as needed.
Definition: ApiBase.php:2388
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below...
Definition: ApiBase.php:88
getErrorFormatter()
Get the error formatter.
Definition: ApiBase.php:591
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:179
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition: deferred.txt:11
Interface for objects which can provide a MediaWiki context on request.
wfGetDB($db, $groups=[], $wiki=false)
Get a Database object.
profileDBOut()
Definition: ApiBase.php:2735
isReadMode()
Indicates whether this module requires read rights.
Definition: ApiBase.php:356
the array() calling protocol came about after MediaWiki 1.4rc1.
validateTimestamp($value, $encParamName)
Validate and normalize of parameters of type 'timestamp'.
Definition: ApiBase.php:1324
getResult()
Get the result object.
Definition: ApiBase.php:577
static $messageMap
Array that maps message keys to error messages.
Definition: ApiBase.php:1673
getWatchlistUser($params)
Gets the user for whom to get the watchlist.
Definition: ApiBase.php:1454
Message subclass that prepends wikitext for API help.
getParameter($paramName, $parseLimit=true)
Get a value for the given parameter.
Definition: ApiBase.php:702
explodeMultiValue($value, $limit)
Split a multi-valued parameter string, like explode()
Definition: ApiBase.php:1196
getDescriptionMessage()
Return the description message.
Definition: ApiBase.php:2315
getModuleProfileName($db=false)
Definition: ApiBase.php:2688
getModuleSourceInfo()
Returns information about the source of this module, if known.
Definition: ApiBase.php:2535
$IP
Definition: WebStart.php:58
getCustomPrinter()
If the module may only be used with a certain format module, it should override this method to return...
Definition: ApiBase.php:260
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:1936
static array $extensionInfo
Maps extension paths to info arrays.
Definition: ApiBase.php:193
requireMaxOneParameter($params, $required)
Die if more than one of a certain set of parameters is set and not false.
Definition: ApiBase.php:740
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:2752
getMain()
Get the main module.
Definition: ApiBase.php:473
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:50
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
static escapeWikiText($v)
A subset of wfEscapeWikiText for BC texts.
Definition: ApiBase.php:1487
getType()
Get the type of target for this particular block.
Definition: Block.php:1360
const GET_VALUES_FOR_HELP
getAllowedParams() flag: When set, the result could take longer to generate, but should be more thoro...
Definition: ApiBase.php:190
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:177
safeProfileOut()
Definition: ApiBase.php:2712
getDB()
Gets a default replica DB connection object.
Definition: ApiBase.php:605
const PARAM_MAX
(integer) Max value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition: ApiBase.php:91
setWatch($watch, $titleObj, $userOption=null)
Set a watch (or unwatch) based the based on a watchlist parameter.
Definition: ApiBase.php:1423
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition: ApiBase.php:112
ApiMain $mMainModule
Definition: ApiBase.php:196
getWatchlistValue($watchlist, $titleObj, $userOption=null)
Return true if we're to watch the page, false if not, null if no change.
Definition: ApiBase.php:870
const PARAM_HELP_MSG_INFO
(array) Specify additional information tags for the parameter.
Definition: ApiBase.php:142
lacksSameOriginSecurity()
Returns true if the current request breaks the same-origin policy.
Definition: ApiBase.php:505
extractRequestParams($parseLimit=true)
Using getAllowedParams(), this function makes an array of the values provided by the user...
Definition: ApiBase.php:678
This code would result in ircNotify being run twice when an article is and once for brion Hooks can return three possible true was required This is the default since MediaWiki *some string
Definition: hooks.txt:177
$value
getParent()
Get the parent of this module.
Definition: ApiBase.php:491
requireOnlyOneParameter($params, $required)
Die if none or more than one of a certain set of parameters is set and not false. ...
Definition: ApiBase.php:714
static makeMessage($msg, IContextSource $context, array $params=null)
Create a Message from a string or array.
Definition: ApiBase.php:1511
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition: hooks.txt:2703
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, this is an array mapping those values to $msg...
Definition: ApiBase.php:157
Interface for messages with machine-readable data for use by the API.
Definition: ApiMessage.php:35
wfUrlencode($s)
We want some things to be included as literal characters in our title URLs for prettiness, which urlencode encodes by default.
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:262
isDeprecated()
Indicates whether this module is deprecated.
Definition: ApiBase.php:381
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiBase.php:322
string $mModuleName
Definition: ApiBase.php:198
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiBase.php:413
IContextSource $context
getConditionalRequestData($condition)
Returns data for HTTP conditional request mechanisms.
Definition: ApiBase.php:442
getParameterFromSettings($paramName, $paramSettings, $parseLimit)
Using the settings determine the value for the given parameter.
Definition: ApiBase.php:912
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
static getBlockInfo(Block $block)
Get basic info about a given block.
const TS_UNIX
Unix time - the number of seconds since 1970-01-01 00:00:00 UTC.
Definition: defines.php:6
wfTimestamp($outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
getRequest()
Get the WebRequest object.
wfMsgReplaceArgs($message, $args)
Replace message parameter keys on the given formatted output.
const PARAM_HELP_MSG_APPEND
((string|array|Message)[]) Specify additional i18n messages to append to the normal message for this ...
Definition: ApiBase.php:132
wfDebugLog($logGroup, $text, $dest= 'all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not...
getTitleOrPageId($params, $load=false)
Get a WikiPage object from a title or pageid param, if possible.
Definition: ApiBase.php:833
msg()
Get a Message object with context set Parameters are the same as wfMessage()
const PARAM_SUBMODULE_PARAM_PREFIX
(string) When PARAM_TYPE is 'submodule', used to indicate the 'g' prefix added by ApiQueryGeneratorBa...
Definition: ApiBase.php:172
static truncateArray(&$arr, $limit)
Truncate an array to a certain length.
Definition: ApiBase.php:1438
profileOut()
Definition: ApiBase.php:2704
validateToken($token, array $params)
Validate the supplied token.
Definition: ApiBase.php:1363
parameterNotEmpty($x)
Callback function used in requireOnlyOneParameter to check whether required parameters are set...
Definition: ApiBase.php:818
getProfileTime()
Definition: ApiBase.php:2720
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock()-offset Set to overwrite offset parameter in $wgRequest set to ''to unsetoffset-wrap String Wrap the message in html(usually something like"&lt
getErrorFromStatus($status, &$extraData=null)
Get error (as code, string) from a Status object.
Definition: ApiBase.php:1619
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context $options
Definition: hooks.txt:1046
getHelpFlags()
Generates the list of flags for the help screen and for action=paraminfo.
Definition: ApiBase.php:2502
const PARAM_RANGE_ENFORCE
(boolean) For PARAM_TYPE 'integer', enforce PARAM_MIN and PARAM_MAX?
Definition: ApiBase.php:118
getProfileDBTime()
Definition: ApiBase.php:2743
getModulePath()
Get the path to this module.
Definition: ApiBase.php:521
getContinuationManager()
Get the continuation manager.
Definition: ApiBase.php:617
getConfig()
Get the Config object.
const LIMIT_SML2
Slow query, apihighlimits limit.
Definition: ApiBase.php:183
const PARAM_SUBMODULE_MAP
(string[]) When PARAM_TYPE is 'submodule', map parameter values to submodule paths.
Definition: ApiBase.php:165
getContext()
Get the base IContextSource object.
const IGNORE_USER_RIGHTS
Definition: User.php:85
$params
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:43
validateUser($value, $encParamName)
Validate and normalize of parameters of type 'user'.
Definition: ApiBase.php:1398
wfDeprecated($function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
const TS_MW
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
Definition: defines.php:11
isInternal()
Indicates whether this module is "internal" Internal API modules are not (yet) intended for 3rd party...
Definition: ApiBase.php:391
static makeTitleSafe($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:535
setContext(IContextSource $context)
Set the language and the title from a context object.
Definition: Message.php:679
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:953
dynamicParameterDocumentation()
Indicate if the module supports dynamically-determined parameters that cannot be included in self::ge...
Definition: ApiBase.php:655
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:457
static dieReadOnly()
Helper function for readonly errors.
Definition: ApiBase.php:2181
static run($event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
$help
Definition: mcc.php:32
const PARAM_MAX2
(integer) Max value allowed for the parameter for users with the apihighlimits right, for PARAM_TYPE 'limit'.
Definition: ApiBase.php:97
string $mModulePrefix
Definition: ApiBase.php:198
const TYPE_AUTO
Definition: Block.php:81
setWarning($warning)
Set warning section for this module.
Definition: ApiBase.php:1543
modifyHelp(array &$help, array $options, array &$tocData)
Called from ApiHelp before the pieces are joined together and returned.
Definition: ApiBase.php:2624
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
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:802
dieContinueUsageIf($condition)
Die with the $prefix.
Definition: ApiBase.php:2229
requirePostedParameters($params, $prefix= 'prefix')
Die if any of the specified parameters were found in the query part of the URL rather than the post b...
Definition: ApiBase.php:786
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter...
Definition: ApiBase.php:125
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 local account $user
Definition: hooks.txt:242
handleParamNormalization($paramName, $value, $rawValue)
Handle when a parameter was Unicode-normalized.
Definition: ApiBase.php:1180
const LIMIT_SML1
Slow query, standard limit.
Definition: ApiBase.php:181
getModuleManager()
Get the module manager, or null if this module has no sub-modules.
Definition: ApiBase.php:247
wfGetAllCallers($limit=3)
Return a string consisting of callers in the stack.
static getTokenTypeSalts()
Get the salts for known token types.
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
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiBase.php:372
error also a ContextSource you ll probably need to make sure the header is varied on $request
Definition: hooks.txt:2573
wfReadOnlyReason()
Check if the site is in read-only mode and return the message if so.
getModulePrefix()
Get parameter prefix (usually two letters or an empty string).
Definition: ApiBase.php:465
validateLimit($paramName, &$value, $min, $max, $botMax=null, $enforceLimits=false)
Validate the value against the minimum and user/bot maximum limits.
Definition: ApiBase.php:1285
getDescription()
Returns the description string for this module.
Definition: ApiBase.php:2643
parseMultiValue($valueName, $value, $allowMultiple, $allowedValues)
Return an array of values that were given in a 'a|b|c' notation, after it optionally validates them a...
Definition: ApiBase.php:1220
static getToken(User $user, MediaWiki\Session\Session $session, $salt)
Get a token from a salt.
getFinalDescription()
Get final module description, after hooks have had a chance to tweak it as needed.
Definition: ApiBase.php:2326
$line
Definition: cdb.php:59
requireAtLeastOneParameter($params, $required)
Die if none of a certain set of parameters is set and not false.
Definition: ApiBase.php:763
const PARAM_ISMULTI
(boolean) Accept multiple pipe-separated values for this parameter (e.g.
Definition: ApiBase.php:53
__construct(ApiMain $mainModule, $moduleName, $modulePrefix= '')
Definition: ApiBase.php:209
wfTransactionalTimeLimit()
Set PHP's time limit to the larger of php.ini or $wgTransactionalTimeLimit.
dieUsage($description, $errorCode, $httpRespCode=0, $extradata=null)
Throw a UsageException, which will (if uncaught) call the main module's error handler and die with an...
Definition: ApiBase.php:1574
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object to manipulate or replace but no entry for that model exists in $wgContentHandlers if desired whether it is OK to use $contentModel on $title Handler functions that modify $ok should generally return false to prevent further hooks from further modifying $ok inclusive $limit
Definition: hooks.txt:1046
getExamples()
Returns usage examples for this module.
Definition: ApiBase.php:2679
profileIn()
Definition: ApiBase.php:2696
dieBlocked(Block $block)
Throw a UsageException, which will (if uncaught) call the main module's error handler and die with an...
Definition: ApiBase.php:1591
$mParamCache
Definition: ApiBase.php:200
static newFromID($id, $from= 'fromdb')
Constructor from a page id.
Definition: WikiPage.php:153
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1046
Extension of RawMessage implementing IApiMessage.
Definition: ApiMessage.php:171
This abstract class implements many basic API functions, and is the base of all API classes...
Definition: ApiBase.php:39
$count
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
getWebUITokenSalt(array $params)
Fetch the salt used in the Web UI corresponding to this module.
Definition: ApiBase.php:426
const PARAM_DEPRECATED
(boolean) Is the parameter deprecated (will show a warning)?
Definition: ApiBase.php:106
array null bool $mModuleSource
Definition: ApiBase.php:202
const DB_REPLICA
Definition: defines.php:22
getParamDescription()
Returns an array of parameter descriptions.
Definition: ApiBase.php:2659
static canAddTagsAccompanyingChange(array $tags, User $user=null)
Is it OK to allow the user to apply all the specified tags at the same time as they edit/make the cha...
Definition: ChangeTags.php:392
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiBase.php:275
parseMsg($error)
Return the error message related to a certain array.
Definition: ApiBase.php:2242
profileDBIn()
Definition: ApiBase.php:2728
const PARAM_MIN
(integer) Lowest value allowed for the parameter, for PARAM_TYPE 'integer' and 'limit'.
Definition: ApiBase.php:100
static dieDebug($method, $message)
Internal code errors should be reported with this method.
Definition: ApiBase.php:2284
logFeatureUsage($feature)
Write logging information for API features to a debug log, for usage analysis.
Definition: ApiBase.php:2293
static doWatchOrUnwatch($watch, Title $title, User $user)
Watch or unwatch a page.
Definition: WatchAction.php:84
dieStatus($status)
Throw a UsageException based on the errors in the Status object.
Definition: ApiBase.php:1663
shouldCheckMaxlag()
Indicates if this module needs maxlag to be checked.
Definition: ApiBase.php:348
static getValidNamespaces()
Returns an array of the namespaces (by integer id) that exist on the wiki.
encodeParamName($paramName)
This method mangles parameter name based on the prefix supplied to the constructor.
Definition: ApiBase.php:665
setContinuationManager($manager)
Set the continuation manager.
Definition: ApiBase.php:631
warnOrDie($msg, $enforceLimits=false)
Adds a warning to the output, else dies.
Definition: ApiBase.php:1554
Definition: Block.php:25
getUser()
Get the User object.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition: hooks.txt:2491
dieUsageMsg($error)
Output the error message related to a certain array.
Definition: ApiBase.php:2192
const PARAM_ALLOW_DUPLICATES
(boolean) Allow the same value to be set more than once when PARAM_ISMULTI is true?
Definition: ApiBase.php:103
getModuleFromPath($path)
Get a module from its module path.
Definition: ApiBase.php:539
This exception will be thrown when dieUsage is called to stop module execution.
Definition: ApiMain.php:1837
getFinalParams($flags=0)
Get final list of parameters, after hooks have had a chance to tweak it as needed.
Definition: ApiBase.php:2359
isMain()
Returns true if this module is the main module ($this === $this->mMainModule), false otherwise...
Definition: ApiBase.php:482
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiBase.php:364
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:300
static newFromSpecifier($value)
Transform a MessageSpecifier or a primitive value used interchangeably with specifiers (a message key...
Definition: Message.php:398
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiBase.php:338