Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.00% covered (warning)
88.00%
22 / 25
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ZObjectFilterIsAttached
88.00% covered (warning)
88.00%
22 / 25
0.00% covered (danger)
0.00%
0 / 1
6.06
0.00% covered (danger)
0.00%
0 / 1
 pass
88.00% covered (warning)
88.00%
22 / 25
0.00% covered (danger)
0.00%
0 / 1
6.06
1<?php
2/**
3 * WikiLambda ZObject Authorization Filter: Is Running Function
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 ZObjectFilterIsAttached implements ZObjectFilter {
19
20    /**
21     * ZObject filter that checks whether the edited implementation or tester is
22     * attached to a function.
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        $functionId = '';
32        $arrayKey = '';
33
34        $type = $fromContent->getZType();
35        switch ( $type ) {
36            // For Implementation:
37            // * get Implementation ZID from $title
38            // * get function ID from Z_IMPLEMENTATION_FUNCTION
39            // * retrieve function from DB
40            // * and check that the function has this ZID in Z_FUNCTION_IMPLEMENTATIONS
41            case ZTypeRegistry::Z_IMPLEMENTATION:
42                $functionId = $fromContent
43                    ->getInnerZObject()
44                    ->getValueByKey( ZTypeRegistry::Z_IMPLEMENTATION_FUNCTION )
45                    ->getZValue();
46                $arrayKey = ZTypeRegistry::Z_FUNCTION_IMPLEMENTATIONS;
47                break;
48
49            // For Tester:
50            // * get Tester ZID from $title
51            // * get function ID from Z_TESTER_FUNCTION
52            // * retrieve function from DB
53            // * and check that the function has this ZID in Z_FUNCTION_TESTERS
54            case ZTypeRegistry::Z_TESTER:
55                $functionId = $fromContent
56                    ->getInnerZObject()
57                    ->getValueByKey( ZTypeRegistry::Z_TESTER_FUNCTION )
58                    ->getZValue();
59                $arrayKey = ZTypeRegistry::Z_FUNCTION_TESTERS;
60                break;
61
62            default:
63                return false;
64        }
65
66        $functionTitle = Title::newFromText( $functionId, NS_MAIN );
67        $zObjectStore = WikiLambdaServices::getZObjectStore();
68        $function = $zObjectStore->fetchZObjectByTitle( $functionTitle );
69
70        if ( $function === false ) {
71            // Function not found; return false
72            return false;
73        }
74        if ( $function->getZType() !== ZTypeRegistry::Z_FUNCTION ) {
75            // Object of wrong type; return false
76            return false;
77        }
78        $array = $function->getInnerZObject()->getValueByKey( $arrayKey )->getSerialized();
79        return in_array( $title->getText(), $array );
80    }
81}