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