-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathIdProvider.php
65 lines (55 loc) · 1.5 KB
/
IdProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/**
* MediaWiki IDProvider Extension
*
* Provides (unique) IDs using different ID algorithms.
*
* @link https://github.com/gesinn-it/IDProvider
*
* @author gesinn.it GmbH & Co. KG
* @license MIT
*/
namespace MediaWiki\Extension\IdProvider;
/**
* The actual IDProvider Functions
*
* They can be used in a programmatic way through this class
* Use the static getId function as the main entry point
*
* @file
* @ingroup Extensions
*/
class IdProvider {
private $generator;
/**
* @var callable Function to decide if string ID already exists, i.e. is already used as a
* WikiPage
*/
private $getUniqueIdChecker;
/**
* @param $generator
* @param null $getUniqueIdChecker
*/
public function __construct( $generator, $getUniqueIdChecker = null ) {
$this->generator = $generator;
$this->getUniqueIdChecker = $getUniqueIdChecker;
}
public function getId( array $params = [] ): string {
$prefix = self::paramGet( $params, 'prefix', '' );
$skipUniqueTest = self::paramGet( $params, 'skipUniqueTest', false );
$prefix = trim( $prefix );
$id = $prefix . $this->generator->generate();
if ( !$skipUniqueTest ) {
while ( !( $this->getUniqueIdChecker )( $id ) ) {
$id = $prefix . $this->generator->generate();
}
}
return $id;
}
public function generatorClass(): string {
return get_class( $this->generator );
}
private static function paramGet( array $params, string $key, $default = null ) {
return isset( $params[$key] ) ? trim( $params[$key] ) : $default;
}
}