Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 93
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryGlobalUsage
0.00% covered (danger)
0.00%
0 / 93
0.00% covered (danger)
0.00%
0 / 6
380
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 1
182
 setIndexedTagName
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
2
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getCacheMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Created on November 8, 2009
4 *
5 * API for MediaWiki 1.8+
6 *
7 * Copyright (C) 2009 Bryan Tong Minh <bryan.tongminh@gmail.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 */
24
25namespace MediaWiki\Extension\GlobalUsage;
26
27use MediaWiki\Api\ApiBase;
28use MediaWiki\Api\ApiQuery;
29use MediaWiki\Api\ApiQueryBase;
30use MediaWiki\Site\SiteLookup;
31use MediaWiki\WikiMap\WikiMap;
32use Wikimedia\ParamValidator\ParamValidator;
33use Wikimedia\ParamValidator\TypeDef\IntegerDef;
34
35class ApiQueryGlobalUsage extends ApiQueryBase {
36    private SiteLookup $siteLookup;
37
38    public function __construct(
39        ApiQuery $query,
40        string $moduleName,
41        SiteLookup $siteLookup
42    ) {
43        parent::__construct( $query, $moduleName, 'gu' );
44        $this->siteLookup = $siteLookup;
45    }
46
47    public function execute() {
48        $params = $this->extractRequestParams();
49        $prop = array_flip( $params['prop'] );
50
51        $allPageIds = $this->getPageSet()->getAllTitlesByNamespace();
52        if ( !empty( $allPageIds[NS_FILE] ) ) {
53            # Create a query and set parameters
54            $pageIds = $allPageIds[NS_FILE];
55            $query = new GlobalUsageQuery( array_map( 'strval', array_keys( $pageIds ) ) );
56            if ( $params['continue'] !== null ) {
57                $this->dieContinueUsageIf( !$query->setOffset( $params['continue'] ) );
58            }
59            $query->setLimit( $params['limit'] );
60            $query->filterLocal( $params['filterlocal'] );
61            $query->filterNamespaces( $params['namespace'] );
62            if ( $params['site'] ) {
63                $query->filterSites( $params['site'] );
64            }
65
66            # Execute the query
67            $query->execute();
68
69            # Create the result
70            $apiResult = $this->getResult();
71
72            foreach ( $query->getResult() as $image => $wikis ) {
73                $pageId = intval( $pageIds[$image] );
74                foreach ( $wikis as $wiki => $result ) {
75                    foreach ( $result as $item ) {
76                        if ( $item['namespace'] ) {
77                            $title = "{$item['namespace']}:{$item['title']}";
78                        } else {
79                            $title = $item['title'];
80                        }
81                        $result = [
82                            'title' => $title,
83                            'wiki' => WikiMap::getWikiName( $wiki )
84                        ];
85                        if ( isset( $prop['url'] ) ) {
86                            // We expand the url because we don't want protocol relative urls
87                            // in API results
88                            $result['url'] = wfExpandUrl(
89                                WikiMap::getForeignUrl( $item['wiki'], $title ), PROTO_CURRENT );
90                        }
91                        if ( isset( $prop['pageid'] ) ) {
92                            $result['pageid'] = $item['id'];
93                        }
94                        if ( isset( $prop['namespace'] ) ) {
95                            $result['ns'] = $item['namespace_id'];
96                        }
97
98                        $fit = $apiResult->addValue(
99                            [ 'query', 'pages', $pageId, 'globalusage' ],
100                            null,
101                            $result
102                        );
103
104                        if ( !$fit ) {
105                            $continue = "{$item['image']}|{$item['wiki']}|{$item['id']}";
106                            $this->setIndexedTagName();
107                            $this->setContinueEnumParameter( 'continue', $continue );
108                            return;
109                        }
110                    }
111                }
112            }
113            $this->setIndexedTagName();
114
115            if ( $query->hasMore() ) {
116                $this->setContinueEnumParameter( 'continue', $query->getContinueString() );
117            }
118        }
119    }
120
121    private function setIndexedTagName() {
122        $result = $this->getResult();
123        $pageIds = $this->getPageSet()->getAllTitlesByNamespace();
124        foreach ( $pageIds[NS_FILE] as $id ) {
125            $result->addIndexedTagName(
126                [ 'query', 'pages', $id, 'globalusage' ],
127                'gu'
128            );
129        }
130    }
131
132    /** @inheritDoc */
133    public function getAllowedParams() {
134        $sites = $this->siteLookup->getSites()->getGlobalIdentifiers();
135        return [
136            'prop' => [
137                ParamValidator::PARAM_DEFAULT => 'url',
138                ParamValidator::PARAM_TYPE => [
139                    'url',
140                    'pageid',
141                    'namespace',
142                ],
143                ParamValidator::PARAM_ISMULTI => true,
144                ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
145            ],
146            'limit' => [
147                ParamValidator::PARAM_DEFAULT => 10,
148                ParamValidator::PARAM_TYPE => 'limit',
149                IntegerDef::PARAM_MIN => 1,
150                IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
151                IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
152            ],
153            'namespace' => [
154                ParamValidator::PARAM_TYPE => 'namespace',
155                ParamValidator::PARAM_DEFAULT => '*',
156                ParamValidator::PARAM_ISMULTI => true,
157            ],
158            'site' => [
159                ParamValidator::PARAM_TYPE => $sites,
160                ParamValidator::PARAM_ISMULTI => true
161            ],
162            'continue' => [
163                ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
164            ],
165            'filterlocal' => false,
166        ];
167    }
168
169    /**
170     * @see ApiBase::getExamplesMessages()
171     * @return array
172     */
173    protected function getExamplesMessages() {
174        return [
175            'action=query&prop=globalusage&titles=File:Example.jpg'
176                => 'apihelp-query+globalusage-example-1',
177        ];
178    }
179
180    /** @inheritDoc */
181    public function getCacheMode( $params ) {
182        return 'public';
183    }
184}