| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\User\TempUser; |
| 4 |
|
| 5 |
use Stringable; |
| 6 |
use UnexpectedValueException; |
| 7 |
use Wikimedia\Rdbms\LikeValue; |
| 8 |
use Wikimedia\Rdbms\Platform\ISQLPlatform; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class Pattern implements Stringable { |
| 17 |
|
| 18 |
private $debugName; |
| 19 |
|
| 20 |
private $pattern; |
| 21 |
|
| 22 |
private $prefix; |
| 23 |
|
| 24 |
private $suffix; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
public function __construct( string $debugName, string $pattern ) { |
| 31 |
$this->debugName = $debugName; |
| 32 |
$this->pattern = $pattern; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public function isMatch( string $name ) { |
| 41 |
$this->init(); |
| 42 |
$match = true; |
| 43 |
if ( $this->prefix !== '' ) { |
| 44 |
$match = str_starts_with( $name, $this->prefix ); |
| 45 |
} |
| 46 |
if ( $match && $this->suffix !== '' ) { |
| 47 |
$match = str_ends_with( $name, $this->suffix ) |
| 48 |
&& strlen( $name ) >= strlen( $this->prefix ) + strlen( $this->suffix ); |
| 49 |
} |
| 50 |
return $match; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
public function generate( $mappedSerial, ?string $year = null ) { |
| 61 |
$this->init(); |
| 62 |
return $this->prefix . |
| 63 |
( $year ? $year . '-' : '' ) . |
| 64 |
$mappedSerial . |
| 65 |
$this->suffix; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public function buildLike( ISQLPlatform $db ) { |
| 76 |
wfDeprecated( __METHOD__, '1.42' ); |
| 77 |
$this->init(); |
| 78 |
return $db->buildLike( |
| 79 |
$this->prefix, |
| 80 |
$db->anyString(), |
| 81 |
$this->suffix |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public function toLikeValue( ISQLPlatform $db ): LikeValue { |
| 92 |
$this->init(); |
| 93 |
return new LikeValue( |
| 94 |
$this->prefix, |
| 95 |
$db->anyString(), |
| 96 |
$this->suffix |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public function extract( string $name ) { |
| 108 |
if ( $this->isMatch( $name ) ) { |
| 109 |
return substr( $name, |
| 110 |
strlen( $this->prefix ), |
| 111 |
strlen( $name ) - strlen( $this->prefix ) - strlen( $this->suffix ) ); |
| 112 |
} |
| 113 |
return null; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
private function init() { |
| 120 |
if ( $this->prefix === null ) { |
| 121 |
$varPos = strpos( $this->pattern, '$1' ); |
| 122 |
if ( $varPos === false ) { |
| 123 |
throw new UnexpectedValueException( __CLASS__ . |
| 124 |
"pattern {$this->debugName} must be of the form \"prefix \$1 suffix\"" ); |
| 125 |
} |
| 126 |
$this->prefix = substr( $this->pattern, 0, $varPos ); |
| 127 |
$this->suffix = substr( $this->pattern, $varPos + strlen( '$1' ) ); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
public function __toString() { |
| 132 |
return $this->pattern; |
| 133 |
} |
| 134 |
} |