MediaWiki  master
ExpiryDef.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use InvalidArgumentException;
10 use Wikimedia\Timestamp\ConvertibleTimestamp;
11 
17 class ExpiryDef extends TypeDef {
18 
20  public const INFINITY_VALS = [ 'infinite', 'indefinite', 'infinity', 'never' ];
21 
28  public const PARAM_USE_MAX = 'param-use-max';
29 
33  public const PARAM_MAX = 'param-max';
34 
35  public function validate( $name, $value, array $settings, array $options ) {
36  try {
37  $expiry = self::normalizeExpiry( $value, TS_ISO_8601 );
38  } catch ( InvalidArgumentException $e ) {
39  $this->failure( 'badexpiry', $name, $value, $settings, $options );
40  }
41 
42  if ( $expiry !== 'infinity' && $expiry < ConvertibleTimestamp::now( TS_ISO_8601 ) ) {
43  $this->failure( 'badexpiry-past', $name, $value, $settings, $options );
44  }
45 
46  $max = $settings[self::PARAM_MAX] ?? null;
47 
48  if ( self::expiryExceedsMax( $expiry, $max ) ) {
49  $dontUseMax = empty( $settings[self::PARAM_USE_MAX] );
50  // Show warning that expiry exceeds the max, and that the max is being used instead.
51  $msg = DataMessageValue::new(
52  $dontUseMax
53  ? 'paramvalidator-badexpiry-duration'
54  : 'paramvalidator-badexpiry-duration-max',
55  [ $max ]
56  );
57  $this->failure( $msg, $name, $value, $settings, $options, $dontUseMax );
58 
59  return self::normalizeExpiry( $max, TS_ISO_8601 );
60  }
61 
62  return $expiry;
63  }
64 
65  public function getHelpInfo( $name, array $settings, array $options ) {
66  $info = parent::getHelpInfo( $name, $settings, $options );
67 
68  $info[ParamValidator::PARAM_TYPE] = MessageValue::new( 'paramvalidator-help-type-expiry' )
69  ->params( empty( $settings[ParamValidator::PARAM_ISMULTI] ) ? 1 : 2 )
70  ->textListParams(
71  // Should be quoted or monospace for presentation purposes,
72  // but textListParams() doesn't do this.
73  array_map( function ( $val ) {
74  return "\"$val\"";
75  }, self::INFINITY_VALS )
76  );
77 
78  return $info;
79  }
80 
92  public static function normalizeExpiry( ?string $expiry = null, ?int $style = null ) {
93  if ( $expiry === null ) {
94  return null;
95  }
96  if ( in_array( $expiry, self::INFINITY_VALS, true ) ) {
97  return 'infinity';
98  }
99 
100  // ConvertibleTimestamp::time() used so we can fake the current time in ExpiryDefTest.
101  $unix = strtotime( $expiry, ConvertibleTimestamp::time() );
102  if ( $unix === false ) {
103  // Invalid expiry.
104  throw new InvalidArgumentException( "Invalid expiry value: {$expiry}" );
105  }
106 
107  // Don't pass 0, since ConvertibleTimestamp interprets that to mean the current timestamp.
108  // '00' does the right thing. Without this check, calling normalizeExpiry()
109  // with 1970-01-01T00:00:00Z incorrectly returns the current time.
110  $expiryConvertibleTimestamp = new ConvertibleTimestamp( $unix === 0 ? '00' : $unix );
111 
112  if ( $style !== null ) {
113  return $expiryConvertibleTimestamp->getTimestamp( $style );
114  }
115 
116  return $expiryConvertibleTimestamp;
117  }
118 
128  public static function normalizeUsingMaxExpiry( ?string $expiry, ?string $maxExpiryDuration, ?int $style ) {
129  if ( self::expiryExceedsMax( $expiry, $maxExpiryDuration ) ) {
130  return self::normalizeExpiry( $maxExpiryDuration, $style );
131  }
132  return self::normalizeExpiry( $expiry, $style );
133  }
134 
142  private static function expiryExceedsMax( ?string $expiry, ?string $max = null ): bool {
143  $expiry = self::normalizeExpiry( $expiry );
144  $max = self::normalizeExpiry( $max );
145 
146  if ( !$max || !$expiry || $expiry === 'infinity' ) {
147  // Either there is no max or given expiry was invalid.
148  return false;
149  }
150 
151  return $expiry > $max;
152  }
153 
154 }
Wikimedia\ParamValidator\TypeDef\ExpiryDef\getHelpInfo
getHelpInfo( $name, array $settings, array $options)
Describe parameter settings in human-readable format.
Definition: ExpiryDef.php:65
Wikimedia\ParamValidator\TypeDef\failure
failure( $failure, $name, $value, array $settings, array $options, $fatal=true)
Record a failure message.
Definition: TypeDef.php:49
Wikimedia\ParamValidator\TypeDef\ExpiryDef\expiryExceedsMax
static expiryExceedsMax(?string $expiry, ?string $max=null)
Given an expiry, test if the normalized value exceeds the given maximum.
Definition: ExpiryDef.php:142
Wikimedia\ParamValidator\TypeDef\ExpiryDef\normalizeUsingMaxExpiry
static normalizeUsingMaxExpiry(?string $expiry, ?string $maxExpiryDuration, ?int $style)
Returns a normalized expiry or the max expiry if the given expiry exceeds it.
Definition: ExpiryDef.php:128
Wikimedia\Message\MessageValue
Value object representing a message for i18n.
Definition: MessageValue.php:16
Wikimedia\ParamValidator\TypeDef
Base definition for ParamValidator types.
Definition: TypeDef.php:19
Wikimedia\ParamValidator\TypeDef\ExpiryDef
Type definition for expiry timestamps.
Definition: ExpiryDef.php:17
Wikimedia\ParamValidator\TypeDef\ExpiryDef\PARAM_MAX
const PARAM_MAX
(int|float) Maximum non-infinity duration.
Definition: ExpiryDef.php:33
Wikimedia\ParamValidator\TypeDef\ExpiryDef\normalizeExpiry
static normalizeExpiry(?string $expiry=null, ?int $style=null)
Normalize a user-inputted expiry in ConvertibleTimestamp.
Definition: ExpiryDef.php:92
Wikimedia\ParamValidator\TypeDef\ExpiryDef\validate
validate( $name, $value, array $settings, array $options)
Validate the value.
Definition: ExpiryDef.php:35
Wikimedia\ParamValidator\ParamValidator\PARAM_ISMULTI
const PARAM_ISMULTI
(bool) Indicate that the parameter is multi-valued.
Definition: ParamValidator.php:112
Wikimedia\Message\DataMessageValue
Value object representing a message for i18n with alternative machine-readable data.
Definition: DataMessageValue.php:23
Wikimedia\Message\DataMessageValue\new
static new( $key, $params=[], $code=null, array $data=null)
Static constructor for easier chaining of ->params() methods.
Definition: DataMessageValue.php:55
Wikimedia\ParamValidator\TypeDef\ExpiryDef\PARAM_USE_MAX
const PARAM_USE_MAX
(bool) If truthy, the value given for the PARAM_MAX setting is used if the provided expiry exceeds it...
Definition: ExpiryDef.php:28
Wikimedia\ParamValidator\TypeDef
Definition: BooleanDef.php:3
Wikimedia\Message\MessageValue\new
static new( $key, $params=[])
Static constructor for easier chaining of ->params() methods.
Definition: MessageValue.php:42
Wikimedia\ParamValidator\ParamValidator\PARAM_TYPE
const PARAM_TYPE
(string|array) Type of the parameter.
Definition: ParamValidator.php:76
Wikimedia\ParamValidator\ParamValidator
Service for formatting and validating API parameters.
Definition: ParamValidator.php:42