Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
96.67% |
29 / 30 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| ZKeyReference | |
96.67% |
29 / 30 |
|
80.00% |
4 / 5 |
10 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| getDefinition | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
1 | |||
| isValid | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getZValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getKeyLabel | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
4.02 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * WikiLambda ZKeyReference |
| 4 | * |
| 5 | * @file |
| 6 | * @ingroup Extensions |
| 7 | * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt |
| 8 | * @license MIT |
| 9 | */ |
| 10 | |
| 11 | namespace MediaWiki\Extension\WikiLambda\ZObjects; |
| 12 | |
| 13 | use MediaWiki\Context\RequestContext; |
| 14 | use MediaWiki\Extension\WikiLambda\Registry\ZTypeRegistry; |
| 15 | use MediaWiki\Extension\WikiLambda\WikiLambdaServices; |
| 16 | use MediaWiki\Extension\WikiLambda\ZObjectUtils; |
| 17 | use MediaWiki\Title\Title; |
| 18 | |
| 19 | class ZKeyReference extends ZObject { |
| 20 | |
| 21 | /** |
| 22 | * Construct a ZKeyReference instance, bypassing the internal ZString formally contained. |
| 23 | * |
| 24 | * @param ZString|string $value |
| 25 | */ |
| 26 | public function __construct( $value ) { |
| 27 | if ( $value instanceof ZString ) { |
| 28 | $this->data[ ZTypeRegistry::Z_KEYREFERENCE_VALUE ] = $value->getZValue(); |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | $this->data[ ZTypeRegistry::Z_KEYREFERENCE_VALUE ] = $value; |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @inheritDoc |
| 37 | */ |
| 38 | public static function getDefinition(): array { |
| 39 | return [ |
| 40 | 'type' => [ |
| 41 | 'type' => ZTypeRegistry::Z_REFERENCE, |
| 42 | 'value' => ZTypeRegistry::Z_KEYREFERENCE, |
| 43 | ], |
| 44 | 'keys' => [ |
| 45 | ZTypeRegistry::Z_KEYREFERENCE_VALUE => [ |
| 46 | 'type' => ZTypeRegistry::BUILTIN_STRING, |
| 47 | 'default' => '' |
| 48 | ], |
| 49 | ], |
| 50 | ]; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @inheritDoc |
| 55 | */ |
| 56 | public function isValid(): bool { |
| 57 | if ( !is_string( $this->data[ ZTypeRegistry::Z_KEYREFERENCE_VALUE ] ) ) { |
| 58 | return false; |
| 59 | } |
| 60 | return ZObjectUtils::isValidZObjectKey( $this->data[ ZTypeRegistry::Z_KEYREFERENCE_VALUE ] ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Get string value of the ZKeyReference object |
| 65 | * |
| 66 | * @return string The identifier of the ZKey referred by this ZObject |
| 67 | */ |
| 68 | public function getZValue() { |
| 69 | return $this->data[ ZTypeRegistry::Z_KEYREFERENCE_VALUE ]; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns the label of the current ZKeyReference, if it is |
| 74 | * a global key. Else it returns the untranslated key. |
| 75 | * |
| 76 | * @return string |
| 77 | */ |
| 78 | public function getKeyLabel() { |
| 79 | $key = $this->data[ ZTypeRegistry::Z_KEYREFERENCE_VALUE ]; |
| 80 | |
| 81 | if ( ZObjectUtils::isValidZObjectGlobalKey( $key ) ) { |
| 82 | $typeZid = ZObjectUtils::getZObjectReferenceFromKey( $key ); |
| 83 | $typeTitle = Title::newFromText( $typeZid, NS_MAIN ); |
| 84 | $zObjectStore = WikiLambdaServices::getZObjectStore(); |
| 85 | $content = $zObjectStore->fetchZObjectByTitle( $typeTitle ); |
| 86 | $language = RequestContext::getMain()->getLanguage(); |
| 87 | if ( $content && $content->isValid() ) { |
| 88 | return ZObjectUtils::getLabelOfGlobalKey( $key, $content->getZObject(), $language ); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return $key; |
| 93 | } |
| 94 | } |