Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ZObjectFilterIsConnectedConverter
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
42
0.00% covered (danger)
0.00%
0 / 1
 pass
0.00% covered (danger)
0.00%
0 / 25
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2/**
3 * WikiLambda ZObject Authorization Filter: Is Connected Converter
4 *
5 * @file
6 * @ingroup Extensions
7 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
8 * @license MIT
9 */
10
11namespace MediaWiki\Extension\WikiLambda\Authorization;
12
13use MediaWiki\Extension\WikiLambda\Registry\ZTypeRegistry;
14use MediaWiki\Extension\WikiLambda\WikiLambdaServices;
15use MediaWiki\Extension\WikiLambda\ZObjectContent;
16use MediaWiki\Title\Title;
17
18class ZObjectFilterIsConnectedConverter implements ZObjectFilter {
19
20    /**
21     * ZObject filter that checks whether the edited converter (serialiser or deserialiser)
22     * is already connected to a type.
23     *
24     * @param ZObjectContent $fromContent
25     * @param ZObjectContent $toContent
26     * @param Title $title
27     * @param array $args
28     * @return bool
29     */
30    public static function pass( $fromContent, $toContent, $title, $args = [] ): bool {
31        $typeId = '';
32        $arrayKey = '';
33
34        $type = $fromContent->getZType();
35        switch ( $type ) {
36            // For Deserialisers:
37            // * get deserialiser ZID from $title
38            // * get type ZID from Z_DESERIALISER_TYPE
39            // * retrieve type from DB
40            // * check that the type has this ZID in Z_TYPE_DESERIALISERS
41            case ZTypeRegistry::Z_DESERIALISER:
42                $typeId = $fromContent
43                    ->getInnerZObject()
44                    ->getValueByKey( ZTypeRegistry::Z_DESERIALISER_TYPE )
45                    ->getZValue();
46                $arrayKey = ZTypeRegistry::Z_TYPE_DESERIALISERS;
47                break;
48
49            // For Serialisers:
50            // * get serialiser ZID from $title
51            // * get type ZID from Z_SERIALISER_TYPE
52            // * retrieve type from DB
53            // * check that the type has this ZID in Z_TYPE_SERIALISERS
54            case ZTypeRegistry::Z_SERIALISER:
55                $typeId = $fromContent
56                    ->getInnerZObject()
57                    ->getValueByKey( ZTypeRegistry::Z_SERIALISER_TYPE )
58                    ->getZValue();
59                $arrayKey = ZTypeRegistry::Z_TYPE_SERIALISERS;
60                break;
61
62            default:
63                return false;
64        }
65
66        $typeTitle = Title::newFromText( $typeId, NS_MAIN );
67        $zObjectStore = WikiLambdaServices::getZObjectStore();
68        $typeObject = $zObjectStore->fetchZObjectByTitle( $typeTitle );
69
70        if ( $typeObject === false ) {
71            // Type object not found; return false
72            return false;
73        }
74        if ( $typeObject->getZType() !== ZTypeRegistry::Z_TYPE ) {
75            // Object of wrong type; return false
76            return false;
77        }
78        $array = $typeObject->getInnerZObject()->getValueByKey( $arrayKey )->getSerialized();
79        return in_array( $title->getText(), $array );
80    }
81}