1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Statement; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Everything we know about statement's ranks and what they are supposed to mean. |
9
|
|
|
* |
10
|
|
|
* @see https://www.mediawiki.org/wiki/Wikibase/DataModel#Ranks_of_Statements |
11
|
|
|
* @see https://meta.wikimedia.org/wiki/Wikidata/Data_model_update#Ranks_and_order |
12
|
|
|
* |
13
|
|
|
* @since 4.4 |
14
|
|
|
* |
15
|
|
|
* @license GNU GPL v2+ |
16
|
|
|
* @author Thiemo Kreuz |
17
|
|
|
*/ |
18
|
|
|
class StatementRank { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Higher values are more preferred. |
22
|
|
|
* TODO: Link to discussion/documentation that guarantees increasing order. |
23
|
|
|
*/ |
24
|
|
|
const DEPRECATED = 0; |
25
|
|
|
const NORMAL = 1; |
26
|
|
|
const PREFERRED = 2; |
27
|
|
|
|
28
|
|
|
private static $names = [ |
29
|
|
|
self::DEPRECATED => 'deprecated', |
30
|
|
|
self::NORMAL => 'normal', |
31
|
|
|
self::PREFERRED => 'preferred', |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return string[] Array mapping all known self::... constants (integers) to string names. |
36
|
|
|
*/ |
37
|
|
|
public static function getNames() { |
38
|
|
|
return self::$names; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return int[] Array mapping string names to all known self::... constants (integers). |
43
|
|
|
*/ |
44
|
|
|
public static function getAllRanks() { |
45
|
|
|
return array_flip( self::$names ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param int $rank |
50
|
|
|
* |
51
|
|
|
* @throws InvalidArgumentException |
52
|
|
|
*/ |
53
|
|
|
public static function assertIsValid( $rank ) { |
54
|
|
|
if ( !self::isValid( $rank ) ) { |
55
|
|
|
throw new InvalidArgumentException( 'Invalid rank' ); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param int $rank |
61
|
|
|
* |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
public static function isValid( $rank ) { |
65
|
|
|
return is_int( $rank ) && array_key_exists( $rank, self::$names ); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param int $rank |
70
|
|
|
* |
71
|
|
|
* @throws InvalidArgumentException |
72
|
|
|
* @return bool Statements with a deprecated (or lower) rank are known to be false. But don't be |
73
|
|
|
* fooled, this does not mean higher ranks are known to be true! |
74
|
|
|
*/ |
75
|
|
|
public static function isFalse( $rank ) { |
76
|
|
|
self::assertIsValid( $rank ); |
77
|
|
|
|
78
|
|
|
return $rank === self::DEPRECATED; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param int|null $rank1 |
83
|
|
|
* @param int|null $rank2 |
84
|
|
|
* |
85
|
|
|
* @throws InvalidArgumentException |
86
|
|
|
* @return bool True if the given ranks are equal. |
87
|
|
|
*/ |
88
|
|
|
public static function isEqual( $rank1, $rank2 ) { |
89
|
|
|
return self::compare( $rank1, $rank2 ) === 0; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param int|null $rank1 |
94
|
|
|
* @param int|null $rank2 |
95
|
|
|
* |
96
|
|
|
* @throws InvalidArgumentException |
97
|
|
|
* @return bool True if the first rank is less preferred than the second. |
98
|
|
|
*/ |
99
|
|
|
public static function isLower( $rank1, $rank2 ) { |
100
|
|
|
return self::compare( $rank1, $rank2 ) === -1; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param int|null $rank1 |
105
|
|
|
* @param int|null $rank2 |
106
|
|
|
* |
107
|
|
|
* @throws InvalidArgumentException |
108
|
|
|
* @return bool True if the first rank is more preferred than the second. |
109
|
|
|
*/ |
110
|
|
|
public static function isHigher( $rank1, $rank2 ) { |
111
|
|
|
return self::compare( $rank1, $rank2 ) === 1; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param int|null $rank1 |
116
|
|
|
* @param int|null $rank2 |
117
|
|
|
* |
118
|
|
|
* @throws InvalidArgumentException |
119
|
|
|
* @return int 0 if the ranks are equal, -1 if the first rank is less preferred than the second, |
120
|
|
|
* or +1 if the first rank is more preferred than the second. |
121
|
|
|
*/ |
122
|
|
|
public static function compare( $rank1, $rank2 ) { |
123
|
|
|
if ( $rank1 !== null ) { |
124
|
|
|
self::assertIsValid( $rank1 ); |
125
|
|
|
} |
126
|
|
|
if ( $rank2 !== null ) { |
127
|
|
|
self::assertIsValid( $rank2 ); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ( $rank1 === $rank2 ) { |
131
|
|
|
return 0; |
132
|
|
|
} elseif ( $rank1 === null || $rank1 < $rank2 ) { |
133
|
|
|
return -1; |
134
|
|
|
} else { |
135
|
|
|
return 1; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param int[]|int $ranks |
141
|
|
|
* @param int [$rank2,...] |
142
|
|
|
* |
143
|
|
|
* @return int|null Best rank in the array or list of arguments, or null if none given. |
144
|
|
|
*/ |
145
|
|
|
public static function findBestRank( $ranks = [] /*...*/ ) { |
146
|
|
|
if ( !is_array( $ranks ) ) { |
147
|
|
|
$ranks = func_get_args(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$best = null; |
151
|
|
|
|
152
|
|
|
foreach ( $ranks as $rank ) { |
153
|
|
|
if ( self::isHigher( $rank, $best ) ) { |
154
|
|
|
$best = $rank; |
155
|
|
|
|
156
|
|
|
if ( $best === self::PREFERRED ) { |
157
|
|
|
break; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $best; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
} |
166
|
|
|
|