Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
34 / 34 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
| DiffItemKeyer | |
100.00% |
34 / 34 |
|
100.00% |
5 / 5 |
24 | |
100.00% |
1 / 1 |
| getJoinKey | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
8 | |||
| uniqueJoinKeys | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
4 | |||
| identifyingKey | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
6 | |||
| isMonolingual | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| languageZidOf | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * WikiLambda DiffItemKeyer: works out which part of a list item identifies it, |
| 4 | * and keys it for pairing across revisions. |
| 5 | * |
| 6 | * @file |
| 7 | * @ingroup Extensions |
| 8 | * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt |
| 9 | * @license MIT |
| 10 | */ |
| 11 | |
| 12 | namespace MediaWiki\Extension\WikiLambda\Diff; |
| 13 | |
| 14 | use MediaWiki\Extension\WikiLambda\Registry\ZTypeRegistry; |
| 15 | |
| 16 | /** |
| 17 | * Answers "which one is this?" for an item of a typed list, structurally. |
| 18 | * |
| 19 | * A list index is a poor way to refer to an item: it means nothing to a reader, |
| 20 | * and it is not stable, since inserting or removing an item renumbers every item |
| 21 | * after it. The item's own content identifies it far better. |
| 22 | * |
| 23 | * This class resolves nothing — no labels, no database, no language — which is |
| 24 | * what lets ZObjectListDiffer use it while diffing, where no such services are |
| 25 | * available. DiffItemIdentifier builds on it to name items for a reader. |
| 26 | */ |
| 27 | class DiffItemKeyer { |
| 28 | |
| 29 | /** |
| 30 | * Types whose identifying key is not the first key of the record. |
| 31 | * |
| 32 | * Stored content is key-sorted, so a record's lowest-numbered key comes |
| 33 | * first, and for most types that key is the one that says which item this |
| 34 | * is. An implementation or a tester starts with the function it belongs to, |
| 35 | * for example. Two shapes must be stated instead. |
| 36 | * |
| 37 | * For a key or an argument declaration the first key is the type — which |
| 38 | * every second key or string argument in the wiki shares — whereas the key |
| 39 | * or argument id names precisely one, and labels well. For a function or an |
| 40 | * error type the identity sorts last, behind a list of arguments or of keys |
| 41 | * that identifies nothing on its own. |
| 42 | */ |
| 43 | private const IDENTIFYING_KEYS = [ |
| 44 | ZTypeRegistry::Z_KEY => ZTypeRegistry::Z_KEY_ID, |
| 45 | ZTypeRegistry::Z_ARGUMENTDECLARATION => ZTypeRegistry::Z_ARGUMENTDECLARATION_ID, |
| 46 | ZTypeRegistry::Z_FUNCTION => ZTypeRegistry::Z_FUNCTION_IDENTITY, |
| 47 | ZTypeRegistry::Z_ERRORTYPE => ZTypeRegistry::Z_ERRORTYPE_ID, |
| 48 | ]; |
| 49 | |
| 50 | /** |
| 51 | * Key an item for pairing against the other revision's items, or return null |
| 52 | * where it cannot be keyed reliably. |
| 53 | * |
| 54 | * A key must be exact and stable, so it stays a ZID: a reference keys on its |
| 55 | * own ZID rather than on any label, a monolingual value on its language, and a |
| 56 | * record on the raw value of whichever key identifies its type. |
| 57 | * |
| 58 | * Callers must not assume keys are unique; two implementations of one function |
| 59 | * share theirs. Use uniqueJoinKeys() to get only the keys that name one item. |
| 60 | * |
| 61 | * @param mixed $item The item, as a nested array or a leaf |
| 62 | * @return string|null |
| 63 | */ |
| 64 | public static function getJoinKey( $item ): ?string { |
| 65 | if ( !is_array( $item ) ) { |
| 66 | return is_string( $item ) && $item !== '' ? $item : null; |
| 67 | } |
| 68 | |
| 69 | // A multilingual container holds at most one entry per language, so the |
| 70 | // language is an exact key for a monolingual value. |
| 71 | if ( self::isMonolingual( $item ) ) { |
| 72 | return self::languageZidOf( $item ); |
| 73 | } |
| 74 | |
| 75 | $key = self::identifyingKey( $item ); |
| 76 | $value = $key === null ? null : $item[$key]; |
| 77 | return is_string( $value ) && $value !== '' ? $value : null; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * The join keys of a list's items that name exactly one item, as a map of key |
| 82 | * to index. |
| 83 | * |
| 84 | * Items sharing a key, and items with no key at all, are left out: they can |
| 85 | * only be paired up by some other means. The list is taken as stored, so its |
| 86 | * leading reference to the type of its items is keyed like any other item, |
| 87 | * and pairs with the other revision's. |
| 88 | * |
| 89 | * @param array $items |
| 90 | * @return array<string,int> Join key => index |
| 91 | */ |
| 92 | public static function uniqueJoinKeys( array $items ): array { |
| 93 | $indices = []; |
| 94 | $shared = []; |
| 95 | foreach ( $items as $index => $item ) { |
| 96 | $key = self::getJoinKey( $item ); |
| 97 | if ( $key === null ) { |
| 98 | continue; |
| 99 | } |
| 100 | if ( array_key_exists( $key, $indices ) ) { |
| 101 | $shared[$key] = true; |
| 102 | continue; |
| 103 | } |
| 104 | $indices[$key] = $index; |
| 105 | } |
| 106 | return array_diff_key( $indices, $shared ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * The key of a record that says which one it is: the one stated for its type, |
| 111 | * or failing that its first key other than the type itself. |
| 112 | * |
| 113 | * @param array $item |
| 114 | * @return string|null |
| 115 | */ |
| 116 | public static function identifyingKey( array $item ): ?string { |
| 117 | $type = $item[ZTypeRegistry::Z_OBJECT_TYPE] ?? null; |
| 118 | if ( is_string( $type ) && array_key_exists( $type, self::IDENTIFYING_KEYS ) ) { |
| 119 | $key = self::IDENTIFYING_KEYS[$type]; |
| 120 | if ( array_key_exists( $key, $item ) ) { |
| 121 | return $key; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | foreach ( $item as $candidate => $unused ) { |
| 126 | if ( (string)$candidate !== ZTypeRegistry::Z_OBJECT_TYPE ) { |
| 127 | return (string)$candidate; |
| 128 | } |
| 129 | } |
| 130 | return null; |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Whether an item is a monolingual string or string set, which are identified |
| 135 | * by their language rather than by any of their keys. |
| 136 | * |
| 137 | * @param array $item |
| 138 | * @return bool |
| 139 | */ |
| 140 | public static function isMonolingual( array $item ): bool { |
| 141 | $type = $item[ZTypeRegistry::Z_OBJECT_TYPE] ?? null; |
| 142 | return $type === ZTypeRegistry::Z_MONOLINGUALSTRING |
| 143 | || $type === ZTypeRegistry::Z_MONOLINGUALSTRINGSET; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * The ZID of a monolingual item's language, or null if it has none or this is |
| 148 | * not a monolingual item. |
| 149 | * |
| 150 | * @param array $item |
| 151 | * @return string|null |
| 152 | */ |
| 153 | public static function languageZidOf( array $item ): ?string { |
| 154 | $languageZid = ( $item[ZTypeRegistry::Z_OBJECT_TYPE] ?? null ) === ZTypeRegistry::Z_MONOLINGUALSTRING |
| 155 | ? ( $item[ZTypeRegistry::Z_MONOLINGUALSTRING_LANGUAGE] ?? null ) |
| 156 | : ( $item[ZTypeRegistry::Z_MONOLINGUALSTRINGSET_LANGUAGE] ?? null ); |
| 157 | return is_string( $languageZid ) && $languageZid !== '' ? $languageZid : null; |
| 158 | } |
| 159 | } |