Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
71.43% covered (warning)
71.43%
30 / 42
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryZFunctionReference
71.43% covered (warning)
71.43%
30 / 42
0.00% covered (danger)
0.00%
0 / 1
16.94
0.00% covered (danger)
0.00%
0 / 1
 __construct
n/a
0 / 0
n/a
0 / 0
1
 run
71.43% covered (warning)
71.43%
30 / 42
0.00% covered (danger)
0.00%
0 / 1
10.89
 getAllowedParams
n/a
0 / 0
n/a
0 / 0
1
 getExamplesMessages
n/a
0 / 0
n/a
0 / 0
2
1<?php
2/**
3 * WikiLambda ZFunction reference helper for the query 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\ActionAPI;
12
13use MediaWiki\Api\ApiBase;
14use MediaWiki\Api\ApiQuery;
15use MediaWiki\Extension\WikiLambda\HttpStatus;
16use MediaWiki\Extension\WikiLambda\Registry\ZErrorTypeRegistry;
17use MediaWiki\Extension\WikiLambda\ZErrorFactory;
18use MediaWiki\Extension\WikiLambda\ZObjectStore;
19use MediaWiki\MediaWikiServices;
20use MediaWiki\Title\Title;
21use Wikimedia\ParamValidator\ParamValidator;
22use Wikimedia\ParamValidator\TypeDef\IntegerDef;
23
24class ApiQueryZFunctionReference extends WikiLambdaApiQueryGeneratorBase {
25
26    /**
27     * @codeCoverageIgnore
28     */
29    public function __construct(
30        ApiQuery $query,
31        string $moduleName,
32        private readonly ZObjectStore $zObjectStore
33    ) {
34        parent::__construct( $query, $moduleName, 'wikilambdafn_' );
35    }
36
37    /**
38     * @inheritDoc
39     */
40    protected function run( $resultPageSet = null ) {
41        // Exit if we're running in non-repo mode (e.g. on a client wiki)
42        if ( !$this->getConfig()->get( 'WikiLambdaEnableRepoMode' ) ) {
43            WikiLambdaApiBase::dieWithZError(
44                ZErrorFactory::createZErrorInstance(
45                    ZErrorTypeRegistry::Z_ERROR_USER_CANNOT_RUN,
46                    []
47                ),
48                HttpStatus::BAD_REQUEST
49            );
50        }
51
52        [
53            'zfunction_id' => $zFunctionId,
54            'type' => $type,
55            'limit' => $limit,
56            'continue' => $continue,
57        ] = $this->extractRequestParams();
58        $result = $this->getResult();
59        $res = $this->zObjectStore->findReferencedZObjectsByZFunctionId( $zFunctionId, $type, $continue, $limit + 1 );
60
61        // If $res has no rows, then return an empty array to indicate that no results were found.
62        if ( $res->numRows() === 0 ) {
63            // Output an empty list under the module name
64            $result->addValue( [ 'query' ], $this->getModuleName(), [] );
65        }
66
67        $zids = [];
68        $i = 0;
69        $lastId = 0;
70        foreach ( $res as $row ) {
71            if ( $i >= $limit ) {
72                break;
73            }
74            $zids[] = [
75                'page_namespace' => NS_MAIN,
76                'zid' => $row->wlzf_ref_zid
77            ];
78            $lastId = $row->wlzf_id;
79            $i++;
80        }
81        unset( $i );
82
83        if ( $res->numRows() > $limit ) {
84            $this->setContinueEnumParameter( 'continue', strval( $lastId + 1 ) );
85        }
86
87        if ( $resultPageSet ) {
88            foreach ( $zids as $index => $entry ) {
89                $resultPageSet->setGeneratorData(
90                    Title::makeTitle( $entry['page_namespace'], $entry['zid'] ),
91                    [ 'index' => $index + $continue + 1 ]
92                );
93            }
94        } else {
95            $result = $this->getResult();
96            foreach ( $zids as $entry ) {
97                $result->addValue( [ 'query', $this->getModuleName() ], null, $entry );
98            }
99        }
100    }
101
102    /**
103     * @inheritDoc
104     * @codeCoverageIgnore
105     */
106    protected function getAllowedParams(): array {
107        return [
108            'zfunction_id' => [
109                ParamValidator::PARAM_TYPE => 'string',
110                ParamValidator::PARAM_DEFAULT => ''
111            ],
112            'type' => [
113                ParamValidator::PARAM_TYPE => 'string',
114                ParamValidator::PARAM_DEFAULT => ''
115            ],
116            'limit' => [
117                ParamValidator::PARAM_TYPE => 'limit',
118                ParamValidator::PARAM_DEFAULT => 10,
119                IntegerDef::PARAM_MIN => 1,
120                IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
121                IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2,
122            ],
123            'continue' => [
124                ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
125            ],
126        ];
127    }
128
129    /**
130     * @see ApiBase::getExamplesMessages()
131     * @return array
132     * @codeCoverageIgnore
133     */
134    protected function getExamplesMessages() {
135        // Don't try to read the latest ZID from the DB on client wikis, we can't.
136        $exampleZid =
137            ( MediaWikiServices::getInstance()->getMainConfig()->get( 'WikiLambdaEnableRepoMode' ) ) ?
138            $this->zObjectStore->findFirstZImplementationFunction() :
139            'Z10000';
140
141        return [
142            "action=query&list=wikilambdafn_search&wikilambdafn_zfunction_id=$exampleZid&wikilambdafn_type=Z14"
143                => 'apihelp-query+wikilambdafn_search-example-simple'
144        ];
145    }
146}