Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
CoreLibrary
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 6
132
0.00% covered (danger)
0.00%
0 / 1
 register
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
2
 toLua
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 asLuaValue
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
42
 getTool
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getList
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 findTools
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2declare( strict_types = 1 );
3/**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 * @file
19 */
20
21namespace MediaWiki\Extension\Toolhub;
22
23use array_unshift;
24use is_array;
25use Scribunto_LuaLibraryBase;
26
27/**
28 * Toolhub API integration for Scribunto.
29 *
30 * @copyright © 2022 Wikimedia Foundation and contributors
31 */
32class CoreLibrary extends Scribunto_LuaLibraryBase {
33
34    /**
35     * Register the library.
36     *
37     * @return array Lua package
38     */
39    public function register(): array {
40        $lib = [
41            'getTool' => [ $this, 'getTool' ],
42            'getList' => [ $this, 'getList' ],
43            'findTools' => [ $this, 'findTools' ],
44        ];
45        $settings = [];
46        return $this->getEngine()->registerInterface(
47            __DIR__ . '/mw.ext.toolhub.lua',
48            $lib,
49            $settings
50        );
51    }
52
53    /**
54     * Cast a value to a Lua compatible form.
55     *
56     * @param mixed $val
57     * @return array
58     */
59    private function toLua( $val ): array {
60        // Return as an array to match Lua semantics of multiple return values
61        return [ $this->asLuaValue( $val ) ];
62    }
63
64    /**
65     * Convert a PHP value to a Lua compatible form.
66     *
67     * @param mixed $val
68     * @return mixed
69     */
70    private function asLuaValue( $val ) {
71        $type = $this->getLuaType( $val );
72        if (
73            $type === 'nil' ||
74            $type === 'function' ||
75            // PHP type without direct Lua mapping
76            preg_match( '/^PHP .*/', $type )
77        ) {
78            // Strip out things that have no native Lua representation
79            return null;
80        }
81        if ( is_array( $val ) ) {
82            foreach ( $val as $key => $value ) {
83                $val[ $key ] = $this->asLuaValue( $value );
84            }
85            // Make arrays be 1-based (Lua) rather than 0-based (PHP) by
86            // prepending a new [0] element and then deleting it.
87            array_unshift( $val, '' );
88            unset( $val[0] );
89        }
90        return $val;
91    }
92
93    /**
94     * Get info for a specific tool.
95     *
96     * @param string $name Name of the tool
97     * @return array
98     */
99    public function getTool( string $name ): array {
100        $this->checkType( 'getTool', 1, $name, 'string' );
101        $api = ToolhubServices::getApiClient();
102        $resp = $api->getToolByName( $name );
103        // FIXME: cache non-negative results
104        return $this->toLua( $resp );
105    }
106
107    /**
108     * Get info for a specific list.
109     *
110     * @param int $id List id
111     * @return array
112     */
113    public function getList( int $id ): array {
114        $this->checkType( 'getList', 1, $id, 'number' );
115        $api = ToolhubServices::getApiClient();
116        $resp = $api->getListById( $id );
117        // FIXME: cache non-negative results
118        return $this->toLua( $resp );
119    }
120
121    /**
122     * Search for tools.
123     *
124     * @param ?string $query User provided query
125     * @param int $page Result page
126     * @param int $pageSize Number of tools per page
127     * @return array
128     */
129    public function findTools(
130        ?string $query = null,
131        int $page = 1,
132        int $pageSize = 25
133    ): array {
134        $this->checkTypeOptional( 'findTools', 1, $query, 'string', null );
135        $this->checkType( 'findTools', 2, $page, 'number' );
136        $this->checkType( 'findTools', 3, $pageSize, 'number' );
137        $api = ToolhubServices::getApiClient();
138        $resp = $api->findTools( $query, $page, $pageSize );
139        // FIXME: cache non-negative results
140        return $this->toLua( $resp );
141    }
142}