Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
42 / 42
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ApiZObjectFetcher
100.00% covered (success)
100.00%
42 / 42
100.00% covered (success)
100.00%
2 / 2
13
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
40 / 40
100.00% covered (success)
100.00%
1 / 1
10
 getAllowedParams
n/a
0 / 0
n/a
0 / 0
1
 getExamplesMessages
n/a
0 / 0
n/a
0 / 0
1
1<?php
2/**
3 * WikiLambda ZObject fetching API
4 *
5 * @file
6 * @ingroup Extensions
7 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
8 * @license MIT
9 */
10
11namespace MediaWiki\Extension\WikiLambda\API;
12
13use MediaWiki\Extension\WikiLambda\Registry\ZErrorTypeRegistry;
14use MediaWiki\Extension\WikiLambda\ZErrorException;
15use MediaWiki\Extension\WikiLambda\ZErrorFactory;
16use MediaWiki\Extension\WikiLambda\ZObjectContentHandler;
17use MediaWiki\Extension\WikiLambda\ZObjectUtils;
18use MediaWiki\MediaWikiServices;
19use MediaWiki\Title\Title;
20use Wikimedia\ParamValidator\ParamValidator;
21
22class ApiZObjectFetcher extends WikiLambdaApiBase {
23
24    /**
25     * @inheritDoc
26     */
27    public function __construct( $query, $moduleName ) {
28        parent::__construct( $query, $moduleName );
29
30        $this->setUp();
31    }
32
33    /**
34     * @inheritDoc
35     */
36    public function execute(): void {
37        $params = $this->extractRequestParams();
38
39        $ZIDs = $params[ 'zids' ];
40
41        $revisions = $params[ 'revisions' ];
42        if (
43            $revisions &&
44            count( $revisions ) !== count( $ZIDs )
45        ) {
46            $zErrorObject = ZErrorFactory::createZErrorInstance(
47                ZErrorTypeRegistry::Z_ERROR_UNKNOWN,
48                [
49                    'message' => "You must specify a revision for each ZID, or none at all."
50                ]
51            );
52            $this->dieWithZError( $zErrorObject );
53        }
54
55        $language = $params[ 'language' ];
56
57        foreach ( $ZIDs as $index => $ZID ) {
58            if ( !ZObjectUtils::isValidZObjectReference( mb_strtoupper( $ZID ) ) ) {
59                $zErrorObject = ZErrorFactory::createZErrorInstance(
60                    ZErrorTypeRegistry::Z_ERROR_INVALID_REFERENCE,
61                    [ 'data' => $ZID ]
62                );
63                $this->dieWithZError( $zErrorObject );
64            } else {
65                $title = Title::newFromText( $ZID, NS_MAIN );
66
67                if ( !$title || !is_a( $title, "Title" ) || !$title->exists() ) {
68                    $zErrorObject = ZErrorFactory::createZErrorInstance(
69                        ZErrorTypeRegistry::Z_ERROR_UNKNOWN_REFERENCE,
70                        [ 'data' => $ZID ]
71                    );
72                    $this->dieWithZError( $zErrorObject );
73                } else {
74                    $revision = $revisions ? $revisions[ $index ] : null;
75
76                    try {
77                        $fetchedContent = ZObjectContentHandler::getExternalRepresentation(
78                            $title,
79                            $language,
80                            $revision
81                        );
82                    } catch ( ZErrorException $e ) {
83                        // This probably means that the requested revision is not known; return
84                        // null for this entry rather than throwing or returning a ZError instance.
85                        $fetchedContent = null;
86                    }
87
88                    $this->getResult()->addValue(
89                        $ZID,
90                        $this->getModuleName(),
91                        $fetchedContent
92                    );
93                }
94            }
95        }
96    }
97
98    /**
99     * @inheritDoc
100     * @codeCoverageIgnore
101     */
102    protected function getAllowedParams(): array {
103        $languageUtils = MediaWikiServices::getInstance()->getLanguageNameUtils();
104
105        return [
106            'zids' => [
107                ParamValidator::PARAM_TYPE => 'string',
108                ParamValidator::PARAM_REQUIRED => true,
109                ParamValidator::PARAM_ISMULTI => true,
110            ],
111            'revisions' => [
112                ParamValidator::PARAM_TYPE => 'string',
113                ParamValidator::PARAM_ISMULTI => true,
114            ],
115            'language' => [
116                ParamValidator::PARAM_TYPE => array_keys( $languageUtils->getLanguageNames() ),
117                ParamValidator::PARAM_REQUIRED => false,
118            ]
119        ];
120    }
121
122    /**
123     * @see ApiBase::getExamplesMessages()
124     * @return array
125     * @codeCoverageIgnore
126     */
127    protected function getExamplesMessages() {
128        return [
129            'action=wikilambda_fetch&zids=Z1'
130                => 'apihelp-wikilambda_fetch-example-single',
131            'action=wikilambda_fetch&zids=Z1&revisions=12'
132                => 'apihelp-wikilambda_fetch-example-single-old',
133            'action=wikilambda_fetch&zids=Z1|Z2|Z3'
134                => 'apihelp-wikilambda_fetch-example-multiple',
135            'action=wikilambda_fetch&zids=Z1|Z2|Z3&revisions=12|14|25'
136                => 'apihelp-wikilambda_fetch-example-multiple-old',
137        ];
138    }
139}