$this->format = $format;
}
+ /**
+ * Test whether a code is a valid API error code
+ *
+ * A valid code contains only ASCII letters, numbers, underscore, and
+ * hyphen and is not the empty string.
+ *
+ * For backwards compatibility, any code beginning 'internal_api_error_' is
+ * also allowed.
+ *
+ * @param string $code
+ * @return bool
+ */
+ public static function isValidApiCode( $code ) {
+ return is_string( $code ) && (
+ preg_match( '/^[a-zA-Z0-9_-]+$/', $code ) ||
+ // TODO: Deprecate this
+ preg_match( '/^internal_api_error_[^\0\r\n]+$/', $code )
+ );
+ }
+
+ /**
+ * Return a formatter like this one but with a different format
+ *
+ * @since 1.32
+ * @param string $format New format.
+ * @return ApiErrorFormatter
+ */
+ public function newWithFormat( $format ) {
+ return new self( $this->result, $this->lang, $format, $this->useDB );
+ }
+
+ /**
+ * Fetch the format for this formatter
+ * @since 1.32
+ * @return string
+ */
+ public function getFormat() {
+ return $this->format;
+ }
+
/**
* Fetch the Language for this formatter
* @since 1.29
* @param string|null $modulePath
* @param StatusValue $status
* @param string[]|string $types 'warning' and/or 'error'
+ * @param string[] $filter Messages to filter out (since 1.33)
*/
public function addMessagesFromStatus(
- $modulePath, StatusValue $status, $types = [ 'warning', 'error' ]
+ $modulePath, StatusValue $status, $types = [ 'warning', 'error' ], array $filter = []
) {
if ( $status->isGood() || !$status->getErrors() ) {
return;
->inLanguage( $this->lang )
->title( $this->getDummyTitle() )
->useDatabase( $this->useDB );
- $this->addWarningOrError( $tag, $modulePath, $msg );
+ if ( !in_array( $msg->getKey(), $filter, true ) ) {
+ $this->addWarningOrError( $tag, $modulePath, $msg );
+ }
}
}
if ( !isset( $options['code'] ) ) {
$class = preg_replace( '#^Wikimedia\\\Rdbms\\\#', '', get_class( $exception ) );
$options['code'] = 'internal_api_error_' . $class;
+ $options['data']['errorclass'] = get_class( $exception );
}
}
$params = [ wfEscapeWikiText( $exception->getMessage() ) ];
parent::__construct( $result, Language::factory( 'en' ), 'none', false );
}
+ public function getFormat() {
+ return 'bc';
+ }
+
public function arrayFromStatus( StatusValue $status, $type = 'error', $format = null ) {
if ( $status->isGood() || !$status->getErrors() ) {
return [];